use of org.apache.commons.lang.text.StrLookup in project cuba by cuba-platform.
the class TestContainer method initAppProperties.
protected void initAppProperties() {
final Properties properties = new Properties();
List<String> locations = getAppPropertiesFiles();
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
for (String location : locations) {
Resource resource = resourceLoader.getResource(location);
if (resource.exists()) {
InputStream stream = null;
try {
stream = resource.getInputStream();
properties.load(stream);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(stream);
}
} else {
log.warn("Resource " + location + " not found, ignore it");
}
}
StrSubstitutor substitutor = new StrSubstitutor(new StrLookup() {
@Override
public String lookup(String key) {
String subst = properties.getProperty(key);
return subst != null ? subst : System.getProperty(key);
}
});
for (Object key : properties.keySet()) {
String value = substitutor.replace(properties.getProperty((String) key));
appProperties.put((String) key, value);
}
File dir;
dir = new File(appProperties.get("cuba.confDir"));
dir.mkdirs();
dir = new File(appProperties.get("cuba.logDir"));
dir.mkdirs();
dir = new File(appProperties.get("cuba.tempDir"));
dir.mkdirs();
dir = new File(appProperties.get("cuba.dataDir"));
dir.mkdirs();
}
use of org.apache.commons.lang.text.StrLookup in project kylo by Teradata.
the class PropertyExpressionResolver method resolveVariables.
/**
* Resolves the variables in the value of the specified property.
*
* @param property the property
* @param metadata the feed
* @return the result of the transformation
*/
private ResolveResult resolveVariables(@Nonnull final NifiProperty property, @Nonnull final FeedMetadata metadata) {
// Filter blank values
final String value = property.getValue();
if (StringUtils.isBlank(value)) {
return new ResolveResult(false, false);
}
final boolean[] hasConfig = { false };
final boolean[] isModified = { false };
StrLookup resolver = new StrLookup() {
@Override
public String lookup(String variable) {
// Resolve configuration variables
final String configValue = getConfigurationPropertyValue(property, variable);
if (configValue != null && property.getValue() != null && !property.getValue().equalsIgnoreCase(configValue)) {
hasConfig[0] = true;
isModified[0] = true;
// if this is the first time we found the config var, set the template value correctly
if (!property.isContainsConfigurationVariables()) {
property.setTemplateValue(property.getValue());
property.setContainsConfigurationVariables(true);
}
return configValue;
}
// Resolve metadata variables
try {
final String metadataValue = getMetadataPropertyValue(metadata, variable);
if (metadataValue != null) {
isModified[0] = true;
return metadataValue;
}
} catch (Exception e) {
log.error("Unable to resolve variable: " + variable, e);
}
return null;
}
};
StrSubstitutor ss = new StrSubstitutor(resolver);
ss.setEnableSubstitutionInVariables(true);
// escape
String val = StringUtils.trim(ss.replace(value));
// fix
property.setValue(val);
return new ResolveResult(hasConfig[0], isModified[0]);
}
use of org.apache.commons.lang.text.StrLookup in project kylo by Teradata.
the class PropertyExpressionResolver method resolveVariables.
/**
* Resolve the str with values from the supplied {@code properties} This will recursively fill out the str looking back at the properties to get the correct value. NOTE the property values will be
* overwritten if replacement is found!
*/
public ResolvedVariables resolveVariables(String str, List<NifiProperty> properties) {
ResolvedVariables variables = new ResolvedVariables(str);
StrLookup resolver = new StrLookup() {
@Override
public String lookup(String key) {
Optional<NifiProperty> optional = properties.stream().filter(prop -> key.equals(prop.getKey())).findFirst();
if (optional.isPresent()) {
NifiProperty property = optional.get();
String value = StringUtils.isNotBlank(property.getValue()) ? property.getValue().trim() : "";
variables.getResolvedVariables().put(property.getKey(), value);
return value;
} else {
return null;
}
}
};
StrSubstitutor ss = new StrSubstitutor(resolver);
variables.setResolvedString(ss.replace(str));
Map<String, String> map = variables.getResolvedVariables();
Map<String, String> vars = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> ss.replace(entry.getValue())));
variables.getResolvedVariables().putAll(vars);
return variables;
}
use of org.apache.commons.lang.text.StrLookup in project kylo by Teradata.
the class PropertyExpressionResolver method replaceAll.
public String replaceAll(String str, String replacement) {
if (str != null) {
StrLookup lookup = new StrLookup() {
@Override
public String lookup(String key) {
return replacement;
}
};
StrSubstitutor ss = new StrSubstitutor(lookup);
return ss.replace(str);
}
return null;
}
Aggregations