Skip to content

Commit

Permalink
Adds new method doDynamicModules() to GrailsPluginManager
Browse files Browse the repository at this point in the history
Enhance `GrailsDynamicPluginRegistryPostProcessor`
Adds new tests
  • Loading branch information
rainboyan committed Aug 20, 2024
1 parent ba8471a commit 9016d58
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2022 the original author or authors.
* Copyright 2004-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -118,9 +118,15 @@ public interface GrailsPluginManager extends ApplicationContextAware, Applicatio

/**
* Called on all plugins so that they can add new methods/properties/constructors etc.
* @since 2023.0
*/
void doDynamicMethods();

/**
* Called on all plugins so that they can register new modules.
*/
void doDynamicModules();

/**
* Executes the {@link Plugin#onStartup(Map)} hook for all plugins
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2023 the original author or authors.
* Copyright 2004-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,6 +78,7 @@
* Abstract implementation of the GrailsPluginManager interface
*
* @author Graeme Rocher
* @author Michael Yan
* @since 0.4
*/
public abstract class AbstractGrailsPluginManager implements GrailsPluginManager {
Expand Down Expand Up @@ -324,6 +325,21 @@ public void doDynamicMethods() {
}
}

public void doDynamicModules() {
checkInitialised();

ApplicationContext ctx = this.applicationContext;
for (GrailsPlugin plugin : this.loadedPlugins) {
if (!plugin.isEnabled(ctx.getEnvironment().getActiveProfiles())) {
continue;
}
if (plugin instanceof DynamicGrailsPlugin) {
DynamicGrailsPlugin dynamicPlugin = (DynamicGrailsPlugin) plugin;
dynamicPlugin.doWithDynamicModules();
}
}
}

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
this.moduleDescriptorFactory.setApplicationContext(applicationContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 the original author or authors.
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,10 +25,14 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;

import grails.plugins.DynamicGrailsPlugin;
import grails.plugins.GrailsPlugin;
import grails.plugins.GrailsPluginManager;

/**
* Use {@link BeanDefinitionRegistryPostProcessor} to load dynamic modules in all Plugins.
*
* @author Michael Yan
* @since 2022.0.0
*/
public class GrailsDynamicPluginRegistryPostProcessor
implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware, Ordered {

Expand All @@ -48,12 +52,7 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
}

GrailsPluginManager pluginManager = beanFactory.getBean(GrailsPluginManager.BEAN_NAME, GrailsPluginManager.class);
for (GrailsPlugin plugin : pluginManager.getAllPlugins()) {
if (plugin instanceof DynamicGrailsPlugin) {
DynamicGrailsPlugin dynamicPlugin = (DynamicGrailsPlugin) plugin;
dynamicPlugin.doWithDynamicModules();
}
}
pluginManager.doDynamicModules();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import grails.core.GrailsApplication;

import org.grails.plugins.IncludingPluginFilter;
import org.grails.plugins.MenuModuleDescriptor;
import org.grails.plugins.MockGrailsApplication;
import org.grails.support.MockApplicationContext;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -112,4 +114,29 @@ public void testAddUserPlugin() {
assertEquals(1, manager.getUserPlugins().length);
}

@Test
public void testDynamicPlugins() {
GroovyClassLoader gcl = new GroovyClassLoader();

Class<?> menuPlugin = gcl.parseClass("class MenuGrailsPlugin extends grails.plugins.DynamicPlugin {\n" +
"def version = '1.0'\n" +
"def providedModules = [org.grails.plugins.MenuModuleDescriptor]\n" +
"Closure doWithDynamicModules() { {->\n" +
"menu(key: \"menu.about\", name: \"About Menu\", i18nNameKey: \"menu.about\")\n" +
"}}\n" +
"}");

MockApplicationContext context = new MockApplicationContext();
GrailsApplication app = new MockGrailsApplication(new Class[] {}, gcl);
DefaultGrailsPluginManager manager = new DefaultGrailsPluginManager(new Class[] { menuPlugin }, app);
manager.setApplicationContext(context);
manager.loadPlugins();
manager.registerProvidedModules();
manager.doDynamicModules();

List<MenuModuleDescriptor> menuModuleDescriptorList = manager.getEnabledModuleDescriptorsByClass(MenuModuleDescriptor.class);

assertEquals(1, menuModuleDescriptorList.size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.grails.plugins

import grails.plugins.GrailsPlugin
import grails.plugins.exceptions.PluginException
import grails.plugins.descriptors.AbstractModuleDescriptor

class MenuModuleDescriptor extends AbstractModuleDescriptor {

String i18n
String title
String link
String location
int order

MenuModuleDescriptor() {
super()
}

@Override
void init(GrailsPlugin plugin, Map args) throws PluginException {
super.init(plugin, args)
this.i18n = args.i18n
this.title = args.title
this.link = args.link
this.location = args.location
}

@Override
String toString() {
StringBuffer sb = new StringBuffer()
sb.append("MenuModuleDescriptor: [")
.append("\n key: ").append(key)
.append("\n name: ").append(name)
.append("\n description: ").append(description)
.append("\n link: ").append(link)
.append("\n title: ").append(title)
.append("\n location: ").append(location)
.append("\n order: ").append(order)
.append("\n params: ").append(params)
.append("]")
sb.toString()
}
}

0 comments on commit 9016d58

Please sign in to comment.