Skip to content

Commit

Permalink
fix: Plugin config loading in Micronaut beans (#13388)
Browse files Browse the repository at this point in the history
This commit changes the behavior of the GrailsApplicationPostProcessor
so that it no longer creates a new plugin manager if a plugin manager
already exists in the application context.

Resolves #11767
Related grails/grails-functional-tests#208
  • Loading branch information
matrei authored Feb 20, 2024
1 parent 7bc852a commit 82a9d0e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,10 @@ class GrailsApplicationPostProcessor implements BeanDefinitionRegistryPostProces

GrailsApplicationPostProcessor(GrailsApplicationLifeCycle lifeCycle, ApplicationContext applicationContext, Class...classes) {
this.lifeCycle = lifeCycle
if(lifeCycle instanceof GrailsApplicationClass) {
this.applicationClass = (GrailsApplicationClass)lifeCycle
}
else {
this.applicationClass = null
}
this.classes = classes != null ? classes : [] as Class[]
grailsApplication = applicationClass != null ? new DefaultGrailsApplication(applicationClass) : new DefaultGrailsApplication()
pluginManager = new DefaultGrailsPluginManager(grailsApplication)
this.applicationClass = lifeCycle instanceof GrailsApplicationClass ? (GrailsApplicationClass) lifeCycle : null
this.classes = classes ?: [] as Class[]
this.grailsApplication = applicationClass ? new DefaultGrailsApplication(applicationClass) : new DefaultGrailsApplication()
this.pluginManager = applicationContext?.getBeanNamesForType(GrailsPluginManager) ? applicationContext.getBean(GrailsPluginManager) : new DefaultGrailsPluginManager(grailsApplication)
if(applicationContext != null) {
setApplicationContext(applicationContext)
}
Expand Down Expand Up @@ -290,30 +285,22 @@ class GrailsApplicationPostProcessor implements BeanDefinitionRegistryPostProces
}
}

@SuppressWarnings("GrMethodMayBeStatic")
private void loadPluginConfigurationsToMicronautContext(ConfigurableApplicationContext applicationContext) {
String[] beanNames = applicationContext.getBeanNamesForType(GrailsPluginManager)
if (beanNames.length == 0) {
// do not continue if PluginManager is not available
ConfigurableApplicationContext parentApplicationContext = (ConfigurableApplicationContext) applicationContext.parent
if (!parentApplicationContext) {
// No Micronaut parent context, return early
return
}

GrailsPluginManager pluginManager = applicationContext.getBean(GrailsPluginManager)
ConfigurableApplicationContext parentApplicationContext = (ConfigurableApplicationContext) applicationContext.parent
ConfigurableEnvironment parentContextEnv = parentApplicationContext.getEnvironment()
if (parentContextEnv instanceof MicronautEnvironment) {
if (log.isDebugEnabled()) {
log.debug("Loading configurations from the plugins to the parent Micronaut context")
}
log.debug('Loading configurations from the plugins to the parent Micronaut context')
final io.micronaut.context.env.Environment micronautEnv = ((io.micronaut.context.env.Environment) parentContextEnv.getEnvironment())
final GrailsPlugin[] plugins = pluginManager.allPlugins
Integer priority = AbstractPropertySourceLoader.DEFAULT_POSITION
Arrays.stream(plugins)
.filter({ GrailsPlugin plugin -> plugin.propertySource != null })
.forEach({ GrailsPlugin plugin ->
if (log.isDebugEnabled()) {
log.debug("Loading configurations from {} plugin to the parent Micronaut context", plugin.name)
}
log.debug('Loading configurations from {} plugin to the parent Micronaut context', plugin.name)
micronautEnv.addPropertySource(PropertySource.of("grails.plugins.$plugin.name", (Map) plugin.propertySource.source, --priority))
})
micronautEnv.refresh()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.grails.plugins

import grails.boot.config.GrailsAutoConfiguration
import grails.core.DefaultGrailsApplication
import grails.core.GrailsApplication
import grails.plugins.GrailsPlugin
import grails.plugins.GrailsPluginManager
import groovy.transform.CompileStatic
import org.grails.plugins.DefaultGrailsPlugin
import org.grails.plugins.MockGrailsPluginManager
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.io.FileSystemResource
Expand All @@ -24,17 +21,17 @@ class GrailsPluginConfigurationClass extends GrailsAutoConfiguration {

@Bean(name = "grailsPluginManager")
GrailsPluginManager getGrailsPluginManager() {
String tempDir = System.getProperty("java.io.tmpdir")
GrailsApplication grailsApplication = new DefaultGrailsApplication()
final MockGrailsPluginManager pluginManager = new MockGrailsPluginManager(grailsApplication)
final List<DefaultGrailsPlugin> plugins = createGrailsPlugins(grailsApplication)
plugins.forEach({ plugin -> pluginManager.registerMockPlugin((GrailsPlugin) plugin)})
return pluginManager
MockGrailsPluginManager pluginManager = new MockGrailsPluginManager()
createGrailsPlugins(pluginManager.application).each {
pluginManager.registerMockPlugin(it)
}
pluginManager
}

private List<DefaultGrailsPlugin> createGrailsPlugins(DefaultGrailsApplication grailsApplication) {
private List<GrailsPlugin> createGrailsPlugins(GrailsApplication grailsApplication) {
final String grailsVersion = '4.0.1'
def gcl = new GroovyClassLoader()

GrailsPlugin plugin = new MockTestGrailsPlugin(gcl.parseClass("""class TestGrailsPlugin {
def version = '1.0.0'
def grailsVersion = '$grailsVersion'
Expand All @@ -45,7 +42,8 @@ class GrailsPluginConfigurationClass extends GrailsAutoConfiguration {
def version = '1.0.0'
def grailsVersion = '$grailsVersion'
}"""), grailsApplication)
[plugin, plugin2]

List<GrailsPlugin>.of(plugin, plugin2)
}

class MockTestGrailsPlugin extends DefaultGrailsPlugin {
Expand Down Expand Up @@ -82,10 +80,6 @@ class GrailsPluginConfigurationClass extends GrailsAutoConfiguration {

class MockTestTwoGrailsPlugin extends DefaultGrailsPlugin {

MockTestTwoGrailsPlugin(Class<?> pluginClass, Resource resource, GrailsApplication application) {
super(pluginClass, resource, application)
}

MockTestTwoGrailsPlugin(Class<?> pluginClass, GrailsApplication application) {
super(pluginClass, application)
}
Expand Down

0 comments on commit 82a9d0e

Please sign in to comment.