]> source.dussan.org Git - pf4j.git/commitdiff
Plugin Extensions should be detected with any ClassLoadingStrategy (#586)
authorAleksandr Sidorov <117445585+etojesaandro@users.noreply.github.com>
Sat, 29 Jun 2024 19:30:03 +0000 (22:30 +0300)
committerGitHub <noreply@github.com>
Sat, 29 Jun 2024 19:30:03 +0000 (22:30 +0300)
pf4j/src/main/java/org/pf4j/PluginClassLoader.java
pf4j/src/test/java/org/pf4j/PluginClassLoaderTest.java

index d11e7b945bbc4080c07a8b955b2cbf09836ccf41..cf0608c3d2cfe1db8b2d8b3b1be2f590a9229ac1 100644 (file)
@@ -181,8 +181,9 @@ public class PluginClassLoader extends URLClassLoader {
      */
     @Override
     public URL getResource(String name) {
+        ClassLoadingStrategy loadingStrategy = getClassLoadingStrategy(name);
         log.trace("Received request to load resource '{}'", name);
-        for (ClassLoadingStrategy.Source classLoadingSource : classLoadingStrategy.getSources()) {
+        for (ClassLoadingStrategy.Source classLoadingSource : loadingStrategy.getSources()) {
             URL url = null;
             switch (classLoadingSource) {
                 case APPLICATION:
@@ -210,9 +211,9 @@ public class PluginClassLoader extends URLClassLoader {
     @Override
     public Enumeration<URL> getResources(String name) throws IOException {
         List<URL> resources = new ArrayList<>();
-
+        ClassLoadingStrategy loadingStrategy = getClassLoadingStrategy(name);
         log.trace("Received request to load resources '{}'", name);
-        for (ClassLoadingStrategy.Source classLoadingSource : classLoadingStrategy.getSources()) {
+        for (ClassLoadingStrategy.Source classLoadingSource : loadingStrategy.getSources()) {
             switch (classLoadingSource) {
                 case APPLICATION:
                     if (getParent() != null) {
@@ -231,6 +232,14 @@ public class PluginClassLoader extends URLClassLoader {
         return Collections.enumeration(resources);
     }
 
+    private ClassLoadingStrategy getClassLoadingStrategy(String name) {
+        ClassLoadingStrategy loadingStrategy = classLoadingStrategy;
+        if (LegacyExtensionFinder.EXTENSIONS_RESOURCE.equals(name)) {
+            loadingStrategy = ClassLoadingStrategy.PAD;
+        }
+        return loadingStrategy;
+    }
+
     /**
      * Closes this class loader.
      * <p>
index e74166ebf5a36edfda47091c312c908d87beb45f..b19bd3bc14dd997504c85ffe3ff608774a2ff7a5 100644 (file)
@@ -20,6 +20,7 @@ import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
+import org.pf4j.processor.LegacyExtensionStorage;
 import org.pf4j.test.PluginZip;
 import org.pf4j.util.FileUtils;
 
@@ -74,6 +75,7 @@ class PluginClassLoaderTest {
         createFile(parentClassPathBase.resolve("META-INF").resolve("file-in-both-parent-and-dependency-and-plugin"));
         createFile(parentClassPathBase.resolve("META-INF").resolve("file-in-both-parent-and-dependency"));
         createFile(parentClassPathBase.resolve("META-INF").resolve("file-in-both-parent-and-plugin"));
+        createFile(parentClassPathBase.resolve(LegacyExtensionStorage.EXTENSIONS_RESOURCE));
     }
 
     private static void createFile(Path pathToFile) throws IOException {
@@ -106,6 +108,7 @@ class PluginClassLoaderTest {
                 .addFile(Paths.get("classes/META-INF/dependency-file"), "dependency")
                 .addFile(Paths.get("classes/META-INF/file-in-both-parent-and-dependency-and-plugin"), "dependency")
                 .addFile(Paths.get("classes/META-INF/file-in-both-parent-and-dependency"), "dependency")
+                .addFile(Paths.get("classes/" + LegacyExtensionStorage.EXTENSIONS_RESOURCE), "dependency")
                 .build();
 
         FileUtils.expandIfZip(pluginDependencyZip.path());
@@ -148,6 +151,7 @@ class PluginClassLoaderTest {
                 .addFile(Paths.get("classes/META-INF/plugin-file"), "plugin")
                 .addFile(Paths.get("classes/META-INF/file-in-both-parent-and-dependency-and-plugin"), "plugin")
                 .addFile(Paths.get("classes/META-INF/file-in-both-parent-and-plugin"), "plugin")
+                .addFile(Paths.get("classes/" + LegacyExtensionStorage.EXTENSIONS_RESOURCE), "plugin")
                 .build();
 
         FileUtils.expandIfZip(pluginZip.path());
@@ -325,6 +329,18 @@ class PluginClassLoaderTest {
         assertNumberOfResourcesAndFirstLineOfFirstElement(3, "parent", resources);
     }
 
+    @Test
+    void parentFirstGetExtensionsIndexExistsInParentAndDependencyAndPlugin() throws URISyntaxException, IOException {
+        URL resource = parentLastPluginClassLoader.getResource(LegacyExtensionFinder.EXTENSIONS_RESOURCE);
+        assertFirstLine("plugin", resource);
+    }
+
+    @Test
+    void parentLastGetExtensionsIndexExistsInParentAndDependencyAndPlugin() throws URISyntaxException, IOException {
+        URL resource = parentLastPluginClassLoader.getResource(LegacyExtensionFinder.EXTENSIONS_RESOURCE);
+        assertFirstLine("plugin", resource);
+    }
+
     @Test
     void isClosed() throws IOException {
         parentLastPluginClassLoader.close();