Skip to content

Commit

Permalink
Merge branch 'master' into fix/publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Jun 18, 2020
2 parents 3983c6a + 703810c commit 49ebc4a
Show file tree
Hide file tree
Showing 22 changed files with 205 additions and 56 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019 Vladimir Orany.
* 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.
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019 Vladimir Orany.
* 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.
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/guide.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019 Vladimir Orany.
* 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.
Expand Down
1 change: 0 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#Thu Jun 18 13:53:17 CEST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019 Vladimir Orany.
* 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.
Expand Down
6 changes: 3 additions & 3 deletions subprojects/micronaut-grails/micronaut-grails.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019 Vladimir Orany.
* 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.
Expand All @@ -28,6 +28,6 @@ dependencies {
testCompile("org.spockframework:spock-spring:$spockVersion") {
exclude group: "org.codehaus.groovy", module: "groovy-all"
}
testCompile group: 'com.agorapulse', name: 'micronaut-aws-sdk', version: '1.2.7'
testCompile group: 'com.amazonaws', name: 'aws-java-sdk-sns', version: '1.11.692'
testCompile group: 'com.agorapulse', name: 'micronaut-aws-sdk-sns', version: '1.2.10.2'
testCompile group: 'com.agorapulse', name: 'micronaut-aws-sdk-sqs', version: '1.2.10.2'
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019 Vladimir Orany.
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019 Vladimir Orany.
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019 Vladimir Orany.
* 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.
Expand All @@ -17,7 +17,7 @@
*/
package com.agorapulse.micronaut.grails;

import io.micronaut.context.DefaultBeanContext;
import io.micronaut.context.DefaultApplicationContext;
import io.micronaut.context.Qualifier;
import io.micronaut.inject.BeanDefinition;
import org.slf4j.Logger;
Expand All @@ -35,7 +35,8 @@
import java.util.*;

import static com.agorapulse.micronaut.grails.GrailsPropertyTranslatingCustomizer.collapse;
import static com.agorapulse.micronaut.grails.PropertyTranslatingCustomizer.*;
import static com.agorapulse.micronaut.grails.PropertyTranslatingCustomizer.grails;
import static com.agorapulse.micronaut.grails.PropertyTranslatingCustomizer.of;

/**
* Adds Micronaut beans to a Grails' Spring application context. This processor will
Expand Down Expand Up @@ -99,19 +100,22 @@ public static GrailsMicronautBeanProcessor.Builder builder(PropertyTranslatingCu
private static final String MICRONAUT_QUALIFIER_PROPERTY_NAME = "micronautQualifier";
private static final String MICRONAUT_SINGLETON_PROPERTY_NAME = "micronautSingleton";

private DefaultBeanContext micronautContext;
private final Map<String, Qualifier<?>> micronautBeanQualifiers;
private DefaultApplicationContext micronautContext;
private final Map<String, TypeAndQualifier<?>> micronautBeanQualifiers;
private final List<PropertyTranslatingCustomizer> customizers;
private final List<String> expectedMapProperties;
private Environment environment;

/**
* @param customizers properties translation customizer
* @param qualifiers the names and qualifiers of the Micronaut beans which should be added to the
* Spring application context.
* @param customizers properties translation customizer
* @param expectedMapProperties list of properties' prefixes which should be converted to map
*/
GrailsMicronautBeanProcessor(Map<String, Qualifier<?>> qualifiers, List<PropertyTranslatingCustomizer> customizers) {
GrailsMicronautBeanProcessor(Map<String, TypeAndQualifier<?>> qualifiers, List<PropertyTranslatingCustomizer> customizers, List<String> expectedMapProperties) {
this.customizers = customizers;
this.micronautBeanQualifiers = qualifiers;
this.expectedMapProperties = expectedMapProperties;
}

@Override
Expand All @@ -120,28 +124,31 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throw new IllegalStateException("Spring environment not set!");
}

micronautContext = new GrailsPropertyTranslatingApplicationContext(environment, of(collapse(customizers)));
micronautContext = new GrailsPropertyTranslatingApplicationContext(environment, of(collapse(customizers)), expectedMapProperties);

micronautContext.start();

NoClassDefFoundError noClassDefFoundError = null;

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

if (beanDefinitions.size() > 1) {
throw new IllegalArgumentException("There is too many candidates for " + micronautBeanQualifier + "! Candidates: " + beanDefinitions);
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, definition.getBeanType());
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());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019 Vladimir Orany.
* 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.
Expand All @@ -20,13 +20,15 @@
import io.micronaut.context.DefaultApplicationContext;
import io.micronaut.context.env.Environment;

