Skip to content

Commit

Permalink
Improvement classloader (apache#8750)
Browse files Browse the repository at this point in the history
* Improvement classloader

* fix checkstyle
  • Loading branch information
dmsolr authored Dec 24, 2020
1 parent a5d93dc commit 1399e19
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.ByteStreams;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatcher.Junction;
import org.apache.shardingsphere.agent.core.cache.AgentObjectPool;
import org.apache.shardingsphere.agent.core.config.AgentConfiguration;
import org.apache.shardingsphere.agent.core.path.AgentPathBuilder;
import org.apache.shardingsphere.agent.core.plugin.definition.PluginDefinition;
import org.apache.shardingsphere.agent.core.plugin.point.PluginInterceptorPoint;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
Expand All @@ -39,18 +51,8 @@
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatcher.Junction;
import org.apache.shardingsphere.agent.core.cache.AgentObjectPool;
import org.apache.shardingsphere.agent.core.config.AgentConfiguration;
import org.apache.shardingsphere.agent.core.path.AgentPathBuilder;
import org.apache.shardingsphere.agent.core.plugin.definition.PluginDefinition;
import org.apache.shardingsphere.agent.core.plugin.point.PluginInterceptorPoint;

/**
* Agent plugin loader.
Expand Down Expand Up @@ -211,6 +213,11 @@ protected Class<?> findClass(final String name) throws ClassNotFoundException {
ZipEntry entry = each.jarFile.getEntry(path);
if (Objects.nonNull(entry)) {
try {
int i = name.lastIndexOf('.');
if (i != -1) {
String packageName = name.substring(0, i);
definePackageInternal(packageName, each.jarFile.getManifest());
}
byte[] data = ByteStreams.toByteArray(each.jarFile.getInputStream(entry));
return defineClass(name, data, 0, data.length);
} catch (final IOException ex) {
Expand All @@ -225,6 +232,20 @@ private String classNameToPath(final String className) {
return className.replace(".", "/") + ".class";
}

private void definePackageInternal(final String packageName, final Manifest manifest) {
if (getPackage(packageName) != null) {
return;
}
Attributes attr = manifest.getMainAttributes();
String specTitle = attr.getValue(Attributes.Name.SPECIFICATION_TITLE);
String specVersion = attr.getValue(Attributes.Name.SPECIFICATION_VERSION);
String specVendor = attr.getValue(Attributes.Name.SPECIFICATION_VENDOR);
String implTitle = attr.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
String implVersion = attr.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
String implVendor = attr.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
super.definePackage(packageName, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, null);
}

@Override
protected Enumeration<URL> findResources(final String name) {
List<URL> resources = Lists.newArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<properties>
<jaeger-client.version>0.31.0</jaeger-client.version>
<opentracing.version>0.31.0</opentracing.version>
<entrypoint.class>org.apache.shardingsphere.agent.plugin.tracing.jaeger.definition.JaegerPluginDefinition</entrypoint.class>
</properties>

Expand All @@ -40,6 +41,16 @@
<artifactId>jaeger-client</artifactId>
<version>${jaeger-client.version}</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
<version>${opentracing.version}</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-util</artifactId>
<version>${opentracing.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shardingsphere</groupId>
Expand Down

0 comments on commit 1399e19

Please sign in to comment.