Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Variable Processing #442

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,33 @@ public static String resolveVariables(CommonLoggerI log, String nodeValue, Colle
// Found recursive reference when resolving variables. Log message and return null.
log.debug("Found a recursive variable reference when resolving ${" + varName + "}");
return null;
} else {
variablesToResolve.add(varName);
}
variablesToResolve.add(varName);
}

for (String nextVariable : variablesToResolve) {
String value = getPropertyValue(nextVariable, props, defaultProps, libDirPropFiles);

if (value != null && !value.isEmpty()) {
Collection<String> thisVariableChain = new HashSet<String> ();
thisVariableChain.add(nextVariable);

if (variableChain != null && !variableChain.isEmpty()) {
thisVariableChain.addAll(variableChain);
}

String resolvedValue = resolveVariables(log, value, thisVariableChain, props, defaultProps, libDirPropFiles);

if (resolvedValue != null) {
String escapedVariable = Matcher.quoteReplacement(nextVariable);
// For Windows, avoid escaping the backslashes in the resolvedValue by changing to forward slashes
resolvedValue = resolvedValue.replace("\\","/");
resolved = resolved.replaceAll("\\$\\{" + escapedVariable + "\\}", resolvedValue);
} else {
// Variable value could not be resolved. Log message and return null.
log.debug("Could not resolve the value " + value + " for variable ${" + nextVariable + "}");
return null;
}
if (value == null || value.isEmpty()) {
// Variable could not be resolved. Log message and return null.
log.debug("Variable " + nextVariable + " cannot be resolved.");
return null;
}

Collection<String> thisVariableChain = new HashSet<String> ();
thisVariableChain.add(nextVariable);
dshimo marked this conversation as resolved.
Show resolved Hide resolved

if (variableChain != null && !variableChain.isEmpty()) {
thisVariableChain.addAll(variableChain);
}

String resolvedValue = resolveVariables(log, value, thisVariableChain, props, defaultProps, libDirPropFiles);

if (resolvedValue != null) {
String escapedVariable = Matcher.quoteReplacement(nextVariable);
// For Windows, avoid escaping the backslashes in the resolvedValue by changing to forward slashes
resolvedValue = resolvedValue.replace("\\","/");
resolved = resolved.replaceAll("\\$\\{" + escapedVariable + "\\}", resolvedValue);
} else {
// Variable could not be resolved. Log message and return null.
log.debug("Variable " + nextVariable + " cannot be resolved.");
Expand All @@ -90,35 +89,58 @@ public static String resolveVariables(CommonLoggerI log, String nodeValue, Colle
return resolved;
}

public static String getPropertyValue(String propertyName, Properties props, Properties defaultProps, Map<String, File> libDirPropFiles) {
// TODO: Integer value properties can be evaluated if 'simple' arithemetic
// TODO: A list of ports can be defined using keyword 'list', e.g. list(httpPort) -> 89,9889 versus literal '89,9889'
public static String getPropertyValue(String propertyName, Properties prop, Properties defaultProps, Map<String, File> libertyDirPropFiles) {
String value = null;
if(!libDirPropFiles.containsKey(propertyName)) {
value = props.getProperty(propertyName);
if (value == null) {
// Check for default value since no other value found.
value = defaultProps.getProperty(propertyName);
}
if (value == null && propertyName.startsWith("env.") && propertyName.length() > 4) {
// Look for property without the 'env.' prefix
String newPropName = propertyName.substring(4);
value = props.getProperty(newPropName);
if (value == null) {
// Check for default value since no other value found.
value = defaultProps.getProperty(newPropName);
}
}
} else {
File envDirectory = libDirPropFiles.get(propertyName);
value = envDirectory.toString();
}

if (value != null && value.startsWith("\"") && value.endsWith("\"")) {
// need to remove beginning/ending quotes
if (value.length() > 2) {
value = value.substring(1, value.length() -1);
if (libertyDirPropFiles.containsKey(propertyName)) {
return stripQuotes(libertyDirPropFiles.get(propertyName).toString());
}

value = lookupProperty(prop, defaultProps, propertyName);
if (value != null) {
return value;
}

// try again with non-alphanumeric values replaced with '_', which is exactly \W in regex
String propertyNameVariation = propertyName.replaceAll("\\W", "_");
value = lookupProperty(prop, defaultProps, propertyNameVariation);
if (value != null) {
return value;
}

// try again with propertyNameVariation.toUpperCase()
propertyNameVariation = propertyNameVariation.toUpperCase();
dshimo marked this conversation as resolved.
Show resolved Hide resolved
value = lookupProperty(prop, defaultProps, propertyNameVariation);
if (value != null) {
return value;
}

// support for versions <19.0.0.3. Look for property without the 'env.' prefix
if (propertyName != null && propertyName.startsWith("env.") && propertyName.length() > 4) {
value = lookupProperty(prop, defaultProps, propertyName.substring(4));
if (value != null) {
return value;
}
}

return value;
}

private static String stripQuotes(String value) {
if (value.startsWith("\"") && value.endsWith("\"") && value.length() > 2) {
return value.substring(1, value.length() - 1);
}
return value;
}


private static String lookupProperty(Properties prop, Properties defaultProps, String propertyName) {
if (prop.containsKey(propertyName)) {
return stripQuotes(prop.getProperty(propertyName));
dshimo marked this conversation as resolved.
Show resolved Hide resolved
}
if (defaultProps.containsKey(propertyName)) {
return stripQuotes(defaultProps.getProperty(propertyName));
}
return null;
}
}
Loading
Loading