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 b29a67b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ protected URL findResource(String moduleName, String name) throws IOException {
return null;
}

@Override
protected Class<?> findClass(String moduleName, String name) {
if (getName().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
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@


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 @@ -40,15 +37,14 @@ public class ModuleSupport {

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 +61,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 +88,7 @@ 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(classLoader.getName(), classLoader, jarFileList);
return wrapJavaModule(agentModule);
}

Expand Down Expand Up @@ -229,32 +225,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 +251,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 b29a67b

Please # to comment.