From 3399fd36dc09b0e65bfb840b6e63920f3f1c209b Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 13 Sep 2017 15:20:43 +1000 Subject: [PATCH] Fix #1814 Java Version utility --- .../org/eclipse/jetty/start/JavaVersion.java | 6 + .../org/eclipse/jetty/util/JavaVersion.java | 166 ++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 jetty-util/src/main/java/org/eclipse/jetty/util/JavaVersion.java diff --git a/jetty-start/src/main/java/org/eclipse/jetty/start/JavaVersion.java b/jetty-start/src/main/java/org/eclipse/jetty/start/JavaVersion.java index 59b667500bb..5fe87ac85e0 100644 --- a/jetty-start/src/main/java/org/eclipse/jetty/start/JavaVersion.java +++ b/jetty-start/src/main/java/org/eclipse/jetty/start/JavaVersion.java @@ -21,8 +21,14 @@ package org.eclipse.jetty.start; import java.util.regex.Matcher; import java.util.regex.Pattern; +/** + * Java Version Utility class. + *

Parses java versions to extract a consistent set of version parts

+ */ public class JavaVersion { + // Copy of code in jetty-util + private static final Pattern PRE_JDK9 = Pattern.compile("1\\.(\\d)(\\.(\\d+)(_(\\d+))?)?(-.+)?"); // Regexp from JEP 223 (http://openjdk.java.net/jeps/223). private static final Pattern JDK9 = Pattern.compile("(\\d+)(\\.(\\d+))?(\\.(\\d+))?((-.+)?(\\+(\\d+)?(-.+)?)?)"); diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/JavaVersion.java b/jetty-util/src/main/java/org/eclipse/jetty/util/JavaVersion.java new file mode 100644 index 00000000000..6dd630251ec --- /dev/null +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/JavaVersion.java @@ -0,0 +1,166 @@ +// +// ======================================================================== +// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Java Version Utility class. + *

Parses java versions to extract a consistent set of version parts

+ */ +public class JavaVersion +{ + // Copy of version in jetty-start + + private static final Pattern PRE_JDK9 = Pattern.compile("1\\.(\\d)(\\.(\\d+)(_(\\d+))?)?(-.+)?"); + // Regexp from JEP 223 (http://openjdk.java.net/jeps/223). + private static final Pattern JDK9 = Pattern.compile("(\\d+)(\\.(\\d+))?(\\.(\\d+))?((-.+)?(\\+(\\d+)?(-.+)?)?)"); + + public static final JavaVersion VERSION = parse(System.getProperty("java.version")); + + public static JavaVersion parse(String version) + { + if (version.startsWith("1.")) + return parsePreJDK9(version); + return parseJDK9(version); + } + + private static JavaVersion parsePreJDK9(String version) + { + Matcher matcher = PRE_JDK9.matcher(version); + if (!matcher.matches()) + throw new IllegalArgumentException("Invalid Java version " + version); + int major = 1; + int minor = Integer.parseInt(matcher.group(1)); + String microGroup = matcher.group(3); + int micro = microGroup == null || microGroup.isEmpty() ? 0 : Integer.parseInt(microGroup); + String updateGroup = matcher.group(5); + int update = updateGroup == null || updateGroup.isEmpty() ? 0 : Integer.parseInt(updateGroup); + String suffix = matcher.group(6); + return new JavaVersion(version, minor, major, minor, micro, update, suffix); + } + + private static JavaVersion parseJDK9(String version) + { + Matcher matcher = JDK9.matcher(version); + if (!matcher.matches()) + throw new IllegalArgumentException("Invalid Java version " + version); + int major = Integer.parseInt(matcher.group(1)); + String minorGroup = matcher.group(3); + int minor = minorGroup == null || minorGroup.isEmpty() ? 0 : Integer.parseInt(minorGroup); + String microGroup = matcher.group(5); + int micro = microGroup == null || microGroup.isEmpty() ? 0 : Integer.parseInt(microGroup); + String suffix = matcher.group(6); + return new JavaVersion(version, major, major, minor, micro, 0, suffix); + } + + private final String version; + private final int platform; + private final int major; + private final int minor; + private final int micro; + private final int update; + private final String suffix; + + private JavaVersion(String version, int platform, int major, int minor, int micro, int update, String suffix) + { + this.version = version; + this.platform = platform; + this.major = major; + this.minor = minor; + this.micro = micro; + this.update = update; + this.suffix = suffix; + } + + /** + * @return the string from which this JavaVersion was created + */ + public String getVersion() + { + return version; + } + + /** + *

Returns the Java Platform version, such as {@code 8} for JDK 1.8.0_92 and {@code 9} for JDK 9.2.4.

+ * + * @return the Java Platform version + */ + public int getPlatform() + { + return platform; + } + + /** + *

Returns the major number version, such as {@code 1} for JDK 1.8.0_92 and {@code 9} for JDK 9.2.4.

+ * + * @return the major number version + */ + public int getMajor() + { + return major; + } + + /** + *

Returns the minor number version, such as {@code 8} for JDK 1.8.0_92 and {@code 2} for JDK 9.2.4.

+ * + * @return the minor number version + */ + public int getMinor() + { + return minor; + } + + /** + *

Returns the micro number version, such as {@code 0} for JDK 1.8.0_92 and {@code 4} for JDK 9.2.4.

+ * + * @return the micro number version + */ + public int getMicro() + { + return micro; + } + + /** + *

Returns the update number version, such as {@code 92} for JDK 1.8.0_92 and {@code 0} for JDK 9.2.4.

+ * + * @return the update number version + */ + public int getUpdate() + { + return update; + } + + /** + *

Returns the remaining string after the version numbers, such as {@code -internal} for + * JDK 1.8.0_92-internal and {@code -ea} for JDK 9-ea, or {@code +13} for JDK 9.2.4+13.

+ * + * @return the remaining string after the version numbers + */ + public String getSuffix() + { + return suffix; + } + + public String toString() + { + return version; + } +}