Skip to content

Commit

Permalink
add lazy property initialization api to SystemPropertiesSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
kkriske committed Dec 5, 2020
1 parent 83326c6 commit aa2506f
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<String> 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
Expand Down

0 comments on commit aa2506f

Please sign in to comment.