From c6502786275ee53fdacee33ac35fc4d08faa7026 Mon Sep 17 00:00:00 2001 From: Aleksandr Sidorov <117445585+etojesaandro@users.noreply.github.com> Date: Sat, 29 Jun 2024 22:30:03 +0300 Subject: [PATCH] Plugin Extensions should be detected with any ClassLoadingStrategy (#586) --- .../main/java/org/pf4j/PluginClassLoader.java | 15 ++++++++++++--- .../java/org/pf4j/PluginClassLoaderTest.java | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/pf4j/src/main/java/org/pf4j/PluginClassLoader.java b/pf4j/src/main/java/org/pf4j/PluginClassLoader.java index d11e7b9..cf0608c 100644 --- a/pf4j/src/main/java/org/pf4j/PluginClassLoader.java +++ b/pf4j/src/main/java/org/pf4j/PluginClassLoader.java @@ -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 getResources(String name) throws IOException { List 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. *

diff --git a/pf4j/src/test/java/org/pf4j/PluginClassLoaderTest.java b/pf4j/src/test/java/org/pf4j/PluginClassLoaderTest.java index e74166e..b19bd3b 100644 --- a/pf4j/src/test/java/org/pf4j/PluginClassLoaderTest.java +++ b/pf4j/src/test/java/org/pf4j/PluginClassLoaderTest.java @@ -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(); -- 2.39.5