use of liquibase.configuration.ConfigurationValueConverter in project liquibase by liquibase.
the class TestSystem method getConfiguredValue.
/**
* Returns the configured value for the given propertyName. It will check (in priority order):
* <ol>
* <li>properties set directly on this object</li>
* <li>liquibase.sdk.testSystem.[name].[profile(s)].propertyName in the order the profiles are set on this object</li>
* <li>liquibase.sdk.testSystem.[name].propertyName</li>
* <li>liquibase.sdk.testSystem.default.propertyName</li>
* </ol>
* <br>
* If a value is not found, it will return null or throw an {@link UnexpectedLiquibaseException} if 'required' is true.
*/
public <T> T getConfiguredValue(String propertyName, ConfigurationValueConverter<T> converter, boolean required) {
ConfigurationValueConverter<T> finalConverter = value -> {
if (value instanceof String && ((String) value).contains("${")) {
final Matcher matcher = Pattern.compile("(\\$\\{.+?})").matcher((String) value);
while (matcher.find()) {
final String config = matcher.group(1).replace("${", "").replace("}", "").trim();
value = ((String) value).replace(matcher.group(1), getConfiguredValue(config, String.class));
}
}
if (converter == null) {
return (T) value;
} else {
return converter.convert(value);
}
};
final SortedMap<String, Object> properties = definition.getProperties();
if (properties.containsKey(propertyName)) {
return finalConverter.convert(properties.get(propertyName));
}
final LiquibaseConfiguration config = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class);
ConfiguredValue<T> configuredValue;
// first check profiles
for (String profile : definition.getProfiles()) {
configuredValue = config.getCurrentConfiguredValue(finalConverter, null, "liquibase.sdk.testSystem." + getDefinition().getName() + ".profiles." + profile + "." + propertyName);
if (configuredValue.found()) {
return configuredValue.getValue();
}
}
configuredValue = config.getCurrentConfiguredValue(finalConverter, null, "liquibase.sdk.testSystem." + getDefinition().getName() + "." + propertyName);
if (configuredValue.found()) {
return configuredValue.getValue();
}
// fall back to "default" setup
configuredValue = config.getCurrentConfiguredValue(finalConverter, null, "liquibase.sdk.testSystem.default." + propertyName);
if (configuredValue.found()) {
return configuredValue.getValue();
}
if (required) {
throw new UnexpectedLiquibaseException("No required liquibase.sdk.testSystem configuration for " + getDefinition().getName() + " of " + propertyName + " set");
}
return null;
}
Aggregations