You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/12/21 11:20:25 UTC

[shardingsphere] branch master updated: Adjust methods sequence of AgentPluginLoader (#8705)

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 340186e  Adjust methods sequence of AgentPluginLoader (#8705)
340186e is described below

commit 340186ecc6fb979874c08cac037bc71272c777b4
Author: Liang Zhang <te...@163.com>
AuthorDate: Mon Dec 21 19:20:00 2020 +0800

    Adjust methods sequence of AgentPluginLoader (#8705)
    
    * Move sequence of AgentPluginLoader.getInstance
    
    * Adjust methods sequence of AgentPluginLoader
---
 .../agent/core/plugin/AgentPluginLoader.java       | 200 ++++++++++-----------
 1 file changed, 100 insertions(+), 100 deletions(-)

diff --git a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java
index 84d7625..786cc6e 100644
--- a/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java
+++ b/shardingsphere-agent/shardingsphere-agent-core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java
@@ -29,6 +29,7 @@ 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.common.AgentPathBuilder;
 import org.apache.shardingsphere.agent.core.config.AgentConfiguration;
 import org.apache.shardingsphere.agent.core.utils.SingletonHolder;
@@ -78,67 +79,10 @@ public final class AgentPluginLoader extends ClassLoader implements Closeable {
         super(AgentPluginLoader.class.getClassLoader());
     }
     
-    @Override
-    protected Class<?> findClass(final String name) throws ClassNotFoundException {
-        String path = classNameToPath(name);
-        for (UberJar jar : jars) {
-            ZipEntry entry = jar.jarFile.getEntry(path);
-            if (Objects.nonNull(entry)) {
-                try {
-                    byte[] data = ByteStreams.toByteArray(jar.jarFile.getInputStream(entry));
-                    return defineClass(name, data, 0, data.length);
-                } catch (final IOException ex) {
-                    log.error("Failed to load class {}.", name, ex);
-                }
-            }
-        }
-        throw new ClassNotFoundException("Class " + name + " not found.");
-    }
-    
-    @Override
-    protected Enumeration<URL> findResources(final String name) {
-        List<URL> resources = Lists.newArrayList();
-        for (UberJar jar : jars) {
-            JarEntry entry = jar.jarFile.getJarEntry(name);
-            if (Objects.nonNull(entry)) {
-                try {
-                    resources.add(new URL("jar:file:" + jar.sourcePath.getAbsolutePath() + "!/" + name));
-                } catch (final MalformedURLException ignored) {
-                }
-            }
-        }
-        return Collections.enumeration(resources);
-    }
-    
-    @Override
-    protected URL findResource(final String name) {
-        for (UberJar jar : jars) {
-            JarEntry entry = jar.jarFile.getJarEntry(name);
-            if (Objects.nonNull(entry)) {
-                try {
-                    return new URL("jar:file:" + jar.sourcePath.getAbsolutePath() + "!/" + name);
-                } catch (final MalformedURLException ignored) {
-                }
-            }
-        }
-        return null;
-    }
-    
-    @Override
-    public void close() {
-        for (UberJar jar : jars) {
-            try {
-                jar.jarFile.close();
-            } catch (final IOException ex) {
-                log.error("close is ", ex);
-            }
-        }
-    }
-    
     /**
-     * To get agent plugin loader instance.
+     * Get agent plugin loader instance.
      *
-     * @return plugin loader
+     * @return agent plugin loader instance
      */
     public static AgentPluginLoader getInstance() {
         if (null == agentPluginLoader) {
@@ -154,7 +98,7 @@ public final class AgentPluginLoader extends ClassLoader implements Closeable {
     /**
      * Load all plugins.
      *
-     * @throws IOException the IO exception
+     * @throws IOException IO exception
      */
     public void loadAllPlugins() throws IOException {
         File[] jarFiles = AgentPathBuilder.getPluginPath().listFiles(file -> file.getName().endsWith(".jar"));
@@ -162,8 +106,7 @@ public final class AgentPluginLoader extends ClassLoader implements Closeable {
             return;
         }
         Map<String, PluginAdviceDefinition> pluginAdviceDefinitionMap = Maps.newHashMap();
-        AgentConfiguration configuration = SingletonHolder.INSTANCE.get(AgentConfiguration.class);
-        List<String> activatedLists = configuration.getActivatedPlugins();
+        List<String> activatedLists = SingletonHolder.INSTANCE.get(AgentConfiguration.class).getActivatedPlugins();
         if (null == activatedLists) {
             activatedLists = Lists.newArrayList();
         }
@@ -215,8 +158,49 @@ public final class AgentPluginLoader extends ClassLoader implements Closeable {
         pluginDefineMap = ImmutableMap.<String, PluginAdviceDefinition>builder().putAll(pluginAdviceDefinitionMap).build();
     }
     
-    private String classNameToPath(final String className) {
-        return className.replace(".", "/") + ".class";
+    /**
+     * Initial all services.
+     */
+    public void initialAllServices() {
+        services.forEach(service -> {
+            try {
+                service.setup();
+                // CHECKSTYLE:OFF
+            } catch (final Throwable ex) {
+                // CHECKSTYLE:ON
+                log.error("Failed to initial service.", ex);
+            }
+        });
+    }
+    
+    /**
+     * Start all services.
+     */
+    public void startAllServices() {
+        services.forEach(service -> {
+            try {
+                service.start();
+                // CHECKSTYLE:OFF
+            } catch (final Throwable ex) {
+                // CHECKSTYLE:ON
+                log.error("Failed to start service.", ex);
+            }
+        });
+    }
+    
+    /**
+     * Shutdown all services.
+     */
+    public void shutdownAllServices() {
+        services.forEach(service -> {
+            try {
+                service.cleanup();
+                // CHECKSTYLE:OFF
+            } catch (final Throwable ex) {
+                // CHECKSTYLE:ON
+                log.error("Failed to shutdown service.", ex);
+            }
+        });
     }
     
     /**
@@ -225,7 +209,7 @@ public final class AgentPluginLoader extends ClassLoader implements Closeable {
      * @return type matcher
      */
     public ElementMatcher<? super TypeDescription> typeMatcher() {
-        return new ElementMatcher.Junction<TypeDescription>() {
+        return new Junction<TypeDescription>() {
             
             @Override
             public boolean matches(final TypeDescription target) {
@@ -290,49 +274,65 @@ public final class AgentPluginLoader extends ClassLoader implements Closeable {
         }
     }
     
-    /**
-     * Initial all services.
-     */
-    public void initialAllServices() {
-        services.forEach(service -> {
-            try {
-                service.setup();
-                // CHECKSTYLE:OFF
-            } catch (final Throwable ex) {
-                // CHECKSTYLE:ON
-                log.error("Failed to initial service.", ex);
+    @Override
+    protected Class<?> findClass(final String name) throws ClassNotFoundException {
+        String path = classNameToPath(name);
+        for (UberJar jar : jars) {
+            ZipEntry entry = jar.jarFile.getEntry(path);
+            if (Objects.nonNull(entry)) {
+                try {
+                    byte[] data = ByteStreams.toByteArray(jar.jarFile.getInputStream(entry));
+                    return defineClass(name, data, 0, data.length);
+                } catch (final IOException ex) {
+                    log.error("Failed to load class {}.", name, ex);
+                }
             }
-        });
+        }
+        throw new ClassNotFoundException("Class " + name + " not found.");
     }
     
-    /**
-     * Start all services.
-     */
-    public void startAllServices() {
-        services.forEach(service -> {
-            try {
-                service.start();
-                // CHECKSTYLE:OFF
-            } catch (final Throwable ex) {
-                // CHECKSTYLE:ON
-                log.error("Failed to start service.", ex);
+    private String classNameToPath(final String className) {
+        return className.replace(".", "/") + ".class";
+    }
+    
+    @Override
+    protected Enumeration<URL> findResources(final String name) {
+        List<URL> resources = Lists.newArrayList();
+        for (UberJar jar : jars) {
+            JarEntry entry = jar.jarFile.getJarEntry(name);
+            if (Objects.nonNull(entry)) {
+                try {
+                    resources.add(new URL("jar:file:" + jar.sourcePath.getAbsolutePath() + "!/" + name));
+                } catch (final MalformedURLException ignored) {
+                }
             }
-        });
+        }
+        return Collections.enumeration(resources);
     }
     
-    /**
-     * Shutdown all services.
-     */
-    public void shutdownAllServices() {
-        services.forEach(service -> {
+    @Override
+    protected URL findResource(final String name) {
+        for (UberJar jar : jars) {
+            JarEntry entry = jar.jarFile.getJarEntry(name);
+            if (Objects.nonNull(entry)) {
+                try {
+                    return new URL("jar:file:" + jar.sourcePath.getAbsolutePath() + "!/" + name);
+                } catch (final MalformedURLException ignored) {
+                }
+            }
+        }
+        return null;
+    }
+    
+    @Override
+    public void close() {
+        for (UberJar jar : jars) {
             try {
-                service.cleanup();
-                // CHECKSTYLE:OFF
-            } catch (final Throwable ex) {
-                // CHECKSTYLE:ON
-                log.error("Failed to shutdown service.", ex);
+                jar.jarFile.close();
+            } catch (final IOException ex) {
+                log.error("close is ", ex);
             }
-        });
+        }
     }
     
     @RequiredArgsConstructor