Skip to content

Commit

Permalink
[LOGMGR-133] Java 9 integration
Browse files Browse the repository at this point in the history
To test on both Java 8 and Java 9, use this command:

mvn clean install -Djava8.home=/path/to/jdk/8
  • Loading branch information
dmlloyd committed Jan 25, 2018
1 parent d3cdf24 commit 5ff1ca2
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 2 deletions.
90 changes: 88 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<version.javax.json>1.0</version.javax.json>
<version.org.byteman>3.0.10</version.org.byteman>
<version.org.glassfish.javax.json>1.0.4</version.org.glassfish.javax.json>
<version.org.jboss.modules.jboss-modules>1.6.2.Final</version.org.jboss.modules.jboss-modules>
<version.org.jboss.modules.jboss-modules>1.7.0.Beta3</version.org.jboss.modules.jboss-modules>
<version.org.wildfly.common.wildfly-common>1.2.0.Final</version.org.wildfly.common.wildfly-common>
<version.junit.junit>4.12</version.junit.junit>

Expand All @@ -63,6 +63,8 @@

<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<jdk.min.version>9</jdk.min.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -132,10 +134,75 @@
</dependency>
</dependencies>

<profiles>
<profile>
<id>java8-test-profile</id>
<activation>
<property>
<name>java8.home</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>java8-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<jvm>${java8.home}/bin/java</jvm>
<additionalClasspathElements>
<additionalClasspathElement>${java8.home}/lib/tools.jar</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0-jboss-1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>8</release>
<buildDirectory>${project.build.directory}</buildDirectory>
<compileSourceRoots>${project.compileSourceRoots}</compileSourceRoots>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
<execution>
<id>compile-java9</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>9</release>
<buildDirectory>${project.build.directory}</buildDirectory>
<compileSourceRoots>${project.basedir}/src/main/java9</compileSourceRoots>
<outputDirectory>${project.build.directory}/classes/META-INF/versions/9</outputDirectory>
<additionalClasspathElements>
<additionalClasspathElement>${project.build.outputDirectory}</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
Expand All @@ -148,7 +215,6 @@
</includes>
<argLine>-Djdk.attach.allowAttachSelf=true</argLine>
<systemPropertyVariables>
<jdk.attach.allowAttachSelf>true</jdk.attach.allowAttachSelf>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<test.log.dir>${project.build.directory}${file.separator}logs${file.separator}</test.log.dir>
<!-- Configured for SocketHandler SSL test -->
Expand All @@ -160,7 +226,19 @@
<org.jboss.test.port>${org.jboss.test.port}</org.jboss.test.port>
<org.jboss.test.alt.port>${org.jboss.test.alt.port}</org.jboss.test.alt.port>
</systemPropertyVariables>
<trimStackTrace>false</trimStackTrace>
</configuration>
<executions>
<execution>
<id>default-test</id>
<configuration>
<classesDirectory>${project.build.directory}/classes/META-INF/versions/9</classesDirectory>
<additionalClasspathElements>
<additionalClasspathElement>${project.build.directory}/classes</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
Expand All @@ -178,8 +256,16 @@
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<dependencies>
<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>biz.aQute.bndlib</artifactId>
<version>3.5.0</version>
</dependency>
</dependencies>
<configuration>
<instructions>
<_fixupmessages>"Classes found in the wrong directory";is:=warning</_fixupmessages>
<Export-Package>
${project.groupId}.*;version=${project.version};-split-package:=error
</Export-Package>
Expand Down
155 changes: 155 additions & 0 deletions src/main/java9/org/jboss/logmanager/JDKSpecific.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package org.jboss.logmanager;

import static java.security.AccessController.doPrivileged;

import java.lang.module.ModuleDescriptor;
import java.security.PrivilegedAction;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

import org.jboss.modules.Module;
import org.jboss.modules.Version;

