mirror of https://github.com/halo-dev/halo
[release-2.17] Fix the problem that plugins without jar file may not be deleted (#6374)
This is an automated cherry-pick of #6334 /assign JohnNiang ```release-note 修复在没有插件文件的情况下可能无法删除插件的问题 ```pull/6410/head
parent
19e45e0d3c
commit
8ff1bc497e
|
@ -25,6 +25,7 @@ import org.pf4j.PluginStateEvent;
|
||||||
import org.pf4j.PluginStateListener;
|
import org.pf4j.PluginStateListener;
|
||||||
import org.pf4j.PluginStatusProvider;
|
import org.pf4j.PluginStatusProvider;
|
||||||
import org.pf4j.PluginWrapper;
|
import org.pf4j.PluginWrapper;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.data.util.Lazy;
|
import org.springframework.data.util.Lazy;
|
||||||
import run.halo.app.infra.SystemVersionSupplier;
|
import run.halo.app.infra.SystemVersionSupplier;
|
||||||
|
@ -39,30 +40,23 @@ import run.halo.app.plugin.event.PluginStartedEvent;
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class HaloPluginManager extends DefaultPluginManager implements SpringPluginManager {
|
public class HaloPluginManager extends DefaultPluginManager
|
||||||
|
implements SpringPluginManager, InitializingBean {
|
||||||
|
|
||||||
private final ApplicationContext rootContext;
|
private final ApplicationContext rootContext;
|
||||||
|
|
||||||
private final Lazy<ApplicationContext> sharedContext;
|
private Lazy<ApplicationContext> sharedContext;
|
||||||
|
|
||||||
private final PluginProperties pluginProperties;
|
private final PluginProperties pluginProperties;
|
||||||
|
|
||||||
|
private final SystemVersionSupplier systemVersionSupplier;
|
||||||
|
|
||||||
public HaloPluginManager(ApplicationContext rootContext,
|
public HaloPluginManager(ApplicationContext rootContext,
|
||||||
PluginProperties pluginProperties,
|
PluginProperties pluginProperties,
|
||||||
SystemVersionSupplier systemVersionSupplier) {
|
SystemVersionSupplier systemVersionSupplier) {
|
||||||
this.pluginProperties = pluginProperties;
|
this.pluginProperties = pluginProperties;
|
||||||
this.rootContext = rootContext;
|
this.rootContext = rootContext;
|
||||||
// We have to initialize share context lazily because the root context has not refreshed
|
this.systemVersionSupplier = systemVersionSupplier;
|
||||||
this.sharedContext = Lazy.of(() -> SharedApplicationContextFactory.create(rootContext));
|
|
||||||
super.runtimeMode = pluginProperties.getRuntimeMode();
|
|
||||||
|
|
||||||
setExactVersionAllowed(pluginProperties.isExactVersionAllowed());
|
|
||||||
setSystemVersion(systemVersionSupplier.get().getNormalVersion());
|
|
||||||
|
|
||||||
super.initialize();
|
|
||||||
|
|
||||||
// the listener must be after the super#initialize
|
|
||||||
addPluginStateListener(new PluginStartedListener());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -71,6 +65,18 @@ public class HaloPluginManager extends DefaultPluginManager implements SpringPlu
|
||||||
// components before properties set.
|
// components before properties set.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
super.runtimeMode = pluginProperties.getRuntimeMode();
|
||||||
|
this.sharedContext = Lazy.of(() -> SharedApplicationContextFactory.create(rootContext));
|
||||||
|
setExactVersionAllowed(pluginProperties.isExactVersionAllowed());
|
||||||
|
setSystemVersion(systemVersionSupplier.get().toStableVersion().toString());
|
||||||
|
|
||||||
|
super.initialize();
|
||||||
|
// the listener must be after the super#initialize
|
||||||
|
addPluginStateListener(new PluginStartedListener());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ExtensionFactory createExtensionFactory() {
|
protected ExtensionFactory createExtensionFactory() {
|
||||||
return new SpringExtensionFactory(this);
|
return new SpringExtensionFactory(this);
|
||||||
|
@ -169,6 +175,10 @@ public class HaloPluginManager extends DefaultPluginManager implements SpringPlu
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PluginWrapper> getDependents(String pluginId) {
|
public List<PluginWrapper> getDependents(String pluginId) {
|
||||||
|
if (getPlugin(pluginId) == null) {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
var dependents = new ArrayList<PluginWrapper>();
|
var dependents = new ArrayList<PluginWrapper>();
|
||||||
var stack = new Stack<String>();
|
var stack = new Stack<String>();
|
||||||
dependencyResolver.getDependents(pluginId).forEach(stack::push);
|
dependencyResolver.getDependents(pluginId).forEach(stack::push);
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
package run.halo.app.plugin;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import com.github.zafarkhaja.semver.Version;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.pf4j.RuntimeMode;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import run.halo.app.infra.SystemVersionSupplier;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class HaloPluginManagerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
PluginProperties pluginProperties;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
SystemVersionSupplier systemVersionSupplier;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
ApplicationContext rootContext;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
HaloPluginManager pluginManager;
|
||||||
|
|
||||||
|
@TempDir
|
||||||
|
Path tempDir;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldGetDependentsWhilePluginsNotResolved() throws Exception {
|
||||||
|
when(pluginProperties.getRuntimeMode()).thenReturn(RuntimeMode.DEPLOYMENT);
|
||||||
|
when(systemVersionSupplier.get()).thenReturn(Version.of(1, 2, 3));
|
||||||
|
pluginManager.afterPropertiesSet();
|
||||||
|
// if we don't invoke resolves
|
||||||
|
var dependents = pluginManager.getDependents("fake-plugin");
|
||||||
|
assertTrue(dependents.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldGetDependentsWhilePluginsResolved() throws Exception {
|
||||||
|
when(pluginProperties.getRuntimeMode()).thenReturn(RuntimeMode.DEPLOYMENT);
|
||||||
|
when(systemVersionSupplier.get()).thenReturn(Version.of(1, 2, 3));
|
||||||
|
pluginManager.afterPropertiesSet();
|
||||||
|
pluginManager.loadPlugins();
|
||||||
|
// if we don't invoke resolves
|
||||||
|
var dependents = pluginManager.getDependents("fake-plugin");
|
||||||
|
assertTrue(dependents.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue