diff --git a/pom.xml b/pom.xml index 2ff02bb..26c8c54 100644 --- a/pom.xml +++ b/pom.xml @@ -125,7 +125,7 @@ - org.wildfly.core + org.wildfly.launcher wildfly-launcher diff --git a/src/main/java/org/wildfly/core/launcher/CliCommandBuilder.java b/src/main/java/org/wildfly/core/launcher/CliCommandBuilder.java index 5952d66..20f0ce3 100644 --- a/src/main/java/org/wildfly/core/launcher/CliCommandBuilder.java +++ b/src/main/java/org/wildfly/core/launcher/CliCommandBuilder.java @@ -654,10 +654,12 @@ public Path getJavaHome() { public List buildArguments() { final List cmd = new ArrayList<>(getJavaOptions()); if (modularLauncher) { - cmd.addAll(JBossModulesCommandBuilder.DEFAULT_MODULAR_VM_ARGUMENTS); - for (final String optionalModularArgument : JBossModulesCommandBuilder.OPTIONAL_DEFAULT_MODULAR_VM_ARGUMENTS) { - if (Jvm.isPackageAvailable(environment.getJvm().getPath(), optionalModularArgument)) { - cmd.add(optionalModularArgument); + if (environment.getJvm().isModular()) { + cmd.addAll(JBossModulesCommandBuilder.DEFAULT_MODULAR_VM_ARGUMENTS); + for (final String optionalModularArgument : JBossModulesCommandBuilder.OPTIONAL_DEFAULT_MODULAR_VM_ARGUMENTS) { + if (Jvm.isPackageAvailable(environment.getJvm().getPath(), optionalModularArgument)) { + cmd.add(optionalModularArgument); + } } } } diff --git a/src/main/java/org/wildfly/core/launcher/DomainCommandBuilder.java b/src/main/java/org/wildfly/core/launcher/DomainCommandBuilder.java index f4e5539..b95f4bd 100644 --- a/src/main/java/org/wildfly/core/launcher/DomainCommandBuilder.java +++ b/src/main/java/org/wildfly/core/launcher/DomainCommandBuilder.java @@ -709,10 +709,12 @@ public List buildArguments() { // PROCESS_CONTROLLER_JAVA_OPTS cmd.addAll(processControllerJavaOpts.asList()); - cmd.addAll(DEFAULT_MODULAR_VM_ARGUMENTS); - for (final String optionalModularArgument : OPTIONAL_DEFAULT_MODULAR_VM_ARGUMENTS) { - if (Jvm.isPackageAvailable(environment.getJvm().getPath(), optionalModularArgument)) { - cmd.add(optionalModularArgument); + if (environment.getJvm().isModular()) { + cmd.addAll(DEFAULT_MODULAR_VM_ARGUMENTS); + for (final String optionalModularArgument : OPTIONAL_DEFAULT_MODULAR_VM_ARGUMENTS) { + if (Jvm.isPackageAvailable(environment.getJvm().getPath(), optionalModularArgument)) { + cmd.add(optionalModularArgument); + } } } if (environment.getJvm().enhancedSecurityManagerAvailable()) { @@ -743,10 +745,12 @@ public List buildArguments() { // HOST_CONTROLLER_JAVA_OPTS cmd.addAll(hostControllerJavaOpts.asList()); - cmd.addAll(DEFAULT_MODULAR_VM_ARGUMENTS); - for (final String optionalModularArgument : OPTIONAL_DEFAULT_MODULAR_VM_ARGUMENTS) { - if (Jvm.isPackageAvailable(environment.getJvm().getPath(), optionalModularArgument)) { - cmd.add(optionalModularArgument); + if (hostControllerJvm.isModular()) { + cmd.addAll(DEFAULT_MODULAR_VM_ARGUMENTS); + for (final String optionalModularArgument : OPTIONAL_DEFAULT_MODULAR_VM_ARGUMENTS) { + if (Jvm.isPackageAvailable(environment.getJvm().getPath(), optionalModularArgument)) { + cmd.add(optionalModularArgument); + } } } if (hostControllerJvm.enhancedSecurityManagerAvailable()) { diff --git a/src/main/java/org/wildfly/core/launcher/JBossModulesCommandBuilder.java b/src/main/java/org/wildfly/core/launcher/JBossModulesCommandBuilder.java index 2b6b73f..5b1977e 100644 --- a/src/main/java/org/wildfly/core/launcher/JBossModulesCommandBuilder.java +++ b/src/main/java/org/wildfly/core/launcher/JBossModulesCommandBuilder.java @@ -619,10 +619,12 @@ public List buildArguments() { cmd.add("-javaagent:" + getModulesJarName()); } cmd.addAll(getJavaOptions()); - cmd.addAll(DEFAULT_MODULAR_VM_ARGUMENTS); - for (final String optionalModularArgument : OPTIONAL_DEFAULT_MODULAR_VM_ARGUMENTS) { - if (Jvm.isPackageAvailable(environment.getJvm().getPath(), optionalModularArgument)) { - cmd.add(optionalModularArgument); + if (environment.getJvm().isModular()) { + cmd.addAll(DEFAULT_MODULAR_VM_ARGUMENTS); + for (final String optionalModularArgument : OPTIONAL_DEFAULT_MODULAR_VM_ARGUMENTS) { + if (Jvm.isPackageAvailable(environment.getJvm().getPath(), optionalModularArgument)) { + cmd.add(optionalModularArgument); + } } } if (environment.getJvm().enhancedSecurityManagerAvailable()) { diff --git a/src/main/java/org/wildfly/core/launcher/Jvm.java b/src/main/java/org/wildfly/core/launcher/Jvm.java index 49a5f80..94adeb2 100644 --- a/src/main/java/org/wildfly/core/launcher/Jvm.java +++ b/src/main/java/org/wildfly/core/launcher/Jvm.java @@ -41,14 +41,16 @@ class Jvm { JAVA_HOME = Paths.get(javaHome); } - private static final Jvm DEFAULT = new Jvm(JAVA_HOME, SUPPORTS_SECURITY_MANGER, ENHANCED_SECURITY_MANAGER); + private static final Jvm DEFAULT = new Jvm(JAVA_HOME, true, SUPPORTS_SECURITY_MANGER, ENHANCED_SECURITY_MANAGER); private final Path path; + private final boolean isModular; private final boolean isSecurityManagerSupported; private final boolean enhancedSecurityManager; - private Jvm(final Path path, final boolean isSecurityManagerSupported, final boolean enhancedSecurityManager) { + private Jvm(final Path path, final boolean isModular, final boolean isSecurityManagerSupported, final boolean enhancedSecurityManager) { this.path = path; + this.isModular = isModular; this.isSecurityManagerSupported = isSecurityManagerSupported; this.enhancedSecurityManager = enhancedSecurityManager; } @@ -88,7 +90,7 @@ static Jvm of(final Path javaHome) { return DEFAULT; } final Path path = validateJavaHome(javaHome); - return new Jvm(path, isSecurityManagerSupported(javaHome), hasEnhancedSecurityManager(javaHome)); + return new Jvm(path, isModularJavaHome(javaHome), isSecurityManagerSupported(javaHome), hasEnhancedSecurityManager(javaHome)); } /** @@ -114,9 +116,8 @@ public Path getPath() { * * @return {@code true} if this is a modular JVM, otherwise {@code false} */ - @Deprecated(forRemoval = true, since = "1.0") public boolean isModular() { - return true; + return isModular; } /** @@ -137,6 +138,52 @@ public boolean enhancedSecurityManagerAvailable() { return enhancedSecurityManager; } + private static boolean isModularJavaHome(final Path javaHome) { + final Path jmodsDir = javaHome.resolve("jmods"); + // If the jmods directory exists we can safely assume this is a modular JDK, note even in a modular JDK this + // may not exist. + if (Files.isDirectory(jmodsDir)) { + return true; + } + // Next check for a $JAVA_HOME/release file, for a JRE this will not exist + final Path releaseFile = javaHome.resolve("release"); + if (Files.isReadable(releaseFile) && Files.isRegularFile(releaseFile)) { + // Read the file and look for a JAVA_VERSION property + try (final BufferedReader reader = Files.newBufferedReader(releaseFile, StandardCharsets.UTF_8)) { + String line; + while ((line = reader.readLine()) != null) { + if (line.startsWith("JAVA_VERSION=")) { + // Get the version value + final int index = line.indexOf('='); + return isModularJavaVersion(line.substring(index + 1).replace("\"", "")); + } + } + } catch (IOException ignore) { + } + } + // Final check is to launch a new process with some modular JVM arguments and check the exit code + return isModular(javaHome); + } + + private static boolean isModularJavaVersion(final String version) { + if (version != null) { + try { + final String[] versionParts = version.split("\\."); + if (versionParts.length == 1) { + return Integer.parseInt(versionParts[0]) >= 9; + } else if (versionParts.length > 1) { + // Check the first part and if one, use the second part + if ("1".equals(versionParts[0])) { + return Integer.parseInt(versionParts[2]) >= 9; + } + return Integer.parseInt(versionParts[0]) >= 9; + } + } catch (Exception ignore) { + } + } + return false; + } + private static boolean isSecurityManagerSupported(final Path javaHome) { // Next check for a $JAVA_HOME/release file, for a JRE this will not exist final Path releaseFile = javaHome.resolve("release"); @@ -209,6 +256,21 @@ private static boolean isSecurityManagerSupportedInJvm(final Path javaHome) { return checkProcessStatus(cmd); } + /** + * Checks to see if the {@code javaHome} is a modular JVM. + * + * @param javaHome the Java Home if {@code null} an attempt to discover the Java Home will be done + * + * @return {@code true} if this is a modular environment + */ + private static boolean isModular(final Path javaHome) { + final List cmd = new ArrayList<>(); + cmd.add(resolveJavaCommand(javaHome)); + cmd.add("--add-modules=java.se"); + cmd.add("-version"); + return checkProcessStatus(cmd); + } + /** * Checks the process status. * diff --git a/src/main/java/org/wildfly/core/launcher/StandaloneCommandBuilder.java b/src/main/java/org/wildfly/core/launcher/StandaloneCommandBuilder.java index 4eeb51b..a0a1895 100644 --- a/src/main/java/org/wildfly/core/launcher/StandaloneCommandBuilder.java +++ b/src/main/java/org/wildfly/core/launcher/StandaloneCommandBuilder.java @@ -572,10 +572,12 @@ public List buildArguments() { cmd.add("-javaagent:" + getModulesJarName()); } cmd.addAll(getJavaOptions()); - cmd.addAll(DEFAULT_MODULAR_VM_ARGUMENTS); - for (final String optionalModularArgument : OPTIONAL_DEFAULT_MODULAR_VM_ARGUMENTS) { - if (Jvm.isPackageAvailable(environment.getJvm().getPath(), optionalModularArgument)) { - cmd.add(optionalModularArgument); + if (environment.getJvm().isModular()) { + cmd.addAll(DEFAULT_MODULAR_VM_ARGUMENTS); + for (final String optionalModularArgument : OPTIONAL_DEFAULT_MODULAR_VM_ARGUMENTS) { + if (Jvm.isPackageAvailable(environment.getJvm().getPath(), optionalModularArgument)) { + cmd.add(optionalModularArgument); + } } } if (environment.getJvm().enhancedSecurityManagerAvailable()) { diff --git a/src/test/java/org/wildfly/core/launcher/JvmTest.java b/src/test/java/org/wildfly/core/launcher/JvmTest.java index be0f1d6..d6230df 100644 --- a/src/test/java/org/wildfly/core/launcher/JvmTest.java +++ b/src/test/java/org/wildfly/core/launcher/JvmTest.java @@ -29,13 +29,15 @@ class JvmTest { @ParameterizedTest @MethodSource("testReleases") - void releaseFile(final String version, final boolean expectedValue) throws Exception { + void releaseFile(final String version, final boolean isModular, final boolean isSecurityManagerSupported) throws Exception { final Path javaHome = createFakeJavaHome(version); try { - assertEquals(expectedValue, Jvm.of(javaHome).isSecurityManagerSupported(), () -> - String.format("Expected version %s to %s support the security manager", version, (expectedValue ? "" : "not"))); + final Jvm jvm = Jvm.of(javaHome); + assertEquals(isModular, jvm.isModular(), () -> String.format("Expected version %s to %s a modular JVM", version, (isModular ? "be" : "not be"))); + assertEquals(isSecurityManagerSupported, jvm.isSecurityManagerSupported(), () -> + String.format("Expected version %s to %s support the security manager", version, (isSecurityManagerSupported ? "" : "not"))); } finally { - Files.walkFileTree(javaHome, new SimpleFileVisitor() { + Files.walkFileTree(javaHome, new SimpleFileVisitor<>() { @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { Files.delete(file); @@ -53,19 +55,22 @@ public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) static Stream testReleases() { return Stream.of( - arguments("", false), - arguments("9", true), - arguments("9.0", true), - arguments("9.0.1", true), - arguments("10", true), - arguments("10.0", true), - arguments("10.0.2", true), - arguments("11", true), - arguments("11.0.1", true), - arguments("21.0.5", true), - arguments("23.0.3", true), - arguments("24", false), - arguments("25.0.1", false) + arguments("", false, false), + arguments("1.8", false, true), + arguments("1.8.0", false, true), + arguments("1.8.0_432", false, true), + arguments("9", true, true), + arguments("9.0", true, true), + arguments("9.0.1", true, true), + arguments("10", true, true), + arguments("10.0", true, true), + arguments("10.0.2", true, true), + arguments("11", true, true), + arguments("11.0.1", true, true), + arguments("21.0.5", true, true), + arguments("23.0.3", true, true), + arguments("24", true, false), + arguments("25.0.1", true, false) ); }