Settings / Config: Allow to explicitly specify external environment variable syntax, in which case its optional

fixes #2855
This commit is contained in:
Shay Banon 2013-04-04 16:30:07 +02:00
parent d758401add
commit a206aa4548
2 changed files with 22 additions and 23 deletions

View File

@ -33,15 +33,11 @@ import java.util.Set;
* <p/>
* <p> Values for substitution can be supplied using a {@link Properties} instance or using a
* {@link PlaceholderResolver}.
*
*
*/
public class PropertyPlaceholder {
private final String placeholderPrefix;
private final String placeholderSuffix;
private final boolean ignoreUnresolvablePlaceholders;
/**
@ -72,24 +68,6 @@ public class PropertyPlaceholder {
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
}
/**
* Replaces all placeholders of format <code>${name}</code> with the corresponding property from the supplied {@link
* Properties}.
*
* @param value the value containing the placeholders to be replaced.
* @param properties the <code>Properties</code> to use for replacement.
* @return the supplied value with placeholders replaced inline.
*/
public String replacePlaceholders(String value, final Properties properties) {
Preconditions.checkNotNull(properties, "Argument 'properties' must not be null.");
return replacePlaceholders(value, new PlaceholderResolver() {
public String resolvePlaceholder(String placeholderName) {
return properties.getProperty(placeholderName);
}
});
}
/**
* Replaces all placeholders of format <code>${name}</code> with the value returned from the supplied {@link
* PlaceholderResolver}.
@ -130,6 +108,9 @@ public class PropertyPlaceholder {
if (propVal == null) {
propVal = defaultValue;
}
if (propVal == null && placeholderResolver.shouldIgnoreMissing(placeholder)) {
propVal = "";
}
if (propVal != null) {
// Recursive invocation, parsing placeholders contained in the
// previously resolved placeholder value.
@ -187,5 +168,7 @@ public class PropertyPlaceholder {
* @return the replacement value or <code>null</code> if no replacement is to be made.
*/
String resolvePlaceholder(String placeholderName);
boolean shouldIgnoreMissing(String placeholderName);
}
}

View File

@ -831,6 +831,10 @@ public class ImmutableSettings implements Settings {
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new PropertyPlaceholder.PlaceholderResolver() {
@Override
public String resolvePlaceholder(String placeholderName) {
if (placeholderName.startsWith("env.")) {
// explicit env var prefix
return System.getenv(placeholderName.substring("env.".length()));
}
String value = System.getProperty(placeholderName);
if (value != null) {
return value;
@ -841,9 +845,21 @@ public class ImmutableSettings implements Settings {
}
return map.get(placeholderName);
}
@Override
public boolean shouldIgnoreMissing(String placeholderName) {
// if its an explicit env var, we are ok with not having a value for it and treat it as optional
if (placeholderName.startsWith("env.")) {
return true;
}
return false;
}
};
for (Map.Entry<String, String> entry : map.entrySet()) {
map.put(entry.getKey(), propertyPlaceholder.replacePlaceholders(entry.getValue(), placeholderResolver));
String value = propertyPlaceholder.replacePlaceholders(entry.getValue(), placeholderResolver);
if (Strings.hasLength(value)) {
map.put(entry.getKey(), value);
}
}
return this;
}