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

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


The following commit(s) were added to refs/heads/master by this push:
     new 183493156 [ISSUE #4243] to avoid load the same ext plugins repeatedly 
(#4260)
183493156 is described below

commit 183493156f8d270baa6794dcc6efe904a1b92265
Author: RayayChung <[email protected]>
AuthorDate: Tue Dec 13 15:34:28 2022 +0800

    [ISSUE #4243] to avoid load the same ext plugins repeatedly (#4260)
    
    * [ISSUE #4243] to avoid load the same ext plugins repeatedly
    
    * [ISSUE #4243] add unit test
    
    * [ISSUE #4243] fix import .* issue
    
    Co-authored-by: ray <[email protected]>
    Co-authored-by: dragon-zhang <[email protected]>
    Co-authored-by: xiaoyu <[email protected]>
---
 .../org/apache/shenyu/web/loader/ShenyuPluginLoader.java   | 14 ++++++++++++++
 .../apache/shenyu/web/loader/ShenyuPluginLoaderTest.java   | 13 +++++++++++++
 2 files changed, 27 insertions(+)

diff --git 
a/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java 
b/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java
index b216b15a0..676687081 100644
--- 
a/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java
+++ 
b/shenyu-web/src/main/java/org/apache/shenyu/web/loader/ShenyuPluginLoader.java
@@ -28,6 +28,7 @@ import 
org.springframework.beans.factory.config.BeanDefinition;
 import org.springframework.beans.factory.support.GenericBeanDefinition;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
 
 import java.io.Closeable;
 import java.io.File;
@@ -106,7 +107,12 @@ public final class ShenyuPluginLoader extends ClassLoader 
implements Closeable {
             return Collections.emptyList();
         }
         List<ShenyuLoaderResult> results = new ArrayList<>();
+        boolean loadNewPlugin = false;
         for (File each : jarFiles) {
+            if 
(jars.stream().map(PluginJar::absolutePath).filter(StringUtils::hasText).anyMatch(p
 -> p.equals(each.getAbsolutePath()))) {
+                continue;
+            }
+            loadNewPlugin = true;
             JarFile jar = new JarFile(each, true);
             jars.add(new PluginJar(jar, each));
             Enumeration<JarEntry> entries = jar.entries();
@@ -120,6 +126,10 @@ public final class ShenyuPluginLoader extends ClassLoader 
implements Closeable {
             }
         }
 
+        if (!loadNewPlugin) {
+            return results;
+        }
+
         names.forEach(className -> {
             Object instance;
             try {
@@ -298,5 +308,9 @@ public final class ShenyuPluginLoader extends ClassLoader 
implements Closeable {
             this.jarFile = jarFile;
             this.sourcePath = sourcePath;
         }
+
+        public String absolutePath() {
+            return sourcePath.getAbsolutePath();
+        }
     }
 }
diff --git 
a/shenyu-web/src/test/java/org/apache/shenyu/web/loader/ShenyuPluginLoaderTest.java
 
b/shenyu-web/src/test/java/org/apache/shenyu/web/loader/ShenyuPluginLoaderTest.java
index 62b9ceaf3..ca5e3e3d6 100644
--- 
a/shenyu-web/src/test/java/org/apache/shenyu/web/loader/ShenyuPluginLoaderTest.java
+++ 
b/shenyu-web/src/test/java/org/apache/shenyu/web/loader/ShenyuPluginLoaderTest.java
@@ -144,6 +144,19 @@ public final class ShenyuPluginLoaderTest {
         definePackageInternal.invoke(ShenyuPluginLoader.getInstance(), 
"org.apache.shenyu.plugin.DividePlugin", manifest);
     }
 
+    @Test
+    public void testLoadSameJarTwice() throws IOException, 
NoSuchFieldException, IllegalAccessException {
+        ShenyuPluginLoader loader = ShenyuPluginLoader.getInstance();
+        Field field = ShenyuPluginLoader.class.getDeclaredField("jars");
+        field.setAccessible(true);
+        field.set(loader, Lists.newArrayList());
+        List<ShenyuLoaderResult> pluginList = 
loader.loadExtendPlugins(path.toString());
+        assertThat(pluginList.size(), is(1));
+        List<ShenyuLoaderResult> neoPluginList = 
loader.loadExtendPlugins(path.toString());
+        assertThat(neoPluginList.size(), is(0));
+        loader.close();
+    }
+
     @Component
     public static class TestComponent {
     }

Reply via email to