Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite JavaVersion #15372

Merged
merged 3 commits into from Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -34,7 +34,7 @@
* The implementation of a constructor utility method would look something like this example:
* <pre>
* public static &lt;S&gt; MethodProbe createLongMethodProbe(Method method, Probe probe, int type) {
* if (JavaVersion.isAtLeast(JAVA_1_8)) {
* if (JavaVersion.isAtLeast(JAVA_8)) {
* return new LongMethodProbeJdk8&lt;S&gt;(method, probe, type);
* } else {
* return new MethodProbe.LongMethodProbe&lt;S&gt;(method, probe, type);
Expand Down
Expand Up @@ -16,8 +16,8 @@

package com.hazelcast.internal.usercodedeployment.impl;

import com.hazelcast.config.UserCodeDeploymentConfig;
import com.hazelcast.cluster.Member;
import com.hazelcast.config.UserCodeDeploymentConfig;
import com.hazelcast.internal.cluster.ClusterService;
import com.hazelcast.internal.usercodedeployment.UserCodeDeploymentClassLoader;
import com.hazelcast.internal.usercodedeployment.UserCodeDeploymentService;
Expand All @@ -27,6 +27,7 @@
import com.hazelcast.nio.IOUtil;
import com.hazelcast.spi.NodeEngine;
import com.hazelcast.spi.impl.operationservice.OperationService;
import com.hazelcast.util.ContextMutexFactory;

import java.io.Closeable;
import java.security.PrivilegedAction;
Expand Down Expand Up @@ -58,7 +59,7 @@ public final class ClassLocator {
private final Filter<Member> memberFilter;
private final UserCodeDeploymentConfig.ClassCacheMode classCacheMode;
private final NodeEngine nodeEngine;
private final ClassloadingMutexProvider mutexFactory = new ClassloadingMutexProvider();
private final ContextMutexFactory mutexFactory = new ContextMutexFactory();
private final ILogger logger;

public ClassLocator(ConcurrentMap<String, ClassSource> classSourceMap,
Expand Down Expand Up @@ -100,7 +101,7 @@ public void defineClassFromClient(final String name, final byte[] classDef) {
// Java 7+ can use locks with per-class granularity while Java 6 has to use a single lock
// mutexFactory abstract these differences away
String mainClassName = extractMainClassName(name);
Closeable classMutex = mutexFactory.getMutexForClass(mainClassName);
Closeable classMutex = mutexFactory.mutexFor(mainClassName);
try {
synchronized (classMutex) {
ClassSource classSource = clientClassSourceMap.get(mainClassName);
Expand All @@ -115,12 +116,7 @@ public void defineClassFromClient(final String name, final byte[] classDef) {
return;
}
} else {
classSource = doPrivileged(new PrivilegedAction<ClassSource>() {
@Override
public ClassSource run() {
return new ClassSource(parent, ClassLocator.this);
}
});
classSource = doPrivileged((PrivilegedAction<ClassSource>) () -> new ClassSource(parent, ClassLocator.this));
clientClassSourceMap.put(mainClassName, classSource);
}
classSource.define(name, classDef);
Expand All @@ -135,7 +131,7 @@ private Class<?> tryToGetClassFromRemote(String name) throws ClassNotFoundExcept
// Java 7+ can use locks with per-class granularity while Java 6 has to use a single lock
// mutexFactory abstract these differences away
String mainClassName = extractMainClassName(name);
Closeable classMutex = mutexFactory.getMutexForClass(mainClassName);
Closeable classMutex = mutexFactory.mutexFor(mainClassName);
try {
synchronized (classMutex) {
ClassSource classSource = classSourceMap.get(mainClassName);
Expand Down

This file was deleted.

@@ -0,0 +1,32 @@
/*
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hazelcast.internal.util;

/**
* Interface used by {@link JavaVersion} and
* {@link JavaVersion.FutureJavaVersion}. This interface is needed only
* to do version comparison safely on runtime environments with versions
* not listed in {@link JavaVersion}.
*/
interface JavaMajorVersion {
/**
* Returns the major version.
*
* @return the major version
*/
int getMajorVersion();
}
151 changes: 107 additions & 44 deletions hazelcast/src/main/java/com/hazelcast/internal/util/JavaVersion.java
Expand Up @@ -16,28 +16,52 @@

package com.hazelcast.internal.util;

import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;

import java.lang.reflect.Method;

/**
* Utility for checking runtime Java version.
*
* <p>
* This class relies on the {@code java.lang.Runtime.Version.major()} method
* available since Java 9, therefore it's accessed via reflection. If
* {@code java.lang.Runtime.Version} is not present, we treat the runtime
* environment as Java 8. Also, if there is any exception thrown during the
* reflective access, we fall back to Java 8.
* <p>
* Since we rely on a public Java API returning the major Java version
* version comparisons can be done safely even with versions that didn't
* exist at the time the given Hazelcast version was released.
* See {@link FutureJavaVersion).
*/
public enum JavaVersion {
UNKNOWN,
JAVA_1_6,
JAVA_1_7,
JAVA_1_8,
JAVA_9,
JAVA_10,
JAVA_11,
JAVA_12;

private static final JavaVersion CURRENT_VERSION = detectCurrentVersion();
public enum JavaVersion implements JavaMajorVersion {
JAVA_8(8),
JAVA_9(9),
JAVA_10(10),
JAVA_11(11),
JAVA_12(12),
JAVA_13(13),
JAVA_14(14);

private static final JavaMajorVersion CURRENT_VERSION = detectCurrentVersion();

private int majorVersion;

JavaVersion(int majorVersion) {
this.majorVersion = majorVersion;
}

@Override
public int getMajorVersion() {
return majorVersion;
}

/**
* Check if the current runtime version is at least the given version.
*
* @param version version to be compared against the current runtime version
* @return Return true if current runtime version of Java is the same or greater than given version.
* When the passed version is {@link #UNKNOWN} then it always returns true.
*/
public static boolean isAtLeast(JavaVersion version) {
return isAtLeast(CURRENT_VERSION, version);
Expand All @@ -53,44 +77,83 @@ public static boolean isAtMost(JavaVersion version) {
return isAtMost(CURRENT_VERSION, version);
}

private static JavaVersion detectCurrentVersion() {
String version = System.getProperty("java.version");
return parseVersion(version);
static boolean isAtLeast(JavaMajorVersion currentVersion, JavaMajorVersion minVersion) {
return currentVersion.getMajorVersion() >= minVersion.getMajorVersion();
}

static JavaVersion parseVersion(String version) {
if (version == null) {
// this should not happen but it's better to stay on the safe side
return UNKNOWN;
}
JavaVersion result = UNKNOWN;
if (version.startsWith("1.")) {
String withoutMajor = version.substring(2, version.length());
if (withoutMajor.startsWith("6")) {
result = JAVA_1_6;
} else if (withoutMajor.startsWith("7")) {
result = JAVA_1_7;
} else if (withoutMajor.startsWith("8")) {
result = JAVA_1_8;
static boolean isAtMost(JavaMajorVersion currentVersion, JavaMajorVersion maxVersion) {
return currentVersion.getMajorVersion() <= maxVersion.getMajorVersion();
}

private static JavaVersion valueOf(int majorVersion) {
for (JavaVersion version : values()) {
if (version.majorVersion == majorVersion) {
return version;
}
} else if (version.startsWith("9")) {
// from version 9 the string does not start with "1."
result = JAVA_9;
} else if (version.startsWith("10")) {
result = JAVA_10;
} else if (version.startsWith("11")) {
result = JAVA_11;
} else if (version.startsWith("12")) {
result = JAVA_12;
}
return result;

return null;
}

static boolean isAtLeast(JavaVersion currentVersion, JavaVersion minVersion) {
return currentVersion.ordinal() >= minVersion.ordinal();
private static JavaMajorVersion detectCurrentVersion() {
final ILogger logger = Logger.getLogger(JavaVersion.class);
final Class runtimeClass;
final Class versionClass;

try {
runtimeClass = Class.forName("java.lang.Runtime");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need reflection here, I think. The Runtime is part of Java from its beginning. Runtime.class should do the job.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Of course :) Fixed.

} catch (ClassNotFoundException e) {
logger.warning("Unable to detect Java version, falling back to Java 8", e);
return JAVA_8;
}

try {
versionClass = Class.forName("java.lang.Runtime$Version");
} catch (ClassNotFoundException e) {
// if Runtime is present but Runtime.Version doesn't, it's Java8 for sure
logger.info("Detected runtime version: Java 8");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduce logging level to fine here?

return JAVA_8;
}

try {
Method versionMethod = runtimeClass.getDeclaredMethod("version");
Object versionObj = versionMethod.invoke(Runtime.getRuntime());
Method majorMethod = versionClass.getDeclaredMethod("major");
int majorVersion = (int) majorMethod.invoke(versionObj);

logger.info("Detected runtime version: Java " + majorVersion);
JavaVersion foundVersion = valueOf(majorVersion);
if (foundVersion != null) {
return foundVersion;
}

return new FutureJavaVersion(majorVersion);

} catch (Exception e) {
logger.warning("Unable to detect Java version, falling back to Java 8", e);
return JAVA_8;
}
}

static boolean isAtMost(JavaVersion currentVersion, JavaVersion minVersion) {
return currentVersion.ordinal() <= minVersion.ordinal();
/**
* Represents a future Java version that has not yet added to the
* {@link JavaVersion} enum. This class allows comparison of known
* versions against future, not yet listed (in the enum) Java versions,
* making {@link JavaVersion#isAtLeast(JavaVersion)} and
* {@link JavaVersion#isAtMost(JavaVersion)} methods usable in this
* case too.
*/
static class FutureJavaVersion implements JavaMajorVersion {
private final int majorVersion;

FutureJavaVersion(int majorVersion) {
this.majorVersion = majorVersion;
}

@Override
public int getMajorVersion() {
return majorVersion;
}
}
}