From 8037b226f3755c89286df941ece92046d0a0e2b4 Mon Sep 17 00:00:00 2001 From: Sajeer Date: Fri, 27 Feb 2026 15:00:36 +0530 Subject: [PATCH] Added the ability to accept environment variables in install feature and productInfo process will accept JAVA_HOME from it --- .../plugins/util/InstallFeatureUtil.java | 40 +++++++++++++++++-- .../util/BaseInstallFeatureUtilTest.java | 2 +- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/openliberty/tools/common/plugins/util/InstallFeatureUtil.java b/src/main/java/io/openliberty/tools/common/plugins/util/InstallFeatureUtil.java index d06597c1f..889aa7ea8 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/util/InstallFeatureUtil.java +++ b/src/main/java/io/openliberty/tools/common/plugins/util/InstallFeatureUtil.java @@ -92,7 +92,7 @@ public abstract class InstallFeatureUtil extends ServerFeatureUtil { private final Collection> keyMap; - + private final Map environmentVariables; /** * An enum for specifying verify option @@ -137,11 +137,14 @@ public enum VerifyOption { * installed in a container. Otherwise null. * @param additionalJsons The list of additional JSONS to search * for features from + * @param verifyValue The verify option value + * @param keyMap The key map for feature verification + * @param environmentVariables Environment variables to pass to processes (e.g., JAVA_HOME) * @throws PluginScenarioException If the current scenario is not supported * @throws PluginExecutionException If properties files cannot be found in the * installDirectory/lib/versions */ - public InstallFeatureUtil(File installDirectory, File buildDirectory, String from, String to, Set pluginListedEsas, List propertiesList, String openLibertyVersion, String containerName, List additionalJsons, String verifyValue, Collection> keyMap) throws PluginScenarioException, PluginExecutionException { + public InstallFeatureUtil(File installDirectory, File buildDirectory, String from, String to, Set pluginListedEsas, List propertiesList, String openLibertyVersion, String containerName, List additionalJsons, String verifyValue, Collection> keyMap, Map environmentVariables) throws PluginScenarioException, PluginExecutionException { this.installDirectory = installDirectory; this.buildDirectory = buildDirectory; this.to = to; @@ -157,6 +160,8 @@ public InstallFeatureUtil(File installDirectory, File buildDirectory, String fro }else { this.keyMap = keyMap; } + + this.environmentVariables = environmentVariables == null ? new HashMap<>() : environmentVariables; try { this.verifyOption = VerifyOption.valueOf(verifyValue); @@ -1119,7 +1124,7 @@ private static int compare(String version1, String version2) { * if product validation failed or could not be run */ private void productInfoValidate() throws PluginExecutionException { - String output = productInfo(installDirectory, "validate"); + String output = productInfo(installDirectory, "validate", environmentVariables); if (output == null) { throw new PluginExecutionException( "Could not perform product validation. The productInfo command returned with no output"); @@ -1140,6 +1145,20 @@ private void productInfoValidate() throws PluginExecutionException { * @throws PluginExecutionException if the exit value of the command was not 0 */ public static String productInfo(File installDirectory, String action) throws PluginExecutionException { + return productInfo(installDirectory, action, null); + } + + /** + * Runs the productInfo command with environment variables and returns the output + * Made public static for tests to use in LMP/LGP + * + * @param installDirectory The directory of the installed runtime + * @param action The action to perform for the productInfo command + * @param envVars Environment variables to set (e.g., JAVA_HOME). Can be null. + * @return The command output + * @throws PluginExecutionException if the exit value of the command was not 0 + */ + public static String productInfo(File installDirectory, String action, Map envVars) throws PluginExecutionException { Process pr = null; BufferedReader in = null; StringBuilder sb = new StringBuilder(); @@ -1152,6 +1171,21 @@ public static String productInfo(File installDirectory, String action) throws Pl productInfoFile = installDirectory + "/bin/productInfo"; } ProcessBuilder pb = new ProcessBuilder(productInfoFile, action); + + // Apply environment variables from toolchain if provided + if (envVars != null && !envVars.isEmpty()) { + pb.environment().putAll(envVars); + } + + // Fallback to system property if JAVA_HOME not in envVars + if (envVars == null || !envVars.containsKey("JAVA_HOME")) { + Properties sysp = System.getProperties(); + String javaHome = sysp.getProperty("java.home"); + if (javaHome != null) { + pb.environment().put("JAVA_HOME", javaHome); + } + } + pr = pb.start(); in = new BufferedReader(new InputStreamReader(pr.getInputStream())); diff --git a/src/test/java/io/openliberty/tools/common/plugins/util/BaseInstallFeatureUtilTest.java b/src/test/java/io/openliberty/tools/common/plugins/util/BaseInstallFeatureUtilTest.java index 3460fc4fa..9e538de11 100644 --- a/src/test/java/io/openliberty/tools/common/plugins/util/BaseInstallFeatureUtilTest.java +++ b/src/test/java/io/openliberty/tools/common/plugins/util/BaseInstallFeatureUtilTest.java @@ -58,7 +58,7 @@ public void setupInstallDir() throws IOException { public class InstallFeatureTestUtil extends InstallFeatureUtil { public InstallFeatureTestUtil(File installDirectory, File buildDirectory, String from, String to, Set pluginListedEsas, List propertiesList, String openLibertyVersion, List additionalJsons, String verifyOption, Collection> keyMap) throws PluginScenarioException, PluginExecutionException { - super(installDirectory, buildDirectory, from, to, pluginListedEsas, propertiesList, openLibertyVersion, null, additionalJsons, verifyOption, keyMap); + super(installDirectory, buildDirectory, from, to, pluginListedEsas, propertiesList, openLibertyVersion, null, additionalJsons, verifyOption, keyMap, null); } @Override