TAMAYA-300 Added tests to improve mutation coverage. Movewd OSGI and MP to extensions.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/a1cd433a Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/a1cd433a Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/a1cd433a Branch: refs/heads/master Commit: a1cd433a9828241664a85f77a950993b14403678 Parents: a91e434 Author: Anatole Tresch <[email protected]> Authored: Tue Oct 17 01:02:53 2017 +0200 Committer: Anatole Tresch <[email protected]> Committed: Tue Oct 17 01:02:54 2017 +0200 ---------------------------------------------------------------------- modules/microprofile/bnd.bnd | 34 ++ modules/microprofile/pom.xml | 158 +++++++ .../microprofile/MicroprofileAdapter.java | 185 ++++++++ .../tamaya/microprofile/MicroprofileConfig.java | 100 +++++ .../microprofile/MicroprofileConfigBuilder.java | 140 ++++++ .../MicroprofileConfigProviderResolver.java | 82 ++++ .../microprofile/MicroprofileConfigSource.java | 78 ++++ .../MicroprofileConfigSourceProvider.java | 64 +++ .../microprofile/MicroprofileConverter.java | 50 +++ .../MicroprofileDefaultProperties.java | 33 ++ .../microprofile/TamayaConfiguration.java | 93 ++++ .../microprofile/TamayaPropertyConverter.java | 48 ++ .../microprofile/TamayaPropertySource.java | 78 ++++ .../TamayaPropertySourceProvider.java | 56 +++ .../microprofile/cdi/BridgingConfigBean.java | 95 ++++ .../microprofile/cdi/ConfiguredField.java | 65 +++ .../microprofile/cdi/ConfiguredMethod.java | 65 +++ .../tamaya/microprofile/cdi/ConfiguredType.java | 86 ++++ .../cdi/MicroprofileCDIExtension.java | 127 ++++++ .../cdi/MicroprofileConfigurationProducer.java | 156 +++++++ .../converter/BooleanAsIntegerConverterFix.java | 61 +++ .../converter/ProviderConverter.java | 98 ++++ .../javax.enterprise.inject.spi.Extension | 20 + .../org.apache.tamaya.spi.PropertyConverter | 21 + .../org.apache.tamaya.spi.PropertySource | 20 + ...croprofile.config.spi.ConfigProviderResolver | 20 + .../microprofile/src/main/resources/beans.xml | 25 ++ .../MicroprofileConfigBuilderTest.java | 99 +++++ .../MicroprofileConfigProviderResolverTest.java | 73 +++ .../MicroprofileConfigProviderTest.java | 62 +++ .../microprofile/MicroprofileConfigTest.java | 95 ++++ .../tck/TamayaConfigArchiveProcessor.java | 79 ++++ .../microprofile/tck/TamayaConfigExtension.java | 36 ++ .../src/test/resources/META-INF/beans.xml | 24 + .../META-INF/microprofile-config.properties | 105 +++++ ....jboss.arquillian.core.spi.LoadableExtension | 19 + .../src/test/resources/sampleconfig.yaml | 18 + modules/microprofile/src/test/tck-suite.xml | 27 ++ modules/osgi/common/bnd.bnd | 33 ++ modules/osgi/common/pom.xml | 91 ++++ .../java/org/apache/tamaya/osgi/Activator.java | 74 ++++ .../java/org/apache/tamaya/osgi/Backups.java | 157 +++++++ .../org/apache/tamaya/osgi/ConfigChanger.java | 215 +++++++++ .../org/apache/tamaya/osgi/ConfigHistory.java | 268 +++++++++++ .../java/org/apache/tamaya/osgi/Policy.java | 31 ++ .../apache/tamaya/osgi/TamayaConfigPlugin.java | 444 +++++++++++++++++++ .../tamaya/osgi/commands/BackupCommands.java | 135 ++++++ .../tamaya/osgi/commands/ConfigCommands.java | 245 ++++++++++ .../tamaya/osgi/commands/HistoryCommands.java | 101 +++++ .../apache/tamaya/osgi/commands/StringUtil.java | 46 ++ .../osgi/commands/TamayaConfigService.java | 206 +++++++++ .../apache/tamaya/osgi/AbstractOSGITest.java | 100 +++++ .../org/apache/tamaya/osgi/ActivatorTest.java | 44 ++ .../org/apache/tamaya/osgi/BackupsTest.java | 107 +++++ .../apache/tamaya/osgi/ConfigHistoryTest.java | 148 +++++++ .../tamaya/osgi/TamayaConfigPluginTest.java | 290 ++++++++++++ .../osgi/commands/BackupCommandsTest.java | 103 +++++ .../osgi/commands/ConfigCommandsTest.java | 204 +++++++++ .../osgi/commands/HistoryCommandsTest.java | 87 ++++ .../META-INF/javaconfiguration.properties | 22 + .../test/resources/org.ops4j.pax.logging.cfg | 48 ++ .../tamaya/gogo/shell/AbstractOSGITest.java | 19 + .../tamaya/gogo/shell/BackupCommandsTest.java | 55 ++- .../tamaya/gogo/shell/ConfigCommandsTest.java | 111 ++++- .../tamaya/gogo/shell/HistoryCommandsTest.java | 11 +- .../tamaya/gogo/shell/SettingsCommandsTest.java | 14 + modules/osgi/injection/bnd.bnd | 34 ++ modules/osgi/injection/pom.xml | 72 +++ .../apache/tamaya/osgi/injection/Activator.java | 95 ++++ .../osgi/injection/ConfigInjectionService.java | 115 +++++ .../OSGIConfigAdminPropertySource.java | 108 +++++ .../injection/OSGIConfigurationInjector.java | 118 +++++ .../osgi/injection/TamayaOSGIInjector.java | 162 +++++++ .../tamaya/osgi/injection/AbstractOSGITest.java | 99 +++++ .../tamaya/osgi/injection/ActivatorTest.java | 43 ++ .../OSGIConfigAdminPropertySourceTest.java | 65 +++ .../OSGIConfigurationInjectorTest.java | 97 ++++ modules/osgi/karaf-features/pom.xml | 47 ++ .../src/main/features/features.xml | 84 ++++ modules/osgi/karaf-shell/bnd.bnd | 38 ++ modules/osgi/karaf-shell/pom.xml | 63 +++ .../karaf/shell/ApplyTamayaConfigCommand.java | 56 +++ .../tamaya/karaf/shell/BackupCreateCommand.java | 56 +++ .../tamaya/karaf/shell/BackupDeleteCommand.java | 47 ++ .../tamaya/karaf/shell/BackupListCommand.java | 47 ++ .../karaf/shell/BackupRestoreCommand.java | 46 ++ .../karaf/shell/DefaultEnableCommand.java | 67 +++ .../karaf/shell/DefaultEnabledCommand.java | 62 +++ .../tamaya/karaf/shell/GetPolicyCommand.java | 42 ++ .../karaf/shell/HistoryDeleteAllCommand.java | 43 ++ .../karaf/shell/HistoryDeleteCommand.java | 45 ++ .../tamaya/karaf/shell/HistoryGetCommand.java | 69 +++ .../karaf/shell/HistoryMaxsizeCommand.java | 41 ++ .../karaf/shell/HistoryMaxsizeSetCommand.java | 47 ++ .../apache/tamaya/karaf/shell/InfoCommand.java | 41 ++ .../tamaya/karaf/shell/OSGIConfigCommand.java | 51 +++ .../tamaya/karaf/shell/PolicyGetCommand.java | 42 ++ .../tamaya/karaf/shell/PolicySetCommand.java | 68 +++ .../karaf/shell/PropagateUpdatesCommand.java | 44 ++ .../karaf/shell/PropagateUpdatesSetCommand.java | 49 ++ .../tamaya/karaf/shell/PropertyGetCommand.java | 49 ++ .../karaf/shell/PropertySourceCommand.java | 41 ++ .../karaf/shell/PropertySourcesCommand.java | 39 ++ .../tamaya/karaf/shell/TamayaConfigCommand.java | 53 +++ .../org/apache/tamaya/karaf/shell/commands | 44 ++ modules/osgi/pom.xml | 107 +++++ modules/pom.xml | 2 + 107 files changed, 8625 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/bnd.bnd ---------------------------------------------------------------------- diff --git a/modules/microprofile/bnd.bnd b/modules/microprofile/bnd.bnd new file mode 100644 index 0000000..da67308 --- /dev/null +++ b/modules/microprofile/bnd.bnd @@ -0,0 +1,34 @@ +-buildpath: \ + osgi.annotation; version=6.0.0,\ + osgi.core; version=6.0,\ + osgi.cmpn; version=6.0 + +-testpath: \ + ${junit} + +javac.source: 1.8 +javac.target: 1.8 + +Bundle-Version: ${version}.${tstamp} +Bundle-Name: Apache Tamaya - Microprofile +Bundle-SymbolicName: org.apache.tamaya.microprofile +Bundle-Description: Apacha Tamaya Config - Microprofile Implementation +Bundle-Category: Implementation +Bundle-Copyright: (C) Apache Foundation +Bundle-License: Apache Licence version 2 +Bundle-Vendor: Apache Software Foundation +Bundle-ContactAddress: [email protected] +Bundle-DocURL: http://tamaya.apache.org +Export-Package: \ + org.apache.tamaya.microprofile,\ + org.apache.tamaya.microprofile.cdi,\ + org.apache.tamaya.microprofile.converter +Import-Package: \ + org.apache.tamaya,\ + org.apache.tamaya.spi,\ + org.eclipse.microprofile.config +Export-Service: \ + org.apache.tamaya.spi.PropertyConverter,\ + org.apache.tamaya.spi.ProperySource,\ + org.eclipse.microprofile.config.spi.ConfigProviderResolver,\ + javax.enterprise.inject.spi.Extension http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/pom.xml ---------------------------------------------------------------------- diff --git a/modules/microprofile/pom.xml b/modules/microprofile/pom.xml new file mode 100644 index 0000000..ff4a8fd --- /dev/null +++ b/modules/microprofile/pom.xml @@ -0,0 +1,158 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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 current 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-extensions</artifactId> + <version>0.4-incubating-SNAPSHOT</version> + </parent> + + <artifactId>tamaya-microprofile</artifactId> + <name>Apache Tamaya Modules - Microprofile API</name> + <packaging>jar</packaging> + + <properties> + <maven.compile.sourceLevel>1.8</maven.compile.sourceLevel> + <maven.compile.targetLevel>1.8</maven.compile.targetLevel> + <microprofile.config.version>1.1</microprofile.config.version> + <geronimo-atinject-1.0-spec.version>1.0</geronimo-atinject-1.0-spec.version> + <geronimo-jcdi-1.1-spec.version>1.0</geronimo-jcdi-1.1-spec.version> + <version.shrinkwrap.resolvers>2.2.6</version.shrinkwrap.resolvers> + <tamaya-version>0.4-incubating-SNAPSHOT</tamaya-version> + <arquillian.version>1.1.13.Final</arquillian.version> + <arquillian-weld-embedded.version>2.0.0.Beta5</arquillian-weld-embedded.version> + <cdi-api.version>2.0</cdi-api.version> + <weld.version>3.0.1.Final</weld.version> + <deltaspike.version>1.1.0</deltaspike.version> + <openejb.version>4.7.1</openejb.version> + </properties> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>java-hamcrest</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-core</artifactId> + <version>${tamaya-version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.tamaya</groupId> + <artifactId>tamaya-api</artifactId> + <version>${tamaya-version}</version> + </dependency> + <dependency> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-functions</artifactId> + <version>${tamaya-version}</version> + </dependency> + <dependency> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-events</artifactId> + <version>${tamaya-version}</version> + </dependency> + <dependency> + <groupId>org.eclipse.microprofile.config</groupId> + <artifactId>microprofile-config-api</artifactId> + <version>${microprofile.config.version}</version> + </dependency> + <dependency> + <groupId>javax.enterprise</groupId> + <artifactId>cdi-api</artifactId> + <version>${cdi-api.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.jboss.weld.se</groupId> + <artifactId>weld-se-shaded</artifactId> + <version>${weld.version}</version> + <scope>test</scope> + </dependency> + <!-- Microprofile TCK support only --> + <dependency> + <groupId>org.jboss.arquillian.testng</groupId> + <artifactId>arquillian-testng-container</artifactId> + <version>${arquillian.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <version>6.9.9</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.jboss.shrinkwrap.resolver</groupId> + <artifactId>shrinkwrap-resolver-depchain</artifactId> + <version>${version.shrinkwrap.resolvers}</version> + <scope>test</scope> + <type>pom</type> + </dependency> + </dependencies> + + <profiles> + <profile> + <id>TCK</id> + <activation> + <activeByDefault>false</activeByDefault> + </activation> + <dependencies> + <dependency> + <groupId>org.eclipse.microprofile.config</groupId> + <artifactId>microprofile-config-tck</artifactId> + <version>${microprofile.config.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.jboss.arquillian.container</groupId> + <artifactId>arquillian-weld-embedded</artifactId> + <version>${arquillian-weld-embedded.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.20.1</version> + <configuration> + <suiteXmlFiles> + <suiteXmlFile>src/test/tck-suite.xml</suiteXmlFile> + </suiteXmlFiles> + </configuration> + </plugin> + </plugins> + </build> + </profile> + </profiles> + +</project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileAdapter.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileAdapter.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileAdapter.java new file mode 100644 index 0000000..8f5002c --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileAdapter.java @@ -0,0 +1,185 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + + +import org.apache.tamaya.*; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.spi.ConfigBuilder; +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.spi.Converter; + +import java.util.*; + +/** + * Utility class for adapting microprofile artifacts into Tamaya artifacts and vice versa. + */ +public final class MicroprofileAdapter{ + + /** + * Singleton constructor. + */ + private MicroprofileAdapter(){} + + /** + * Converts a Tamaya {@link Configuration} into a Microprofile.io {@link Config}. + * @param config the Tamaya {@link Configuration} instance, not null. + * @return the corresponding Microprofile.io {@link Config} instance, never null. + */ + public static Config toConfig(Configuration config){ + if(config instanceof TamayaConfiguration){ + return ((TamayaConfiguration)config).getConfig(); + } + return new MicroprofileConfig(config); + } + + /** + * Converts a Microprofile {@link Config}s into Tamaya {@link Configuration}s. + * @param config the Microprofile {@link Config} instance, not null. + * @return the corresponding Tamaya {@link Configuration} instance, never null. + */ + public static Configuration toConfiguration(Config config){ + if(config instanceof MicroprofileConfig){ + return ((MicroprofileConfig)config).getConfiguration(); + } + return new TamayaConfiguration(config); + } + + /** + * Converts a Tamaya {@link PropertySource}s into a Microprofile.io {@link ConfigSource}. + * @param propertySources the Tamaya {@link PropertySource} instances, not null. + * @return the corresponding Microprofile.io {@link ConfigSource} instance, never null. + */ + public static List<ConfigSource> toConfigSources(Iterable<PropertySource> propertySources) { + List<ConfigSource> configSources = new ArrayList<>(); + for(PropertySource ps:propertySources){ + configSources.add(toConfigSource(ps)); + } + Collections.reverse(configSources); + return configSources; + } + + /** + * Converts a Microprofile {@link ConfigSource}s into Tamaya {@link PropertySource}s. + * @param configSources the Microprofile {@link ConfigSource} instances, not null. + * @return the corresponding Tamaya {@link PropertySource} instances, never null. + */ + public static List<PropertySource> toPropertySources(Iterable<ConfigSource> configSources) { + List<PropertySource> propertySources = new ArrayList<>(); + for(ConfigSource cs:configSources){ + propertySources.add(toPropertySource(cs)); + } + return propertySources; + } + + /** + * Converts a Tamaya {@link PropertySource} into a Microprofile.io {@link ConfigSource}. + * @param propertySource the Tamaya {@link PropertySource} instance, not null. + * @return the corresponding Microprofile.io {@link ConfigSource} instance, never null. + */ + public static ConfigSource toConfigSource(PropertySource propertySource) { + if(propertySource instanceof TamayaPropertySource){ + return ((TamayaPropertySource)propertySource).getConfigSource(); + } + return new MicroprofileConfigSource(propertySource); + } + + /** + * Converts a Microprofile {@link ConfigSource} into a Tamaya {@link PropertySource}. + * @param configSource the Microprofile {@link ConfigSource} instance, not null. + * @return the corresponding Tamaya {@link PropertySource} instance, never null. + */ + public static PropertySource toPropertySource(ConfigSource configSource) { + if(configSource instanceof MicroprofileConfigSource){ + return ((MicroprofileConfigSource)configSource).getPropertySource(); + } + return new TamayaPropertySource(configSource); + } + + /** + * Converts a Microprofile {@link Converter} into a Tamaya {@link PropertyConverter}. + * @param converter the Microprofile {@link Converter} instance, not null. + * @param <T> the target type + * @return the corresponding Tamaya {@link PropertyConverter} instance, never null. + */ + public static <T> PropertyConverter<T> toPropertyConverter(Converter<T> converter) { + if(converter instanceof MicroprofileConverter){ + return ((MicroprofileConverter)converter).getPropertyConverter(); + } + return new TamayaPropertyConverter(converter); + } + + /** + * Converts a Tamaya {@link PropertyConverter} into a Microprofile.io {@link Converter}. + * @param converter the Tamaya {@link PropertyConverter} instance, not null. + * @param <T> the target type + * @return the corresponding Microprofile.io {@link Converter} instance, never null. + */ + public static <T> Converter<T> toConverter(PropertyConverter<T> converter) { + if(converter instanceof TamayaPropertyConverter){ + return ((TamayaPropertyConverter)converter).getConverter(); + } + return new MicroprofileConverter(converter); + } + + /** + * Converts a Tamaya {@link ConfigurationContextBuilder} into a Microprofile.io {@link ConfigBuilder}. + * @param builder the Tamaya {@link ConfigurationContextBuilder} instance, not null. + * @return the corresponding Microprofile.io {@link ConfigBuilder} instance, never null. + */ + public static ConfigBuilder toConfigBuilder(ConfigurationContextBuilder builder) { + return new MicroprofileConfigBuilder(builder); + } + + /** + * Converts the given Tamaya key, value map into a corresponding String based map, hereby + * omitting all meta-entries. + * @param properties the Tamaya key, value map, not null. + * @return the corresponding String based map, never null. + */ + public static Map<String, String> toStringMap(Map<String, PropertyValue> properties) { + Map<String, String> valueMap = new HashMap<>(properties.size()); + for(Map.Entry<String,PropertyValue> en:properties.entrySet()){ + if(en.getValue().getValue()!=null) { + valueMap.put(en.getKey(), en.getValue().getValue()); + } + } + return valueMap; + } + + /** + * Converts the given String based key, value map into a corresponding String,PropertyValue + * based map. + * @param properties the String based key, value map, not null. + * @param source the source of the entries, not null. + * @return the corresponding String,PropertyValue based map, never null. + */ + public static Map<String, PropertyValue> toPropertyValueMap(Map<String, String> properties, String source) { + Map<String, PropertyValue> valueMap = new HashMap<>(properties.size()); + for(Map.Entry<String,String> en:properties.entrySet()){ + valueMap.put(en.getKey(), PropertyValue.of(en.getKey(), en.getValue(), source)); + } + return valueMap; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfig.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfig.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfig.java new file mode 100644 index 0000000..61c3cdc --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfig.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.events.FrozenConfiguration; +import org.apache.tamaya.spi.PropertySource; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.spi.ConfigSource; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.*; + +/** + * Microprofile {@link ConfigSource} implementation that wraps a {@link PropertySource} instance. + */ +public class MicroprofileConfig implements Config, Serializable { + + private Configuration delegate; + + public MicroprofileConfig(Configuration delegate){ + this.delegate = Objects.requireNonNull(delegate); + } + + public Configuration getConfiguration(){ + return this.delegate; + } + + + @Override + public <T> T getValue(String propertyName, Class<T> propertyType) { + T value = null; + try{ + value = delegate.get(propertyName, propertyType); + }catch(ConfigException e){ + if(e.toString().contains("Unparseable")){ + throw new IllegalArgumentException("Invalid type: " + propertyType.getName()); + } + } + if(value == null){ + throw new NoSuchElementException("No such config property: " + propertyName); + } + return value; + } + + @Override + public <T> Optional<T> getOptionalValue(String propertyName, Class<T> propertyType) { + return Optional.ofNullable(delegate.get(propertyName, propertyType)); + } + + @Override + public Iterable<String> getPropertyNames() { + return delegate.getProperties().keySet(); + } + + @Override + public Iterable<ConfigSource> getConfigSources() { + return MicroprofileAdapter.toConfigSources(delegate.getContext().getPropertySources()); + } + + @Override + public String toString() { + return "MicroprofileConfig{" + + "delegate=" + delegate + + '}'; + } + + private void writeObject(ObjectOutputStream out) throws IOException{ + if(!(this.delegate instanceof Serializable)){ + out.writeObject(FrozenConfiguration.of(this.delegate)); + }else { + out.writeObject(this.delegate); + } + } + + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{ + this.delegate = (Configuration)in.readObject(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilder.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilder.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilder.java new file mode 100644 index 0000000..896311c --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigBuilder.java @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.ServiceContextManager; +import org.apache.tamaya.spisupport.EnvironmentPropertySource; +import org.apache.tamaya.spisupport.PropertySourceComparator; +import org.apache.tamaya.spisupport.SystemPropertySource; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.spi.ConfigBuilder; +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.spi.ConfigSourceProvider; +import org.eclipse.microprofile.config.spi.Converter; + +import java.util.Objects; + +/** + * Created by atsticks on 23.03.17. + */ +final class MicroprofileConfigBuilder implements ConfigBuilder{ + + private ConfigurationContextBuilder contextBuilder; + private ClassLoader classLoader; + + MicroprofileConfigBuilder(ConfigurationContextBuilder contextBuilder){ + this.contextBuilder = Objects.requireNonNull(contextBuilder); + contextBuilder.addDefaultPropertyConverters(); + } + + public ConfigurationContextBuilder getConfigurationContextBuilder(){ + return contextBuilder; + } + + /** + * Add the default config sources appearing on the builder's classpath + * including: + * <ol> + * <li>System properties</li> + * <li>Environment properties</li> + * <li>/META-INF/microprofile-config.properties</li> + * </ol> + * + * @return the ConfigBuilder with the default config sources + */ + @Override + public ConfigBuilder addDefaultSources() { + contextBuilder.addPropertySources( + new SystemPropertySource(400), + new EnvironmentPropertySource(300), + new MicroprofileDefaultProperties()); + contextBuilder.sortPropertySources(PropertySourceComparator.getInstance()); + return this; + } + + /** + * Add ConfigSources registered using the ServiceLoader. + * @return the ConfigBuilder with the added config sources + */ + @Override + public ConfigBuilder addDiscoveredSources() { + for(ConfigSource configSource: ServiceContextManager.getServiceContext().getServices(ConfigSource.class)){ + contextBuilder.addPropertySources(MicroprofileAdapter.toPropertySource(configSource)); + } + for(ConfigSourceProvider configSourceProvider: ServiceContextManager.getServiceContext().getServices(ConfigSourceProvider.class)){ + contextBuilder.addPropertySources(MicroprofileAdapter.toPropertySources(configSourceProvider.getConfigSources( + Thread.currentThread().getContextClassLoader() + ))); + } + contextBuilder.sortPropertySources(PropertySourceComparator.getInstance()); + return this; + } + + /** + * Add Converters registered using the ServiceLoader. + * @return the ConfigBuilder with the added config converters + */ + @Override + public ConfigBuilder addDiscoveredConverters() { + for(Converter<?> converter: ServiceContextManager.getServiceContext().getServices(Converter.class)){ + TypeLiteral targetType = TypeLiteral.of( + TypeLiteral.getGenericInterfaceTypeParameters(converter.getClass(),Converter.class)[0]); + contextBuilder.addPropertyConverters(targetType, + MicroprofileAdapter.toPropertyConverter(converter)); + } + return this; + } + + @Override + public ConfigBuilder forClassLoader(ClassLoader loader) { + this.classLoader = loader; + return this; + } + + @Override + public ConfigBuilder withSources(ConfigSource... sources) { + for(ConfigSource source:sources){ + contextBuilder.addPropertySources(MicroprofileAdapter.toPropertySource(source)); + } + return this; + } + + @Override + public ConfigBuilder withConverters(Converter<?>... converters) { + for(Converter<?> converter:converters){ + TypeLiteral lit = TypeLiteral.of(converter.getClass()); + TypeLiteral target = TypeLiteral.of(lit.getType()); + contextBuilder.removePropertyConverters(target); + contextBuilder.addPropertyConverters( + target, + MicroprofileAdapter.toPropertyConverter(converter)); + } + return this; + } + + @Override + public Config build() { + return MicroprofileAdapter.toConfig(ConfigurationProvider.createConfiguration( + contextBuilder.build() + )); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderResolver.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderResolver.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderResolver.java new file mode 100644 index 0000000..92a928a --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigProviderResolver.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.spi.ConfigBuilder; +import org.eclipse.microprofile.config.spi.ConfigProviderResolver; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; + +/** + * Created by atsticks on 23.03.17. + */ +public class MicroprofileConfigProviderResolver extends ConfigProviderResolver { + + private Map<ClassLoader, Config> configs = new ConcurrentHashMap<>(); + + @Override + public Config getConfig() { + return getConfig(Thread.currentThread().getContextClassLoader()); + } + + @Override + public Config getConfig(ClassLoader loader) { + Config config = this.configs.get(loader); + if(config==null){ + ConfigurationContextBuilder builder = ConfigurationProvider.getConfigurationContextBuilder(); + builder.addDefaultPropertyConverters(); + MicroprofileConfigBuilder microConfigBuilder = new MicroprofileConfigBuilder(builder); + microConfigBuilder.addDefaultSources(); + microConfigBuilder.addDiscoveredSources(); + config = microConfigBuilder.build(); + this.configs.put(loader, config); + } + return config; + } + + @Override + public ConfigBuilder getBuilder() { + return new MicroprofileConfigBuilder(ConfigurationProvider.getConfigurationContextBuilder()); + } + + @Override + public void registerConfig(Config config, ClassLoader classLoader) { + if(configs.containsKey(classLoader)){ + Logger.getLogger(getClass().getName()) + .warning("Replacing existing config for classloader: " + classLoader); +// throw new IllegalArgumentException("Already a config registered with classloader: " + classLoader); + } + this.configs.put(classLoader, config); + } + + @Override + public void releaseConfig(Config config) { + for(Map.Entry<ClassLoader, Config> en: this.configs.entrySet()){ + if(en.getValue().equals(config)){ + this.configs.remove(en.getKey()); + return; + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigSource.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigSource.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigSource.java new file mode 100644 index 0000000..39ca119 --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigSource.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; +import org.eclipse.microprofile.config.spi.ConfigSource; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** + * Microprofile {@link ConfigSource} implementation that wraps a {@link PropertySource} instance. + */ +public class MicroprofileConfigSource implements ConfigSource{ + + private PropertySource delegate; + + public MicroprofileConfigSource(PropertySource propertySource){ + this.delegate = Objects.requireNonNull(propertySource); + } + + public PropertySource getPropertySource(){ + return this.delegate; + } + + @Override + public int getOrdinal() { + return delegate.getOrdinal(); + } + + @Override + public String getName() { + return delegate.getName(); + } + + @Override + public String getValue(String key) { + PropertyValue value = delegate.get(key); + if(value!=null){ + return value.getValue(); + } + return null; + } + + @Override + public Map<String, String> getProperties() { + return toMap(delegate.getProperties()); + } + + private Map<String, String> toMap(Map<String, PropertyValue> properties) { + Map<String, String> valueMap = new HashMap<>(properties.size()); + for(Map.Entry<String,PropertyValue> en:properties.entrySet()){ + if(en.getValue().getValue()!=null) { + valueMap.put(en.getKey(), en.getValue().getValue()); + } + } + return valueMap; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigSourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigSourceProvider.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigSourceProvider.java new file mode 100644 index 0000000..5176940 --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConfigSourceProvider.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertySourceProvider; +import org.apache.tamaya.spi.PropertyValue; +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.spi.ConfigSourceProvider; + +import java.util.*; + +/** + * Microprofile {@link ConfigSource} implementation that wraps a {@link PropertySource} instance. + */ +public class MicroprofileConfigSourceProvider implements ConfigSourceProvider{ + + private PropertySourceProvider delegate; + + public MicroprofileConfigSourceProvider(PropertySourceProvider propertySourceProvider){ + this.delegate = Objects.requireNonNull(propertySourceProvider); + } + + public PropertySourceProvider getPropertySourceProvider(){ + return this.delegate; + } + + + private Map<String, String> toMap(Map<String, PropertyValue> properties) { + Map<String, String> valueMap = new HashMap<>(properties.size()); + for(Map.Entry<String,PropertyValue> en:properties.entrySet()){ + if(en.getValue().getValue()!=null) { + valueMap.put(en.getKey(), en.getValue().getValue()); + } + } + return valueMap; + } + + @Override + public Iterable<ConfigSource> getConfigSources(ClassLoader forClassLoader) { + if(delegate instanceof TamayaPropertySourceProvider){ + return ((TamayaPropertySourceProvider)delegate).getConfigSourceProvider() + .getConfigSources(forClassLoader); + }else { + return MicroprofileAdapter.toConfigSources(delegate.getPropertySources()); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConverter.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConverter.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConverter.java new file mode 100644 index 0000000..cb6aab2 --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileConverter.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + + +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.spi.Converter; + +import java.util.Objects; + +/** + * Property source implementation that wraps a Microprofile {@link ConfigSource} instance. + */ +public class MicroprofileConverter<T> implements Converter<T> { + + private PropertyConverter<T> delegate; + + public MicroprofileConverter(PropertyConverter<T> delegate){ + this.delegate = Objects.requireNonNull(delegate); + } + + public PropertyConverter<T> getPropertyConverter(){ + return this.delegate; + } + + @Override + public T convert(String value) { + return delegate.convert(value, new ConversionContext.Builder("microprofile:no-key", TypeLiteral.of( + TypeLiteral.of(getClass()).getType())).build()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileDefaultProperties.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileDefaultProperties.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileDefaultProperties.java new file mode 100644 index 0000000..cfe06bd --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/MicroprofileDefaultProperties.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + +import org.apache.tamaya.spisupport.PropertiesResourcePropertySource; + + +/** + * Default property source for config properties in the classpath. + */ +public class MicroprofileDefaultProperties extends PropertiesResourcePropertySource{ + + public MicroprofileDefaultProperties() { + super("META-INF/microprofile-config.properties", null); + setDefaultOrdinal(100); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaConfiguration.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaConfiguration.java new file mode 100644 index 0000000..0f27821 --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaConfiguration.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + +import org.apache.tamaya.*; +import org.apache.tamaya.spi.ConfigurationContext; +import org.eclipse.microprofile.config.Config; + +import java.util.*; + +/** + * Created by atsticks on 23.03.17. + */ +public class TamayaConfiguration implements Configuration{ + + private Config delegate; + + public TamayaConfiguration(Config config){ + this.delegate = Objects.requireNonNull(config); + } + + public Config getConfig(){ + return delegate; + } + + @Override + public String get(String key) { + return this.delegate.getOptionalValue(key, String.class).orElse(null); + } + + @Override + public String getOrDefault(String key, String defaultValue) { + return this.delegate.getOptionalValue(key, String.class).orElse(defaultValue); + } + + @Override + public <T> T getOrDefault(String key, Class<T> type, T defaultValue) { + return this.delegate.getOptionalValue(key, type).orElse(defaultValue); + } + + @Override + public <T> T get(String key, Class<T> type) { + return this.delegate.getOptionalValue(key, type).orElseThrow( + () -> new NoSuchElementException("Missing key: " + key)); + } + + @Override + public <T> T get(String key, TypeLiteral<T> type) { + return this.delegate.getOptionalValue(key, type.getRawType()).orElseThrow( + () -> new NoSuchElementException("Missing key: " + key)); + } + + @Override + public <T> T getOrDefault(String key, TypeLiteral<T> type, T defaultValue) { + return null; + } + + @Override + public Map<String, String> getProperties() { + return null; + } + + @Override + public Configuration with(ConfigOperator operator) { + return operator.operate(this); + } + + @Override + public <T> T query(ConfigQuery<T> query) { + return query.query(this); + } + + @Override + public ConfigurationContext getContext() { + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertyConverter.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertyConverter.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertyConverter.java new file mode 100644 index 0000000..a83008a --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertyConverter.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.spi.Converter; + +import java.util.Objects; + +/** + * Property source implementation that wraps a Microprofile {@link ConfigSource} instance. + */ +public class TamayaPropertyConverter<T> implements PropertyConverter<T> { + + private Converter<T> delegate; + + public TamayaPropertyConverter(Converter<T> delegate){ + this.delegate = Objects.requireNonNull(delegate); + } + + public Converter<T> getConverter(){ + return this.delegate; + } + + @Override + public T convert(String value, ConversionContext context) { + return delegate.convert(value); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySource.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySource.java new file mode 100644 index 0000000..f526514 --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySource.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; +import org.eclipse.microprofile.config.spi.ConfigSource; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +/** + * Property source implementation that wraps a Microprofile {@link ConfigSource} instance. + */ +public class TamayaPropertySource implements PropertySource{ + + private ConfigSource delegate; + + public TamayaPropertySource(ConfigSource configSource){ + this.delegate = Objects.requireNonNull(configSource); + } + + public ConfigSource getConfigSource(){ + return this.delegate; + } + + @Override + public int getOrdinal() { + return delegate.getOrdinal(); + } + + @Override + public String getName() { + return Optional.ofNullable(delegate.getName()) + .orElse(delegate.toString()); + } + + @Override + public PropertyValue get(String key) { + return PropertyValue.of(key, delegate.getValue(key),getName()); + } + + @Override + public Map<String, PropertyValue> getProperties() { + return toValueMap(delegate.getProperties()); + } + + private Map<String, PropertyValue> toValueMap(Map<String, String> properties) { + Map<String, PropertyValue> valueMap = new HashMap<>(properties.size()); + for(Map.Entry<String,String> en:properties.entrySet()){ + valueMap.put(en.getKey(), PropertyValue.of(en.getKey(), en.getValue(), getName())); + } + return valueMap; + } + + @Override + public boolean isScannable() { + return true; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySourceProvider.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySourceProvider.java new file mode 100644 index 0000000..5b0bcf7 --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/TamayaPropertySourceProvider.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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; + + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertySourceProvider; +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.spi.ConfigSourceProvider; + +import java.util.*; + +/** + * Microprofile {@link ConfigSource} implementation that wraps a {@link PropertySource} instance. + */ +public class TamayaPropertySourceProvider implements PropertySourceProvider{ + + private ConfigSourceProvider delegate; + + public TamayaPropertySourceProvider(ConfigSourceProvider configSourceProvider){ + this.delegate = Objects.requireNonNull(configSourceProvider); + } + + public ConfigSourceProvider getConfigSourceProvider(){ + return this.delegate; + } + + + @Override + public Collection<PropertySource> getPropertySources() { + if(delegate instanceof MicroprofileConfigSourceProvider){ + return ((MicroprofileConfigSourceProvider)delegate).getPropertySourceProvider() + .getPropertySources(); + }else { + return MicroprofileAdapter.toPropertySources( + delegate.getConfigSources(Thread.currentThread().getContextClassLoader())); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/BridgingConfigBean.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/BridgingConfigBean.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/BridgingConfigBean.java new file mode 100644 index 0000000..a08f76f --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/BridgingConfigBean.java @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.cdi; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.InjectionPoint; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Objects; +import java.util.Set; + +/** + * Internally used conversion bean. + */ +final class BridgingConfigBean implements Bean<Object> { + + private final Bean<Object> delegate; + private final Set<Type> types; + + public BridgingConfigBean(final Bean delegate, final Set<Type> types) { + this.types = types; + this.delegate = Objects.requireNonNull(delegate); + } + + @Override + public Set<Type> getTypes() { + return types; + } + + @Override + public Class<?> getBeanClass() { + return delegate.getBeanClass(); + } + + @Override + public Set<InjectionPoint> getInjectionPoints() { + return delegate.getInjectionPoints(); + } + + @Override + public String getName() { + return delegate.getName(); + } + + @Override + public Set<Annotation> getQualifiers() { + return delegate.getQualifiers(); + } + + @Override + public Class<? extends Annotation> getScope() { + return delegate.getScope(); + } + + @Override + public Set<Class<? extends Annotation>> getStereotypes() { + return delegate.getStereotypes(); + } + + @Override + public boolean isAlternative() { + return delegate.isAlternative(); + } + + @Override + public boolean isNullable() { + return false; + // delegate.isNullable(); + } + + @Override + public Object create(CreationalContext<Object> creationalContext) { + return this.delegate.create(creationalContext); + } + + @Override + public void destroy(Object instance, CreationalContext<Object> creationalContext) { + delegate.destroy(instance, creationalContext); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredField.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredField.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredField.java new file mode 100644 index 0000000..29d7122 --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredField.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.cdi; + +import org.apache.tamaya.Configuration; + +import javax.enterprise.inject.spi.InjectionPoint; +import java.lang.reflect.Field; + +/** + * CDI implementation for event publishing of configured instances. + */ +public final class ConfiguredField { + + private final Field field; + private String key; + + ConfiguredField(InjectionPoint injectionPoint, String key){ + this.field = (Field)injectionPoint.getMember(); + this.key = key; + } + + public Class<?> getType() { + return field.getType(); + } + + public String getKey() { + return key; + } + + public Field getAnnotatedField() { + return field; + } + + public String getName() { + return field.getName(); + } + + public String getSignature() { + return getName()+':'+field.getType().getName(); + } + + public void configure(Object instance, Configuration config) { + throw new UnsupportedOperationException("Use CDI annotations for configuration injection."); + } + + @Override + public String toString() { + return "CDIConfiguredField["+getSignature()+']'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredMethod.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredMethod.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredMethod.java new file mode 100644 index 0000000..90204fe --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredMethod.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.cdi; + +import org.apache.tamaya.Configuration; + +import javax.enterprise.inject.spi.InjectionPoint; +import java.lang.reflect.Method; + +/** + * Implementation of a configured methods for CDI module. + */ +public final class ConfiguredMethod { + + private final Method method; + private String key; + + ConfiguredMethod(InjectionPoint injectionPoint, String key){ + this.method = (Method)injectionPoint.getMember(); + this.key = key; + } + + public String getKey() { + return key; + } + + public Class<?>[] getParameterTypes() { + return method.getParameterTypes(); + } + + public Method getAnnotatedMethod() { + return method; + } + + public String getName() { + return method.getName(); + } + + public String getSignature() { + return null; + } + + public void configure(Object instance, Configuration config) { + throw new UnsupportedOperationException("Use CDI annotations for configuration injection."); + } + + @Override + public String toString() { + return "CDIConfiguredMethod["+getSignature()+']'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredType.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredType.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredType.java new file mode 100644 index 0000000..535a556 --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/ConfiguredType.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.cdi; + +import org.apache.tamaya.Configuration; + +import javax.enterprise.inject.spi.InjectionPoint; +import java.lang.reflect.Field; +import java.lang.reflect.Member; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +/** + * Event published for items configured by CDI extensions. This is for example used by the documentation module + * to automatically track the configuration endpoints for documentation. + */ +public final class ConfiguredType { + + private final Class<?> type; + private final List<ConfiguredMethod> methods = new ArrayList<>(); + private final List<ConfiguredField> fields = new ArrayList<>(); + + public ConfiguredType(Class<?> type){ + this.type = Objects.requireNonNull(type); + } + + public Class getType() { + return type; + } + + public String getName() { + return type.getName(); + } + + public Collection<ConfiguredField> getConfiguredFields() { + return null; + } + + public Collection<ConfiguredMethod> getConfiguredMethods() { + return null; + } + + public void configure(Object instance, Configuration config) { + throw new UnsupportedOperationException("Use CDI annotations for configuration injection."); + } + + /** + * Used to build up during injection point processing. + * @param injectionPoint the CDI injection point, not null. + * @param key the possible config key, not null. + */ + void addConfiguredMember(InjectionPoint injectionPoint, String key) { + Member member = injectionPoint.getMember(); + if(member instanceof Field){ + this.fields.add(new ConfiguredField(injectionPoint, key)); + } else if(member instanceof Method){ + this.methods.add(new ConfiguredMethod(injectionPoint, key)); + } + } + + @Override + public String toString() { + return "CDIConfiguredType{" + + "type=" + type + + ", methods=" + methods + + ", fields=" + fields + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileCDIExtension.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileCDIExtension.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileCDIExtension.java new file mode 100644 index 0000000..0be929b --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileCDIExtension.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.cdi; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +import javax.enterprise.event.Observes; +import javax.enterprise.inject.Instance; +import javax.enterprise.inject.spi.AfterBeanDiscovery; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.Extension; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.enterprise.inject.spi.ProcessBean; +import javax.enterprise.inject.spi.ProcessProducerMethod; +import javax.inject.Provider; +import java.lang.reflect.AnnotatedType; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.HashSet; +import java.util.Set; +import java.util.logging.Logger; + + +/** + * CDI Extension module that adds injection mechanism for configuration. + * + * @see org.eclipse.microprofile.config.Config + * @see org.eclipse.microprofile.config.inject.ConfigProperty + */ +public class MicroprofileCDIExtension implements Extension { + + private static final Logger LOG = Logger.getLogger(MicroprofileCDIExtension.class.getName()); + + private final Set<Type> types = new HashSet<>(); + private Bean<?> convBean; + + /** + * Constructor for loading logging its load. + */ + public MicroprofileCDIExtension(){ + LOG.finest("Loading Tamaya Microprofile Support..."); + } + + /** + * Method that checks the configuration injection points during deployment for available configuration. + * @param pb the bean to process. + * @param beanManager the bean manager to notify about new injections. + */ + public void retrieveTypes(@Observes final ProcessBean<?> pb, BeanManager beanManager) { + + final Set<InjectionPoint> ips = pb.getBean().getInjectionPoints(); + ConfiguredType configuredType = new ConfiguredType(pb.getBean().getBeanClass()); + + boolean configured = false; + for (InjectionPoint injectionPoint : ips) { + if (injectionPoint.getAnnotated().isAnnotationPresent(ConfigProperty.class)) { + LOG.fine("Configuring: " + injectionPoint); + final ConfigProperty annotation = injectionPoint.getAnnotated().getAnnotation(ConfigProperty.class); + String key = !annotation.name().isEmpty()?annotation.name():MicroprofileConfigurationProducer.getDefaultKey(injectionPoint); + configuredType.addConfiguredMember(injectionPoint, key); + Type originalType = injectionPoint.getType(); + Type convertedType = unwrapType(originalType); + types.add(convertedType); + configured = true; + LOG.finest(() -> "Enabling Tamaya Microprofile Configuration on bean: " + configuredType.getName()); + }else if(injectionPoint.getMember() instanceof Method){ + Method method = (Method)injectionPoint.getMember(); + for(AnnotatedType paramType: method.getAnnotatedParameterTypes()){ + if(paramType.isAnnotationPresent(ConfigProperty.class)) { + LOG.fine("Configuring method: " + injectionPoint); + final ConfigProperty annotation = paramType.getAnnotation(ConfigProperty.class); + String key = !annotation.name().isEmpty() ? annotation.name() : MicroprofileConfigurationProducer.getDefaultKey(injectionPoint); + configuredType.addConfiguredMember(injectionPoint, key); + Type originalType = paramType.getType(); + Type convertedType = unwrapType(originalType); + types.add(convertedType); + configured = true; + LOG.finest(() -> "Enabling Tamaya Microprofile Configuration on bean: " + configuredType.getName()); + } + } + } + } + if(configured) { + beanManager.fireEvent(configuredType); + } + } + + + public void captureConvertBean(@Observes final ProcessProducerMethod<?, ?> ppm) { + if (ppm.getAnnotated().isAnnotationPresent(ConfigProperty.class)) { + convBean = ppm.getBean(); + } + } + + public void addConverter(@Observes final AfterBeanDiscovery abd, final BeanManager bm) { + if(!types.isEmpty() && convBean!=null) { + abd.addBean(new BridgingConfigBean(convBean, types)); + } + } + + private Type unwrapType(Type type) { + if(type instanceof ParameterizedType) { + Type rawType = ((ParameterizedType) type).getRawType(); + if(rawType == Provider.class || rawType == Instance.class) { + return ((ParameterizedType) type).getActualTypeArguments()[0]; + } + } + return type; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/a1cd433a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java ---------------------------------------------------------------------- diff --git a/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java new file mode 100644 index 0000000..bae7287 --- /dev/null +++ b/modules/microprofile/src/main/java/org/apache/tamaya/microprofile/cdi/MicroprofileConfigurationProducer.java @@ -0,0 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.cdi; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.eclipse.microprofile.config.spi.ConfigBuilder; +import org.eclipse.microprofile.config.spi.ConfigProviderResolver; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Produces; +import javax.enterprise.inject.spi.Annotated; +import javax.enterprise.inject.spi.AnnotatedField; +import javax.enterprise.inject.spi.AnnotatedType; +import javax.enterprise.inject.spi.InjectionPoint; +import javax.inject.Provider; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.List; +import java.util.Optional; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Producer bean for configuration properties. + */ +@ApplicationScoped +public class MicroprofileConfigurationProducer { + + private static final Logger LOGGER = Logger.getLogger(MicroprofileConfigurationProducer.class.getName()); + + @Produces + @ConfigProperty + public Object resolveAndConvert(final InjectionPoint injectionPoint) { + LOGGER.finest( () -> "Inject: " + injectionPoint); + final ConfigProperty annotation = injectionPoint.getAnnotated().getAnnotation(ConfigProperty.class); + String key = annotation.name(); + if(key.isEmpty()){ + key = getDefaultKey(injectionPoint); + } + + // unless the extension is not installed, this should never happen because the extension + // enforces the resolvability of the config + + String defaultTextValue = annotation.defaultValue().equals(ConfigProperty.UNCONFIGURED_VALUE) ? null : annotation.defaultValue(); + ConversionContext conversionContext = createConversionContext(key, injectionPoint); + 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())); + } + LOGGER.finest(String.format("Injecting %s for key %s in class %s", key, value.toString(), injectionPoint.toString())); + return value; + } + + static String getDefaultKey(InjectionPoint injectionPoint) { + Class declaringType = injectionPoint.getMember().getDeclaringClass(); + return declaringType.getCanonicalName() + "." + injectionPoint.getMember().getName(); + } + + static ConversionContext createConversionContext(String key, InjectionPoint injectionPoint) { + final Type targetType = injectionPoint.getAnnotated().getBaseType(); + Configuration config = ConfigurationProvider.getConfiguration(); + ConversionContext.Builder builder = new ConversionContext.Builder(config, + ConfigurationProvider.getConfiguration().getContext(), key, TypeLiteral.of(targetType)); + if(targetType instanceof ParameterizedType){ + ParameterizedType pt = (ParameterizedType)targetType; + if(pt.getRawType().equals(Provider.class)) { + builder = new ConversionContext.Builder(config, + ConfigurationProvider.getConfiguration().getContext(), key, + TypeLiteral.of(pt.getActualTypeArguments()[0])); + } + } + if (injectionPoint.getMember() instanceof AnnotatedElement) { + AnnotatedElement annotated = (AnnotatedElement)injectionPoint.getMember(); + if(annotated.isAnnotationPresent(ConfigProperty.class)) { + builder.setAnnotatedElement(annotated); + } + }else if(injectionPoint.getMember() instanceof Method){ + Method method = (Method)injectionPoint.getMember(); + for(Type type:method.getParameterTypes()){ + if(type instanceof AnnotatedElement){ + AnnotatedElement annotated = (AnnotatedElement)type; + if(annotated.isAnnotationPresent(ConfigProperty.class)) { + builder.setAnnotatedElement(annotated); + } + } + } + } + return builder.build(); + } + + static Object resolveValue(String defaultTextValue, ConversionContext context, InjectionPoint injectionPoint) { + Config config = ConfigProviderResolver.instance().getConfig(); + String textValue = config.getOptionalValue(context.getKey(), String.class).orElse(defaultTextValue); + if(String.class.equals(context.getTargetType().getRawType())){ + return textValue; + } + Object value = null; + if (textValue != null || Optional.class.equals(context.getTargetType().getRawType())) { + LOGGER.log(Level.FINEST, () -> "Converting KEY: " + context.getKey() + "("+context.getTargetType()+"), textValue: " + textValue); + List<PropertyConverter> converters = ConfigurationProvider.getConfiguration().getContext() + .getPropertyConverters((TypeLiteral)context.getTargetType()); + for (PropertyConverter<Object> converter : converters) { + try { + value = converter.convert(textValue, context); + if (value != null) { + LOGGER.log(Level.FINEST, "Parsed default value from '" + textValue + "' into " + + injectionPoint); + break; + } + } catch (Exception e) { + LOGGER.log(Level.FINEST, "Failed to convert value '" + textValue + "' for " + + injectionPoint, e); + } + } + } + return value; + } + + @Produces + public Config getConfiguration(){ + return ConfigProvider.getConfig(); + } + + @Produces + public ConfigBuilder getConfigBuilder(){ + return ConfigProviderResolver.instance().getBuilder(); + } + + +}
