Skip to content

Commit

Permalink
Allow plugin to listen the event the plugin has started (#6234)
Browse files Browse the repository at this point in the history
#### What type of PR is this?

/kind feature
/area core
/milestone 2.17.x

#### What this PR does / why we need it:

This PR add support for allowing plugin to listen the event that the plugin has started. Below is an example of listening the event in plugin:

```java
    @eventlistener
    void onPluginStartedEvent(PluginStartedEvent event) {
        // do something.
    }
```

See #5339 (comment) for more.

#### Which issue(s) this PR fixes:

Fixes #5339 (comment)

#### Special notes for your reviewer:

1. Create a plugin, add the listener above and write some logs
2. Build and install the plugin
3. Start plugin and see the logs you wrote

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

```release-note
支持在插件中监听已启动事件
```
  • Loading branch information
JohnNiang authored Jul 1, 2024
1 parent b673e4a commit 3875251
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 3875251

Please sign in to comment.