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

zhaojinchao 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 a1bdd254bec Refactor AgentPathBuilder (#23382)
a1bdd254bec is described below

commit a1bdd254becec22517576bbb3378f63a16d4eb1e
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Jan 6 23:57:43 2023 +0800

    Refactor AgentPathBuilder (#23382)
---
 .../agent/core/ShardingSphereAgent.java            |  7 ++--
 .../AgentPathBuilder.java => path/AgentPath.java}  | 39 ++++++++--------------
 .../core/plugin/loader/AgentPluginLoader.java      | 10 +++---
 .../plugin/loader/PluginConfigurationLoader.java   | 10 +++---
 .../loader/PluginConfigurationLoaderTest.java      |  7 ++--
 5 files changed, 29 insertions(+), 44 deletions(-)

diff --git 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/ShardingSphereAgent.java
 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/ShardingSphereAgent.java
index d764d36d021..a242e01ca56 100644
--- 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/ShardingSphereAgent.java
+++ 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/ShardingSphereAgent.java
@@ -23,6 +23,7 @@ import net.bytebuddy.ByteBuddy;
 import net.bytebuddy.agent.builder.AgentBuilder;
 import net.bytebuddy.dynamic.scaffold.TypeValidation;
 import net.bytebuddy.matcher.ElementMatchers;
+import org.apache.shardingsphere.agent.core.path.AgentPath;
 import 
org.apache.shardingsphere.agent.core.plugin.yaml.plugin.loader.PluginConfigurationLoader;
 import org.apache.shardingsphere.agent.core.logging.LoggingListener;
 import org.apache.shardingsphere.agent.core.plugin.PluginJar;
@@ -33,6 +34,7 @@ import 
org.apache.shardingsphere.agent.core.transformer.AgentTransformer;
 import 
org.apache.shardingsphere.agent.core.plugin.advisor.AdvisorConfiguration;
 import org.apache.shardingsphere.agent.api.PluginConfiguration;
 
+import java.io.File;
 import java.io.IOException;
 import java.lang.instrument.Instrumentation;
 import java.util.Collection;
@@ -52,9 +54,10 @@ public final class ShardingSphereAgent {
      * @throws IOException IO exception
      */
     public static void premain(final String args, final Instrumentation 
instrumentation) throws IOException {
-        Map<String, PluginConfiguration> pluginConfigs = 
PluginConfigurationLoader.load();
+        File rootPath = AgentPath.getRootPath();
+        Map<String, PluginConfiguration> pluginConfigs = 
PluginConfigurationLoader.load(rootPath);
+        Collection<PluginJar> pluginJars = AgentPluginLoader.load(rootPath);
         boolean isEnhancedForProxy = isEnhancedForProxy();
-        Collection<PluginJar> pluginJars = AgentPluginLoader.load();
         Map<String, AdvisorConfiguration> advisorConfigs = 
AdvisorConfigurationLoader.load(pluginJars, pluginConfigs.keySet(), 
isEnhancedForProxy);
         setUpAgentBuilder(instrumentation, pluginConfigs, pluginJars, 
advisorConfigs, isEnhancedForProxy);
     }
diff --git 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/path/AgentPathBuilder.java
 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/path/AgentPath.java
similarity index 71%
rename from 
agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/path/AgentPathBuilder.java
rename to 
agent/core/src/main/java/org/apache/shardingsphere/agent/core/path/AgentPath.java
index 2a9af14b02b..c9a40132000 100644
--- 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/path/AgentPathBuilder.java
+++ 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/path/AgentPath.java
@@ -15,41 +15,32 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.agent.core.plugin.yaml.path;
+package org.apache.shardingsphere.agent.core.path;
 
 import com.google.common.base.Preconditions;
 import lombok.AccessLevel;
-import lombok.Getter;
 import lombok.NoArgsConstructor;
 
 import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.util.Arrays;
-import java.util.Collection;
 import java.util.Objects;
 
 /**
- * Agent path builder.
+ * Agent path.
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class AgentPathBuilder {
+public final class AgentPath {
     
-    @Getter
-    private static File agentPath;
-    
-    @Getter
-    private static Collection<File> pluginClassPaths;
-    
-    static {
-        agentPath = buildAgentPath();
-        pluginClassPaths = buildAgentPluginClassPaths();
-    }
-    
-    private static File buildAgentPath() {
-        String classResourcePath = String.join("", 
AgentPathBuilder.class.getName().replaceAll("\\.", "/"), ".class");
-        URL resource = 
Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResource(classResourcePath),
 "Can not locate agent jar file");
+    /**
+     * Get agent root path.
+     * 
+     * @return agent root path
+     */
+    public static File getRootPath() {
+        String classResourcePath = String.join("", 
AgentPath.class.getName().replaceAll("\\.", "/"), ".class");
+        URL resource = 
Objects.requireNonNull(ClassLoader.getSystemClassLoader().getResource(classResourcePath),
 "Can not locate agent jar file.");
         String url = resource.toString();
         int existFileInJarIndex = url.indexOf('!');
         boolean isInJar = existFileInJarIndex > -1;
@@ -60,10 +51,10 @@ public final class AgentPathBuilder {
         String realUrl = url.substring(url.indexOf("file:"), fileInJarIndex);
         try {
             File agentJarFile = new File(new URL(realUrl).toURI());
-            Preconditions.checkState(agentJarFile.exists(), "Can not locate 
agent jar file by URL `%s`", url);
+            Preconditions.checkState(agentJarFile.exists(), "Can not locate 
agent jar file by URL `%s`.", url);
             return agentJarFile.getParentFile();
         } catch (final MalformedURLException | URISyntaxException ex) {
-            throw new IllegalStateException(String.format("Can not locate 
agent jar file by URL `%s`", url));
+            throw new IllegalStateException(String.format("Can not locate 
agent jar file by URL `%s`.", url));
         }
     }
     
@@ -72,8 +63,4 @@ public final class AgentPathBuilder {
         String classLocation = url.substring(prefixLength, url.length() - 
classResourcePath.length());
         return new File(classLocation);
     }
-    
-    private static Collection<File> buildAgentPluginClassPaths() {
-        return Arrays.asList(new File(String.join("/", agentPath.getPath(), 
"lib")), new File(String.join("/", agentPath.getPath(), "plugins")));
-    }
 }
diff --git 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/loader/AgentPluginLoader.java
 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/loader/AgentPluginLoader.java
index 4bfe4f4bf5b..5db242e78e0 100644
--- 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/loader/AgentPluginLoader.java
+++ 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/loader/AgentPluginLoader.java
@@ -21,7 +21,6 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.agent.core.logging.LoggerFactory;
 import org.apache.shardingsphere.agent.core.logging.LoggerFactory.Logger;
-import org.apache.shardingsphere.agent.core.plugin.yaml.path.AgentPathBuilder;
 import org.apache.shardingsphere.agent.core.plugin.PluginJar;
 
 import java.io.File;
@@ -44,15 +43,14 @@ public final class AgentPluginLoader {
     /**
      * Load plugin jars.
      * 
+     * @param agentRootPath agent root path
      * @return plugin jars
      * @throws IOException IO exception
      */
-    public static Collection<PluginJar> load() throws IOException {
+    public static Collection<PluginJar> load(final File agentRootPath) throws 
IOException {
         List<File> jarFiles = new LinkedList<>();
-        AgentPathBuilder.getPluginClassPaths().forEach(each -> 
jarFiles.addAll(collectJarFiles(each)));
-        if (jarFiles.isEmpty()) {
-            return Collections.emptyList();
-        }
+        jarFiles.addAll(collectJarFiles(new File(String.join("/", 
agentRootPath.getPath(), "lib"))));
+        jarFiles.addAll(collectJarFiles(new File(String.join("/", 
agentRootPath.getPath(), "plugins"))));
         Collection<PluginJar> result = new LinkedList<>();
         for (File each : jarFiles) {
             result.add(new PluginJar(new JarFile(each, true), each));
diff --git 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/plugin/loader/PluginConfigurationLoader.java
 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/plugin/loader/PluginConfigurationLoader.java
index 819540d55f3..8b95eaf1e20 100644
--- 
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/plugin/loader/PluginConfigurationLoader.java
+++ 
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/yaml/plugin/loader/PluginConfigurationLoader.java
@@ -22,7 +22,6 @@ import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.agent.api.PluginConfiguration;
 import 
org.apache.shardingsphere.agent.core.plugin.yaml.plugin.entity.YamlAgentConfiguration;
 import 
org.apache.shardingsphere.agent.core.plugin.yaml.plugin.swapper.YamlPluginsConfigurationSwapper;
-import org.apache.shardingsphere.agent.core.plugin.yaml.path.AgentPathBuilder;
 import org.yaml.snakeyaml.Yaml;
 
 import java.io.File;
@@ -44,15 +43,16 @@ public final class PluginConfigurationLoader {
     /**
      * Load plugin configurations.
      *
+     * @param agentRootPath agent root path
      * @return plugin configurations
      * @throws IOException IO exception
      */
-    public static Map<String, PluginConfiguration> load() throws IOException {
-        File configFile = new File(AgentPathBuilder.getAgentPath(), 
CONFIG_PATH);
-        return 
load(configFile).map(YamlPluginsConfigurationSwapper::swap).orElse(Collections.emptyMap());
+    public static Map<String, PluginConfiguration> load(final File 
agentRootPath) throws IOException {
+        File configFile = new File(agentRootPath, CONFIG_PATH);
+        return 
loadYAML(configFile).map(YamlPluginsConfigurationSwapper::swap).orElse(Collections.emptyMap());
     }
     
-    private static Optional<YamlAgentConfiguration> load(final File yamlFile) 
throws IOException {
+    private static Optional<YamlAgentConfiguration> loadYAML(final File 
yamlFile) throws IOException {
         try (
                 FileInputStream fileInputStream = new 
FileInputStream(yamlFile);
                 InputStreamReader inputStreamReader = new 
InputStreamReader(fileInputStream)) {
diff --git 
a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/yaml/plugin/loader/PluginConfigurationLoaderTest.java
 
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/yaml/plugin/loader/PluginConfigurationLoaderTest.java
index d0ecc1bf3bf..62fbf578f33 100644
--- 
a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/yaml/plugin/loader/PluginConfigurationLoaderTest.java
+++ 
b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/yaml/plugin/loader/PluginConfigurationLoaderTest.java
@@ -17,9 +17,7 @@
 
 package org.apache.shardingsphere.agent.core.plugin.yaml.plugin.loader;
 
-import org.apache.shardingsphere.agent.core.plugin.yaml.path.AgentPathBuilder;
 import org.junit.Test;
-import org.mockito.internal.configuration.plugins.Plugins;
 
 import java.io.File;
 import java.io.IOException;
@@ -34,9 +32,8 @@ public final class PluginConfigurationLoaderTest {
     private static final String DEFAULT_CONFIG_PATH = "/conf/agent.yaml";
     
     @Test
-    public void assertLoad() throws IOException, ReflectiveOperationException {
-        
Plugins.getMemberAccessor().set(AgentPathBuilder.class.getDeclaredField("agentPath"),
 AgentPathBuilder.class, new File(getResourceUrl()));
-        assertNotNull(PluginConfigurationLoader.load());
+    public void assertLoad() throws IOException {
+        assertNotNull(PluginConfigurationLoader.load(new 
File(getResourceUrl())));
     }
     
     private String getResourceUrl() throws UnsupportedEncodingException {

Reply via email to