Skip to content

Commit

Permalink
Merge pull request #4034 from vlumi/issue-4025
Browse files Browse the repository at this point in the history
PAYARA-3911 Fix domain.xml java-home is not honored in version-specific jvm-options (#4025)
  • Loading branch information
arjantijms authored Jul 4, 2019
2 parents cd17fdf + acd3723 commit b02b03d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2018] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2019] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.admin.launcher;

Expand All @@ -60,6 +60,8 @@
import static com.sun.enterprise.admin.launcher.GFLauncherConstants.*;
import com.sun.enterprise.util.JDK;
import fish.payara.admin.launcher.PayaraDefaultJvmOptions;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand All @@ -70,7 +72,8 @@
* @author bnevins
*/
public abstract class GFLauncher {


private static final Pattern JAVA_VERSION_PATTERN = Pattern.compile(".* version \"([^\"\\-]+)(-.*)?\".*");
private final List<String> commandLine = new ArrayList<String>();
private final List<String> jvmOptionsList = new ArrayList<String>();
private final GFLauncherInfo info;
Expand Down Expand Up @@ -187,6 +190,14 @@ public void setup() throws GFLauncherException, MiniXmlParserException {
}
info.setAdminAddresses(parser.getAdminAddresses());
javaConfig = new JavaConfig(parser.getJavaConfig());
// Set the config java-home value as the Java home for the environment,
// unless it is empty or it is already refering to a substitution of
// the environment variable.
String jhome = javaConfig.getJavaHome();
if (GFLauncherUtils.ok(jhome) && !jhome.trim().equals("${" + JAVA_ROOT_PROPERTY + "}")) {
asenvProps.put(JAVA_ROOT_PROPERTY, jhome);
}
setJavaExecutable();
setupProfilerAndJvmOptions(parser);
setupUpgradeSecurity();

Expand All @@ -206,21 +217,13 @@ public void setup() throws GFLauncherException, MiniXmlParserException {
sysPropsFromXml = parser.getSystemProperties();
asenvProps.put(INSTANCE_ROOT_PROPERTY, getInfo().getInstanceRootDir().getPath());

// Set the config java-home value as the Java home for the environment,
// unless it is empty or it is already refering to a substitution of
// the environment variable.
String jhome = javaConfig.getJavaHome();
if (GFLauncherUtils.ok(jhome) && !jhome.trim().equals("${" + JAVA_ROOT_PROPERTY + "}")) {
asenvProps.put(JAVA_ROOT_PROPERTY, jhome);
}
debugOptions = getDebug();
parseDebug();
parser.setupConfigDir(getInfo().getConfigDir(), getInfo().getInstallDir());
setLogFilename(parser);
resolveAllTokens();
fixLogFilename();
GFLauncherLogger.addLogFileHandler(logFilename);
setJavaExecutable();
setClasspath();
setCommandLine();
setJvmOptions();
Expand Down Expand Up @@ -867,9 +870,10 @@ private void setupProfilerAndJvmOptions(MiniXmlParser parser) throws MiniXmlPars
parser.getProfilerJvmOptions(),
parser.getProfilerSystemProperties());

Optional<JDK.Version> jdkVersion = getConfiguredJdkVersion(javaExe);
List<String> rawJvmOptions = parser.getJvmOptions()
.stream()
.filter(fullOption -> JDK.isCorrectJDK(fullOption.minVersion, fullOption.maxVersion))
.filter(fullOption -> JDK.isCorrectJDK(jdkVersion, fullOption.minVersion, fullOption.maxVersion))
.map(option -> option.option)
.collect(Collectors.toList());
rawJvmOptions.addAll(getSpecialSystemProperties());
Expand All @@ -884,7 +888,35 @@ private void setupProfilerAndJvmOptions(MiniXmlParser parser) throws MiniXmlPars
// PAYARA-1681 - Add default Payara JVM options if an override isn't in place
addDefaultJvmOptions();
}


/**
* Get the Java version from the given path to a Java executable.
*
* @param javaExePath The full path to the executable java command.
* @return The Java version as a JDK.Version object, if successful.
* @throws GFLauncherException
*/
private Optional<JDK.Version> getConfiguredJdkVersion(String javaExePath) throws GFLauncherException {
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec(javaExePath + " -version");
p.waitFor();
try (BufferedReader b = new BufferedReader(new InputStreamReader(p.getErrorStream()))) {
String line = b.readLine();
if (line == null) {
return Optional.empty();
}
Matcher m = JAVA_VERSION_PATTERN.matcher(line);
if (m.matches()) {
return Optional.ofNullable(JDK.getVersion(m.group(1)));
}
}
return Optional.empty();
} catch (IOException | InterruptedException ex) {
throw new GFLauncherException("nojvm");
}
}

private void addDefaultJvmOptions() {
if (!jvmOptions.getCombinedMap().containsKey(PayaraDefaultJvmOptions.GRIZZLY_DEFAULT_MEMORY_MANAGER_PROPERTY)) {
jvmOptions.sysProps.put(PayaraDefaultJvmOptions.GRIZZLY_DEFAULT_MEMORY_MANAGER_PROPERTY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018] [Payara Foundation and/or its affiliates]
// Portions Copyright [2018-2019] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.util;

Expand Down Expand Up @@ -240,12 +240,25 @@ public static Version getVersion() {
}

public static boolean isCorrectJDK(Optional<Version> minVersion, Optional<Version> maxVersion) {
return isCorrectJDK(Optional.of(JDK_VERSION), minVersion, maxVersion);
}

/**
* Check if the reference version falls between the minVersion and maxVersion.
*
* @param reference The version to compare; falls back to the current JDK version if empty.
* @param minVersion The inclusive minimum version.
* @param maxVersion The inclusive maximum version.
* @return true if within the version range, false otherwise
*/
public static boolean isCorrectJDK(Optional<Version> reference, Optional<Version> minVersion, Optional<Version> maxVersion) {
Version version = reference.orElse(JDK_VERSION);
boolean correctJDK = true;
if (minVersion.isPresent()) {
correctJDK = JDK_VERSION.newerOrEquals(minVersion.get());
correctJDK = version.newerOrEquals(minVersion.get());
}
if (correctJDK && maxVersion.isPresent()) {
correctJDK = JDK_VERSION.olderOrEquals(maxVersion.get());
correctJDK = version.olderOrEquals(maxVersion.get());
}
return correctJDK;
}
Expand Down

0 comments on commit b02b03d

Please sign in to comment.