import java.util.List;

class GrailsPropertyTranslatingApplicationContext extends DefaultApplicationContext {

private final Environment environment;

GrailsPropertyTranslatingApplicationContext(org.springframework.core.env.Environment environment, PropertyTranslatingCustomizer customizer) {
GrailsPropertyTranslatingApplicationContext(org.springframework.core.env.Environment environment, PropertyTranslatingCustomizer customizer, List<String> expectedMapProperties) {
super(environment.getActiveProfiles());
this.environment = new GrailsPropertyTranslatingEnvironment(environment, customizer);
this.environment = new GrailsPropertyTranslatingEnvironment(environment, customizer, expectedMapProperties);
this.environment.addPropertySource(new EnvVarLikeSystemPropertiesPropertySource());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019 Vladimir Orany.
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2019 Vladimir Orany.
* 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.
Expand All @@ -19,21 +19,67 @@

import io.micronaut.context.env.DefaultEnvironment;
import io.micronaut.core.convert.ArgumentConversionContext;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;

import javax.annotation.Nullable;
import java.util.Optional;
import java.util.Set;
import java.util.*;

class GrailsPropertyTranslatingEnvironment extends DefaultEnvironment {

private final Environment environment;
private final PropertyTranslatingCustomizer customizer;

GrailsPropertyTranslatingEnvironment(Environment environment, PropertyTranslatingCustomizer customizer) {
GrailsPropertyTranslatingEnvironment(Environment environment, PropertyTranslatingCustomizer customizer, List<String> expectedMapProperties) {
super(environment.getActiveProfiles());
this.environment = environment;
this.customizer = customizer;

if (environment instanceof AbstractEnvironment) {
Map<String, Object> multilayer = new LinkedHashMap<>();
AbstractEnvironment abEnv = (AbstractEnvironment) environment;
for (PropertySource<?> source : abEnv.getPropertySources()) {
if (source instanceof MapPropertySource) {
MapPropertySource mps = (MapPropertySource) source;
mps.getSource().forEach((k, v) -> {
Optional<String> expectedPrefix = expectedMapProperties.stream().filter(k::startsWith).findFirst();
if (expectedPrefix.isPresent()) {
String[] parts = k.split("\\.");
Map<String, Object> currentLevelMap = multilayer;
String prefix = "";
for (int i = 0; i < parts.length - 1; i++) {
String part = parts[i];
String currentKey = prefix + part;
prefix = prefix.length() == 0 ? part + "." : prefix + part + ".";

if (!currentKey.startsWith(expectedPrefix.get())) {
continue;
}

Object currentOrNewMap;
if (expectedPrefix.get().equals(currentKey)) {
currentOrNewMap = new LinkedHashMap<>();
multilayer.put(currentKey, currentOrNewMap);
} else {
currentOrNewMap = currentLevelMap.computeIfAbsent(part, key -> new LinkedHashMap<>());
}

if (currentOrNewMap instanceof Map) {
currentLevelMap = (Map<String, Object>) currentOrNewMap;
} else {
// conflict - cannot convert key to map of maps
return;
}
}
currentLevelMap.put(parts[parts.length - 1], v);
}
});
}
}
abEnv.getPropertySources().addLast(new MapPropertySource("multilayer", multilayer));
}
}

@Override
Expand Down Expand Up @@ -88,4 +134,4 @@ public <T> Optional<T> getProperty(@Nullable String name, ArgumentConversionCont
return Optional.empty();
}

}
}

0 comments on commit 49ebc4a

Please sign in to comment.