Skip to content

Commit cf2b09c

Browse files
committed
feat: package all in one agent jar
1 parent 9220c18 commit cf2b09c

File tree

30 files changed

+529
-190
lines changed

30 files changed

+529
-190
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,9 @@ or build the artifacts with the following commands. The build process supports J
7676
`mvn clean install -DskipTests`
7777

7878
The agent jar is in the folder `arex-agent-jar/` after the build process.
79-
There will be two jar files in the folder.
8079

8180
```other
8281
arex-agent.jar
83-
arex-agent-bootstrap.jar
8482
```
8583

8684
If you wanna jar with version, build the artifacts with the following commands.

arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/AgentInitializer.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ public class AgentInitializer {
1010

1111
private static ClassLoader classLoader;
1212

13-
public static void initialize(Instrumentation inst, File agentFile, String agentArgs)
13+
/**
14+
* @param parentClassLoader Normally, the parentClassLoader should be ClassLoaders.AppClassLoader.
15+
*/
16+
public static void initialize(Instrumentation inst, File agentFile, String agentArgs, ClassLoader parentClassLoader)
1417
throws Exception {
1518
if (classLoader != null) {
1619
return;
@@ -20,7 +23,7 @@ public static void initialize(Instrumentation inst, File agentFile, String agent
2023
System.setProperty(ConfigConstants.SHADED_LOGGER_SHOW_DATE_TIME, "true");
2124
System.setProperty(ConfigConstants.SHADED_LOGGER_DATE_TIME_FORMAT, "yyyy-MM-dd HH:mm:ss:SSS");
2225
File[] extensionFiles = getExtensionJarFiles(agentFile);
23-
classLoader = createAgentClassLoader(agentFile, extensionFiles);
26+
classLoader = new AgentClassLoader(agentFile, parentClassLoader, extensionFiles);
2427
InstrumentationHolder.setAgentClassLoader(classLoader);
2528
InstrumentationHolder.setInstrumentation(inst);
2629
AgentInstaller installer = createAgentInstaller(inst, agentFile, agentArgs);
@@ -40,10 +43,6 @@ private static void addJarToLoaderSearch(File agentFile, File[] extensionFiles)
4043
}
4144
}
4245

43-
private static AgentClassLoader createAgentClassLoader(File agentFile, File[] extensionFiles) {
44-
return new AgentClassLoader(agentFile, getParentClassLoader(), extensionFiles);
45-
}
46-
4746
private static File[] getExtensionJarFiles(File jarFile) {
4847
String extensionDir = jarFile.getParent() + "/extensions/";
4948
return new File(extensionDir).listFiles(AgentInitializer::isJar);
@@ -58,8 +57,4 @@ private static AgentInstaller createAgentInstaller(Instrumentation inst, File fi
5857
Constructor<?> constructor = clazz.getDeclaredConstructor(Instrumentation.class, File.class, String.class);
5958
return (AgentInstaller) constructor.newInstance(inst, file, agentArgs);
6059
}
61-
62-
private static ClassLoader getParentClassLoader() {
63-
return AgentInitializer.class.getClassLoader();
64-
}
6560
}

arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/constants/ConfigConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ private ConfigConstants() {
3333
public static final String SHADED_LOGGER_SHOW_DATE_TIME = "shaded.org.slf4j.simpleLogger.showDateTime";
3434
public static final String SHADED_LOGGER_DATE_TIME_FORMAT = "shaded.org.slf4j.simpleLogger.dateTimeFormat";
3535
public static final String COVERAGE_PACKAGES = "arex.coverage.packages";
36+
public static final String THIRD_PARTY_JAR_FILE_PATH = "arex.third.party.jar.file.path";
37+
public static final String APP_CLASSLOADER_NAME = "jdk.internal.loader.ClassLoaders$AppClassLoader";
3638
}

arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/util/AdviceClassesCollector.java

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@
55

66
import java.io.File;
77
import java.io.IOException;
8+
import java.lang.reflect.Method;
9+
import java.net.URL;
10+
import java.net.URLClassLoader;
811
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
914
import java.util.jar.JarEntry;
15+
import java.util.jar.JarFile;
1016
import java.util.jar.JarInputStream;
17+
import java.util.stream.Stream;
18+
19+
import io.arex.agent.bootstrap.constants.ConfigConstants;
1120
import net.bytebuddy.dynamic.ClassFileLocator;
1221

1322
public class AdviceClassesCollector {
@@ -18,6 +27,7 @@ public class AdviceClassesCollector {
1827
private static final String CLASS_SERIALIZER_PREFIX = "io/arex/foundation/serializer";
1928
private static final String CLASS_SUFFIX = ".class";
2029
private static final int CLASS_SUFFIX_LENGTH = CLASS_SUFFIX.length();
30+
private static final String TEMP_DIR = System.getProperty("java.io.tmpdir") + File.separator + "arex" + File.separator + "third-party";
2131

2232
private AdviceClassesCollector() {
2333
}
@@ -34,13 +44,14 @@ public void addJarToLoaderSearch(File file) {
3444
private void addJarToLoaderSearch(File file, boolean isExtensionJar) {
3545
try (JarInputStream jarInputStream = new JarInputStream(Files.newInputStream(file.toPath()))) {
3646
JarEntry jarEntry;
47+
JarFile jarFile = new JarFile(file);
3748
do {
3849
jarEntry = jarInputStream.getNextJarEntry();
3950

4051
if (jarEntry != null && !jarEntry.isDirectory()) {
4152
String entryName = jarEntry.getName();
4253
if (ServiceLoader.match(entryName)) {
43-
ServiceLoader.buildCache(file, jarEntry, entryName);
54+
ServiceLoader.buildCache(jarFile, jarEntry, entryName);
4455
}
4556
// exclude package io.arex.inst.runtime/extension, not class, and shaded class.
4657
boolean isFilterEntry = StringUtil.isEmpty(entryName) ||
@@ -62,7 +73,7 @@ private void addJarToLoaderSearch(File file, boolean isExtensionJar) {
6273

6374
} while (jarEntry != null);
6475
} catch (Throwable ex) {
65-
System.err.printf("add jar classes to advice failed, file: %s%n", file.getAbsolutePath());
76+
System.err.printf("add jar classes to advice failed, file: %s, exception: %s%n", file.getAbsolutePath(), ex);
6677
}
6778
}
6879

@@ -95,6 +106,62 @@ private void addClassToInjectorCache(String adviceClassName) {
95106
}
96107
}
97108

109+
private static void appendToClassLoaderSearch(ClassLoader classLoader, File jarFile) {
110+
try {
111+
Method addURL = Class.forName("java.net.URLClassLoader").getDeclaredMethod("addURL", URL.class);
112+
addURL.setAccessible(true);
113+
114+
if (classLoader instanceof URLClassLoader) {
115+
addURL.invoke(classLoader, jarFile.toURI().toURL());
116+
}
117+
118+
/*
119+
* Due to Java 8 vs java 9+ incompatibility issues
120+
* See https://stackoverflow.com/questions/46694600/java-9-compatability-issue-with-classloader-getsystemclassloader/51584718
121+
*/
122+
ClassLoader urlClassLoader = ClassLoader.getSystemClassLoader();
123+
if (!(urlClassLoader instanceof URLClassLoader)) {
124+
try (URLClassLoader tempClassLoader = new URLClassLoader(new URL[] {jarFile.toURI().toURL()}, urlClassLoader)) {
125+
addURL.invoke(tempClassLoader, jarFile.toURI().toURL());
126+
appendToAppClassLoaderSearch(classLoader, jarFile);
127+
}
128+
} else {
129+
addURL.invoke(urlClassLoader, jarFile.toURI().toURL());
130+
appendToAppClassLoaderSearch(classLoader, jarFile);
131+
}
132+
} catch (Exception e) {
133+
System.err.printf("appendToClassLoaderSearch failed, classLoader: %s, jarFile: %s%n",
134+
classLoader.getClass().getName(), jarFile.getAbsolutePath());
135+
}
136+
}
137+
138+
/**
139+
* append jar jdk.internal.loader.ClassLoaders.AppClassLoader
140+
* if java >= 11 need add jvm option:--add-opens=java.base/jdk.internal.loader=ALL-UNNAMED
141+
* @param classLoader
142+
* @param jarFile
143+
*/
144+
private static void appendToAppClassLoaderSearch(ClassLoader classLoader, File jarFile) throws Exception {
145+
Class<? extends ClassLoader> loaderClass = classLoader.getClass();
146+
if (JdkUtils.isJdk11() && ConfigConstants.APP_CLASSLOADER_NAME.equalsIgnoreCase(loaderClass.getName())) {
147+
Method classPathMethod = loaderClass.getDeclaredMethod("appendToClassPathForInstrumentation", String.class);
148+
classPathMethod.setAccessible(true);
149+
classPathMethod.invoke(classLoader, jarFile.getPath());
150+
}
151+
}
152+
153+
public void addSpecificJarToLoaderSearch(String jarPackageName) {
154+
if (StringUtil.isEmpty(TEMP_DIR)) {
155+
return;
156+
}
157+
String searchPath = TEMP_DIR + File.separator + jarPackageName;
158+
try (Stream<Path> pathStream = Files.find(Paths.get(searchPath), Integer.MAX_VALUE, (path, attr) -> attr.isRegularFile())) {
159+
pathStream.forEach(path -> appendToClassLoaderSearch(Thread.currentThread().getContextClassLoader(), path.toFile()));
160+
} catch (IOException e) {
161+
System.err.printf("addSpecificJarToLoaderSearch failed, searchPath: %s%n", searchPath);
162+
}
163+
}
164+
98165
private byte[] getBytes(String name, ClassLoader loader) throws IOException {
99166
ClassFileLocator locator = ClassFileLocator.ForClassLoader.of(loader);
100167
return locator.locate(name).resolve();

arex-instrumentation-foundation/src/main/java/io/arex/foundation/util/JdkUtils.java renamed to arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/util/JdkUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.arex.foundation.util;
1+
package io.arex.agent.bootstrap.util;
22

33
public class JdkUtils {
44
public static final int JDK_11 = 11;

arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/util/ServiceLoader.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.arex.agent.bootstrap.util;
22

33
import java.io.BufferedReader;
4-
import java.io.File;
54
import java.io.InputStream;
65
import java.io.InputStreamReader;
76
import java.io.Reader;
@@ -48,9 +47,8 @@ public static <T> List<T> load(Class<T> service, ClassLoader loader) {
4847
* SERVICE_CACHE: key: io.arex.inst.runtime.serializer.StringSerializable
4948
* value: [io.arex.foundation.serializer.gson.GsonSerializer, io.arex.foundation.serializer.jackson.JacksonSerializer]
5049
*/
51-
public static void buildCache(File file, JarEntry jarEntry, String entryName) {
52-
try(JarFile jarFile = new JarFile(file);
53-
InputStream inputStream = jarFile.getInputStream(jarEntry)) {
50+
public static void buildCache(JarFile jarFile, JarEntry jarEntry, String entryName) {
51+
try(InputStream inputStream = jarFile.getInputStream(jarEntry)) {
5452
List<String> serviceList = readAllLines(inputStream);
5553
if (CollectionUtil.isNotEmpty(serviceList)) {
5654
String className = entryName.substring(PREFIX.length());

arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/AgentInitializerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static void tearDown() {
3737
@Test
3838
void testFirstInitialize() {
3939
try (MockedConstruction<AgentClassLoader> mocked = Mockito.mockConstruction(AgentClassLoader.class, (mock, context) -> Mockito.doReturn(InstrumentationInstallerTest.class).when(mock).loadClass(any()))){
40-
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null));
40+
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null, AgentInitializerTest.class.getClassLoader()));
4141
} catch (Throwable ex) {
4242
ex.printStackTrace();
4343
}
@@ -46,8 +46,8 @@ void testFirstInitialize() {
4646
@Test
4747
void testDoubleInitialize() {
4848
try (MockedConstruction<AgentClassLoader> mocked = Mockito.mockConstruction(AgentClassLoader.class, (mock, context) -> Mockito.doReturn(InstrumentationInstallerTest.class).when(mock).loadClass(any()))){
49-
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null));
50-
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null));
49+
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null, AgentInitializerTest.class.getClassLoader()));
50+
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null, AgentInitializerTest.class.getClassLoader()));
5151
} catch (Throwable ex) {
5252
ex.printStackTrace();
5353
}

arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/CreateFileCommon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static File createFile(String name) {
8080
return createFile(path, name);
8181
}
8282

83-
private static File createFile(String path, String name) {
83+
public static File createFile(String path, String name) {
8484
try {
8585
File file = new File(path + name);
8686
if (!file.getParentFile().exists()) {

arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/util/AdviceClassesCollectorTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import java.lang.reflect.Method;
1010
import java.net.URL;
1111
import java.net.URLClassLoader;
12-
import java.nio.file.Path;
13-
import java.nio.file.Paths;
1412

1513
import org.junit.jupiter.api.AfterAll;
1614
import org.junit.jupiter.api.BeforeAll;
@@ -81,9 +79,11 @@ void testNull() {
8179
}
8280

8381
@Test
84-
public void test() {
85-
final File file = new File("D:\\Users\\yongwuhe\\IdeaProjects\\arex-agent-java\\arex-agent-jar\\arex-agent-0.3.6.jar");
86-
String enrtyName = "META-INF/services/com.fasterxml.jackson.core.JsonFactory";
87-
final Path path = Paths.get(file.getAbsolutePath() + "!/" + enrtyName);
82+
void addJarToLoaderSearch() {
83+
// no file
84+
assertDoesNotThrow(() -> AdviceClassesCollector.INSTANCE.addSpecificJarToLoaderSearch("jackson"));
85+
// file path
86+
File file = CreateFileCommon.createFile(System.getProperty("java.io.tmpdir"),"/arex/third-party/jackson/jackson-test.jar");
87+
assertDoesNotThrow(() -> AdviceClassesCollector.INSTANCE.addSpecificJarToLoaderSearch("jackson"));
8888
}
89-
}
89+
}

arex-instrumentation-foundation/src/test/java/io/arex/foundation/util/JdkUtilsTest.java renamed to arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/util/JdkUtilsTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package io.arex.foundation.util;
2-
3-
import static org.junit.jupiter.api.Assertions.*;
1+
package io.arex.agent.bootstrap.util;
42

53
import org.junit.jupiter.api.AfterEach;
64
import org.junit.jupiter.api.BeforeEach;
75
import org.junit.jupiter.api.Test;
86

9-
class JdkUtilsTest {
7+
import static org.junit.jupiter.api.Assertions.*;
108

9+
class JdkUtilsTest {
1110
@BeforeEach
1211
void setUp() {
1312
}
@@ -25,4 +24,5 @@ void getJavaVersion() {
2524
void isJdk11() {
2625
assertInstanceOf(Boolean.class, JdkUtils.isJdk11());
2726
}
28-
}
27+
28+
}

arex-agent-core/src/main/java/io/arex/agent/instrumentation/BaseAgentInstaller.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
import io.arex.agent.bootstrap.util.FileUtils;
77
import io.arex.foundation.config.ConfigManager;
88
import io.arex.foundation.healthy.HealthManager;
9-
import io.arex.foundation.serializer.jackson.JacksonSerializer;
109
import io.arex.foundation.services.ConfigService;
1110
import io.arex.foundation.services.DataCollectorService;
1211
import io.arex.foundation.services.TimerService;
1312
import io.arex.foundation.util.NetUtils;
1413
import io.arex.inst.runtime.context.RecordLimiter;
15-
import io.arex.inst.runtime.serializer.Serializer;
1614
import io.arex.inst.runtime.service.DataCollector;
1715
import io.arex.inst.runtime.service.DataService;
1816

@@ -113,16 +111,8 @@ private void timedReportStatus() {
113111
private void initDependentComponents() {
114112
TraceContextManager.init(NetUtils.getIpAddress());
115113
RecordLimiter.init(HealthManager::acquire);
116-
initSerializer();
117114
initDataCollector();
118115
}
119-
120-
/**
121-
* add class to user loader search. ex: ParallelWebappClassLoader
122-
*/
123-
private void initSerializer() {
124-
Serializer.builder(JacksonSerializer.INSTANCE).build();
125-
}
126116
private void initDataCollector() {
127117
DataCollector collector = DataCollectorService.INSTANCE;
128118
if (ConfigManager.INSTANCE.isLocalStorage()) {

arex-agent/pom.xml

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,43 @@
245245
</configuration>
246246
</plugin>
247247

248+
<plugin>
249+
<groupId>org.apache.maven.plugins</groupId>
250+
<artifactId>maven-assembly-plugin</artifactId>
251+
<version>3.3.0</version>
252+
<configuration>
253+
<finalName>arex</finalName>
254+
<descriptors>
255+
<descriptor>${xml-path}</descriptor>
256+
</descriptors>
257+
<archive>
258+
<manifest>
259+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
260+
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
261+
</manifest>
262+
<manifestEntries>
263+
<Premain-Class>io.arex.agent.ArexJavaAgent</Premain-Class>
264+
<Agent-Class>io.arex.agent.ArexJavaAgent</Agent-Class>
265+
<Can-Redefine-Classes>true</Can-Redefine-Classes>
266+
<Can-Retransform-Classes>true</Can-Retransform-Classes>
267+
<Can-Set-Native-Method-Prefix>true</Can-Set-Native-Method-Prefix>
268+
<Build-Time>${maven.build.timestamp}</Build-Time>
269+
<Built-By>arextest.com</Built-By>
270+
</manifestEntries>
271+
</archive>
272+
</configuration>
273+
274+
<executions>
275+
<execution>
276+
<id>make-assembly</id>
277+
<phase>package</phase>
278+
<goals>
279+
<goal>single</goal>
280+
</goals>
281+
</execution>
282+
</executions>
283+
</plugin>
284+
248285
<plugin>
249286
<groupId>org.apache.maven.plugins</groupId>
250287
<artifactId>maven-shade-plugin</artifactId>
@@ -282,7 +319,6 @@
282319
<include>net.bytebuddy:byte-buddy</include>
283320
<include>org.slf4j:slf4j-simple</include>
284321
<include>io.arex:**</include>
285-
<include>com.fasterxml.jackson.core:**</include>
286322
</includes>
287323
</artifactSet>
288324
</configuration>
@@ -304,7 +340,6 @@
304340
</delete>
305341
<copy todir="../arex-agent-jar">
306342
<fileset dir="../arex-agent/target/" includes="arex-agent*.jar" />
307-
<fileset dir="../arex-agent-bootstrap/target/" includes="arex-agent-bootstrap*.jar" />
308343
</copy>
309344
</target>
310345
</configuration>

0 commit comments

Comments
 (0)