Skip to content

Commit

Permalink
refactor: custom endpoints to rotuer function register for plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Nov 23, 2023
1 parent 8f21e83 commit 15bbb0f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 43 deletions.
@@ -0,0 +1,50 @@
package run.halo.app.plugin;

import lombok.experimental.UtilityClass;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.lang.Nullable;

/**
* Utility class that allows for convenient registration of common bean post-processors for
* {@link PluginApplicationContext}.
*
* @author guqing
* @since 2.11.0
*/
@UtilityClass
public class BeanPostProcessorUtils {

public static final String CUSTOM_ENDPOINT_PROCESSOR_BEAN_NAME =
"run.halo.app.plugin.pluginCustomEndpointBeanFactoryPostProcessor";

/**
* Register all relevant annotation post-processors in the given registry.
*
* @param registry the registry to operate on
*/
public static void registerBeanPostProcessors(BeanDefinitionRegistry registry) {
registerBeanPostProcessors(registry, null);
}

/**
* Register all relevant annotation post-processors in the given registry.
*
* @param registry the registry to operate on
* @param source the configuration source element (already extracted)
* that this registration was triggered from. May be {@code null}
*/
public static void registerBeanPostProcessors(
BeanDefinitionRegistry registry, @Nullable Object source) {

if (!registry.containsBeanDefinition(CUSTOM_ENDPOINT_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(
PluginCustomEndpointBeanFactoryPostProcessor.class);
def.setSource(source);
def.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(CUSTOM_ENDPOINT_PROCESSOR_BEAN_NAME, def);
}
// add more post processors
}
}
Expand Up @@ -36,6 +36,8 @@
*/
@Slf4j
public class PluginApplicationInitializer {


protected final HaloPluginManager haloPluginManager;

private final ExtensionContextRegistry contextRegistry = ExtensionContextRegistry.getInstance();
Expand Down Expand Up @@ -89,6 +91,8 @@ private PluginApplicationContext createPluginApplicationContext(String pluginId)
AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);
stopWatch.stop();

BeanPostProcessorUtils.registerBeanPostProcessors(beanFactory);

beanFactory.registerSingleton("pluginContext", createPluginContext(plugin));
// TODO deprecated
beanFactory.registerSingleton("pluginWrapper", haloPluginManager.getPlugin(pluginId));
Expand Down
@@ -0,0 +1,34 @@
package run.halo.app.plugin;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.lang.NonNull;
import org.springframework.web.reactive.function.server.RouterFunction;
import run.halo.app.core.extension.endpoint.CustomEndpoint;
import run.halo.app.core.extension.endpoint.CustomEndpointsBuilder;

/**
* <p>A {@link CustomEndpoint} initialization {@link BeanFactoryPostProcessor} to build
* {@link RouterFunction}s to bean factory.</p>
*
* @author guqing
* @since 2.11.0
*/
public class PluginCustomEndpointBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

@Override
public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory)
throws BeansException {
String[] customEndpointBeans = beanFactory.getBeanNamesForType(CustomEndpoint.class);
if (customEndpointBeans.length == 0) {
return;
}
CustomEndpointsBuilder endpointBuilder = new CustomEndpointsBuilder();
for (String beanName : customEndpointBeans) {
CustomEndpoint customEndpoint = (CustomEndpoint) beanFactory.getBean(beanName);
endpointBuilder.add(customEndpoint);
}
beanFactory.registerSingleton("pluginCustomEndpointRouter", endpointBuilder.build());
}
}

This file was deleted.

0 comments on commit 15bbb0f

Please sign in to comment.