/**
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
final class JDKSpecific {

static final StackWalker WALKER = doPrivileged(new GetStackWalkerAction());

private JDKSpecific() {}

private static final boolean JBOSS_MODULES;

static {
boolean jbossModules = false;
try {
Module.getStartTime();
jbossModules = true;
} catch (Throwable ignored) {}
JBOSS_MODULES = jbossModules;
}

static Class<?> findCallingClass(Set<ClassLoader> rejectClassLoaders) {
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
return doPrivileged(new FindCallingClassAction(rejectClassLoaders));
} else {
return WALKER.walk(new FindFirstWalkFunction(rejectClassLoaders));
}
}

static void calculateCaller(ExtLogRecord logRecord) {
WALKER.walk(new CallerCalcFunction(logRecord));
}

static void calculateJdkModule(final ExtLogRecord logRecord, final Class<?> clazz) {
final java.lang.Module module = clazz.getModule();
if (module != null) {
logRecord.setSourceModuleName(module.getName());
final ModuleDescriptor descriptor = module.getDescriptor();
if (descriptor != null) {
final Optional<ModuleDescriptor.Version> optional = descriptor.version();
if (optional.isPresent()) {
logRecord.setSourceModuleVersion(optional.get().toString());
} else {
logRecord.setSourceModuleVersion(null);
}
}
}
}

static void calculateModule(final ExtLogRecord logRecord, final Class<?> clazz) {
final Module module = Module.forClass(clazz);
if (module != null) {
logRecord.setSourceModuleName(module.getName());
final Version version = module.getVersion();
if (version != null) {
logRecord.setSourceModuleVersion(version.toString());
} else {
logRecord.setSourceModuleVersion(null);
}
} else {
calculateJdkModule(logRecord, clazz);
}
}

static final class CallerCalcFunction implements Function<Stream<StackWalker.StackFrame>, Void> {
private final ExtLogRecord logRecord;

CallerCalcFunction(final ExtLogRecord logRecord) {
this.logRecord = logRecord;
}

public Void apply(final Stream<StackWalker.StackFrame> stream) {
final String loggerClassName = logRecord.getLoggerClassName();
final Iterator<StackWalker.StackFrame> iterator = stream.iterator();
boolean found = false;
while (iterator.hasNext()) {
final StackWalker.StackFrame frame = iterator.next();
final Class<?> clazz = frame.getDeclaringClass();
if (clazz.getName().equals(loggerClassName)) {
// next entry could be the one we want!
found = true;
} else if (found) {
logRecord.setSourceClassName(frame.getClassName());
logRecord.setSourceMethodName(frame.getMethodName());
logRecord.setSourceFileName(frame.getFileName());
logRecord.setSourceLineNumber(frame.getLineNumber());
if (JBOSS_MODULES) {
calculateModule(logRecord, clazz);
} else {
calculateJdkModule(logRecord, clazz);
}
return null;
}
}
logRecord.setUnknownCaller();
return null;
}
}

static final class GetStackWalkerAction implements PrivilegedAction<StackWalker> {
GetStackWalkerAction() {}

public StackWalker run() {
return StackWalker.getInstance(EnumSet.of(StackWalker.Option.RETAIN_CLASS_REFERENCE));
}
}

static final class FindCallingClassAction implements PrivilegedAction<Class<?>> {
private final Set<ClassLoader> rejectClassLoaders;

FindCallingClassAction(final Set<ClassLoader> rejectClassLoaders) {
this.rejectClassLoaders = rejectClassLoaders;
}

public Class<?> run() {
return WALKER.walk(new FindFirstWalkFunction(rejectClassLoaders));
}
}

static final class FindFirstWalkFunction implements Function<Stream<StackWalker.StackFrame>, Class<?>> {
private final Set<ClassLoader> rejectClassLoaders;

FindFirstWalkFunction(final Set<ClassLoader> rejectClassLoaders) {
this.rejectClassLoaders = rejectClassLoaders;
}

public Class<?> apply(final Stream<StackWalker.StackFrame> stream) {
final Iterator<StackWalker.StackFrame> iterator = stream.iterator();
while (iterator.hasNext()) {
final Class<?> clazz = iterator.next().getDeclaringClass();
final ClassLoader classLoader = clazz.getClassLoader();
if (! rejectClassLoaders.contains(classLoader)) {
return clazz;
}
}
return null;
}
}
}
32 changes: 32 additions & 0 deletions src/main/java9/org/jboss/logmanager/LogLevelInitTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017 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.PrivilegedAction;

/**
*/
class LogLevelInitTask implements PrivilegedAction<Void> {
LogLevelInitTask() {
}

public Void run() {
return null;
}
}

0 comments on commit 5ff1ca2

Please sign in to comment.