From aa2506f45757f7a8d6e0964e0369246c268c08a6 Mon Sep 17 00:00:00 2001 From: Kristof Dhondt Date: Sat, 5 Dec 2020 21:39:06 +0100 Subject: [PATCH] add lazy property initialization api to SystemPropertiesSupport --- .../svm/core/jdk/SystemPropertiesSupport.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SystemPropertiesSupport.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SystemPropertiesSupport.java index 9e238260353d3..51c2528f22fea 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SystemPropertiesSupport.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SystemPropertiesSupport.java @@ -84,6 +84,7 @@ protected SystemPropertiesSupport() { properties = new Properties(); savedProperties = new HashMap<>(); readOnlySavedProperties = Collections.unmodifiableMap(savedProperties); + lazyRuntimeValues = new HashMap<>(); for (String key : HOSTED_PROPERTIES) { String value = System.getProperty(key); @@ -116,13 +117,12 @@ protected SystemPropertiesSupport() { initializeProperty("java.awt.printerjob", System.getProperty("java.awt.printerjob")); } - lazyRuntimeValues = new HashMap<>(); - lazyRuntimeValues.put("user.name", this::userName); - lazyRuntimeValues.put("user.home", this::userHome); - lazyRuntimeValues.put("user.dir", this::userDir); - lazyRuntimeValues.put("java.io.tmpdir", this::tmpdirValue); - lazyRuntimeValues.put("os.version", this::osVersionValue); - lazyRuntimeValues.put("java.vm.version", VM::getVersion); + initializeLazyProperty("user.name", this::userName); + initializeLazyProperty("user.home", this::userHome); + initializeLazyProperty("user.dir", this::userDir); + initializeLazyProperty("java.io.tmpdir", this::tmpdirValue); + initializeLazyProperty("os.version", this::osVersionValue); + initializeLazyProperty("java.vm.version", VM::getVersion); } private void ensureFullyInitialized() { @@ -176,6 +176,14 @@ public void initializeProperty(String key, String value) { properties.setProperty(key, value); } + /** + * Lazily initializes a property at runtime when it is first requested, based on the supplier's + * return value. + */ + public void initializeLazyProperty(String key, Supplier value) { + lazyRuntimeValues.put(key, value); + } + public String setProperty(String key, String value) { /* * The return value of setProperty is the previous value of the key, so we need to ensure