[FEATURE] JVMHelper class extension (#667)

This commit is contained in:
microwin7 2023-07-16 09:12:41 +03:00 committed by GitHub
parent 55c0cdfa0d
commit 3a82065889
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 47 deletions

View file

@ -77,25 +77,7 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
@Override @Override
public String findLibrary(String name) { public String findLibrary(String name) {
return nativePath.concat(IOHelper.PLATFORM_SEPARATOR).concat(getNativePrefix()).concat(name).concat(getNativeEx()); return nativePath.concat(IOHelper.PLATFORM_SEPARATOR).concat(JVMHelper.NATIVE_PREFIX).concat(name).concat(JVMHelper.NATIVE_EXTENSION);
}
public String getNativeEx() {
if (JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE)
return ".dll";
else if (JVMHelper.OS_TYPE == JVMHelper.OS.LINUX)
return ".so";
else if (JVMHelper.OS_TYPE == JVMHelper.OS.MACOSX)
return ".dylib";
return "";
}
public String getNativePrefix() {
if (JVMHelper.OS_TYPE == JVMHelper.OS.LINUX)
return "lib";
else if (JVMHelper.OS_TYPE == JVMHelper.OS.MACOSX)
return "lib";
return "";
} }
public void addAllowedPackage(String pkg) { public void addAllowedPackage(String pkg) {

View file

@ -24,6 +24,8 @@ public final class JVMHelper {
// System properties // System properties
public static final String OS_VERSION = OPERATING_SYSTEM_MXBEAN.getVersion(); public static final String OS_VERSION = OPERATING_SYSTEM_MXBEAN.getVersion();
public static final ARCH ARCH_TYPE = getArch(System.getProperty("os.arch")); public static final ARCH ARCH_TYPE = getArch(System.getProperty("os.arch"));
public static final String NATIVE_EXTENSION = getNativeExtension(OS_TYPE);
public static final String NATIVE_PREFIX = getNativePrefix(OS_TYPE);
public static final int JVM_BITS = Integer.parseInt(System.getProperty("sun.arch.data.model")); public static final int JVM_BITS = Integer.parseInt(System.getProperty("sun.arch.data.model"));
// Public static fields // Public static fields
public static final Runtime RUNTIME = Runtime.getRuntime(); public static final Runtime RUNTIME = Runtime.getRuntime();
@ -82,6 +84,29 @@ public static int getBuild() {
} }
public static String getNativeExtension(JVMHelper.OS OS_TYPE) {
switch (OS_TYPE) {
case MUSTDIE:
return ".dll";
case LINUX:
return ".so";
case MACOSX:
return ".dylib";
default:
throw new InternalError(String.format("Unsupported OS TYPE '%s'", OS_TYPE));
}
}
public static String getNativePrefix(JVMHelper.OS OS_TYPE) {
switch (OS_TYPE) {
case LINUX:
case MACOSX:
return "lib";
default:
return "";
}
}
public static void appendVars(ProcessBuilder builder, Map<String, String> vars) { public static void appendVars(ProcessBuilder builder, Map<String, String> vars) {
builder.environment().putAll(vars); builder.environment().putAll(vars);
} }
@ -179,7 +204,6 @@ public static void verifySystemProperties(Class<?> mainClass, boolean requireSys
LogHelper.debug("Verifying JVM architecture"); LogHelper.debug("Verifying JVM architecture");
} }
public enum ARCH { public enum ARCH {
X86("x86"), X86_64("x86-64"), ARM64("arm64"), ARM32("arm32"); X86("x86"), X86_64("x86-64"), ARM64("arm64"), ARM32("arm32");

View file

@ -20,13 +20,12 @@ public final class JVMHelper {
public static final OperatingSystemMXBean OPERATING_SYSTEM_MXBEAN = public static final OperatingSystemMXBean OPERATING_SYSTEM_MXBEAN =
ManagementFactory.getOperatingSystemMXBean(); ManagementFactory.getOperatingSystemMXBean();
public static final OS OS_TYPE = OS.byName(OPERATING_SYSTEM_MXBEAN.getName()); public static final OS OS_TYPE = OS.byName(OPERATING_SYSTEM_MXBEAN.getName());
public static final int OS_BITS = getCorrectOSArch();
// System properties // System properties
public static final String OS_VERSION = OPERATING_SYSTEM_MXBEAN.getVersion(); public static final String OS_VERSION = OPERATING_SYSTEM_MXBEAN.getVersion();
public static final int OS_BITS = getCorrectOSArch();
public static final ARCH ARCH_TYPE = getArch(System.getProperty("os.arch")); public static final ARCH ARCH_TYPE = getArch(System.getProperty("os.arch"));
public static final String NATIVE_EXTENSION = getNativeExtension(OS_TYPE);
public static final String NATIVE_PREFIX = getNativePrefix(OS_TYPE);
public static final int JVM_BITS = Integer.parseInt(System.getProperty("sun.arch.data.model")); public static final int JVM_BITS = Integer.parseInt(System.getProperty("sun.arch.data.model"));
// Public static fields // Public static fields
public static final Runtime RUNTIME = Runtime.getRuntime(); public static final Runtime RUNTIME = Runtime.getRuntime();
@ -45,16 +44,6 @@ public final class JVMHelper {
private JVMHelper() { private JVMHelper() {
} }
public enum ARCH {
X86("x86"), X86_64("x86-64"), ARM64("arm64"), ARM32("arm32");
public final String name;
ARCH(String name) {
this.name = name;
}
}
public static ARCH getArch(String arch) { public static ARCH getArch(String arch) {
if (arch.equals("amd64") || arch.equals("x86-64") || arch.equals("x86_64")) return ARCH.X86_64; if (arch.equals("amd64") || arch.equals("x86-64") || arch.equals("x86_64")) return ARCH.X86_64;
if (arch.equals("i386") || arch.equals("i686") || arch.equals("x86")) return ARCH.X86; if (arch.equals("i386") || arch.equals("i686") || arch.equals("x86")) return ARCH.X86;
@ -72,6 +61,29 @@ public static int getBuild() {
return Runtime.version().update(); return Runtime.version().update();
} }
public static String getNativeExtension(JVMHelper.OS OS_TYPE) {
switch (OS_TYPE) {
case MUSTDIE:
return ".dll";
case LINUX:
return ".so";
case MACOSX:
return ".dylib";
default:
throw new InternalError(String.format("Unsupported OS TYPE '%s'", OS_TYPE));
}
}
public static String getNativePrefix(JVMHelper.OS OS_TYPE) {
switch (OS_TYPE) {
case LINUX:
case MACOSX:
return "lib";
default:
return "";
}
}
public static void appendVars(ProcessBuilder builder, Map<String, String> vars) { public static void appendVars(ProcessBuilder builder, Map<String, String> vars) {
builder.environment().putAll(vars); builder.environment().putAll(vars);
} }
@ -86,19 +98,16 @@ public static Class<?> firstClass(String... names) throws ClassNotFoundException
throw new ClassNotFoundException(Arrays.toString(names)); throw new ClassNotFoundException(Arrays.toString(names));
} }
public static void fullGC() { public static void fullGC() {
RUNTIME.gc(); RUNTIME.gc();
RUNTIME.runFinalization(); RUNTIME.runFinalization();
LogHelper.debug("Used heap: %d MiB", RUNTIME.totalMemory() - RUNTIME.freeMemory() >> 20); LogHelper.debug("Used heap: %d MiB", RUNTIME.totalMemory() - RUNTIME.freeMemory() >> 20);
} }
public static String[] getClassPath() { public static String[] getClassPath() {
return System.getProperty("java.class.path").split(File.pathSeparator); return System.getProperty("java.class.path").split(File.pathSeparator);
} }
public static URL[] getClassPathURL() { public static URL[] getClassPathURL() {
String[] cp = System.getProperty("java.class.path").split(File.pathSeparator); String[] cp = System.getProperty("java.class.path").split(File.pathSeparator);
URL[] list = new URL[cp.length]; URL[] list = new URL[cp.length];
@ -139,34 +148,28 @@ private static int getCorrectOSArch() {
return System.getProperty("os.arch").contains("64") ? 64 : 32; return System.getProperty("os.arch").contains("64") ? 64 : 32;
} }
public static String getEnvPropertyCaseSensitive(String name) { public static String getEnvPropertyCaseSensitive(String name) {
return System.getenv().get(name); return System.getenv().get(name);
} }
public static boolean isJVMMatchesSystemArch() { public static boolean isJVMMatchesSystemArch() {
return JVM_BITS == OS_BITS; return JVM_BITS == OS_BITS;
} }
public static String jvmProperty(String name, String value) { public static String jvmProperty(String name, String value) {
return String.format("-D%s=%s", name, value); return String.format("-D%s=%s", name, value);
} }
public static String systemToJvmProperty(String name) { public static String systemToJvmProperty(String name) {
return String.format("-D%s=%s", name, System.getProperties().getProperty(name)); return String.format("-D%s=%s", name, System.getProperties().getProperty(name));
} }
public static void addSystemPropertyToArgs(Collection<String> args, String name) { public static void addSystemPropertyToArgs(Collection<String> args, String name) {
String property = System.getProperty(name); String property = System.getProperty(name);
if (property != null) if (property != null)
args.add(String.format("-D%s=%s", name, property)); args.add(String.format("-D%s=%s", name, property));
} }
public static void verifySystemProperties(Class<?> mainClass, boolean requireSystem) { public static void verifySystemProperties(Class<?> mainClass, boolean requireSystem) {
Locale.setDefault(Locale.US); Locale.setDefault(Locale.US);
// Verify class loader // Verify class loader
@ -178,6 +181,16 @@ public static void verifySystemProperties(Class<?> mainClass, boolean requireSys
LogHelper.debug("Verifying JVM architecture"); LogHelper.debug("Verifying JVM architecture");
} }
public enum ARCH {
X86("x86"), X86_64("x86-64"), ARM64("arm64"), ARM32("arm32");
public final String name;
ARCH(String name) {
this.name = name;
}
}
public enum OS { public enum OS {
MUSTDIE("mustdie"), LINUX("linux"), MACOSX("macosx"); MUSTDIE("mustdie"), LINUX("linux"), MACOSX("macosx");