Skip to content

Instantly share code, notes, and snippets.

@Lambeaux
Last active February 7, 2018 23:20
Show Gist options
  • Save Lambeaux/a6fad345dd42ddc3acbd1fe17f3139db to your computer and use it in GitHub Desktop.
Save Lambeaux/a6fad345dd42ddc3acbd1fe17f3139db to your computer and use it in GitHub Desktop.
First pass at property expansions
/**
* Returns a new {@link Dictionary} where values containing system property notation such as
* {@code ${my.system.property.name}} are substituted with their appropriate values.
*/
private Dictionary<String, Object> expand(Dictionary<String, Object> original) {
Dictionary<String, Object> expanded = new Hashtable<>();
for (String key : list(original.keys())) {
Object val = original.get(key);
if (val instanceof String) {
expanded.put(
key,
Stream.of(val)
.map(String.class::cast)
.flatMap(PROP_PATTERN::splitAsStream)
.map(this::substituteProperty)
.collect(Collectors.joining()));
} else {
expanded.put(key, val);
}
}
return expanded;
}
private String substituteProperty(String fragment) {
if (PROP_PATTERN.matcher(fragment).matches()) {
String propertyName = fragment.substring(2, fragment.length() - 1);
String propertyValue = System.getProperty(propertyName);
return (propertyValue == null) ? "" : propertyValue;
}
return fragment;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment