Skip to content

Commit

Permalink
Merge pull request #20 from jamezp/issue19
Browse files Browse the repository at this point in the history
[19] Add back the support to check if the JVM is a modular JVM if the…
  • Loading branch information
jamezp authored Nov 27, 2024
2 parents ff2c8a1 + 09ec8e5 commit 6e428ef
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 43 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<exclusions>
<!-- Use this launcher API -->
<exclusion>
<groupId>org.wildfly.core</groupId>
<groupId>org.wildfly.launcher</groupId>
<artifactId>wildfly-launcher</artifactId>
</exclusion>
</exclusions>
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/wildfly/core/launcher/CliCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,12 @@ public Path getJavaHome() {
public List<String> buildArguments() {
final List<String> 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);
}
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/org/wildfly/core/launcher/DomainCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,12 @@ public List<String> 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()) {
Expand Down Expand Up @@ -743,10 +745,12 @@ public List<String> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,12 @@ public List<String> 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()) {
Expand Down
72 changes: 67 additions & 5 deletions src/main/java/org/wildfly/core/launcher/Jvm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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");
Expand Down Expand Up @@ -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<String> cmd = new ArrayList<>();
cmd.add(resolveJavaCommand(javaHome));
cmd.add("--add-modules=java.se");
cmd.add("-version");
return checkProcessStatus(cmd);
}

/**
* Checks the process status.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,12 @@ public List<String> 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()) {
Expand Down
39 changes: 22 additions & 17 deletions src/test/java/org/wildfly/core/launcher/JvmTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>() {
Files.walkFileTree(javaHome, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
Files.delete(file);
Expand All @@ -53,19 +55,22 @@ public FileVisitResult postVisitDirectory(final Path dir, final IOException exc)

static Stream<Arguments> 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)
);
}

Expand Down

0 comments on commit 6e428ef

Please sign in to comment.