-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
608 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
gruVersion = 0.6.3 | ||
druVersion = 0.4.4 | ||
groovyVersion = 2.5.1 | ||
micronautVersion = 1.0.0.M3 | ||
gruVersion = 0.6.5 | ||
druVersion = 0.4.5 | ||
groovyVersion = 2.5.2 | ||
micronautVersion = 1.0.0.M4 | ||
spockVersion = 1.2-groovy-2.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
dependencies { | ||
compile "io.micronaut:spring" | ||
|
||
testCompile 'org.springframework:spring-test:5.0.8.RELEASE' | ||
testCompile("org.spockframework:spock-spring:$spockVersion") { | ||
exclude group: "org.codehaus.groovy", module: "groovy-all" | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
...-grails/src/main/groovy/com/agorapulse/micronaut/grails/GrailsMicronautBeanProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright 2017-2018 original authors | ||
* | ||
* 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 | ||
* | ||
* http: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.DefaultBeanContext; | ||
import io.micronaut.context.Qualifier; | ||
import io.micronaut.core.naming.NameUtils; | ||
import io.micronaut.inject.qualifiers.Qualifiers; | ||
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.EnvironmentAware; | ||
import org.springframework.core.env.Environment; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* 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. | ||
* | ||
* The Grails properties can be translated to Micronaut properties if required. | ||
* | ||
* @author jeffbrown | ||
* @author musketyr | ||
* @since 1.0 | ||
*/ | ||
public class GrailsMicronautBeanProcessor implements BeanFactoryPostProcessor, DisposableBean, EnvironmentAware { | ||
|
||
private static final String MICRONAUT_BEAN_TYPE_PROPERTY_NAME = "micronautBeanType"; | ||
private static final String MICRONAUT_CONTEXT_PROPERTY_NAME = "micronautContext"; | ||
private static final String MICRONAUT_SINGLETON_PROPERTY_NAME = "micronautSingleton"; | ||
|
||
private DefaultBeanContext micronautContext; | ||
private final List<Class<?>> micronautBeanQualifierTypes; | ||
private final PropertyTranslatingCustomizer customizer; | ||
private Environment environment; | ||
|
||
/** | ||
* @param customizer properties translation customizer | ||
* @param qualifierTypes The types associated with the | ||
* Micronaut beans which should be added to the | ||
* Spring application context. | ||
*/ | ||
public GrailsMicronautBeanProcessor(PropertyTranslatingCustomizer customizer, Class<?>... qualifierTypes) { | ||
this.customizer = customizer; | ||
this.micronautBeanQualifierTypes = Arrays.asList(qualifierTypes); | ||
} | ||
|
||
public GrailsMicronautBeanProcessor(Class<?>... qualifierTypes) { | ||
this(PropertyTranslatingCustomizer.grails().build(), qualifierTypes); | ||
} | ||
|
||
@Override | ||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { | ||
if (environment == null) { | ||
throw new IllegalStateException("Spring environment not set!"); | ||
} | ||
|
||
micronautContext = new GrailsPropertyTranslatingApplicationContext(environment, customizer); | ||
|
||
micronautContext.start(); | ||
|
||
micronautBeanQualifierTypes | ||
.forEach(micronautBeanQualifierType -> { | ||
Qualifier<Object> micronautBeanQualifier; | ||
if (micronautBeanQualifierType.isAnnotation()) { | ||
micronautBeanQualifier = Qualifiers.byStereotype((Class<? extends Annotation>) micronautBeanQualifierType); | ||
} else { | ||
micronautBeanQualifier = Qualifiers.byType(micronautBeanQualifierType); | ||
} | ||
micronautContext.getBeanDefinitions(micronautBeanQualifier) | ||
.forEach(definition -> { | ||
final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder | ||
.rootBeanDefinition("io.micronaut.spring.beans.MicronautSpringBeanFactory"); | ||
beanDefinitionBuilder.addPropertyValue(MICRONAUT_BEAN_TYPE_PROPERTY_NAME, definition.getBeanType()); | ||
beanDefinitionBuilder.addPropertyValue(MICRONAUT_CONTEXT_PROPERTY_NAME, micronautContext); | ||
beanDefinitionBuilder.addPropertyValue(MICRONAUT_SINGLETON_PROPERTY_NAME, definition.isSingleton()); | ||
String name = definition.getName(); | ||
if (name.equals(definition.getBeanType().getName())) { | ||
name = NameUtils.decapitalize(definition.getBeanType().getSimpleName()); | ||
} | ||
((DefaultListableBeanFactory) beanFactory).registerBeanDefinition(name, beanDefinitionBuilder.getBeanDefinition()); | ||
}); | ||
}); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
if (micronautContext != null) { | ||
micronautContext.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public void setEnvironment(Environment environment) { | ||
this.environment = environment; | ||
} | ||
|
||
/** | ||
* Returns the property name representation of the given name. | ||
* | ||
* Ported from grails.util.GrailsNameUtils | ||
* | ||
* @param name The name to convert | ||
* @return The property name representation | ||
*/ | ||
|
||
} | ||
|
20 changes: 20 additions & 0 deletions
20
...n/groovy/com/agorapulse/micronaut/grails/GrailsPropertyTranslatingApplicationContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.agorapulse.micronaut.grails; | ||
|
||
import io.micronaut.context.DefaultApplicationContext; | ||
import io.micronaut.context.env.Environment; | ||
|
||
class GrailsPropertyTranslatingApplicationContext extends DefaultApplicationContext { | ||
|
||
private final Environment environment; | ||
|
||
GrailsPropertyTranslatingApplicationContext(org.springframework.core.env.Environment environment, PropertyTranslatingCustomizer customizer) { | ||
super(environment.getActiveProfiles()); | ||
this.environment = new GrailsPropertyTranslatingEnvironment(environment, customizer); | ||
} | ||
|
||
@Override | ||
public io.micronaut.context.env.Environment getEnvironment() { | ||
return environment; | ||
} | ||
|
||
} |
102 changes: 102 additions & 0 deletions
102
.../src/main/groovy/com/agorapulse/micronaut/grails/GrailsPropertyTranslatingCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.agorapulse.micronaut.grails; | ||
|
||
import java.util.*; | ||
import java.util.regex.Pattern; | ||
|
||
class GrailsPropertyTranslatingCustomizer implements PropertyTranslatingCustomizer, PropertyTranslatingCustomizer.Builder { | ||
|
||
private static class PrefixReplacement { | ||
final String original; | ||
final String replacement; | ||
|
||
PrefixReplacement(String original, String replacement) { | ||
this.original = original; | ||
this.replacement = replacement; | ||
} | ||
|
||
} | ||
|
||
private static final String GRAILS_PREFIX = "grails."; | ||
private static final String MICRONAUT_PREFIX = "micronaut."; | ||
private static final String EMPTY_STRING = ""; | ||
|
||
static GrailsPropertyTranslatingCustomizer create() { | ||
return new GrailsPropertyTranslatingCustomizer(); | ||
} | ||
|
||
static GrailsPropertyTranslatingCustomizer standard() { | ||
return new GrailsPropertyTranslatingCustomizer() | ||
.replacePrefix(MICRONAUT_PREFIX, GRAILS_PREFIX) | ||
.replacePrefix(EMPTY_STRING, GRAILS_PREFIX); | ||
} | ||
|
||
private final List<PrefixReplacement> prefixPrefixReplacements = new ArrayList<>(); | ||
private final Set<Pattern> ignored = new LinkedHashSet<>(); | ||
|
||
private GrailsPropertyTranslatingCustomizer() { | ||
// prevents instantiation | ||
} | ||
|
||
/** | ||
* Replace property name prefixes. | ||
* @param original original prefix, can be empty | ||
* @param replacement new prefix, can be empty | ||
* @return self | ||
*/ | ||
@Override | ||
public GrailsPropertyTranslatingCustomizer replacePrefix(String original, String replacement) { | ||
prefixPrefixReplacements.add(new PrefixReplacement(original, replacement)); | ||
return this; | ||
} | ||
|
||
/** | ||
* Ignores exact match of the property name. | ||
* @param property the property to be ignored | ||
* @return self | ||
*/ | ||
@Override | ||
public GrailsPropertyTranslatingCustomizer ignore(String property) { | ||
ignored.add(Pattern.compile(Pattern.quote(property))); | ||
return this; | ||
} | ||
|
||
/** | ||
* Ignores by regex match of the property pattern. | ||
* @param propertyPattern the pattern of properties being ignored | ||
* @return self | ||
*/ | ||
@Override | ||
public GrailsPropertyTranslatingCustomizer ignoreAll(String propertyPattern) { | ||
ignored.add(Pattern.compile(propertyPattern)); | ||
return this; | ||
} | ||
|
||
@Override | ||
public PropertyTranslatingCustomizer build() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public Set<String> getAlternativeNames(String name) { | ||
if (name == null || name.length() == 0) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
if (ignored.stream().anyMatch(p -> p.matcher(name).matches())) { | ||
return Collections.emptySet(); | ||
} | ||
|
||
Set<String> keys = new LinkedHashSet<>(prefixPrefixReplacements.size()); | ||
prefixPrefixReplacements.forEach(replacement -> { | ||
if ((replacement.original.isEmpty() || name.startsWith(replacement.original)) && (replacement.replacement.isEmpty() || !name.startsWith(replacement.replacement))) { | ||
String keyNoPrefix = name.substring(replacement.original.length()); | ||
String alternativeKey = replacement.replacement + keyNoPrefix; | ||
if (!alternativeKey.equals(name)) { | ||
keys.add(alternativeKey); | ||
} | ||
} | ||
}); | ||
return keys; | ||
} | ||
|
||
} |
Oops, something went wrong.