Skip to content

Commit

Permalink
keeping the 1.x behaviour for legacy mode
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Nov 30, 2020
1 parent 451144d commit 6c83ee4
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TestController {
@Inject DirectlyInjected someDirectlyInjected
@Inject @Named("test") InjectedWithQualifier someInjectedWithQualifier


// beans using MicronautImporter can be injected without @Inject but requires a matching name
InjectedUsingBridge injectedUsingBridge
InjectedUsingBridgeWithDifferentName otherInjected
Expand Down Expand Up @@ -76,4 +77,11 @@ class TestController {
] as JSON)
}

def profiles() {
render([
someDirectlyInjected: someDirectlyInjected?.micronautContext?.environment?.activeNames,
injectedUsingBridge: injectedUsingBridge?.micronautContext?.environment?.activeNames
] as JSON)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ abstract class AbstractApplicationSpec extends Specification {

abstract Class<?> getApplicationClass()

boolean getMicronautBridgePackageApplied() {
return true
}

void setupSpec() {
Throwable th = null
Thread.start {
Expand Down Expand Up @@ -79,7 +83,7 @@ abstract class AbstractApplicationSpec extends Specification {
void 'package is applied'() {
expect:
context.getBean(DirectlyInjected).micronautContext.environment.packages.contains(ManagerService.package.name)
context.getBean(InjectedUsingBridge).micronautContext.environment.packages.contains(ManagerService.package.name)
context.getBean(InjectedUsingBridge).micronautContext.environment.packages.contains(ManagerService.package.name) == micronautBridgePackageApplied
}

void 'fetch services'() {
Expand Down Expand Up @@ -122,4 +126,14 @@ abstract class AbstractApplicationSpec extends Specification {
}
}

void 'check for active profiles'() {
expect:
gru.test {
get '/test/profiles'
expect {
json 'profiles.json'
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ class DefaultApplicationSpec extends AbstractApplicationSpec {
return DefaultApplication
}

@Override
boolean getMicronautBridgePackageApplied() {
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ class LegacyApplicationSpec extends AbstractApplicationSpec {
return LegacyApplication
}

@Override
boolean getMicronautBridgePackageApplied() {
return false
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"someDirectlyInjected": [
"micronaut-grails",
"micronaut-grails-strict",
"test"
],
"injectedUsingBridge": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"someDirectlyInjected": [
"micronaut-grails",
"micronaut-grails-bridge",
"test"
],
"injectedUsingBridge": [
"micronaut-grails",
"micronaut-grails-bridge",
"test"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"someDirectlyInjected": [
"test"
],
"injectedUsingBridge": [
"test",
"web"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"someDirectlyInjected": [
"micronaut-grails",
"micronaut-grails-legacy",
"test"
],
"injectedUsingBridge": [
"test",
"micronaut-grails",
"micronaut-grails-legacy",
"web"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.grails;

import io.micronaut.context.Qualifier;
import io.micronaut.inject.BeanDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.Nonnull;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

/**
* Adds Micronaut beans to a Grails' Spring application context. This processor will
* find all of the Micronaut beans of the specified types and add them as beans to the Spring application context.
*
* No property translation will happen.
* <p>
*
* @since 2.0.3
*/
public class DefaultGrailsMicronautBeanProcessor implements BeanFactoryPostProcessor, DisposableBean, ApplicationContextAware {

static final Logger LOGGER = LoggerFactory.getLogger(DefaultGrailsMicronautBeanProcessor.class);

private static final String MICRONAUT_BEAN_TYPE_PROPERTY_NAME = "micronautBeanType";
private static final String MICRONAUT_CONTEXT_PROPERTY_NAME = "micronautContext";
private static final String MICRONAUT_QUALIFIER_PROPERTY_NAME = "micronautQualifier";
private static final String MICRONAUT_SINGLETON_PROPERTY_NAME = "micronautSingleton";

private io.micronaut.context.ApplicationContext micronautContext;
private ApplicationContext springContext;
private final Map<String, TypeAndQualifier<?>> micronautBeanQualifiers;

/**
* @param qualifiers the names and qualifiers of the Micronaut beans which should be added to the
* Spring application context.
*/
DefaultGrailsMicronautBeanProcessor(Map<String, TypeAndQualifier<?>> qualifiers) {
this.micronautBeanQualifiers = qualifiers;
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
micronautContext = initializeMicronautContext();

NoClassDefFoundError noClassDefFoundError = null;

for (Map.Entry<String, TypeAndQualifier<?>> entry : micronautBeanQualifiers.entrySet()) {
String name = entry.getKey();
Class type = entry.getValue().getType();
Qualifier micronautBeanQualifier = entry.getValue().getQualifier();
try {
Collection<BeanDefinition<?>> beanDefinitions = type == null
? micronautContext.getBeanDefinitions(micronautBeanQualifier)
: micronautContext.getBeanDefinitions(type, micronautBeanQualifier);

if (beanDefinitions.size() > 1) {
throw new IllegalArgumentException("There is too many candidates of type " + type + " for " + micronautBeanQualifier + "! Candidates: " + beanDefinitions);
}

Optional<BeanDefinition<?>> firstBean = beanDefinitions.stream().findFirst();
BeanDefinition<?> definition = firstBean.orElseThrow(()-> new IllegalArgumentException("There is no candidate for " + micronautBeanQualifier));

final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
.rootBeanDefinition(GrailsMicronautBeanFactory.class);
beanDefinitionBuilder.addPropertyValue(MICRONAUT_BEAN_TYPE_PROPERTY_NAME, type == null ? definition.getBeanType() : type);
beanDefinitionBuilder.addPropertyValue(MICRONAUT_QUALIFIER_PROPERTY_NAME, micronautBeanQualifier);
beanDefinitionBuilder.addPropertyValue(MICRONAUT_CONTEXT_PROPERTY_NAME, micronautContext);
beanDefinitionBuilder.addPropertyValue(MICRONAUT_SINGLETON_PROPERTY_NAME, definition.isSingleton());

((DefaultListableBeanFactory) beanFactory).registerBeanDefinition(name, beanDefinitionBuilder.getBeanDefinition());
} catch (NoClassDefFoundError error) {
LOGGER.error("Exception loading class for qualifier {}. Bean {} will not be available in the runtime", micronautBeanQualifier, name);
LOGGER.error("Current class loader: {}", printClassLoader(getClass().getClassLoader()));
LOGGER.error("Parent class loader: {}", printClassLoader(getClass().getClassLoader().getParent()));
LOGGER.error("Current class path: {}", System.getProperty("java.class.path"));
noClassDefFoundError = error;
}
}

if (noClassDefFoundError == null) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Successfully added following beans to the spring contest {} ", micronautBeanQualifiers);
LOGGER.debug("Current class loader: {}", printClassLoader(getClass().getClassLoader()));
LOGGER.debug("Parent class loader: {}", printClassLoader(getClass().getClassLoader().getParent()));
LOGGER.trace("Current class path: {}", System.getProperty("java.class.path"));
}
return;
}

throw noClassDefFoundError;
}

protected io.micronaut.context.ApplicationContext initializeMicronautContext() {
return springContext.getBean(MicronautContextHolder.class).getContext();
}

private static String printClassLoader(ClassLoader classLoader) {
if (classLoader instanceof URLClassLoader) {
return "URLClassLoader for URLS:" + Arrays.toString(((URLClassLoader) classLoader).getURLs());
}
if (classLoader == null) {
return null;
}
return classLoader.toString();
}

@Override
public void destroy() {
if (micronautContext != null) {
micronautContext.close();
}
}

@Override
public void setApplicationContext(@Nonnull ApplicationContext applicationContext) throws BeansException {
this.springContext = applicationContext;
}
}

0 comments on commit 6c83ee4

Please sign in to comment.