Skip to content

Commit

Permalink
Support shared event mechanism
Browse files Browse the repository at this point in the history
Signed-off-by: John Niang <[email protected]>
  • Loading branch information
JohnNiang committed Jan 26, 2024
1 parent 93c8c8e commit 55d7b54
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
Expand All @@ -28,7 +29,6 @@
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.Exceptions;
import run.halo.app.PluginApplicationContextFactory;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.infra.properties.HaloProperties;
import run.halo.app.plugin.event.HaloPluginBeforeStopEvent;
Expand Down Expand Up @@ -133,6 +133,15 @@ public ApplicationContext create(String pluginId) {
pluginRouterFunctionManager
);
});

rootContext.getBeanProvider(SharedEventListenerRegistry.class)
.ifUnique(listenerRegistry -> {
var shareEventListenerAdapter = new ShareEventListenerAdapter(listenerRegistry);
beanFactory.registerSingleton(
"shareEventListenerAdapter",
shareEventListenerAdapter
);
});
sw.stop();

sw.start("LoadComponents");
Expand Down Expand Up @@ -162,6 +171,31 @@ public ApplicationContext create(String pluginId) {
return context;
}

private static class ShareEventListenerAdapter {

private final SharedEventListenerRegistry listenerRegistry;

private ApplicationListener<ApplicationEvent> listener;

private ShareEventListenerAdapter(SharedEventListenerRegistry listenerRegistry) {
this.listenerRegistry = listenerRegistry;
}

@EventListener
public void onApplicationEvent(ContextRefreshedEvent event) {
this.listener = sharedEvent -> event.getApplicationContext().publishEvent(sharedEvent);
listenerRegistry.register(this.listener);
}

@EventListener(ContextClosedEvent.class)
public void onApplicationEvent() {
if (this.listener != null) {
this.listenerRegistry.unregister(this.listener);
}
}

}

private static class FinderManager {

private final String pluginId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package run.halo.app.plugin;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class DefaultSharedEventListenerRegistry implements
ApplicationListener<ApplicationEvent>, SharedEventListenerRegistry {

private final List<ApplicationListener<ApplicationEvent>> listeners;

public DefaultSharedEventListenerRegistry() {
listeners = new CopyOnWriteArrayList<>();
}

@Override
public void onApplicationEvent(ApplicationEvent event) {
if (!event.getClass().isAnnotationPresent(SharedEvent.class)) {
return;
}
listeners.forEach(listener -> listener.onApplicationEvent(event));
}

public void register(ApplicationListener<ApplicationEvent> listener) {
this.listeners.add(listener);
}

public void unregister(ApplicationListener<ApplicationEvent> listener) {
this.listeners.remove(listener);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package run.halo.app;
package run.halo.app.plugin;

import org.springframework.context.ApplicationContext;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package run.halo.app.plugin;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

public interface SharedEventListenerRegistry {

void register(ApplicationListener<ApplicationEvent> listener);

void unregister(ApplicationListener<ApplicationEvent> listener);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.pf4j.Plugin;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import run.halo.app.PluginApplicationContextFactory;
import run.halo.app.plugin.event.SpringPluginStartedEvent;
import run.halo.app.plugin.event.SpringPluginStartingEvent;
import run.halo.app.plugin.event.SpringPluginStoppingEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.pf4j.Plugin;
import org.pf4j.PluginFactory;
import org.pf4j.PluginWrapper;
import run.halo.app.PluginApplicationContextFactory;

/**
* The default implementation for PluginFactory.
Expand Down

0 comments on commit 55d7b54

Please sign in to comment.