Repository: incubator-tamaya-sandbox Updated Branches: refs/heads/master 66f7b1e3f -> 2b878fa18
TAMAYA-260 Fixed MP Optional and Provider injection (and a corresponding issue in core in the Optional converter). Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/9f79a5a5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/9f79a5a5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/9f79a5a5 Branch: refs/heads/master Commit: 9f79a5a53692bff04c23d233e52b9c1a607e6f43 Parents: 66f7b1e Author: Anatole Tresch <[email protected]> Authored: Thu Oct 12 11:46:48 2017 +0200 Committer: Anatole Tresch <[email protected]> Committed: Thu Oct 12 11:59:05 2017 +0200 ---------------------------------------------------------------------- microprofile/pom.xml | 2 +- .../cdi/MicroprofileConfigurationProducer.java | 21 ------- .../converter/ProviderConverter.java | 42 ++++++++++++- .../imported/OptionalValuesBean.java | 64 ++++++++++++++++++++ .../tck/TamayaConfigArchiveProcessor.java | 2 + microprofile/src/test/tck-suite.xml | 14 ++--- 6 files changed, 113 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/9f79a5a5/microprofile/pom.xml ---------------------------------------------------------------------- diff --git a/microprofile/pom.xml b/microprofile/pom.xml index e3bad3e..a7639a6 100644 --- a/microprofile/pom.xml +++ b/microprofile/pom.xml @@ -92,7 +92,7 @@ under the License. </dependency> <dependency> <groupId>org.jboss.weld.se</groupId> - <artifactId>weld-se-shaded</artifactId> + <artifactId>weld-se</artifactId> <version>${weld.version}</version> <scope>test</scope> </dependency> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/9f79a5a5/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java ---------------------------------------------------------------------- diff --git a/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java b/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java index 8445407..c260023 100644 --- a/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java +++ b/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java @@ -136,26 +136,5 @@ public class MicroprofileConfigurationProducer { return ConfigProviderResolver.instance().getBuilder(); } -// @Produces -// @ConfigProperty -// public Provider getConfiguredProvider(InjectionPoint injectionPoint){ -// final ConfigProperty annotation = injectionPoint.getAnnotated().getAnnotation(ConfigProperty.class); -// String key = annotation.name(); -// -// // unless the extension is not installed, this should never happen because the extension -// // enforces the resolvability of the config -// -// String defaultTextValue = annotation.defaultValue().isEmpty() ? null : annotation.defaultValue(); -// ConversionContext conversionContext = createConversionContext(key, injectionPoint); -// return () -> { -// Object value = resolveValue(defaultTextValue, conversionContext, injectionPoint); -// if (value == null) { -// throw new ConfigException(String.format( -// "Can't resolve any of the possible config keys: %s to the required target type: %s, supported formats: %s", -// key, conversionContext.getTargetType(), conversionContext.getSupportedFormats().toString())); -// } -// return value; -// }; -// } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/9f79a5a5/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/ProviderConverter.java ---------------------------------------------------------------------- diff --git a/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/ProviderConverter.java b/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/ProviderConverter.java index b8b5068..163481d 100644 --- a/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/ProviderConverter.java +++ b/microprofile/src/main/java/org/apache/tamaya/microprofile/converter/ProviderConverter.java @@ -19,6 +19,8 @@ package org.apache.tamaya.microprofile.converter; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.ConfigQuery; +import org.apache.tamaya.Configuration; import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConversionContext; import org.apache.tamaya.spi.PropertyConverter; @@ -26,6 +28,9 @@ import org.apache.tamaya.spi.PropertyConverter; import javax.annotation.Priority; import javax.inject.Provider; import java.lang.reflect.Type; +import java.util.List; +import java.util.Objects; +import java.util.logging.Level; import java.util.logging.Logger; /** @@ -34,14 +39,15 @@ import java.util.logging.Logger; @Priority(-1) public class ProviderConverter implements PropertyConverter<Provider> { - private final Logger LOG = Logger.getLogger(getClass().getName()); + private static final Logger LOG = Logger.getLogger(ProviderConverter.class.getName()); @Override public Provider convert(String value, ConversionContext context) { return () -> { try{ Type targetType = context.getTargetType().getType(); - return context.getConfiguration().get(value, TypeLiteral.of(targetType)); + ConvertQuery converter = new ConvertQuery(value, TypeLiteral.of(targetType)); + return context.getConfiguration().query(converter); }catch(Exception e){ throw new ConfigException("Error evaluating config value.", e); } @@ -57,4 +63,36 @@ public class ProviderConverter implements PropertyConverter<Provider> { public int hashCode(){ return getClass().hashCode(); } + + private static final class ConvertQuery<T> implements ConfigQuery<T> { + + private String rawValue; + private TypeLiteral<T> type; + + public ConvertQuery(String rawValue, TypeLiteral<T> type) { + this.rawValue = Objects.requireNonNull(rawValue); + this.type = Objects.requireNonNull(type); + } + + @Override + public T query(Configuration config) { + List<PropertyConverter<T>> converters = config.getContext().getPropertyConverters(type); + ConversionContext context = new ConversionContext.Builder(type).setConfigurationContext(config.getContext()) + .setConfiguration(config).setKey(ConvertQuery.class.getName()).build(); + for(PropertyConverter<?> conv: converters) { + try{ + if(conv instanceof ProviderConverter){ + continue; + } + T result = (T)conv.convert(rawValue, context); + if(result!=null){ + return result; + } + }catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Converter "+ conv +" failed to convert to " + type); + } + } + return null; + } + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/9f79a5a5/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/OptionalValuesBean.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/OptionalValuesBean.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/OptionalValuesBean.java new file mode 100644 index 0000000..b962f21 --- /dev/null +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/imported/OptionalValuesBean.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2016-2017 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * 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://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 org.apache.tamaya.microprofile.imported; +/** + * Declare a bean for config property injections. + * @author <a href="mailto:[email protected]">Mark Struberg</a> + * @author <a href="mailto:[email protected]">Emily Jiang</a> + */ + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +import javax.enterprise.context.Dependent; +import javax.inject.Inject; +import javax.inject.Singleton; +import java.util.Optional; + +@Singleton +public class OptionalValuesBean { + @Inject + @ConfigProperty(name="my.optional.int.property") + private Optional<Integer> intProperty; + + @Inject + @ConfigProperty(name="my.notexisting.property") + private Optional<Integer> notexistingProperty; + + private Optional<String> notAnnotatedStringValue; + + private Optional<String> stringValue; + + @Inject + public void setStringValue(@ConfigProperty(name="my.optional.string.property") Optional<String> stringValue) { + this.stringValue = stringValue; + } + + public Optional<String> getStringValue() { + return stringValue; + } + + public Optional<Integer> getIntProperty() { + return intProperty; + } + + public Optional<Integer> getNotexistingProperty() { + return notexistingProperty; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/9f79a5a5/microprofile/src/test/java/org/apache/tamaya/microprofile/tck/TamayaConfigArchiveProcessor.java ---------------------------------------------------------------------- diff --git a/microprofile/src/test/java/org/apache/tamaya/microprofile/tck/TamayaConfigArchiveProcessor.java b/microprofile/src/test/java/org/apache/tamaya/microprofile/tck/TamayaConfigArchiveProcessor.java index 7108c4a..61cd11c 100644 --- a/microprofile/src/test/java/org/apache/tamaya/microprofile/tck/TamayaConfigArchiveProcessor.java +++ b/microprofile/src/test/java/org/apache/tamaya/microprofile/tck/TamayaConfigArchiveProcessor.java @@ -18,6 +18,7 @@ */ package org.apache.tamaya.microprofile.tck; +import org.apache.tamaya.core.internal.converters.OptionalConverter; import org.apache.tamaya.microprofile.MicroprofileAdapter; import org.apache.tamaya.microprofile.MicroprofileConfigProviderResolver; import org.apache.tamaya.microprofile.cdi.MicroprofileCDIExtension; @@ -66,6 +67,7 @@ public class TamayaConfigArchiveProcessor implements ApplicationArchiveProcessor .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") .addAsServiceProvider(ConfigProviderResolver.class, MicroprofileConfigProviderResolver.class) .addAsServiceProvider(PropertyConverter.class, BooleanAsIntegerConverterFix.class) + .addAsServiceProvider(PropertyConverter.class, OptionalConverter.class) .addAsServiceProvider(Extension.class, MicroprofileCDIExtension.class); ((WebArchive) applicationArchive).addAsLibraries( configJar) http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/9f79a5a5/microprofile/src/test/tck-suite.xml ---------------------------------------------------------------------- diff --git a/microprofile/src/test/tck-suite.xml b/microprofile/src/test/tck-suite.xml index 7efa750..84d36ad 100644 --- a/microprofile/src/test/tck-suite.xml +++ b/microprofile/src/test/tck-suite.xml @@ -18,12 +18,10 @@ specific language governing permissions and limitations under the License. --> <suite name="microprofile-config-TCK" verbose="2" configfailurepolicy="continue" > - - <test name="microprofile-config 1.0 TCK"> - <packages> - <package name="org.eclipse.microprofile.config.tck.*"> - </package> - </packages> - </test> - + <test name="microprofile-config 1.1 TCK"> + <packages> + <package name="org.eclipse.microprofile.config.tck.*"> + </package> + </packages> + </test> </suite> \ No newline at end of file
