From 2018a865f8754d41f12035a196a5e872642e8403 Mon Sep 17 00:00:00 2001 From: James Perkins Date: Tue, 12 Feb 2019 08:06:38 -0800 Subject: [PATCH] [LOGMGR-218] Follows up on the previous commit which could potentially initialize JBoss Modules too early for some use cases. One specific use case is WildFly embedded. --- .../org/jboss/logmanager/JDKSpecific.java | 32 --------- .../main/java/org/jboss/logmanager/Jvm.java | 67 +++++++++++++++++++ .../jboss/logmanager/LogLevelInitTask.java | 2 +- .../org/jboss/logmanager/JDKSpecific.java | 9 --- .../main/java9/org/jboss/logmanager/Jvm.java | 35 ++++++++++ .../org/jboss/logmanager/Environment.java | 4 +- 6 files changed, 105 insertions(+), 44 deletions(-) create mode 100644 core/src/main/java/org/jboss/logmanager/Jvm.java create mode 100644 core/src/main/java9/org/jboss/logmanager/Jvm.java diff --git a/core/src/main/java/org/jboss/logmanager/JDKSpecific.java b/core/src/main/java/org/jboss/logmanager/JDKSpecific.java index 78025367..fb6d14d3 100644 --- a/core/src/main/java/org/jboss/logmanager/JDKSpecific.java +++ b/core/src/main/java/org/jboss/logmanager/JDKSpecific.java @@ -23,10 +23,7 @@ import java.security.PrivilegedAction; import java.util.Collection; import java.util.LinkedHashSet; -import java.util.List; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.jboss.modules.Module; import org.jboss.modules.Version; @@ -39,7 +36,6 @@ private JDKSpecific() {} private static final Gateway GATEWAY; private static final boolean JBOSS_MODULES; - private static final boolean MODULAR_JVM; static { GATEWAY = AccessController.doPrivileged(new PrivilegedAction() { @@ -53,23 +49,6 @@ public Gateway run() { jbossModules = true; } catch (Throwable ignored) {} JBOSS_MODULES = jbossModules; - - // Get the current Java version and determine, by JVM version level, if this is a modular JDK - final String value = AccessController.doPrivileged(new PrivilegedAction() { - @Override - public String run() { - return System.getProperty("java.specification.version"); - } - }); - // Shouldn't happen, but we'll assume we're not a modular environment - boolean modularJvm = false; - if (value != null) { - final Matcher matcher = Pattern.compile("^(?:1\\.)?(\\d+)$").matcher(value); - if (matcher.find()) { - modularJvm = Integer.parseInt(matcher.group(1)) >= 9; - } - } - MODULAR_JVM = modularJvm; } static final class Gateway extends SecurityManager { @@ -78,17 +57,6 @@ protected Class[] getClassContext() { } } - /** - * Determines whether or not this is a modular JVM. The version of the {@code java.specification.version} is checked - * to determine if the version is greater than or equal to 9. This is required to disable specific features/hacks - * for older JVM's when the log manager is loaded on the boot class path which doesn't support multi-release JAR's. - * - * @return {@code true} if determined to be a modular JVM, otherwise {@code false} - */ - static boolean isModularJvm() { - return MODULAR_JVM; - } - static Class findCallingClass(Set rejectClassLoaders) { for (Class caller : GATEWAY.getClassContext()) { final ClassLoader classLoader = caller.getClassLoader(); diff --git a/core/src/main/java/org/jboss/logmanager/Jvm.java b/core/src/main/java/org/jboss/logmanager/Jvm.java new file mode 100644 index 00000000..fbb0e7fa --- /dev/null +++ b/core/src/main/java/org/jboss/logmanager/Jvm.java @@ -0,0 +1,67 @@ +/* + * JBoss, Home of Professional Open Source. + * + * Copyright 2019 Red Hat, Inc., and individual contributors + * as indicated by the @author tags. + * + * 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 org.jboss.logmanager; + +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * This is required to be separate from the {@link JDKSpecific} helper. It's specifically required for WildFly embedded + * as the {@link JDKSpecific} initializes JBoss Modules which could cause issues if it's initialized too early. This + * avoids the early initialization. + * + * @author James R. Perkins + */ +final class Jvm { + private static final boolean MODULAR_JVM; + + static { + + // Get the current Java version and determine, by JVM version level, if this is a modular JDK + final String value = AccessController.doPrivileged(new PrivilegedAction() { + @Override + public String run() { + return System.getProperty("java.specification.version"); + } + }); + // Shouldn't happen, but we'll assume we're not a modular environment + boolean modularJvm = false; + if (value != null) { + final Matcher matcher = Pattern.compile("^(?:1\\.)?(\\d+)$").matcher(value); + if (matcher.find()) { + modularJvm = Integer.parseInt(matcher.group(1)) >= 9; + } + } + MODULAR_JVM = modularJvm; + } + + /** + * Determines whether or not this is a modular JVM. The version of the {@code java.specification.version} is checked + * to determine if the version is greater than or equal to 9. This is required to disable specific features/hacks + * for older JVM's when the log manager is loaded on the boot class path which doesn't support multi-release JAR's. + * + * @return {@code true} if determined to be a modular JVM, otherwise {@code false} + */ + static boolean isModular() { + return MODULAR_JVM; + } +} diff --git a/core/src/main/java/org/jboss/logmanager/LogLevelInitTask.java b/core/src/main/java/org/jboss/logmanager/LogLevelInitTask.java index b14ac980..1c0f9735 100644 --- a/core/src/main/java/org/jboss/logmanager/LogLevelInitTask.java +++ b/core/src/main/java/org/jboss/logmanager/LogLevelInitTask.java @@ -41,7 +41,7 @@ class LogLevelInitTask implements PrivilegedAction { public Void run() { // If this is a modular JVM ignore the level hack. The check here is required when the log manager is added to // the boot class path. The boot class path does not support multi-release JAR's. - if (JDKSpecific.isModularJvm()) { + if (Jvm.isModular()) { return null; } /* This mysterious-looking hack is designed to trick JDK logging into not leaking classloaders and diff --git a/core/src/main/java9/org/jboss/logmanager/JDKSpecific.java b/core/src/main/java9/org/jboss/logmanager/JDKSpecific.java index 5ce980d5..f3ce6f36 100644 --- a/core/src/main/java9/org/jboss/logmanager/JDKSpecific.java +++ b/core/src/main/java9/org/jboss/logmanager/JDKSpecific.java @@ -55,15 +55,6 @@ private JDKSpecific() {} JBOSS_MODULES = jbossModules; } - /** - * Always returns {@code true}. - * - * @return {@code true} - */ - static boolean isModularJvm() { - return true; - } - static Class findCallingClass(Set rejectClassLoaders) { final SecurityManager sm = System.getSecurityManager(); if (sm != null) { diff --git a/core/src/main/java9/org/jboss/logmanager/Jvm.java b/core/src/main/java9/org/jboss/logmanager/Jvm.java new file mode 100644 index 00000000..d264253a --- /dev/null +++ b/core/src/main/java9/org/jboss/logmanager/Jvm.java @@ -0,0 +1,35 @@ +/* + * JBoss, Home of Professional Open Source. + * + * Copyright 2019 Red Hat, Inc., and individual contributors + * as indicated by the @author tags. + * + * 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 org.jboss.logmanager; + +/** + * @author James R. Perkins + */ +final class Jvm { + + /** + * Always returns {@code true}. + * + * @return {@code true} + */ + static boolean isModular() { + return true; + } +} diff --git a/core/src/test/java/org/jboss/logmanager/Environment.java b/core/src/test/java/org/jboss/logmanager/Environment.java index 1d51c412..b1a4161d 100644 --- a/core/src/test/java/org/jboss/logmanager/Environment.java +++ b/core/src/test/java/org/jboss/logmanager/Environment.java @@ -31,9 +31,9 @@ public class Environment { * * @return {@code true} if this is a modular JVM (Java 9+), otherwise {@code false} * - * @see JDKSpecific#isModularJvm() + * @see Jvm#isModular() */ public static boolean isModularJvm() { - return JDKSpecific.isModularJvm(); + return Jvm.isModular(); } }