Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: plugin resource loading to only load from plugin itself instead of delegating to core #4663

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p>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.</p>
* <p>By default, this class loader loads the resources from the plugin only - if the resource not
* found in the plugin, it returns null.</p>
*
* @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<URL> getResources(String name) throws IOException {
return this.findResources(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,8 +115,8 @@ protected PluginLoader createPluginLoader() {
public ClassLoader loadPlugin(Path pluginPath,
PluginDescriptor pluginDescriptor) {
PluginClassLoader pluginClassLoader =
new PluginClassLoader(pluginManager, pluginDescriptor,
guqing marked this conversation as resolved.
Show resolved Hide resolved
getClass().getClassLoader(), ClassLoadingStrategy.APD);
new DefaultPluginClassLoader(pluginManager,
pluginDescriptor, getClass().getClassLoader());
pluginClassLoader.addFile(pluginPath.toFile());
return pluginClassLoader;
}
Expand Down Expand Up @@ -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
Expand Down