This is an automated email from the ASF dual-hosted git repository.

panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 69ca375fa7d Refactor AgentExtraClassLoader (#23678)
69ca375fa7d is described below

commit 69ca375fa7dd50d30a97f99679886e18b4a8fd3a
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Jan 20 15:20:30 2023 +0800

    Refactor AgentExtraClassLoader (#23678)
    
    * Refactor AgentLoggerFactory
    
    * Refactor AgentExtraClassLoader
---
 .../core/classloader/AgentExtraClassLoader.java    | 40 +++++++++++-----------
 .../agent/core/log/AgentLoggerClassLoader.java     |  4 +--
 .../agent/core/log/AgentLoggerFactory.java         | 34 ++++++++----------
 3 files changed, 37 insertions(+), 41 deletions(-)

diff --git 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/classloader/AgentExtraClassLoader.java
 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/classloader/AgentExtraClassLoader.java
index bf22c13a4f3..c6367f8f680 100644
--- 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/classloader/AgentExtraClassLoader.java
+++ 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/classloader/AgentExtraClassLoader.java
@@ -44,24 +44,24 @@ public abstract class AgentExtraClassLoader extends 
ClassLoader {
         registerAsParallelCapable();
     }
     
-    private final Collection<JarFile> pluginJars;
+    private final Collection<JarFile> extraJars;
     
-    private final Collection<File> resourcePaths;
+    private final Collection<File> extraResourcePaths;
     
-    public AgentExtraClassLoader(final ClassLoader appClassLoader, final 
Collection<JarFile> pluginJars) {
-        this(appClassLoader, pluginJars, Collections.emptyList());
+    public AgentExtraClassLoader(final ClassLoader appClassLoader, final 
Collection<JarFile> extraJars) {
+        this(appClassLoader, extraJars, Collections.emptyList());
     }
     
-    public AgentExtraClassLoader(final ClassLoader appClassLoader, final 
Collection<JarFile> pluginJars, final Collection<File> resourcePaths) {
+    public AgentExtraClassLoader(final ClassLoader appClassLoader, final 
Collection<JarFile> extraJars, final Collection<File> extraResourcePaths) {
         super(appClassLoader);
-        this.pluginJars = pluginJars;
-        this.resourcePaths = resourcePaths;
+        this.extraJars = extraJars;
+        this.extraResourcePaths = extraResourcePaths;
     }
     
     @Override
     protected Class<?> findClass(final String name) throws 
ClassNotFoundException {
         String path = convertClassNameToPath(name);
-        for (JarFile each : pluginJars) {
+        for (JarFile each : extraJars) {
             ZipEntry entry = each.getEntry(path);
             if (null == entry) {
                 continue;
@@ -80,18 +80,18 @@ public abstract class AgentExtraClassLoader extends 
ClassLoader {
         return String.join("", className.replace(".", "/"), ".class");
     }
     
-    private void definePackage(final String className, final JarFile 
pluginJar) throws IOException {
+    private void definePackage(final String className, final JarFile extraJar) 
throws IOException {
         int index = className.lastIndexOf('.');
         if (-1 == index) {
             return;
         }
         String packageName = className.substring(0, index);
         if (null == getPackage(packageName)) {
-            definePackage(packageName, pluginJar.getManifest());
+            definePackage(packageName, extraJar.getManifest());
         }
     }
     
-    private void definePackage(final String packageName, final Manifest 
manifest) {
+    private void definePackage(final String name, final Manifest manifest) {
         Attributes attributes = manifest.getMainAttributes();
         String specTitle = 
attributes.getValue(Attributes.Name.SPECIFICATION_TITLE);
         String specVersion = 
attributes.getValue(Attributes.Name.SPECIFICATION_VERSION);
@@ -99,18 +99,18 @@ public abstract class AgentExtraClassLoader extends 
ClassLoader {
         String implTitle = 
attributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
         String implVersion = 
attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
         String implVendor = 
attributes.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
-        definePackage(packageName, specTitle, specVersion, specVendor, 
implTitle, implVersion, implVendor, null);
+        definePackage(name, specTitle, specVersion, specVendor, implTitle, 
implVersion, implVendor, null);
     }
     
-    private Class<?> defineClass(final String name, final JarFile pluginJar, 
final ZipEntry entry) throws IOException {
-        byte[] data = ByteStreams.toByteArray(pluginJar.getInputStream(entry));
+    private Class<?> defineClass(final String name, final JarFile extraJar, 
final ZipEntry entry) throws IOException {
+        byte[] data = ByteStreams.toByteArray(extraJar.getInputStream(entry));
         return defineClass(name, data, 0, data.length);
     }
     
     @Override
     protected Enumeration<URL> findResources(final String name) {
         Collection<URL> result = new LinkedList<>();
-        for (JarFile each : pluginJars) {
+        for (JarFile each : extraJars) {
             findResource(name, each).ifPresent(result::add);
         }
         if (result.isEmpty()) {
@@ -121,17 +121,17 @@ public abstract class AgentExtraClassLoader extends 
ClassLoader {
     
     @Override
     protected URL findResource(final String name) {
-        return pluginJars.stream().map(each -> findResource(name, 
each)).filter(Optional::isPresent).findFirst().filter(Optional::isPresent).map(Optional::get)
+        return extraJars.stream().map(each -> findResource(name, 
each)).filter(Optional::isPresent).findFirst().filter(Optional::isPresent).map(Optional::get)
                 .orElseGet(() -> 
findResourcesFromResourcePaths(name).stream().findFirst().orElse(null));
     }
     
-    private Optional<URL> findResource(final String name, final JarFile 
pluginJar) {
-        JarEntry entry = pluginJar.getJarEntry(name);
+    private Optional<URL> findResource(final String name, final JarFile 
extraJar) {
+        JarEntry entry = extraJar.getJarEntry(name);
         if (null == entry) {
             return Optional.empty();
         }
         try {
-            return Optional.of(new URL(String.format("jar:file:%s!/%s", 
pluginJar.getName(), name)));
+            return Optional.of(new URL(String.format("jar:file:%s!/%s", 
extraJar.getName(), name)));
         } catch (final MalformedURLException ignored) {
             return Optional.empty();
         }
@@ -139,7 +139,7 @@ public abstract class AgentExtraClassLoader extends 
ClassLoader {
     
     private Collection<URL> findResourcesFromResourcePaths(final String name) {
         Collection<URL> result = new LinkedList<>();
-        Collection<File> resourceFiles = resourcePaths.stream().map(each -> 
new File(String.join(File.separator, each.getPath(), 
name))).filter(File::exists).collect(Collectors.toList());
+        Collection<File> resourceFiles = extraResourcePaths.stream().map(each 
-> new File(String.join(File.separator, each.getPath(), 
name))).filter(File::exists).collect(Collectors.toList());
         for (File each : resourceFiles) {
             try {
                 result.add(each.toURI().toURL());
diff --git 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/log/AgentLoggerClassLoader.java
 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/log/AgentLoggerClassLoader.java
index a762f42e165..010a49e8a93 100644
--- 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/log/AgentLoggerClassLoader.java
+++ 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/log/AgentLoggerClassLoader.java
@@ -29,8 +29,8 @@ import java.util.jar.JarFile;
  */
 public final class AgentLoggerClassLoader extends AgentExtraClassLoader {
     
-    public AgentLoggerClassLoader(final Collection<JarFile> pluginJars, final 
File resourcePath) {
-        super(AgentLoggerFactory.class.getClassLoader().getParent(), 
pluginJars, Collections.singleton(resourcePath));
+    public AgentLoggerClassLoader(final Collection<JarFile> loggingJars, final 
File resourcePath) {
+        super(AgentLoggerFactory.class.getClassLoader().getParent(), 
loggingJars, Collections.singleton(resourcePath));
     }
     
     public AgentLoggerClassLoader() {
diff --git 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/log/AgentLoggerFactory.java
 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/log/AgentLoggerFactory.java
index 97b947a69f3..c2327a22f37 100644
--- 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/log/AgentLoggerFactory.java
+++ 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/log/AgentLoggerFactory.java
@@ -66,34 +66,21 @@ public final class AgentLoggerFactory {
         return classLoader;
     }
     
-    @SneakyThrows({URISyntaxException.class, IOException.class})
+    @SneakyThrows(URISyntaxException.class)
     private static AgentLoggerClassLoader getAgentLoggerClassLoader() {
         File agentFle = new 
File(AgentLoggerFactory.class.getProtectionDomain().getCodeSource().getLocation().toURI());
-        if (agentFle.isFile() && agentFle.getName().endsWith(".jar")) {
-            Collection<JarFile> pluginJars = getPluginJars(getJarFiles(new 
File(String.join(File.separator, AgentPath.getRootPath().getPath(), "lib"))));
-            File resourcePath = new File(String.join(File.separator, 
AgentPath.getRootPath().getPath(), "conf"));
-            return new AgentLoggerClassLoader(pluginJars, resourcePath);
-        }
-        return new AgentLoggerClassLoader();
-    }
-    
-    private static Collection<JarFile> getPluginJars(final Collection<File> 
jarFiles) throws IOException {
-        Collection<JarFile> result = new LinkedList<>();
-        for (File each : jarFiles) {
-            result.add(new JarFile(each, true));
-        }
-        return result;
+        return agentFle.isFile() && agentFle.getName().endsWith(".jar") ? new 
AgentLoggerClassLoader(getLoggingJars(), getLoggingResourcePath()) : new 
AgentLoggerClassLoader();
     }
     
     @SneakyThrows(IOException.class)
-    private static Collection<File> getJarFiles(final File file) {
-        Collection<File> result = new LinkedList<>();
-        Files.walkFileTree(file.toPath(), new SimpleFileVisitor<Path>() {
+    private static Collection<JarFile> getLoggingJars() {
+        Collection<JarFile> result = new LinkedList<>();
+        Files.walkFileTree(new File(String.join(File.separator, 
AgentPath.getRootPath().getPath(), "lib")).toPath(), new 
SimpleFileVisitor<Path>() {
             
             @Override
             public FileVisitResult visitFile(final Path path, final 
BasicFileAttributes attributes) {
                 if (path.toFile().isFile() && 
path.toFile().getName().endsWith(".jar")) {
-                    result.add(path.toFile());
+                    result.add(getJarFile(path));
                 }
                 return FileVisitResult.CONTINUE;
             }
@@ -101,6 +88,15 @@ public final class AgentLoggerFactory {
         return result;
     }
     
+    @SneakyThrows(IOException.class)
+    private static JarFile getJarFile(final Path path) {
+        return new JarFile(path.toFile(), true);
+    }
+    
+    private static File getLoggingResourcePath() {
+        return new File(String.join(File.separator, 
AgentPath.getRootPath().getPath(), "conf"));
+    }
+    
     /**
      * Logger.
      */

Reply via email to