From f7ccb65f2ebae101439093fe0c9babe3d11f8281 Mon Sep 17 00:00:00 2001 From: guqing Date: Wed, 27 Sep 2023 10:39:43 +0800 Subject: [PATCH 1/2] refactor: plugin resource loading to only load from plugin itself instead of delegating to core --- .../app/plugin/DefaultPluginClassLoader.java | 40 +++++++++++++++++++ .../app/plugin/PluginAutoConfiguration.java | 9 ++--- 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 application/src/main/java/run/halo/app/plugin/DefaultPluginClassLoader.java diff --git a/application/src/main/java/run/halo/app/plugin/DefaultPluginClassLoader.java b/application/src/main/java/run/halo/app/plugin/DefaultPluginClassLoader.java new file mode 100644 index 0000000000..1b15d61be8 --- /dev/null +++ b/application/src/main/java/run/halo/app/plugin/DefaultPluginClassLoader.java @@ -0,0 +1,40 @@ +package run.halo.app.plugin; + +import java.io.IOException; +import java.net.URL; +import java.util.Enumeration; +import lombok.extern.slf4j.Slf4j; +import org.pf4j.ClassLoadingStrategy; +import org.pf4j.PluginClassLoader; +import org.pf4j.PluginDescriptor; +import org.pf4j.PluginManager; + +/** + *

Plugin manager should create one instance of this class for every available plugin. By + * default, this class loader is a Parent First ClassLoader - it loads the classes from the + * parent class loader before loads from the plugin. Use classLoadingStrategy to change + * the loading strategy.

+ *

By default, this class loader loads the resources from the plugin only - if the resource not + * found in the plugin, it returns null.

+ * + * @author guqing + * @since 2.10.0 + */ +@Slf4j +public class DefaultPluginClassLoader extends PluginClassLoader { + public DefaultPluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, + ClassLoader parent) { + super(pluginManager, pluginDescriptor, parent, ClassLoadingStrategy.APD); + } + + @Override + public URL getResource(String name) { + log.trace("Received request to load resource '{}'", name); + return this.findResource(name); + } + + @Override + public Enumeration getResources(String name) throws IOException { + return this.findResources(name); + } +} diff --git a/application/src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java b/application/src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java index bd74031438..e88f4d8dbe 100644 --- a/application/src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java +++ b/application/src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java @@ -10,7 +10,6 @@ import java.time.Instant; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.pf4j.ClassLoadingStrategy; import org.pf4j.CompoundPluginLoader; import org.pf4j.CompoundPluginRepository; import org.pf4j.DefaultPluginRepository; @@ -116,8 +115,8 @@ protected PluginLoader createPluginLoader() { public ClassLoader loadPlugin(Path pluginPath, PluginDescriptor pluginDescriptor) { PluginClassLoader pluginClassLoader = - new PluginClassLoader(pluginManager, pluginDescriptor, - getClass().getClassLoader(), ClassLoadingStrategy.APD); + new DefaultPluginClassLoader(pluginManager, + pluginDescriptor, getClass().getClassLoader()); pluginClassLoader.addFile(pluginPath.toFile()); return pluginClassLoader; } @@ -159,8 +158,8 @@ DevelopmentPluginLoader createDevelopmentPluginLoader(PluginManager pluginManage @Override protected PluginClassLoader createPluginClassLoader(Path pluginPath, PluginDescriptor pluginDescriptor) { - return new PluginClassLoader(pluginManager, pluginDescriptor, - getClass().getClassLoader(), ClassLoadingStrategy.APD); + return new DefaultPluginClassLoader(pluginManager, + pluginDescriptor, getClass().getClassLoader()); } @Override From a815db0e67fcc60f961e486d141ab98ac0ab54be Mon Sep 17 00:00:00 2001 From: guqing Date: Wed, 27 Sep 2023 17:50:45 +0800 Subject: [PATCH 2/2] refactor: plugin classloader config --- .../app/plugin/DefaultPluginClassLoader.java | 40 ------------------- .../app/plugin/PluginAutoConfiguration.java | 19 +-------- 2 files changed, 1 insertion(+), 58 deletions(-) delete mode 100644 application/src/main/java/run/halo/app/plugin/DefaultPluginClassLoader.java diff --git a/application/src/main/java/run/halo/app/plugin/DefaultPluginClassLoader.java b/application/src/main/java/run/halo/app/plugin/DefaultPluginClassLoader.java deleted file mode 100644 index 1b15d61be8..0000000000 --- a/application/src/main/java/run/halo/app/plugin/DefaultPluginClassLoader.java +++ /dev/null @@ -1,40 +0,0 @@ -package run.halo.app.plugin; - -import java.io.IOException; -import java.net.URL; -import java.util.Enumeration; -import lombok.extern.slf4j.Slf4j; -import org.pf4j.ClassLoadingStrategy; -import org.pf4j.PluginClassLoader; -import org.pf4j.PluginDescriptor; -import org.pf4j.PluginManager; - -/** - *

Plugin manager should create one instance of this class for every available plugin. By - * default, this class loader is a Parent First ClassLoader - it loads the classes from the - * parent class loader before loads from the plugin. Use classLoadingStrategy to change - * the loading strategy.

- *

By default, this class loader loads the resources from the plugin only - if the resource not - * found in the plugin, it returns null.

- * - * @author guqing - * @since 2.10.0 - */ -@Slf4j -public class DefaultPluginClassLoader extends PluginClassLoader { - public DefaultPluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor, - ClassLoader parent) { - super(pluginManager, pluginDescriptor, parent, ClassLoadingStrategy.APD); - } - - @Override - public URL getResource(String name) { - log.trace("Received request to load resource '{}'", name); - return this.findResource(name); - } - - @Override - public Enumeration getResources(String name) throws IOException { - return this.findResources(name); - } -} diff --git a/application/src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java b/application/src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java index e88f4d8dbe..6bf8c85a5e 100644 --- a/application/src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java +++ b/application/src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java @@ -16,7 +16,6 @@ import org.pf4j.DevelopmentPluginLoader; import org.pf4j.JarPluginLoader; import org.pf4j.JarPluginRepository; -import org.pf4j.PluginClassLoader; import org.pf4j.PluginDescriptor; import org.pf4j.PluginLoader; import org.pf4j.PluginManager; @@ -110,17 +109,7 @@ protected PluginLoader createPluginLoader() { } else { return new CompoundPluginLoader() .add(createDevelopmentPluginLoader(this), this::isDevelopment) - .add(new JarPluginLoader(this) { - @Override - public ClassLoader loadPlugin(Path pluginPath, - PluginDescriptor pluginDescriptor) { - PluginClassLoader pluginClassLoader = - new DefaultPluginClassLoader(pluginManager, - pluginDescriptor, getClass().getClassLoader()); - pluginClassLoader.addFile(pluginPath.toFile()); - return pluginClassLoader; - } - }); + .add(new JarPluginLoader(this)); } } @@ -155,12 +144,6 @@ protected PluginRepository createPluginRepository() { DevelopmentPluginLoader createDevelopmentPluginLoader(PluginManager pluginManager) { return new DevelopmentPluginLoader(pluginManager) { - @Override - protected PluginClassLoader createPluginClassLoader(Path pluginPath, - PluginDescriptor pluginDescriptor) { - return new DefaultPluginClassLoader(pluginManager, - pluginDescriptor, getClass().getClassLoader()); - } @Override public ClassLoader loadPlugin(Path pluginPath,