Revert "refactor: modify plugin class loading order to follow parent delegation mechanism (#7258)" (#7290)

#### What type of PR is this?
/kind cleanup

#### What this PR does / why we need it:
撤回对插件类加载顺序的改动这可能导致破坏性更新

同时,不在考虑修改加载顺序问题,由于社区版和专业版引入的依赖不同插件无法以社区版为依赖基准保证功能在专业版也可用,举个例子:
1. 插件引入了 okhttp4 作为依赖,这可能是插件引入的依赖所附带的
2. 在社区版没有问题,插件开发者也是这么测试的
3. 但是在专业版中引入了 okhttp3 作为依赖,此时插件在专业版就不可用了因为插件依赖了 okhttp4 的功能

通过上述问题就导致了不可预知的问题

#### Does this PR introduce a user-facing change?

```release-note
撤回对插件类加载顺序的改动这可能导致破坏性更新
```
pull/7291/head
guqing 2025-03-13 12:33:07 +08:00 committed by GitHub
parent 30c97d8ea6
commit 5743cee598
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 3 additions and 101 deletions

View File

@ -1,12 +1,10 @@
package run.halo.app.plugin.loader;
package run.halo.app.plugin;
import java.nio.file.Files;
import java.nio.file.Path;
import org.pf4j.DevelopmentPluginLoader;
import org.pf4j.PluginClassLoader;
import org.pf4j.PluginDescriptor;
import org.pf4j.PluginManager;
import run.halo.app.plugin.PluginProperties;
public class DevPluginLoader extends DevelopmentPluginLoader {
@ -42,11 +40,4 @@ public class DevPluginLoader extends DevelopmentPluginLoader {
// Currently we only support a plugin loading from directory in dev mode.
return Files.isDirectory(pluginPath);
}
@Override
protected PluginClassLoader createPluginClassLoader(Path pluginPath,
PluginDescriptor pluginDescriptor) {
return new HaloPluginClassLoader(this.pluginManager, pluginDescriptor,
this.getClass().getClassLoader());
}
}

View File

@ -13,7 +13,6 @@ import org.pf4j.ExtensionFactory;
import org.pf4j.ExtensionFinder;
import org.pf4j.JarPluginLoader;
import org.pf4j.JarPluginRepository;
import org.pf4j.PluginDescriptor;
import org.pf4j.PluginDescriptorFinder;
import org.pf4j.PluginFactory;
import org.pf4j.PluginLoader;
@ -28,8 +27,6 @@ import org.springframework.context.ApplicationContext;
import org.springframework.data.util.Lazy;
import run.halo.app.infra.SystemVersionSupplier;
import run.halo.app.plugin.event.PluginStartedEvent;
import run.halo.app.plugin.loader.DevPluginLoader;
import run.halo.app.plugin.loader.HaloPluginClassLoader;
/**
* PluginManager to hold the main ApplicationContext.
@ -109,15 +106,7 @@ public class HaloPluginManager extends DefaultPluginManager
protected PluginLoader createPluginLoader() {
var compoundLoader = new CompoundPluginLoader();
compoundLoader.add(new DevPluginLoader(this, this.pluginProperties), this::isDevelopment);
compoundLoader.add(new JarPluginLoader(this) {
@Override
public ClassLoader loadPlugin(Path pluginPath, PluginDescriptor pluginDescriptor) {
var pluginClassLoader = new HaloPluginClassLoader(this.pluginManager,
pluginDescriptor, this.getClass().getClassLoader());
pluginClassLoader.addFile(pluginPath.toFile());
return pluginClassLoader;
}
});
compoundLoader.add(new JarPluginLoader(this));
return compoundLoader;
}

View File

@ -51,7 +51,7 @@ public class SpringPlugin extends Plugin {
// try to stop plugin for cleaning resources if something went wrong
log.error(
"Cleaning up plugin resources for plugin {} due to not being able to start plugin.",
pluginId, t);
pluginId);
this.stop();
// propagate exception to invoker.
throw t;

View File

@ -1,78 +0,0 @@
package run.halo.app.plugin.loader;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.pf4j.ClassLoadingStrategy;
import org.pf4j.PluginClassLoader;
import org.pf4j.PluginDescriptor;
import org.pf4j.PluginManager;
@Slf4j
public class HaloPluginClassLoader extends PluginClassLoader {
/**
* see also <a href="https://github.com/halo-dev/halo/issues/4610">gh-4610</a>.
*/
private final ClassLoadingStrategy resourceLoadingStrategy = ClassLoadingStrategy.PDA;
public HaloPluginClassLoader(PluginManager pluginManager, PluginDescriptor pluginDescriptor,
ClassLoader parent) {
super(pluginManager, pluginDescriptor, parent, ClassLoadingStrategy.APD);
}
@Override
public URL getResource(String name) {
for (ClassLoadingStrategy.Source classLoadingSource :
resourceLoadingStrategy.getSources()) {
URL url = switch (classLoadingSource) {
case APPLICATION -> super.getResource(name);
case PLUGIN -> this.findResource(name);
case DEPENDENCIES -> this.findResourceFromDependencies(name);
};
if (url != null) {
log.trace("Found resource '{}' in {} classpath", name,
classLoadingSource);
return url;
}
log.trace("Couldn't find resource '{}' in {}", name,
classLoadingSource);
}
return null;
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
List<URL> resources = new ArrayList<>();
log.trace("Received request to load resources '{}'", name);
for (ClassLoadingStrategy.Source classLoadingSource :
resourceLoadingStrategy.getSources()) {
switch (classLoadingSource) {
case APPLICATION:
if (this.getParent() != null) {
resources.addAll(
Collections.list(this.getParent().getResources(name)));
}
break;
case PLUGIN:
resources.addAll(Collections.list(this.findResources(name)));
break;
case DEPENDENCIES:
resources.addAll(this.findResourcesFromDependencies(name));
break;
default:
// Do nothing
}
}
return Collections.enumeration(resources);
}
}