Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2017, 2025.
* (C) Copyright IBM Corporation 2017, 2026.
*
* 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 @@ -34,6 +34,8 @@
import java.util.Set;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
Expand All @@ -46,6 +48,7 @@
import javax.xml.xpath.XPathFactory;

import io.openliberty.tools.common.plugins.util.LibertyPropFilesUtility;
import io.openliberty.tools.common.plugins.util.OSUtil;
import io.openliberty.tools.common.plugins.util.PluginExecutionException;
import org.apache.commons.io.comparator.NameFileComparator;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -88,6 +91,10 @@ public class ServerConfigDocument {
private static final XPathExpression XPATH_SERVER_INCLUDE;
public static final XPathExpression XPATH_SERVER_VARIABLE;
private static final XPathExpression XPATH_ALL_SERVER_APPLICATIONS;
// Windows style: !VAR!
private static final Pattern WINDOWS_EXPANSION_VAR_PATTERN;
// Linux style: ${VAR}
private static final Pattern LINUX_EXPANSION_VAR_PATTERN;


static {
Expand All @@ -106,6 +113,8 @@ public class ServerConfigDocument {
// correct
throw new RuntimeException(ex);
}
WINDOWS_EXPANSION_VAR_PATTERN = Pattern.compile("!(\\w+)!");
LINUX_EXPANSION_VAR_PATTERN = Pattern.compile("\\$\\{(\\w+)\\}");
}

public Set<String> getLocations() {
Expand Down Expand Up @@ -321,6 +330,42 @@ public void processServerEnv() throws Exception, FileNotFoundException {
parsePropertiesFromFile(new File(libertyDirectoryPropertyToFile.get(ServerFeatureUtil.WLP_USER_DIR),
"shared" + File.separator + serverEnvString));
parsePropertiesFromFile(getFileFromConfigDirectory(serverEnvString));
props.forEach((k, v) -> props.setProperty((String)k, resolveExpansionProperties(props, (String)v)));
}

/**
* Resolves both Windows (!VAR!) and Linux (${VAR}) placeholders.
*/
private String resolveExpansionProperties(Properties props, String value) {
if (value == null) return null;
// Resolve Linux style or Windows style
String result;
if (OSUtil.isWindows()) {
result = resolveByPattern(props, value, WINDOWS_EXPANSION_VAR_PATTERN);
} else {
result = resolveByPattern(props, value, LINUX_EXPANSION_VAR_PATTERN);
}
return result;
}

private String resolveByPattern(Properties props, String value, Pattern pattern) {
StringBuilder sb = new StringBuilder(value);
Matcher matcher = pattern.matcher(sb);

while (matcher.find()) {
String varName = matcher.group(1);
String replacement = props.getProperty(varName);
if (replacement != null) {
// Recursively resolve the replacement
String resolvedReplacement = resolveExpansionProperties(props, replacement);
String sbBeforeReplacement = sb.toString();
sb.replace(matcher.start(), matcher.end(), resolvedReplacement);
log.debug("Found a recursive variable reference when resolving "+ sbBeforeReplacement +" in server.env. Resolved value is " + sb);
// Reset matcher because the string length changed
matcher = pattern.matcher(sb);
}
}
return sb.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corporation 2023, 2024.
* (C) Copyright IBM Corporation 2023, 2026.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*/
package io.openliberty.tools.common.plugins.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
Expand Down Expand Up @@ -91,7 +92,14 @@ public void testAppLocationUsesLibertyProperty() throws Exception {
assertTrue("App location four not found.", locFourFound);
assertTrue("App location five not found.", locFiveFound);
assertTrue("App location six not found.", locSixFound);

if (OSUtil.isWindows()) {
assertEquals("Variable Expanded for !VAR!", "DEFINED_VAL", scd.getProperties().getProperty("this2_value"));
assertEquals("Variable Expanded for ${VAR}", "DEFINED\\old_value\\dir", scd.getProperties().getProperty("this5_value"));
} else {
assertEquals("Variable Expanded for ${VAR}", "DEFINED_VAL", scd.getProperties().getProperty("this3_value"));
assertEquals("Variable Expanded for ${VAR}", "DEFINED/old_value/dir", scd.getProperties().getProperty("this4_value"));
}
assertEquals("Variable not Expanded for !this_val", "!this_val", scd.getProperties().getProperty("this6_value"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ keystore_password=C7ANPlAi0MQD154BJ5ZOURn
http.port=1111
overriden_value=old_value
this_value=DEFINED
bootstrap.properties.override=false
bootstrap.properties.override=false
this2_value=!this_value!_VAL
this3_value=${this_value}_VAL
this4_value=${this_value}/${overriden_value}/dir
this5_value=!this_value!\\!overriden_value!\\dir
this6_value=!this_val