Skip to content

Commit

Permalink
[#6826] Improve Jdk9+ compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jul 31, 2020
1 parent 678a337 commit c35a479
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 36 deletions.
14 changes: 2 additions & 12 deletions agent/src/main/resources/profiles/local/log4j2.xml
Expand Up @@ -6,8 +6,7 @@
<Property name="rolling-date-format">%d{yyyy-MM-dd}</Property>
<Property name="backupsize">50m</Property>

<Property name="file_message_pattern">%d{MM-dd HH:mm:ss.sss} [%15.15t] %-5level %-40.40logger{1.}:%-3L -- %msg%n
</Property>
<Property name="file_message_pattern">%d{MM-dd HH:mm:ss.sss} [%15.15t] %-5level %-40.40logger{1.} -- %msg%n</Property>
</Properties>

<Appenders>
Expand All @@ -23,16 +22,7 @@
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="20">
<Delete basePath="${logging_dir}/" maxDepth="1">
<IfFileName glob="pinpoint-*-*.log">
<IfAny>
<IfAccumulatedFileSize exceeds="${backupsize}" />
<IfAccumulatedFileCount exceeds="20" />
</IfAny>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>

Expand Down
14 changes: 2 additions & 12 deletions agent/src/main/resources/profiles/release/log4j2.xml
Expand Up @@ -6,8 +6,7 @@
<Property name="rolling-date-format">%d{yyyy-MM-dd}</Property>
<Property name="backupsize">50m</Property>

<Property name="file_message_pattern">%d{MM-dd HH:mm:ss.sss} [%15.15t] %-5level %-40.40logger{1.}:%-3L -- %msg%n
</Property>
<Property name="file_message_pattern">%d{MM-dd HH:mm:ss.sss} [%15.15t] %-5level %-40.40logger{1.} -- %msg%n</Property>
</Properties>

<Appenders>
Expand All @@ -24,16 +23,7 @@
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="20">
<Delete basePath="${logging_dir}/" maxDepth="1">
<IfFileName glob="pinpoint-*-*.log">
<IfAny>
<IfAccumulatedFileSize exceeds="${backupsize}" />
<IfAccumulatedFileCount exceeds="20" />
</IfAny>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>

Expand Down
Expand Up @@ -62,6 +62,8 @@ public void setup() {
baseModule.addExports("jdk.internal.misc", bootstrapModule);
baseModule.addExports("jdk.internal.module", bootstrapModule);

// baseModule.addExports("java.lang.reflect", bootstrapModule);

}

public void defineAgentModule(ClassLoader classLoader, URL[] jarFileList) {
Expand All @@ -70,7 +72,8 @@ public void defineAgentModule(ClassLoader classLoader, URL[] jarFileList) {

prepareAgentModule(classLoader, agentModule);

addPermissionToLog4jModule(agentModule);
// addPermissionToLog4jModule(agentModule);
addPermissionToLog4j2Module(agentModule);
addPermissionToGuiceModule(agentModule);

}
Expand All @@ -97,6 +100,18 @@ private void addPermissionToLog4jModule(JavaModule agentModule) {
agentModule.addReads(desktopModule);
}

private void addPermissionToLog4j2Module(JavaModule agentModule) {
// required org.apache.logging.log4j.util.Reflection
// JavaModule reflect = loadModule("java.lang.reflect");
// agentModule.addReads(reflect);
// required log4j2
// java.xml
// pinpoint.agent/pinpoint.agent/org.apache.logging.log4j.core.config.xml.XmlConfiguration.<init>(XmlConfiguration.java:138)
// java.desktop
// pinpoint.agent/pinpoint.agent/org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:369)
addPermissionToLog4jModule(agentModule);
}

private void addPermissionToGuiceModule(JavaModule agentModule) {
JavaModule loggingModule = loadModule("java.logging");
agentModule.addReads(loggingModule);
Expand Down
Expand Up @@ -250,22 +250,61 @@ private URL[] resolveLib(AgentDirectory classPathResolver) {

return libUrlList.toArray(new URL[0]);
}
private static String PINPIONT_PREFIX = "pinpoint-";

private List<URL> resolveLib(List<URL> urlList) {
if (DEFAULT_AGENT.equalsIgnoreCase(getAgentType())) {
final List<URL> releaseLib = new ArrayList<URL>(urlList.size());
for (URL url : urlList) {
//
if (!url.toExternalForm().contains("pinpoint-profiler-test")) {
releaseLib.add(url);
}
}
return releaseLib;
final List<URL> releaseLib = filterTest(urlList);
return order(releaseLib);
} else {
logger.info("load " + PLUGIN_TEST_AGENT + " lib");
// plugin test
return urlList;
return order(urlList);
}
}

private List<URL> order(List<URL> releaseLib) {
final List<URL> orderList = new ArrayList<URL>(releaseLib.size());
// pinpoint module first
for (URL url : releaseLib) {
String fileName = getFileName(url);
if (fileName == null) {
continue;
}
if (fileName.startsWith(PINPIONT_PREFIX)) {
orderList.add(url);
}
}
for (URL url : releaseLib) {
String fileName = getFileName(url);
if (fileName == null) {
continue;
}
if (!fileName.startsWith(PINPIONT_PREFIX)) {
orderList.add(url);
}
}
return orderList;
}

private String getFileName(URL url) {
final String externalFrom = url.toExternalForm();
final int lastIndex = externalFrom.lastIndexOf('/');
if (lastIndex == -1) {
return null;
}
return externalFrom.substring(lastIndex + 1);
}

private List<URL> filterTest(List<URL> urlList) {
final List<URL> releaseLib = new ArrayList<URL>(urlList.size());
for (URL url : urlList) {
String externalFrom = url.toExternalForm();
if (!externalFrom.contains("pinpoint-profiler-test")) {
releaseLib.add(url);
}
}
return releaseLib;
}

}
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -127,7 +127,7 @@
<plugin.shade.version>3.1.1</plugin.shade.version>
<plugin.assembly.version>3.1.1</plugin.assembly.version>
<plugin.war.version>3.2.2</plugin.war.version>
<plugin.jar.version>3.1.0</plugin.jar.version>
<plugin.jar.version>3.2.0</plugin.jar.version>
<plugin.antrun.version>1.8</plugin.antrun.version>

<!--<plugin.clover.verison>4.3.0</plugin.clover.verison>-->
Expand Down
55 changes: 54 additions & 1 deletion profiler-logging/pom.xml
Expand Up @@ -17,7 +17,7 @@
<jdk.version>1.6</jdk.version>
<jdk.home>${env.JAVA_6_HOME}</jdk.home>

<!-- <plugin.animal-sniffer.skip>true</plugin.animal-sniffer.skip>-->
<plugin.animal-sniffer.skip>true</plugin.animal-sniffer.skip>
</properties>

<dependencies>
Expand Down Expand Up @@ -69,4 +69,57 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile-java-6</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>${jdk.version}</source>
<source>${jdk.version}</source>
<executable>${jdk.home}/bin/javac</executable>
</configuration>
</execution>
<execution>
<id>compile-java-9</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>9</release>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/main/java9</compileSourceRoot>
</compileSourceRoots>
<multiReleaseOutput>true</multiReleaseOutput>
<executable>${env.JAVA_9_HOME}/bin/javac</executable>
<!-- <release>9</release>-->
<!-- <compileSourceRoots>-->
<!-- <compileSourceRoot>${project.basedir}/src/main/java9</compileSourceRoot>-->
<!-- </compileSourceRoots>-->
<!-- <outputDirectory>${project.build.outputDirectory}/META-INF/versions/9</outputDirectory>-->
<!-- <executable>${env.JAVA_9_HOME}/bin/javac</executable>-->
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Expand Up @@ -8,9 +8,11 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.util.ReflectionUtil;

import java.io.File;
import java.net.URI;
import java.security.CodeSource;

public class Log4j2LoggingSystem implements LoggingSystem {
public static final String CONTEXT_NAME = "pinpoint-agent-logging-context";
Expand All @@ -36,6 +38,7 @@ public void start() {

BootLogger bootLogger = BootLogger.getLogger(this.getClass());
bootLogger.info("logPath:" + uri);
patchReflectionUtilForJava9(bootLogger);

this.loggerContext = getLoggerContext(uri);
// this.loggerContext = getLoggerContext2(uri);
Expand All @@ -49,6 +52,12 @@ public void start() {
bindPLoggerFactory(this.binder);
}

private void patchReflectionUtilForJava9(BootLogger bootLogger) {
Class<ReflectionUtil> reflectionUtilClass = ReflectionUtil.class;
CodeSource codeSource = reflectionUtilClass.getProtectionDomain().getCodeSource();
bootLogger.info("patch ReflectionUtil codeSource:" + codeSource);
}

private Logger getLoggerContextLogger() {
return loggerContext.getLogger(getClass().getName());
}
Expand Down

0 comments on commit c35a479

Please sign in to comment.