Skip to content

Commit

Permalink
Require Java 11 (minimum) at run time
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlloyd authored and radcortez committed Sep 1, 2022
1 parent cbf8cc1 commit 50705fd
Show file tree
Hide file tree
Showing 35 changed files with 54 additions and 638 deletions.
60 changes: 3 additions & 57 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,6 @@ jobs:
with:
version: ${{matrix.java}}

- uses: AdoptOpenJDK/install-jdk@v1
name: set up JDK 10
with:
version: 10
targets: 'JAVA_HOME_10'

- uses: AdoptOpenJDK/install-jdk@v1
name: set up JDK 9
with:
version: 9
targets: 'JAVA_HOME_9'

- uses: AdoptOpenJDK/install-jdk@v1
name: set up JDK 8
with:
version: 8
targets: 'JAVA_HOME_8'

- name: Cache local Maven repository
uses: actions/cache@v2
with:
Expand All @@ -65,7 +47,7 @@ jobs:
- name: build with maven
run: |
mvn -q -N io.takari:maven:0.7.7:wrapper -Dmaven=3.8.3
./mvnw -B formatter:validate verify --file pom.xml -Djava8.home=$JAVA_HOME_8 -Djava9.home=$JAVA_HOME_9 -Djava10.home=$JAVA_HOME_10
./mvnw -B formatter:validate verify --file pom.xml
build-windows:
runs-on: windows-latest
Expand All @@ -80,24 +62,6 @@ jobs:
with:
version: 11

- uses: AdoptOpenJDK/install-jdk@v1
name: set up JDK 10
with:
version: 10
targets: 'JAVA_HOME_10'

- uses: AdoptOpenJDK/install-jdk@v1
name: set up JDK 9
with:
version: 9
targets: 'JAVA_HOME_9'

- uses: AdoptOpenJDK/install-jdk@v1
name: set up JDK 8
with:
version: 8
targets: 'JAVA_HOME_8'

- name: Cache local Maven repository
uses: actions/cache@v2
with:
Expand All @@ -110,7 +74,7 @@ jobs:
shell: bash
run: |
mvn -q -N io.takari:maven:0.7.7:wrapper -Dmaven=3.8.3
./mvnw -B formatter:validate verify --file pom.xml -Djava8.home=$JAVA_HOME_8 -Djava9.home=$JAVA_HOME_9 -Djava10.home=$JAVA_HOME_10
./mvnw -B formatter:validate verify --file pom.xml
build-macos:
runs-on: macos-latest
Expand All @@ -125,24 +89,6 @@ jobs:
with:
version: 11

- uses: AdoptOpenJDK/install-jdk@v1
name: set up JDK 10
with:
version: 10
targets: 'JAVA_HOME_10'

- uses: AdoptOpenJDK/install-jdk@v1
name: set up JDK 9
with:
version: 9
targets: 'JAVA_HOME_9'

- uses: AdoptOpenJDK/install-jdk@v1
name: set up JDK 8
with:
version: 8
targets: 'JAVA_HOME_8'

- name: Cache local Maven repository
uses: actions/cache@v2
with:
Expand All @@ -154,7 +100,7 @@ jobs:
- name: build with maven
run: |
mvn -q -N io.takari:maven:0.7.7:wrapper -Dmaven=3.8.3
./mvnw -B formatter:validate verify --file pom.xml -Djava8.home=$JAVA_HOME_8 -Djava9.home=$JAVA_HOME_9 -Djava10.home=$JAVA_HOME_10
./mvnw -B formatter:validate verify --file pom.xml
quality:
needs: [build-linux,build-macos,build-windows]
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,69 +1,26 @@
package io.smallrye.common.classloader;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;

import sun.misc.Unsafe;

public class ClassDefiner {
private static final Unsafe unsafe;
private static final Method defineClass;
public static Class<?> defineClass(MethodHandles.Lookup lookup, Class<?> parent, String className, byte[] classBytes) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(DefineClassPermission.getInstance());
}

static {
unsafe = AccessController.doPrivileged(new PrivilegedAction<Unsafe>() {
public Unsafe run() {
return AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
@Override
public Class<?> run() {
try {
final Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
return (Unsafe) field.get(null);
MethodHandles.Lookup privateLookupIn = MethodHandles.privateLookupIn(parent, lookup);
return privateLookupIn.defineClass(classBytes);
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
} catch (NoSuchFieldException e) {
throw new NoSuchFieldError(e.getMessage());
}
}
});

defineClass = AccessController.doPrivileged(new PrivilegedAction<Method>() {
@Override
public Method run() {
try {
return Unsafe.class.getMethod("defineClass", String.class, byte[].class, int.class, int.class,
ClassLoader.class, ProtectionDomain.class);
} catch (NoSuchMethodException e) {
throw new NoSuchMethodError(e.getMessage());
}
}
});
}

public static Class<?> defineClass(MethodHandles.Lookup lookup, Class<?> parent, String className, byte[] classBytes) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(DefineClassPermission.getInstance());
}

// The className for Unsafe.defineClass must match the one in classBytes, so we can try to verify the package
// with the parent
String parentPkg = parent.getPackage().getName();
String classPkg = className.substring(0, className.lastIndexOf('.'));

if (!parentPkg.equals(classPkg)) {
throw new IllegalArgumentException("Class not in same package as lookup class");
}

try {
return (Class<?>) defineClass.invoke(unsafe, className, classBytes, 0, classBytes.length, parent.getClassLoader(),
null);
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
} catch (InvocationTargetException e) {
throw new IllegalStateException(e.getMessage());
}
}
}

