Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.URL;
import java.util.Properties;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import io.oneagent.plugin.classloader.PluginClassLoader;
import io.oneagent.plugin.config.PluginConfig;
Expand Down Expand Up @@ -96,15 +97,29 @@ public void start() throws PluginException {
}

Class<?> clazz = null;
if (this.pluginConfig.isAppendToSystemClassLoaderSearch()) {
this.instrumentation.appendToSystemClassLoaderSearch(new JarFile(agentJarFile));
this.parentClassLoader = ClassLoader.getSystemClassLoader();
} else {
this.parentClassLoader = new PluginClassLoader(new URL[] { agentJarFile.toURI().toURL() },
this.getClass().getClassLoader());
if(this.pluginConfig.isAppendToSystemClassLoaderSearch()){
JarFile agentJar = null;
try {
agentJar = new JarFile( agentJarFile, false);
verifyJarManifestMainClassIsThis(agentJarFile, agentJar);
instrumentation.appendToBootstrapClassLoaderSearch(agentJar);
instrumentation.appendToSystemClassLoaderSearch(agentJar);
clazz = Class.forName(className,false,null);
} catch (IOException e) {
throw new PluginException("agentJar",e);
}
}else{
if (this.pluginConfig.isAppendToSystemClassLoaderSearch()) {
this.instrumentation.appendToSystemClassLoaderSearch(new JarFile(agentJarFile));
this.parentClassLoader = ClassLoader.getSystemClassLoader();
} else {
this.parentClassLoader = new PluginClassLoader(new URL[] { agentJarFile.toURI().toURL() },
this.getClass().getClassLoader());
}

clazz = parentClassLoader.loadClass(className);
}

clazz = parentClassLoader.loadClass(className);

// 反射调用启动
Method method = clazz.getMethod(methodName, String.class, Instrumentation.class);
Expand All @@ -115,6 +130,18 @@ public void start() throws PluginException {

}

private static void verifyJarManifestMainClassIsThis(File jarFile, JarFile agentJar)
throws IOException {
Manifest manifest = agentJar.getManifest();
if (manifest.getMainAttributes().getValue("Premain-Class") == null) {
throw new IllegalStateException(
"The agent was not installed, because the agent was found in '"
+ jarFile
+ "', which doesn't contain a Premain-Class manifest attribute. Make sure that you"
+ " haven't included the agent jar file inside of an application uber jar.");
}
}

@Override
public void stop() throws PluginException {
// TODO Auto-generated method stub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class TraditionalPluginConfigImpl extends PluginConfigImpl {

private boolean appendToSystemClassLoaderSearch;

private boolean appendToBootstrapClassLoaderSearch;

private String agentArgs;


Expand All @@ -37,6 +39,8 @@ public TraditionalPluginConfigImpl(Properties globalProperties, Properties plugi

this.appendToSystemClassLoaderSearch = this.propertyResolver.getProperty("appendToSystemClassLoaderSearch", Boolean.class, Boolean.TRUE);

this.appendToBootstrapClassLoaderSearch = this.propertyResolver.getProperty("appendToBootstrapClassLoaderSearch", Boolean.class, Boolean.FALSE);

}


Expand All @@ -48,6 +52,10 @@ public boolean isAppendToSystemClassLoaderSearch() {
return appendToSystemClassLoaderSearch;
}

public boolean isAppendToBootstrapClassLoaderSearch() {
return appendToBootstrapClassLoaderSearch;
}

public String getAgentArgs() {
return agentArgs;
}
Expand Down