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

Var processing changes for ci gradle and ci maven. #462

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2023
* (C) Copyright IBM Corporation 2023, 2424
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,10 +32,10 @@ public class VariableUtility {
* Attempts to resolve all variables in the passed in nodeValue. Variable value/defaultValue can reference other variables.
* This method is called recursively to resolve the variables. The variableChain collection keeps track of the variable references
* in a resolution chain in order to prevent an infinite loop. The variableChain collection should be passed as null on the initial call.
*
*
* NOTE: This method also replaces all back slashes with forward slashes
*/
public static String resolveVariables(CommonLoggerI log, String nodeValue, Collection<String> variableChain,
public static String resolveVariables(CommonLoggerI log, String nodeValue, Collection<String> variableChain,
Properties props, Properties defaultProps, Map<String, File> libDirPropFiles) {

// For Windows, avoid escaping the backslashes in the resolvedValue by changing to forward slashes
Expand All @@ -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);

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,61 @@ 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();
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 == null) {
return null;
}
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));
}
if (defaultProps.containsKey(propertyName)) {
return stripQuotes(defaultProps.getProperty(propertyName));
}
return null;
}
}
Loading
Loading