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 00000000000..1b15d61be84 --- /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 40d4df246f7..0bfad7a74ae 100644 --- a/application/src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java +++ b/application/src/main/java/run/halo/app/plugin/PluginAutoConfiguration.java @@ -9,7 +9,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; @@ -114,8 +113,8 @@ protected PluginLoader createPluginLoader() { @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 @@ -141,8 +140,8 @@ public ClassLoader loadPlugin(Path pluginPath, 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;