This file was deleted.

File renamed without changes.
File renamed without changes.
79 changes: 2 additions & 77 deletions cpu/src/main/java/io/smallrye/common/cpu/ProcessorInfo.java
Original file line number Diff line number Diff line change
@@ -1,88 +1,13 @@
package io.smallrye.common.cpu;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Locale;

/**
* Provides general information about the processors on this host.
* Provides general information about the processors on this host (Java 9 version).
*/
public class ProcessorInfo {
private ProcessorInfo() {
}

private static final String CPUS_ALLOWED = "Cpus_allowed:";

/**
* Returns the number of processors available to this process. On most operating systems this method
* simply delegates to {@link Runtime#availableProcessors()}. However, on Linux, this strategy
* is insufficient, since the JVM does not take into consideration the process' CPU set affinity
* which is employed by cgroups and numactl. Therefore this method will analyze the Linux proc filesystem
* to make the determination. Since the CPU affinity of a process can be change at any time, this method does
* not cache the result. Calls should be limited accordingly.
* <br>
* Note tha on Linux, both SMT units (Hyper-Threading) and CPU cores are counted as a processor.
*
* @return the available processors on this system.
*/
public static int availableProcessors() {
if (System.getSecurityManager() != null) {
return AccessController.doPrivileged((PrivilegedAction<Integer>) () -> Integer.valueOf(determineProcessors()))
.intValue();
}

return determineProcessors();
}

private static int determineProcessors() {
int javaProcs = Runtime.getRuntime().availableProcessors();
if (!isLinux()) {
return javaProcs;
}

int maskProcs = 0;

try {
maskProcs = readCPUMask();
} catch (Exception e) {
// yum
}

return maskProcs > 0 ? Math.min(javaProcs, maskProcs) : javaProcs;
}

private static int readCPUMask() throws IOException {
try (FileInputStream stream = new FileInputStream("/proc/self/status")) {
try (InputStreamReader inputReader = new InputStreamReader(stream, StandardCharsets.US_ASCII)) {
try (BufferedReader reader = new BufferedReader(inputReader)) {
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith(CPUS_ALLOWED)) {
int count = 0;
int start = CPUS_ALLOWED.length();
for (int i = start; i < line.length(); i++) {
final int v = Character.digit(line.charAt(i), 16);
if (v != -1) {
count += Integer.bitCount(v);
}
}
return count;
}
}
}
}
}

return -1;
}

private static boolean isLinux() {
String osArch = System.getProperty("os.name", "unknown").toLowerCase(Locale.US);
return (osArch.contains("linux"));
return Runtime.getRuntime().availableProcessors();
}
}
13 changes: 0 additions & 13 deletions cpu/src/main/java9/io/smallrye/common/cpu/ProcessorInfo.java

This file was deleted.

File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion io/src/main/java/io/smallrye/common/io/jar/JarEntries.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class JarEntries {
* On Java 10+, a getRealName() method was added
*/
public static String getRealName(JarEntry jarEntry) {
return jarEntry.getName();
return jarEntry.getRealName();
}
}
25 changes: 8 additions & 17 deletions io/src/main/java/io/smallrye/common/io/jar/JarFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,51 @@

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipFile;

/**
* Java 8 variant of a JDK-specific class for working with {@code JarFile}s.
* Java 9+ variant of a JDK-specific class for working with {@code JarFile}s.
*/
public class JarFiles {
/**
* Returns an equivalent of {@code new JarFile(name)}. On Java 8, that's exactly what is returned.
* On Java 9+, an equivalent that is multi-release-enabled is returned.
*/
public static JarFile create(String name) throws IOException {
return new JarFile(name);
return new JarFile(new File(name), true, ZipFile.OPEN_READ, JarFile.runtimeVersion());
}

/**
* Returns an equivalent of {@code new JarFile(name, verify)}. On Java 8, that's exactly what is returned.
* On Java 9+, an equivalent that is multi-release-enabled is returned.
*/
public static JarFile create(String name, boolean verify) throws IOException {
return new JarFile(name, verify);
return new JarFile(new File(name), verify, ZipFile.OPEN_READ, JarFile.runtimeVersion());
}

/**
* Returns an equivalent of {@code new JarFile(file)}. On Java 8, that's exactly what is returned.
* On Java 9+, an equivalent that is multi-release-enabled is returned.
*/
public static JarFile create(File file) throws IOException {
return new JarFile(file);
return new JarFile(file, true, ZipFile.OPEN_READ, JarFile.runtimeVersion());
}

/**
* Returns an equivalent of {@code new JarFile(file, verify)}. On Java 8, that's exactly what is returned.
* On Java 9+, an equivalent that is multi-release-enabled is returned.
*/
public static JarFile create(File file, boolean verify) throws IOException {
return new JarFile(file, verify);
return new JarFile(file, verify, ZipFile.OPEN_READ, JarFile.runtimeVersion());
}

/**
* Returns true if this {@link JarFile} is a multi-release jar. On Java 8 this is done by browsing the manifest.
* On Java 9+, there is a isMultiRelease method
*/
public static boolean isMultiRelease(JarFile jarFile) {
String value = null;
try {
Manifest manifest = jarFile.getManifest();
if (manifest != null) {
value = manifest.getMainAttributes().getValue("Multi-Release");
}
} catch (IOException e) {
throw new UncheckedIOException("Cannot read manifest attributes", e);
}
return Boolean.parseBoolean(value);
return jarFile.isMultiRelease();
}

}
Loading

0 comments on commit 50705fd

Please sign in to comment.