Skip to content

Commit 3d36dae

Browse files
committed
add lazy property initialization api to SystemPropertiesSupport
1 parent b254ed5 commit 3d36dae

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SystemPropertiesSupport.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ protected SystemPropertiesSupport() {
8787
properties = new Properties();
8888
savedProperties = new HashMap<>();
8989
readOnlySavedProperties = Collections.unmodifiableMap(savedProperties);
90+
lazyRuntimeValues = new HashMap<>();
9091

9192
for (String key : HOSTED_PROPERTIES) {
9293
String value = System.getProperty(key);
@@ -114,19 +115,18 @@ protected SystemPropertiesSupport() {
114115
initializeProperty("java.awt.printerjob", System.getProperty("java.awt.printerjob"));
115116
}
116117

117-
lazyRuntimeValues = new HashMap<>();
118-
lazyRuntimeValues.put("user.name", this::userName);
119-
lazyRuntimeValues.put("user.home", this::userHome);
120-
lazyRuntimeValues.put("user.dir", this::userDir);
121-
lazyRuntimeValues.put("java.io.tmpdir", this::tmpdirValue);
122-
lazyRuntimeValues.put("os.version", this::osVersionValue);
123-
lazyRuntimeValues.put("java.vm.version", VM::getVersion);
118+
initializeLazyProperty("user.name", this::userName);
119+
initializeLazyProperty("user.home", this::userHome);
120+
initializeLazyProperty("user.dir", this::userDir);
121+
initializeLazyProperty("java.io.tmpdir", this::tmpdirValue);
122+
initializeLazyProperty("os.version", this::osVersionValue);
123+
initializeLazyProperty("java.vm.version", VM::getVersion);
124124

125125
String targetName = System.getProperty("svm.targetName");
126126
if (targetName != null) {
127127
initializeProperty("os.name", targetName);
128128
} else {
129-
lazyRuntimeValues.put("os.name", this::osNameValue);
129+
initializeLazyProperty("os.name", this::osNameValue);
130130
}
131131

132132
String targetArch = System.getProperty("svm.targetArch");
@@ -188,6 +188,14 @@ public void initializeProperty(String key, String value) {
188188
properties.setProperty(key, value);
189189
}
190190

191+
/**
192+
* Lazily initializes a property at runtime when it is first requested, based on the supplier's
193+
* return value.
194+
*/
195+
public void initializeLazyProperty(String key, Supplier<String> value) {
196+
lazyRuntimeValues.put(key, value);
197+
}
198+
191199
public String setProperty(String key, String value) {
192200
/*
193201
* The return value of setProperty is the previous value of the key, so we need to ensure

0 commit comments

Comments
 (0)