Skip to content

Commit

Permalink
Merge pull request #18 from MeroRai/PAYARA-4228
Browse files Browse the repository at this point in the history
PAYARA-4228 Support vendor specific JVM options in Payara Server
  • Loading branch information
jGauravGupta authored Dec 9, 2019
2 parents a7e778c + 22c7308 commit 86c12d4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,45 @@ public static int getSubMinor() {
public static int getUpdate() {
return update;
}

public static String getVendor() {
return vendor;
}

public static class Version {

private final Optional<String> vendor;
private final int major;
private final Optional<Integer> minor;
private final Optional<Integer> subminor;
private final Optional<Integer> update;

private Version(String string) {
private Version(String version) {
// Split java version into its constituent parts, i.e.
// 1.2.3.4 -> [ 1, 2, 3, 4]
// 1.2.3u4 -> [ 1, 2, 3, 4]
// 1.2.3_4 -> [ 1, 2, 3, 4]
String[] split = string.split("[\\._u\\-]+");
if (version.contains("-")) {
String[] versionSplit = version.split("-");
this.vendor = versionSplit.length > 0 ? Optional.of(versionSplit[0]) : Optional.empty();
version = versionSplit.length > 1 ? versionSplit[1] : "";
} else {
this.vendor = Optional.empty();
}
String[] split = version.split("[\\._u\\-]+");

major = split.length > 0 ? Integer.parseInt(split[0]) : 0;
minor = split.length > 1 ? Optional.of(Integer.parseInt(split[1])) : Optional.empty();
subminor = split.length > 2 ? Optional.of(Integer.parseInt(split[2])) : Optional.empty();
update = split.length > 3 ? Optional.of(Integer.parseInt(split[3])) : Optional.empty();
}
private Version(int major, int minor, int subminor, int update) {
this.major = major;
this.minor = Optional.of(minor);
this.subminor = Optional.of(subminor);

private Version(int major, int minor, int subminor, int update, String vendor) {
this.major = major;
this.minor = Optional.of(minor);
this.subminor = Optional.of(subminor);
this.update = Optional.of(update);
this.vendor = Optional.of(vendor);
}

public boolean newerThan(Version version) {
Expand Down Expand Up @@ -221,6 +234,10 @@ public String toString() {
}

public static Version getVersion(String string) {
return getVersion(string, null);
}

public static Version getVersion(String string, String vendor) {
if (string != null && string.matches("([0-9]+[\\._u\\-]+)*[0-9]+")) {
// Make sure the string is a valid JDK version, i.e.
// 1.8.0_162 or something that is returned by "java -version"
Expand All @@ -230,24 +247,27 @@ public static Version getVersion(String string) {
return null;
}

public static Version getVersion(String jv, String javaSpecificationVersion) {
public static Version getVersion(String jv, String javaSpecificationVersion, String vendor) {
int[] versions = parseVersions(jv, javaSpecificationVersion);
return new Version(versions[MAJOR], versions[MINOR], versions[SUBMINOR], versions[UPDATE]);

return new Version(versions[MAJOR], versions[MINOR], versions[SUBMINOR], versions[UPDATE], vendor);
}

public static Version getVersion() {
return new Version(major, minor, subminor, update);
return new Version(major, minor, subminor, update, vendor);
}

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

public static boolean isCorrectJDK(Version JDKversion, Optional<Version> minVersion, Optional<Version> maxVersion) {
public static boolean isCorrectJDK(Version JDKversion, Optional<String> vendor, Optional<Version> minVersion, Optional<Version> maxVersion) {
boolean correctJDK = true;

if (minVersion.isPresent()) {
if (vendor.isPresent()) {
correctJDK = JDKversion.vendor.get().contains(vendor.get());
}
if (correctJDK && minVersion.isPresent()) {
correctJDK = JDKversion.newerOrEquals(minVersion.get());
}
if (correctJDK && maxVersion.isPresent()) {
Expand Down Expand Up @@ -279,6 +299,7 @@ public static String toStringStatic() {
private static int minor;
private static int subminor;
private static int update;
private static String vendor;

// DO initialize these variables. You'll be sorry if you don't!
private final static int MAJOR = 0;
Expand All @@ -287,7 +308,7 @@ public static String toStringStatic() {
private final static int UPDATE = 3;

// DO NOT initialize this variable. You'll again be sorry if you do!
private static Version JDK_VERSION;
public static Version JDK_VERSION;

private static void initialize() {

Expand All @@ -296,7 +317,8 @@ private static void initialize() {
minor = subminor = update = 0;

try {
String jv = System.getProperty("java.version");
String javaVersion = System.getProperty("java.version");
vendor = System.getProperty("java.vendor");
/*In JEP 223 java.specification.version will be a single number versioning , not a dotted versioning . So if we get a single
integer as versioning we know that the JDK is post JEP 223
For JDK 8:
Expand All @@ -308,7 +330,7 @@ private static void initialize() {
*/
String javaSpecificationVersion = System.getProperty("java.specification.version");

int[] versions = parseVersions(jv, javaSpecificationVersion);
int[] versions = parseVersions(javaVersion, javaSpecificationVersion);

major = versions[MAJOR];
minor = versions[MINOR];
Expand All @@ -319,7 +341,7 @@ private static void initialize() {
// ignore -- use defaults
}

JDK_VERSION = new Version(major, minor, subminor, update);
JDK_VERSION = new Version(major, minor, subminor, update, vendor);
}

static int[] parseVersions(String jv, String javaSpecificationVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.eclipse.payara.tools.sdk.server.JDK.isCorrectJDK;
import static org.eclipse.payara.tools.sdk.server.JDK.JDK_VERSION;
import static org.eclipse.payara.tools.sdk.server.ServerTasks.StartMode.DEBUG;
import static org.eclipse.payara.tools.sdk.server.parser.TreeParser.readXml;
import static org.eclipse.payara.tools.sdk.utils.JavaUtils.javaVmExecutableFullPath;
Expand Down Expand Up @@ -146,24 +147,14 @@ public static ResultProcess startServer(PayaraServer server, StartupArgs args, S
if (!readXml(new File(domainXmlPath), jvmConfigReader)) {
throw new PayaraIdeException(LOGGER.excMsg(METHOD, "readXMLerror"), domainXmlPath);
}

String[] versions = JavaUtils.getJavaVersionString(args.getJavaHome()).split(":");

JDK.Version targetJDKVersion;

if (versions.length == 1) {
// Simple way, just using version string
targetJDKVersion = JDK.getVersion(versions[0]);
} else {
// More eleborate way using both version string and spec version
targetJDKVersion = JDK.getVersion(versions[0], versions[1]);
}


JDK.Version targetJDKVersion = JDK_VERSION != null ? JDK_VERSION : getJavaVersion(args);

// Filter out all options that are not applicable
List<String> optList
= jvmConfigReader.getJvmOptions()
.stream()
.filter(fullOption -> isCorrectJDK(targetJDKVersion, fullOption.minVersion, fullOption.maxVersion))
.filter(fullOption -> isCorrectJDK(targetJDKVersion, fullOption.vendor, fullOption.minVersion, fullOption.maxVersion))
.map(fullOption -> fullOption.option)
.collect(toList());

Expand Down Expand Up @@ -236,6 +227,21 @@ public static ResultProcess startServer(PayaraServer server, StartupArgs args, S
}
}

private static JDK.Version getJavaVersion(StartupArgs args) {
String[] versions = JavaUtils.getJavaVersionString(args.getJavaHome()).split(":");
JDK.Version targetJDKVersion;

if (versions.length == 1) {
// Simple way, just using version string
targetJDKVersion = JDK.getVersion(versions[0]);
} else {
// More eleborate way using both version string and spec version
targetJDKVersion = JDK.getVersion(versions[0], versions[1], System.getProperty("java.vendor"));
}

return targetJDKVersion;
}

public static Integer getDebugPort(ResultProcess process) {
Matcher debugPortMatcher = debugPortPattern.matcher(process.getValue().getArguments());
if (debugPortMatcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.payara.tools.sdk.server.JDK;
import org.eclipse.payara.tools.sdk.server.parser.TreeParser.NodeListener;
import org.eclipse.payara.tools.sdk.server.parser.TreeParser.Path;
Expand Down Expand Up @@ -205,6 +204,7 @@ public boolean isMonitoringEnabled() {
public static class JvmOption {

public final String option;
public final Optional<String> vendor;
public final Optional<JDK.Version> minVersion;
public final Optional<JDK.Version> maxVersion;

Expand All @@ -221,18 +221,27 @@ public static class JvmOption {
public JvmOption(String option) {
Matcher matcher = PATTERN.matcher(option);
if (matcher.matches()) {
this.minVersion = Optional.ofNullable(JDK.getVersion(matcher.group(1)));
if (matcher.group(1).contains("-")) {
String[] parts = matcher.group(1).split("-");
this.vendor = Optional.ofNullable(parts[0]);
this.minVersion = Optional.ofNullable(JDK.getVersion(parts[1]));
} else {
this.vendor = Optional.empty();
this.minVersion = Optional.ofNullable(JDK.getVersion(matcher.group(1)));
}
this.maxVersion = Optional.ofNullable(JDK.getVersion(matcher.group(2)));
this.option = matcher.group(3);
} else {
this.option = option;
this.vendor = Optional.empty();
this.minVersion = Optional.empty();
this.maxVersion = Optional.empty();
}
}

public JvmOption(String option, String minVersion, String maxVersion) {
this.option = option;
this.vendor = Optional.empty();
this.minVersion = Optional.ofNullable(JDK.getVersion(minVersion));
this.maxVersion = Optional.ofNullable(JDK.getVersion(maxVersion));
}
Expand Down

0 comments on commit 86c12d4

Please sign in to comment.