Skip to content

Commit

Permalink
[#8890] Implemented findClass in Java9ClassLoader
Browse files Browse the repository at this point in the history
Removed manually registering providers which occurs duplication
  • Loading branch information
smilu97 committed Apr 25, 2023
1 parent a8de915 commit 11c7c7e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class Java9ClassLoader extends URLClassLoader {
private final ClassLoader parent;
private final ProfilerLibClass profilerLibClass;

private volatile String moduleName = null;

public Java9ClassLoader(String name, URL[] urls, ClassLoader parent, List<String> libClass) {
super(name, urls, parent);
this.parent = parent;
Expand All @@ -55,6 +57,18 @@ protected URL findResource(String moduleName, String name) throws IOException {
return null;
}

@Override
protected Class<?> findClass(String moduleName, String name) {
if (this.moduleName != null || this.moduleName.equals(moduleName)) {
try {
return this.findClass(name);
} catch (ClassNotFoundException e) {
return null;
}
}
return null;
}

@Override
public InputStream getResourceAsStream(String name) {
return super.getResourceAsStream(name);
Expand Down Expand Up @@ -125,6 +139,10 @@ private boolean onLoadClass(String name) {
return profilerLibClass.onLoadClass(name);
}

public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}

@Override
public String toString() {
return "Java9ClassLoader{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
package com.navercorp.pinpoint.bootstrap.java9.module;


import com.navercorp.pinpoint.bootstrap.java9.classloader.Java9ClassLoader;
import com.navercorp.pinpoint.bootstrap.module.JavaModule;
import com.navercorp.pinpoint.bootstrap.module.Providers;
import com.navercorp.pinpoint.common.util.JvmUtils;
import com.navercorp.pinpoint.common.util.JvmVersion;

import java.lang.instrument.Instrumentation;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -34,21 +32,22 @@
*/
public class ModuleSupport {

private static final String JAVA9_PINPOINT_AGENT_MODULE_NAME = "pinpoint.agent";

private final Instrumentation instrumentation;

private final ModuleLogger logger = ModuleLogger.getLogger(this.getClass().getName());

private final JavaModule javaBaseModule;
private final JavaModule bootstrapModule;
private final List<String> allowedProviders;

ModuleSupport(Instrumentation instrumentation, List<String> allowedProviders) {
ModuleSupport(Instrumentation instrumentation) {
this.instrumentation = Objects.requireNonNull(instrumentation, "instrumentation");
this.allowedProviders = Objects.requireNonNull(allowedProviders, "allowedProviders");
this.javaBaseModule = wrapJavaModule(Object.class);
this.bootstrapModule = wrapJavaModule(this.getClass());
}

@SuppressWarnings("unused") // Used implicitly
public void setup() {
// pinpoint module name : unnamed
JavaModule bootstrapModule = getBootstrapModule();
Expand All @@ -65,8 +64,8 @@ public void setup() {

}

@SuppressWarnings("unused") // Used implicitly
public void defineAgentModule(ClassLoader classLoader, URL[] jarFileList) {

final JavaModule agentModule = newAgentModule(classLoader, jarFileList);

prepareAgentModule(classLoader, agentModule);
Expand All @@ -92,7 +91,12 @@ private void addPermissionToValueAnnotation(JavaModule agentModule) {

private JavaModule newAgentModule(ClassLoader classLoader, URL[] jarFileList) {
ModuleBuilder moduleBuilder = new ModuleBuilder();
final Module agentModule = moduleBuilder.defineModule("pinpoint.agent", classLoader, jarFileList);
final Module agentModule = moduleBuilder.defineModule(JAVA9_PINPOINT_AGENT_MODULE_NAME, classLoader, jarFileList);

if (classLoader instanceof Java9ClassLoader) {
((Java9ClassLoader) classLoader).setModuleName(JAVA9_PINPOINT_AGENT_MODULE_NAME);
}

return wrapJavaModule(agentModule);
}

Expand Down Expand Up @@ -229,32 +233,6 @@ private void prepareAgentModule(final ClassLoader classLoader, JavaModule agentM
final String watchEventServiceName = "org.apache.logging.log4j.core.util.WatchEventService";
Class<?> watchEventServiceClazz = forName(watchEventServiceName, classLoader);
agentModule.addUses(watchEventServiceClazz);

List<Providers> providersList = agentModule.getProviders();
for (Providers providers : providersList) {
final String service = providers.getService();
if (isAllowedProvider(service)) {
logger.info("load provider:" + providers);
Class<?> serviceClass = forName(providers.getService(), classLoader);
List<Class<?>> providerClassList = loadProviderClassList(providers.getProviders(), classLoader);
agentModule.addProvides(serviceClass, providerClassList);
} else {
logger.info("discard provider:" + providers);
}
}
}

public boolean isAllowedProvider(String serviceName) {
return allowedProviders.contains(serviceName);
}

private List<Class<?>> loadProviderClassList(List<String> classNameList, ClassLoader classLoader) {
List<Class<?>> providerClassList = new ArrayList<>();
for (String providerClassName : classNameList) {
Class<?> providerClass = forName(providerClassName, classLoader);
providerClassList.add(providerClass);
}
return providerClassList;
}

private Class<?> forName(String className, ClassLoader classLoader) {
Expand All @@ -281,7 +259,7 @@ private JavaModule loadModule(String moduleName) {
// throw new ModuleException(moduleName + " not found");
}

private JavaModule wrapJavaModule(Class clazz) {
private JavaModule wrapJavaModule(Class<?> clazz) {
return new Java9Module(instrumentation, clazz.getModule());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package com.navercorp.pinpoint.bootstrap.java9.module;

import java.lang.instrument.Instrumentation;
import java.util.Arrays;
import java.util.List;

/**
* @author Woonduk Kang(emeroad)
Expand All @@ -28,16 +26,6 @@ public ModuleSupportFactory() {
}

public ModuleSupport newModuleSupport(Instrumentation instrumentation) {
// Dynamic changes are required?
// move to pinpoint.config?
List<String> allowedProviders = Arrays.asList(
"io.grpc.NameResolverProvider",
"com.navercorp.pinpoint.agent.plugin.proxy.common.ProxyRequestMetadataProvider",
"com.navercorp.pinpoint.profiler.context.recorder.proxy.ProxyRequestParserProvider",
"io.grpc.ManagedChannelProvider",
"io.grpc.LoadBalancerProvider"
);

return new ModuleSupport(instrumentation, allowedProviders);
return new ModuleSupport(instrumentation);
}
}

0 comments on commit 11c7c7e

Please # to comment.