Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Jul 1, 2024
2 parents 45d1fd5 + 3875251 commit 02ebe1d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package run.halo.app.plugin.event;

import org.springframework.context.ApplicationEvent;

/**
* The event that is published when a plugin is really started, and is only for plugin internal use.
*
* @author johnniang
* @since 2.17.0
*/
public class PluginStartedEvent extends ApplicationEvent {

public PluginStartedEvent(Object source) {
super(source);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
import org.pf4j.PluginFactory;
import org.pf4j.PluginLoader;
import org.pf4j.PluginRepository;
import org.pf4j.PluginState;
import org.pf4j.PluginStateEvent;
import org.pf4j.PluginStateListener;
import org.pf4j.PluginStatusProvider;
import org.pf4j.PluginWrapper;
import org.springframework.context.ApplicationContext;
import org.springframework.data.util.Lazy;
import run.halo.app.infra.SystemVersionSupplier;
import run.halo.app.plugin.event.PluginStartedEvent;

/**
* PluginManager to hold the main ApplicationContext.
Expand Down Expand Up @@ -56,6 +60,9 @@ public HaloPluginManager(ApplicationContext rootContext,
setSystemVersion(systemVersionSupplier.get().getNormalVersion());

super.initialize();

// the listener must be after the super#initialize
addPluginStateListener(new PluginStartedListener());
}

@Override
Expand Down Expand Up @@ -175,4 +182,30 @@ public List<PluginWrapper> getDependents(String pluginId) {
}
return dependents;
}

/**
* Listener for plugin started event.
*
* @author johnniang
* @since 2.17.0
*/
private static class PluginStartedListener implements PluginStateListener {

@Override
public void pluginStateChanged(PluginStateEvent event) {
if (PluginState.STARTED.equals(event.getPluginState())) {
var plugin = event.getPlugin().getPlugin();
if (plugin instanceof SpringPlugin springPlugin) {
try {
springPlugin.getApplicationContext()
.publishEvent(new PluginStartedEvent(this));
} catch (Throwable t) {
var pluginId = event.getPlugin().getPluginId();
log.warn("Error while publishing plugin started event for plugin {}",
pluginId, t);
}
}
}
}
}
}

This file was deleted.

0 comments on commit 02ebe1d

Please sign in to comment.