TAMAYA-320 Updated implementation for latest state of JSR 382.
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/8ad95aa9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/8ad95aa9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/8ad95aa9 Branch: refs/heads/master Commit: 8ad95aa928530de4a52e87a6b6f53aaa08aaca30 Parents: e0d15cb Author: Anatole Tresch <[email protected]> Authored: Sun Nov 18 22:18:44 2018 +0100 Committer: Anatole Tresch <[email protected]> Committed: Sun Nov 18 22:18:44 2018 +0100 ---------------------------------------------------------------------- configjsr/README.md | 11 +- configjsr/bnd.bnd | 4 +- configjsr/pom.xml | 30 ++- .../org/apache/tamaya/jsr382/JavaConfig.java | 100 ------- .../apache/tamaya/jsr382/JavaConfigAdapter.java | 187 +++++--------- .../tamaya/jsr382/JavaConfigAdapterFactory.java | 227 ++++++++++++++++ .../apache/tamaya/jsr382/JavaConfigBuilder.java | 136 ---------- .../tamaya/jsr382/JavaConfigBuilderAdapter.java | 161 ++++++++++++ .../tamaya/jsr382/JavaConfigConverter.java | 49 ---- .../jsr382/JavaConfigConverterAdapter.java | 64 +++++ .../jsr382/JavaConfigDefaultProperties.java | 33 --- ...vaConfigDefaultPropertiesPropertySource.java | 38 +++ .../jsr382/JavaConfigProviderResolver.java | 82 ------ .../apache/tamaya/jsr382/JavaConfigSource.java | 33 +++ .../tamaya/jsr382/JavaConfigSourceProvider.java | 13 +- .../jsr382/PriorizedPropertyConverter.java | 60 +++++ .../tamaya/jsr382/TamayaConfigAccessor.java | 258 +++++++++++++++++++ .../jsr382/TamayaConfigProviderResolver.java | 91 +++++++ .../tamaya/jsr382/TamayaConfigSnapshot.java | 57 ++++ .../tamaya/jsr382/TamayaConfiguration.java | 93 ------- .../jsr382/TamayaConfigurationAdapter.java | 110 ++++++++ .../tamaya/jsr382/TamayaPropertyConverter.java | 47 ---- .../jsr382/TamayaPropertyConverterAdapter.java | 62 +++++ .../tamaya/jsr382/TamayaPropertySource.java | 78 ------ .../jsr382/TamayaPropertySourceAdapter.java | 88 +++++++ .../jsr382/TamayaPropertySourceProvider.java | 55 ---- .../TamayaPropertySourceProviderAdapter.java | 69 +++++ .../cdi/JavaConfigConfigurationProducer.java | 7 +- .../jsr382/converter/ProviderConverter.java | 2 +- .../javax.config.spi.ConfigProviderResolver | 2 +- .../org.apache.tamaya.spi.PropertySource | 2 +- .../tamaya/jsr382/JavaConfigAdapterTest.java | 55 ++-- .../jsr382/JavaConfigConfigProviderTest.java | 10 +- .../tamaya/jsr382/JavaConfigConfigTest.java | 14 +- .../tamaya/jsr382/JavaConfigConverterTest.java | 2 +- .../jsr382/JavaConfigDefaultPropertiesTest.java | 2 +- .../org/apache/tamaya/jsr382/SmokeExamples.java | 147 +++++++++++ .../tamaya/jsr382/TamayaPropertySourceTest.java | 4 +- .../tck/TamayaConfigArchiveProcessor.java | 8 +- pom.xml | 1 + 40 files changed, 1630 insertions(+), 862 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/README.md ---------------------------------------------------------------------- diff --git a/configjsr/README.md b/configjsr/README.md index 8398ed5..8b825f3 100644 --- a/configjsr/README.md +++ b/configjsr/README.md @@ -1,10 +1,7 @@ -# Sandbox implementation of the JSR 382 API +# Apache Tamaya - JSR 382 API Implementation -As long as the API is not properly published to an official Maven repository you need to checkout the sourcecode from: +This module implements the current JSR 382 Configuration API. It will be +released as a final extension, when the JSR published a stable +release to build against. -https://github.com/eclipse/ConfigJSR - -Once -https://github.com/eclipse/ConfigJSR/issues/1 -is done we may be able to properly add this module to our builds. http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/bnd.bnd ---------------------------------------------------------------------- diff --git a/configjsr/bnd.bnd b/configjsr/bnd.bnd index 4ebd38a..45a48a9 100644 --- a/configjsr/bnd.bnd +++ b/configjsr/bnd.bnd @@ -11,9 +11,9 @@ javac.target: 1.8 Automatic-Module-Name: org.apache.tamaya.jsr382 Bundle-Version: ${version}.${tstamp} -Bundle-Name: Apache Tamaya - JSR 382 +Bundle-Name: Apache Tamaya - JSR 382 Support Bundle-SymbolicName: org.apache.tamaya.jsr382 -Bundle-Description: Apacha Tamaya Config - JSR 382 Implementation +Bundle-Description: Apacha Tamaya Config - JSR 382 API Implementation Bundle-Category: Implementation Bundle-Copyright: (C) Apache Foundation Bundle-License: Apache Licence version 2 http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/pom.xml ---------------------------------------------------------------------- diff --git a/configjsr/pom.xml b/configjsr/pom.xml index 0c2feb5..fb52cc9 100644 --- a/configjsr/pom.xml +++ b/configjsr/pom.xml @@ -37,7 +37,6 @@ under the License. <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> @@ -61,33 +60,40 @@ under the License. <dependency> <groupId>org.apache.tamaya</groupId> <artifactId>tamaya-core</artifactId> - <version>${tamaya-version}</version> + <version>${project.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tamaya</groupId> <artifactId>tamaya-api</artifactId> - <version>${tamaya-version}</version> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.tamaya.ext</groupId> + <artifactId>tamaya-injection-api</artifactId> + <version>${project.version}</version> + <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tamaya.ext</groupId> <artifactId>tamaya-functions</artifactId> - <version>${tamaya-version}</version> + <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.tamaya.ext</groupId> <artifactId>tamaya-events</artifactId> - <version>${tamaya-version}</version> + <version>${project.version}</version> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>${assertj.version}</version> </dependency> + <!-- Config API --> <dependency> - <groupId>javax.config</groupId> - <artifactId>javaconfig-api</artifactId> - <version>${jsr.version}</version> + <groupId>com.github.eclipse</groupId> + <artifactId>ConfigJSR</artifactId> + <version>bdab2f22923ba83e93a841f61e177c95796cfb68</version> </dependency> <dependency> <groupId>org.mockito</groupId> @@ -165,4 +171,12 @@ under the License. </profile> </profiles> + <!-- Used to resolve the latest Config JSR API release. --> + <repositories> + <repository> + <id>jitpack.io</id> + <url>https://jitpack.io</url> + </repository> + </repositories> + </project> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfig.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfig.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfig.java deleted file mode 100644 index a53dfc2..0000000 --- a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfig.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.jsr382; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.Configuration; -import org.apache.tamaya.events.FrozenConfiguration; -import org.apache.tamaya.spi.PropertySource; - -import javax.config.Config; -import javax.config.spi.ConfigSource; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; -import java.util.*; - -/** - * JavaConfig {@link javax.config.spi.ConfigSource} implementation that wraps a {@link PropertySource} instance. - */ -public class JavaConfig implements Config, Serializable { - - private Configuration delegate; - - public JavaConfig(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 JavaConfigAdapter.toConfigSources(delegate.getContext().getPropertySources()); - } - - @Override - public String toString() { - return "JavaConfigConfig{" + - "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-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapter.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapter.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapter.java index 6641dc0..ed0f283 100644 --- a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapter.java +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapter.java @@ -18,165 +18,102 @@ */ package org.apache.tamaya.jsr382; - -import org.apache.tamaya.*; -import org.apache.tamaya.spi.*; +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.events.FrozenConfiguration; import javax.config.Config; -import javax.config.spi.ConfigBuilder; +import javax.config.ConfigAccessor; +import javax.config.ConfigSnapshot; import javax.config.spi.ConfigSource; -import javax.config.spi.Converter; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.util.*; /** - * Utility class for adapting JavaConfig artifacts into Tamaya artifacts and vice versa. + * JavaConfig {@link javax.config.Config} implementation that wraps a Tamaya {@link Configuration} instance. */ -public final class JavaConfigAdapter{ +public class JavaConfigAdapter implements Config, Serializable { - /** - * Singleton constructor. - */ - private JavaConfigAdapter(){} + private Configuration delegate; /** - * Converts a Tamaya {@link Configuration} into a JavaConfig.io {@link javax.config.Config}. - * @param config the Tamaya {@link Configuration} instance, not null. - * @return the corresponding JavaConfig.io {@link Config} instance, never null. + * Creates a new JSR configuration instance based on the given Tamaya configuration. + * @param delegate the configuration, not null.s */ - public static Config toConfig(Configuration config){ - if(config instanceof TamayaConfiguration){ - return ((TamayaConfiguration)config).getConfig(); - } - return new JavaConfig(config); + public JavaConfigAdapter(Configuration delegate){ + this.delegate = Objects.requireNonNull(delegate); } /** - * Converts a JavaConfig {@link Config}s into Tamaya {@link Configuration}s. - * @param config the JavaConfig {@link Config} instance, not null. - * @return the corresponding Tamaya {@link Configuration} instance, never null. + * Access the current configuration delegate. + * @return the Tamaya configuration delegate, never null. */ - public static Configuration toConfiguration(Config config){ - if(config instanceof JavaConfig){ - return ((JavaConfig)config).getConfiguration(); - } - return new TamayaConfiguration(config); + public Configuration getConfiguration(){ + return this.delegate; } - /** - * Converts a Tamaya {@link PropertySource}s into a JavaConfig.io {@link ConfigSource}. - * @param propertySources the Tamaya {@link PropertySource} instances, not null. - * @return the corresponding JavaConfig.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 JavaConfig {@link ConfigSource}s into Tamaya {@link PropertySource}s. - * @param configSources the JavaConfig {@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)); + @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()); + } } - return propertySources; + if(value == null){ + throw new NoSuchElementException("No such config property: " + propertyName); + } + return value; } - /** - * Converts a Tamaya {@link PropertySource} into a JavaConfig.io {@link ConfigSource}. - * @param propertySource the Tamaya {@link PropertySource} instance, not null. - * @return the corresponding JavaConfig.io {@link ConfigSource} instance, never null. - */ - public static ConfigSource toConfigSource(PropertySource propertySource) { - if(propertySource instanceof TamayaPropertySource){ - return ((TamayaPropertySource)propertySource).getConfigSource(); - } - return new JavaConfigSource(propertySource); + @Override + public <T> Optional<T> getOptionalValue(String propertyName, Class<T> propertyType) { + return delegate.getOptional(propertyName, propertyType); } - /** - * Converts a JavaConfig {@link ConfigSource} into a Tamaya {@link PropertySource}. - * @param configSource the JavaConfig {@link ConfigSource} instance, not null. - * @return the corresponding Tamaya {@link PropertySource} instance, never null. - */ - public static PropertySource toPropertySource(ConfigSource configSource) { - if(configSource instanceof JavaConfigSource){ - return ((JavaConfigSource)configSource).getPropertySource(); - } - return new TamayaPropertySource(configSource); + @Override + public ConfigAccessor<String> access(String name) { + return new TamayaConfigAccessor(this, name); } - /** - * Converts a JavaConfig {@link Converter} into a Tamaya {@link PropertyConverter}. - * @param converter the JavaConfig {@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 JavaConfigConverter){ - return ((JavaConfigConverter)converter).getPropertyConverter(); - } - return new TamayaPropertyConverter(converter); + @Override + public ConfigSnapshot snapshotFor(ConfigAccessor<?>... configAccessors) { + return new TamayaConfigSnapshot(this.delegate, configAccessors); } - /** - * Converts a Tamaya {@link PropertyConverter} into a JavaConfig.io {@link Converter}. - * @param converter the Tamaya {@link PropertyConverter} instance, not null. - * @param <T> the target type - * @return the corresponding JavaConfig.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 JavaConfigConverter(converter); + @Override + public Iterable<String> getPropertyNames() { + return delegate.getProperties().keySet(); } - /** - * Converts a Tamaya {@link ConfigurationContextBuilder} into a JavaConfig.io {@link ConfigBuilder}. - * @param builder the Tamaya {@link ConfigurationContextBuilder} instance, not null. - * @return the corresponding JavaConfig.io {@link ConfigBuilder} instance, never null. - */ - public static ConfigBuilder toConfigBuilder(ConfigurationBuilder builder) { - return new JavaConfigBuilder(builder); + @Override + public Iterable<ConfigSource> getConfigSources() { + return JavaConfigAdapterFactory.toConfigSources(delegate.getContext().getPropertySources()); } - /** - * 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()); - } + private void writeObject(ObjectOutputStream out) throws IOException{ + if(!(this.delegate instanceof Serializable)){ + out.writeObject(this.delegate.getSnapshot()); + }else { + out.writeObject(this.delegate); } - 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; + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{ + this.delegate = (Configuration)in.readObject(); + } + + @Override + public String toString() { + return "Tamaya Config{" + + "delegate=" + delegate + + '}'; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapterFactory.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapterFactory.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapterFactory.java new file mode 100644 index 0000000..b64e7dd --- /dev/null +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigAdapterFactory.java @@ -0,0 +1,227 @@ +/* + * 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.jsr382; + + +import org.apache.tamaya.*; +import org.apache.tamaya.spi.*; +import org.apache.tamaya.spisupport.DefaultConfigurationContext; +import org.apache.tamaya.spisupport.DefaultMetaDataProvider; +import org.apache.tamaya.spisupport.MetadataProvider; + +import javax.config.Config; +import javax.config.spi.ConfigBuilder; +import javax.config.spi.ConfigSource; +import javax.config.spi.Converter; +import java.util.*; + +/** + * Utility class for adapting JavaConfig artifacts into Tamaya artifacts and vice versa. + */ +public final class JavaConfigAdapterFactory { + + /** + * Default converter priority as defined by Config JSR. + */ + private static final int DEFAULT_CONVERTER_PRIORITY = 100; + + /** + * Singleton constructor. + */ + private JavaConfigAdapterFactory(){} + + /** + * Converts a Tamaya {@link Configuration} into a JavaConfig.io {@link javax.config.Config}. + * @param config the Tamaya {@link Configuration} instance, not null. + * @return the corresponding JavaConfig.io {@link Config} instance, never null. + */ + public static Config toConfig(Configuration config){ + if(config instanceof TamayaConfigurationAdapter){ + return ((TamayaConfigurationAdapter)config).getConfig(); + } + return new JavaConfigAdapter(config); + } + + /** + * Converts a JavaConfig {@link Config}s into Tamaya {@link Configuration}s. + * @param config the JavaConfig {@link Config} instance, not null. + * @return the corresponding Tamaya {@link Configuration} instance, never null. + */ + public static Configuration toConfiguration(Config config){ + if(config instanceof JavaConfigAdapter){ + return ((JavaConfigAdapter)config).getConfiguration(); + } + return new TamayaConfigurationAdapter(config); + } + + /** + * Converts a JavaConfig {@link Config}s into Tamaya {@link ConfigurationContext}s. + * @param config the JavaConfig {@link Config} instance, not null. + * @return the corresponding Tamaya {@link ConfigurationContext} instance, never null. + */ + public static ConfigurationContext toConfigurationContext(Config config) { + ServiceContext serviceContext = ServiceContextManager.getServiceContext(); + List<PropertySource> sources = new ArrayList<>(); + for (ConfigSource cs : config.getConfigSources()) { + sources.add(toPropertySource(cs)); + } + Map<TypeLiteral<?>, List<PropertyConverter<?>>> converters = new HashMap<>(); + for (PropertyConverter<?> conv : serviceContext.getServices(PropertyConverter.class)){ + converters.computeIfAbsent(TypeLiteral.of( + TypeLiteral.getGenericInterfaceTypeParameters(conv.getClass(), PropertyConverter.class)[0]), + (k) -> new ArrayList<>()).add(conv); + } + return new DefaultConfigurationContext( + ServiceContextManager.getServiceContext(), + Collections.emptyList(), + sources, + converters, + serviceContext.getService(MetadataProvider.class, () -> new DefaultMetaDataProvider())); + + } + + /** + * Converts a Tamaya {@link PropertySource}s into a JavaConfig.io {@link ConfigSource}. + * @param propertySources the Tamaya {@link PropertySource} instances, not null. + * @return the corresponding JavaConfig.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 JavaConfig {@link ConfigSource}s into Tamaya {@link PropertySource}s. + * @param configSources the JavaConfig {@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 JavaConfig.io {@link ConfigSource}. + * @param propertySource the Tamaya {@link PropertySource} instance, not null. + * @return the corresponding JavaConfig.io {@link ConfigSource} instance, never null. + */ + public static ConfigSource toConfigSource(PropertySource propertySource) { + if(propertySource instanceof TamayaPropertySourceAdapter){ + return ((TamayaPropertySourceAdapter)propertySource).getConfigSource(); + } + return new JavaConfigSource(propertySource); + } + + /** + * Converts a JavaConfig {@link ConfigSource} into a Tamaya {@link PropertySource}. + * @param configSource the JavaConfig {@link ConfigSource} instance, not null. + * @return the corresponding Tamaya {@link PropertySource} instance, never null. + */ + public static PropertySource toPropertySource(ConfigSource configSource) { + if(configSource instanceof JavaConfigSource){ + return ((JavaConfigSource)configSource).getPropertySource(); + } + return new TamayaPropertySourceAdapter(configSource); + } + + /** + * Converts a JavaConfig {@link Converter} into a Tamaya {@link PropertyConverter}. + * @param converter the JavaConfig {@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, int priority) { + if(converter instanceof JavaConfigConverterAdapter){ + return PriorizedPropertyConverter.of(((JavaConfigConverterAdapter)converter).getPropertyConverter(), priority); + } + return new TamayaPropertyConverterAdapter(converter); + } + + /** + * Converts a JavaConfig {@link Converter} into a Tamaya {@link PropertyConverter} using default priority of + * {@code 100}. + * @param converter the JavaConfig {@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) { + return toPropertyConverter(converter, DEFAULT_CONVERTER_PRIORITY); + } + + /** + * Converts a Tamaya {@link PropertyConverter} into a JavaConfig.io {@link Converter}. + * @param converter the Tamaya {@link PropertyConverter} instance, not null. + * @param <T> the target type + * @return the corresponding JavaConfig.io {@link Converter} instance, never null. + */ + public static <T> Converter<T> toConverter(PropertyConverter<T> converter) { + if(converter instanceof TamayaPropertyConverterAdapter){ + return ((TamayaPropertyConverterAdapter)converter).getConverter(); + } + return new JavaConfigConverterAdapter(converter); + } + + /** + * Converts a Tamaya {@link ConfigurationBuilder} into a JavaConfig.io {@link ConfigBuilder}. + * @param builder the Tamaya {@link ConfigurationBuilder} instance, not null. + * @return the corresponding JavaConfig.io {@link ConfigBuilder} instance, never null. + */ + public static ConfigBuilder toConfigBuilder(ConfigurationBuilder builder) { + return new JavaConfigBuilderAdapter(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-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigBuilder.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigBuilder.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigBuilder.java deleted file mode 100644 index f623d01..0000000 --- a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigBuilder.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * 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.jsr382; - -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.*; -import org.apache.tamaya.spisupport.PropertySourceComparator; -import org.apache.tamaya.spisupport.propertysource.EnvironmentPropertySource; -import org.apache.tamaya.spisupport.propertysource.SystemPropertySource; - -import javax.config.Config; -import javax.config.spi.ConfigBuilder; -import javax.config.spi.ConfigSource; -import javax.config.spi.ConfigSourceProvider; -import javax.config.spi.Converter; -import java.util.Objects; - -/** - * Created by atsticks on 23.03.17. - */ -final class JavaConfigBuilder implements ConfigBuilder{ - - private ConfigurationBuilder configBuilder; - private ClassLoader classLoader; - - JavaConfigBuilder(ConfigurationBuilder contextBuilder){ - this.configBuilder = Objects.requireNonNull(contextBuilder); - contextBuilder.addDefaultPropertyConverters(); - } - - public ConfigurationBuilder getConfigurationBuilder(){ - return configBuilder; - } - - /** - * Add the default config sources appearing on the builder's classpath - * including: - * <ol> - * <li>System properties</li> - * <li>Environment properties</li> - * <li>/META-INF/JavaConfig-config.properties</li> - * </ol> - * - * @return the ConfigBuilder with the default config sources - */ - @Override - public ConfigBuilder addDefaultSources() { - configBuilder.addPropertySources( - new SystemPropertySource(400), - new EnvironmentPropertySource(300), - new JavaConfigDefaultProperties()); - configBuilder.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)){ - configBuilder.addPropertySources(JavaConfigAdapter.toPropertySource(configSource)); - } - for(ConfigSourceProvider configSourceProvider: ServiceContextManager.getServiceContext().getServices(ConfigSourceProvider.class)){ - configBuilder.addPropertySources(JavaConfigAdapter.toPropertySources(configSourceProvider.getConfigSources( - Thread.currentThread().getContextClassLoader() - ))); - } - configBuilder.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]); - configBuilder.addPropertyConverters(targetType, - JavaConfigAdapter.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){ - configBuilder.addPropertySources(JavaConfigAdapter.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()); - configBuilder.removePropertyConverters(target); - configBuilder.addPropertyConverters( - target, - JavaConfigAdapter.toPropertyConverter(converter)); - } - return this; - } - - @Override - public Config build() { - return JavaConfigAdapter.toConfig(configBuilder.build()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigBuilderAdapter.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigBuilderAdapter.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigBuilderAdapter.java new file mode 100644 index 0000000..f7d0ceb --- /dev/null +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigBuilderAdapter.java @@ -0,0 +1,161 @@ +/* + * 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.jsr382; + +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.*; +import org.apache.tamaya.spisupport.PropertySourceComparator; +import org.apache.tamaya.spisupport.propertysource.EnvironmentPropertySource; +import org.apache.tamaya.spisupport.propertysource.SystemPropertySource; + +import javax.config.Config; +import javax.config.spi.ConfigBuilder; +import javax.config.spi.ConfigSource; +import javax.config.spi.ConfigSourceProvider; +import javax.config.spi.Converter; +import java.util.Objects; + +/** + * Created by atsticks on 23.03.17. + */ +final class JavaConfigBuilderAdapter implements ConfigBuilder{ + + private ConfigurationBuilder configBuilder; + + /** + * Create a new ConfigBuilder using the given Tamaya config builder. + * @param configBuilder + */ + JavaConfigBuilderAdapter(ConfigurationBuilder configBuilder){ + this.configBuilder = Objects.requireNonNull(configBuilder); + configBuilder.addDefaultPropertyConverters(); + } + + /** + * Access the underlying Tamaya {@link ConfigurationBuilder}. + * @return the Tamaya builder, not null. + */ + public ConfigurationBuilder getConfigurationBuilder(){ + return configBuilder; + } + + /** + * Add the default config sources appearing on the builder's classpath + * including: + * <ol> + * <li>System properties</li> + * <li>Environment properties</li> + * <li>/META-INF/JavaConfig-config.properties</li> + * </ol> + * + * @return the ConfigBuilder with the default config sources + */ + @Override + public ConfigBuilder addDefaultSources() { + configBuilder.addPropertySources( + new SystemPropertySource(400), + new EnvironmentPropertySource(300), + new JavaConfigDefaultPropertiesPropertySource()); + configBuilder.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)){ + configBuilder.addPropertySources(JavaConfigAdapterFactory.toPropertySource(configSource)); + } + for(ConfigSourceProvider configSourceProvider: ServiceContextManager.getServiceContext().getServices(ConfigSourceProvider.class)){ + configBuilder.addPropertySources(JavaConfigAdapterFactory.toPropertySources(configSourceProvider.getConfigSources( + Thread.currentThread().getContextClassLoader() + ))); + } + configBuilder.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]); + configBuilder.addPropertyConverters(targetType, + JavaConfigAdapterFactory.toPropertyConverter(converter)); + } + return this; + } + + @Override + public ConfigBuilder forClassLoader(ClassLoader classLoader) { + this.configBuilder.setClassLoader(classLoader); + return this; + } + + @Override + public ConfigBuilder withSources(ConfigSource... sources) { + for(ConfigSource source:sources){ + configBuilder.addPropertySources(JavaConfigAdapterFactory.toPropertySource(source)); + } + return this; + } + + @Override + public <T> ConfigBuilder withConverter(Class<T> aClass, int priority, Converter<T> converter) { + TypeLiteral lit = TypeLiteral.of(aClass); + TypeLiteral target = TypeLiteral.of(aClass); + configBuilder.removePropertyConverters(target); + configBuilder.addPropertyConverters( + target, + JavaConfigAdapterFactory.toPropertyConverter(converter, priority)); + return this; + } + + @Override + public ConfigBuilder withConverters(Converter<?>... converters) { + for(Converter<?> converter:converters){ + TypeLiteral lit = TypeLiteral.of(converter.getClass()); + TypeLiteral target = TypeLiteral.of(lit.getType()); + configBuilder.removePropertyConverters(target); + configBuilder.addPropertyConverters( + target, + JavaConfigAdapterFactory.toPropertyConverter(converter)); + } + return this; + } + + @Override + public Config build() { + return JavaConfigAdapterFactory.toConfig(configBuilder.build()); + } + + @Override + public String toString() { + return "JavaConfigBuilderAdapter{" + + "configBuilder=" + configBuilder + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigConverter.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigConverter.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigConverter.java deleted file mode 100644 index 0d33710..0000000 --- a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigConverter.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.jsr382; - - -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.ConversionContext; -import org.apache.tamaya.spi.PropertyConverter; - -import javax.config.spi.Converter; -import java.util.Objects; - -/** - * Property source implementation that wraps a Java config {@link javax.config.spi.ConfigSource} instance. - */ -public class JavaConfigConverter<T> implements Converter<T> { - - private PropertyConverter<T> delegate; - - public JavaConfigConverter(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("JavaConfig:no-key", TypeLiteral.of( - TypeLiteral.of(getClass()).getType())).build()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigConverterAdapter.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigConverterAdapter.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigConverterAdapter.java new file mode 100644 index 0000000..2c4f563 --- /dev/null +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigConverterAdapter.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.jsr382; + + +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import javax.config.spi.Converter; +import java.util.Objects; + +/** + * Tamaya converter implementation that wraps a Java config {@link javax.config.spi.Converter} instance. + */ +public class JavaConfigConverterAdapter<T> implements Converter<T> { + + private PropertyConverter<T> delegate; + + /** + * Creates a new Converter, baed on the given Tamaya {@link org.apache.tamaya.spi.PropertyConverter}. + * @param delegate the delegate, not null. + */ + public JavaConfigConverterAdapter(PropertyConverter<T> delegate){ + this.delegate = Objects.requireNonNull(delegate); + } + + /** + * Access the underlying Tamaya converter. + * @return the Tamaya converter, not null. + */ + public PropertyConverter<T> getPropertyConverter(){ + return this.delegate; + } + + @Override + public T convert(String value) { + return delegate.convert(value, new ConversionContext.Builder("JavaConfig:no-key", TypeLiteral.of( + TypeLiteral.of(getClass()).getType())).build()); + } + + @Override + public String toString() { + return "JavaConfigConverterAdapter{" + + "delegate=" + delegate + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigDefaultProperties.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigDefaultProperties.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigDefaultProperties.java deleted file mode 100644 index 314911d..0000000 --- a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigDefaultProperties.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.jsr382; - -import org.apache.tamaya.spisupport.propertysource.PropertiesResourcePropertySource; - - -/** - * Default property source for config properties in the classpath. - */ -public class JavaConfigDefaultProperties extends PropertiesResourcePropertySource { - - public JavaConfigDefaultProperties() { - super("META-INF/javaconfig.properties", null); - setDefaultOrdinal(100); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigDefaultPropertiesPropertySource.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigDefaultPropertiesPropertySource.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigDefaultPropertiesPropertySource.java new file mode 100644 index 0000000..0bf52f9 --- /dev/null +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigDefaultPropertiesPropertySource.java @@ -0,0 +1,38 @@ +/* + * 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.jsr382; + +import org.apache.tamaya.spisupport.propertysource.PropertiesResourcePropertySource; + + +/** + * Default property source for config properties in the classpath. + */ +public class JavaConfigDefaultPropertiesPropertySource extends PropertiesResourcePropertySource { + + public JavaConfigDefaultPropertiesPropertySource() { + super("META-INF/javaconfig.properties", null); + setDefaultOrdinal(100); + } + + @Override + public String toString() { + return "JavaConfigDefaultPropertiesPropertySource{}"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigProviderResolver.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigProviderResolver.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigProviderResolver.java deleted file mode 100644 index 88b1e0d..0000000 --- a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigProviderResolver.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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.jsr382; - -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.spi.ConfigurationBuilder; - -import javax.config.Config; -import javax.config.spi.ConfigBuilder; -import javax.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 JavaConfigProviderResolver 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){ - ConfigurationBuilder builder = ConfigurationProvider.getConfigurationBuilder(); - builder.addDefaultPropertyConverters(); - JavaConfigBuilder microConfigBuilder = new JavaConfigBuilder(builder); - microConfigBuilder.addDefaultSources(); - microConfigBuilder.addDiscoveredSources(); - config = microConfigBuilder.build(); - this.configs.put(loader, config); - } - return config; - } - - @Override - public ConfigBuilder getBuilder() { - return new JavaConfigBuilder(ConfigurationProvider.getConfigurationBuilder()); - } - - @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-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigSource.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigSource.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigSource.java index 16bf7a9..5af6f78 100644 --- a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigSource.java +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigSource.java @@ -25,6 +25,8 @@ import javax.config.spi.ConfigSource; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.Set; +import java.util.function.Consumer; /** * Javaconfig {@link ConfigSource} implementation that wraps a {@link PropertySource} instance. @@ -75,4 +77,35 @@ public class JavaConfigSource implements ConfigSource{ return valueMap; } + /** + * The callback should get invoked if an attribute change got detected inside the ConfigSource. + * + * @param callback will be set by the {@link javax.config.Config} after this + * {@code ConfigSource} got created and before any configured values + * get served. + * @return ChangeSupport informing the {@link javax.config.Config} implementation about support for changes by this source + * @see ChangeSupport + */ + // TODO implement change support in Tamaya + public ChangeSupport setOnAttributeChange(Consumer<Set<String>> callback) { + switch(delegate.getChangeSupport()){ + case SUPPORTED: + delegate.addChangeListener((s,ps) -> { + callback.accept(s); + }); + return ChangeSupport.SUPPORTED; + case IMMUTABLE: + return ChangeSupport.IMMUTABLE; + case UNSUPPORTED: + default: + return ChangeSupport.UNSUPPORTED; + } + } + + @Override + public String toString() { + return "JavaConfigSource{" + + "delegate=" + delegate + + '}'; + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigSourceProvider.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigSourceProvider.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigSourceProvider.java index 2b34eac..1efd4ba 100644 --- a/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigSourceProvider.java +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/JavaConfigSourceProvider.java @@ -54,11 +54,18 @@ public class JavaConfigSourceProvider implements ConfigSourceProvider{ @Override public Iterable<ConfigSource> getConfigSources(ClassLoader forClassLoader) { - if(delegate instanceof TamayaPropertySourceProvider){ - return ((TamayaPropertySourceProvider)delegate).getConfigSourceProvider() + if(delegate instanceof TamayaPropertySourceProviderAdapter){ + return ((TamayaPropertySourceProviderAdapter)delegate).getConfigSourceProvider() .getConfigSources(forClassLoader); }else { - return JavaConfigAdapter.toConfigSources(delegate.getPropertySources()); + return JavaConfigAdapterFactory.toConfigSources(delegate.getPropertySources()); } } + + @Override + public String toString() { + return "JavaConfigSourceProvider{" + + "delegate=" + delegate + + '}'; + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/PriorizedPropertyConverter.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/PriorizedPropertyConverter.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/PriorizedPropertyConverter.java new file mode 100644 index 0000000..b80f3d4 --- /dev/null +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/PriorizedPropertyConverter.java @@ -0,0 +1,60 @@ +/* + * 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.jsr382; + +import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.PropertyConverter; + +import java.util.Objects; + +final class PriorizedPropertyConverter<T> implements PropertyConverter<T> { + + private final PropertyConverter<T> delegate; + private int priority; + + public PriorizedPropertyConverter(PropertyConverter<T> propertyConverter, int priority) { + this.priority = priority; + this.delegate = Objects.requireNonNull(propertyConverter); + } + + public static <T> PropertyConverter<T> of(PropertyConverter<T> propertyConverter, int priority) { + if(propertyConverter instanceof PriorizedPropertyConverter){ + return ((PriorizedPropertyConverter)propertyConverter).setPriority(priority); + } + return new PriorizedPropertyConverter<>(propertyConverter, priority); + } + + private PropertyConverter<T> setPriority(int priority) { + this.priority = priority; + return this; + } + + @Override + public T convert(String value, ConversionContext context) { + return delegate.convert(value, context); + } + + @Override + public String toString() { + return "PriorizedPropertyConverter{" + + "delegate=" + delegate + + ", priority=" + priority + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigAccessor.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigAccessor.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigAccessor.java new file mode 100644 index 0000000..d243ece --- /dev/null +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigAccessor.java @@ -0,0 +1,258 @@ +/* + * 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.jsr382; + +import org.apache.tamaya.TypeLiteral; + +import javax.config.ConfigAccessor; +import javax.config.ConfigSnapshot; +import javax.config.spi.Converter; +import java.util.*; +import java.util.concurrent.TimeUnit; + +/** + * Tamaya implementation type for the {@link ConfigAccessor} type. + * @param <T> the target type. + */ +class TamayaConfigAccessor<T> implements ConfigAccessor<T> { + + private JavaConfigAdapter javaConfigAdapter; + + private String name; + private Converter<T> customConverter; + private TypeLiteral<T> targetType; + private T typedDefaultValue; + private String stringDefaultValue; + private List<ConfigAccessor<String>> fallbackAccessors = new ArrayList<>(); + private List<String> lookupSuffixes = new ArrayList<>(); + private boolean evaluateVariables = true; + private long timeout = 0; + private T cachedValue; + + + /** + * Constructor. + * @param javaConfigAdapter + * @param name + */ + TamayaConfigAccessor(JavaConfigAdapter javaConfigAdapter, String name) { + this.javaConfigAdapter = Objects.requireNonNull(javaConfigAdapter); + this.name = Objects.requireNonNull(name); + this.targetType = TypeLiteral.of(String.class); + } + + private TamayaConfigAccessor(TamayaConfigAccessor<?> accessor, TypeLiteral<T> target) { + this.name = accessor.name; + this.targetType = target; + this.stringDefaultValue = accessor.stringDefaultValue; + this.javaConfigAdapter = javaConfigAdapter; + this.evaluateVariables = accessor.evaluateVariables; + this.lookupSuffixes.addAll(accessor.lookupSuffixes); + // What to do if the fallback accessors do not match, e.g. with collection types... + this.fallbackAccessors.addAll(accessor.fallbackAccessors); + } + + /** + * Access the list of al current possible candidate keys to evaluate a value for the given accessor. + * @return the list of al current possible candidate keys, not null. + */ + public List<String> getCandidateKeys() { + List<String> keys = new ArrayList<>(); + List<List<String>> listList = new ArrayList<>(); + List<String> maxList = new ArrayList<>(lookupSuffixes); + while(!maxList.isEmpty()){ + listList.add(new ArrayList(maxList)); + maxList.remove(0); + } + for(List<String> list:listList) { + keys.addAll(getSuffixKeys(list)); + } + keys.add(getPropertyName()); + return keys; + } + + private List<String> getSuffixKeys(List<String> list) { + List<String> result = new ArrayList<>(); + while(!list.isEmpty()){ + result.add(getPropertyName()+'.'+ String.join(".", list)); + if(list.size()>1) { + list.remove(list.size() - 2); + }else{ + list.remove(0); + } + } + return result; + } + + @Override + public <N> ConfigAccessor<N> as(Class<N> aClass) { + return new TamayaConfigAccessor<N>(this, TypeLiteral.of(aClass)); + } + + @Override + public ConfigAccessor<List<T>> asList() { + TypeLiteral<List<T>> target = new TypeLiteral<List<T>>(); + return new TamayaConfigAccessor<List<T>>(this, target); + } + + @Override + public ConfigAccessor<Set<T>> asSet() { + TypeLiteral<Set<T>> target = new TypeLiteral<Set<T>>(); + return new TamayaConfigAccessor<Set<T>>(this, target); + } + + @Override + public ConfigAccessor<T> useConverter(Converter<T> converter) { + this.customConverter = converter; + return this; + } + + @Override + public ConfigAccessor<T> withDefault(T defaultValue) { + this.typedDefaultValue = defaultValue; + return this; + } + + @Override + public ConfigAccessor<T> withStringDefault(String defaultValue) { + this.stringDefaultValue = defaultValue; + return this; + } + + @Override + public ConfigAccessor<T> cacheFor(long duration, TimeUnit timeUnit) { + this.cachedValue = getValue(); + this.timeout = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(duration, timeUnit); + return this; + } + + @Override + public ConfigAccessor<T> evaluateVariables(boolean evaluateVariables) { + this.evaluateVariables = evaluateVariables; + return this; + } + + @Override + public ConfigAccessor<T> addLookupSuffix(String lookupSuffix) { + this.lookupSuffixes.add(lookupSuffix); + return this; + } + + @Override + public ConfigAccessor<T> addLookupSuffix(ConfigAccessor<String> configAccessor) { + this.fallbackAccessors.add(configAccessor); + return this; + } + + @Override + public T getValue() { + if(this.timeout > System.currentTimeMillis()){ + return this.cachedValue; + } + T value = getValueInternal(name); + if(value==null){ + value = getValueInternalFromDefaults(); + } + if(value==null){ + throw new IllegalArgumentException("No such value: " + name); + } + return value; + } + + private T getValueInternal(String key) { + T value = null; + if(customConverter!=null){ + String textVal = javaConfigAdapter.getConfiguration().getOrDefault(name, String.class, null); + if(textVal==null){ + textVal = stringDefaultValue; + } + if(textVal!=null) { + value = customConverter.convert(textVal); + } + }else { + value = javaConfigAdapter.getConfiguration().getOrDefault(name, targetType, null); + } + return value; + } + + private T getValueInternalFromDefaults() { + T value = null; + if(customConverter!=null){ + String textVal = stringDefaultValue; + if(textVal!=null) { + value = customConverter.convert(textVal); + } + } + // Should we also try to convert with the String default value and existing converters? + if(value==null){ + value = typedDefaultValue; + } + return value; + } + + @Override + public T getValue(ConfigSnapshot configSnapshot) { + return ((TamayaConfigSnapshot)configSnapshot).getConfiguration() + .get(name, targetType); + } + + @Override + public Optional<T> getOptionalValue(ConfigSnapshot configSnapshot) { + return Optional.ofNullable(((TamayaConfigSnapshot)configSnapshot).getConfiguration() + .getOrDefault(name, targetType, null)); + } + + @Override + public Optional<T> getOptionalValue() { + return Optional.empty(); + } + + @Override + public String getPropertyName() { + return this.name; + } + + @Override + public String getResolvedPropertyName() { + return null; + } + + @Override + public T getDefaultValue() { + return typedDefaultValue; + } + + @Override + public String toString() { + return "TamayaConfigAccessor{" + + "javaConfigAdapter=" + javaConfigAdapter + + ", name='" + name + '\'' + + ", customConverter=" + customConverter + + ", targetType=" + targetType + + ", typedDefaultValue=" + typedDefaultValue + + ", stringDefaultValue='" + stringDefaultValue + '\'' + + ", fallbackAccessors=" + fallbackAccessors + + ", lookupSuffixes=" + lookupSuffixes + + ", evaluateVariables=" + evaluateVariables + + ", timeout=" + timeout + + ", cachedValue=" + cachedValue + + '}'; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigProviderResolver.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigProviderResolver.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigProviderResolver.java new file mode 100644 index 0000000..03c8071 --- /dev/null +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigProviderResolver.java @@ -0,0 +1,91 @@ +/* + * 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.jsr382; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.spi.ConfigurationBuilder; + +import javax.config.Config; +import javax.config.spi.ConfigBuilder; +import javax.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 TamayaConfigProviderResolver 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){ + ConfigurationBuilder builder = Configuration.createConfigurationBuilder(); + builder.setClassLoader(loader); + builder.addDefaultPropertyConverters(); + JavaConfigBuilderAdapter javaConfigBuilder = new JavaConfigBuilderAdapter(builder); + javaConfigBuilder.addDefaultSources(); + javaConfigBuilder.addDiscoveredSources(); + config = javaConfigBuilder.build(); + this.configs.put(loader, config); + } + return config; + } + + @Override + public ConfigBuilder getBuilder() { + return new JavaConfigBuilderAdapter(Configuration.createConfigurationBuilder()); + } + + @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; + } + } + } + + @Override + public String toString() { + return "TamayaConfigProviderResolver{" + + "configs=" + configs.size() + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigSnapshot.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigSnapshot.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigSnapshot.java new file mode 100644 index 0000000..a148cf2 --- /dev/null +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigSnapshot.java @@ -0,0 +1,57 @@ +/* + * 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.jsr382; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.spisupport.DefaultConfigurationSnapshot; + +import javax.config.ConfigAccessor; +import javax.config.ConfigSnapshot; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Implementation of a {@link ConfigSnapshot} based on Tamaya resources. + */ +class TamayaConfigSnapshot implements ConfigSnapshot { + + private DefaultConfigurationSnapshot snapshot; + private List<ConfigAccessor<?>> accessors = new ArrayList<>(); + + public TamayaConfigSnapshot(Configuration config, ConfigAccessor<?>... configAccessors){ + Set<String> keys = new HashSet<>(); + for(ConfigAccessor<?> accessor:configAccessors){ + keys.addAll(((TamayaConfigAccessor)accessor).getCandidateKeys()); + } + this.snapshot = new DefaultConfigurationSnapshot(config, keys); + } + + public Configuration getConfiguration(){ + return snapshot; + } + + @Override + public String toString() { + return "TamayaConfigSnapshot{" + + "snapshot=" + snapshot + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfiguration.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfiguration.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfiguration.java deleted file mode 100644 index c381366..0000000 --- a/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfiguration.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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.jsr382; - -import org.apache.tamaya.*; -import org.apache.tamaya.spi.ConfigurationContext; - -import javax.config.Config; -import java.util.*; - -/** - * Configuration implementation that wraps a Javaconfig {@link Config} instance. - */ -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-sandbox/blob/8ad95aa9/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigurationAdapter.java ---------------------------------------------------------------------- diff --git a/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigurationAdapter.java b/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigurationAdapter.java new file mode 100644 index 0000000..37a7e16 --- /dev/null +++ b/configjsr/src/main/java/org/apache/tamaya/jsr382/TamayaConfigurationAdapter.java @@ -0,0 +1,110 @@ +/* + * 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.jsr382; + +import org.apache.tamaya.*; +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spisupport.DefaultConfigurationSnapshot; + +import javax.config.Config; +import java.util.*; + +/** + * Configuration implementation that wraps a Javaconfig {@link Config} instance. + */ +class TamayaConfigurationAdapter implements Configuration{ + + private Config delegate; + private ConfigurationContext context; + + /** + * Creates a new adapter based on the given {@link Config}. + * @param config the config, not null. + */ + public TamayaConfigurationAdapter(Config config){ + this.delegate = Objects.requireNonNull(config); + this.context = JavaConfigAdapterFactory.toConfigurationContext(config); + } + + /** + * Get the underlying config. + * @return the underlying config, not null. + */ + 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 delegate.getOptionalValue(key, type.getRawType()).orElse(defaultValue); + } + + @Override + public Map<String, String> getProperties() { + Map<String,String> properties = new HashMap<>(); + for(String key:this.delegate.getPropertyNames()) { + properties.put(key, delegate.getValue(key, String.class)); + } + return properties; + } + + @Override + public ConfigurationSnapshot getSnapshot(Iterable<String> keys){ + return new DefaultConfigurationSnapshot(this, keys); + } + + @Override + public ConfigurationContext getContext() { + return context; + } + + @Override + public String toString() { + return "TamayaConfigurationAdapter{" + + "delegate=" + delegate + + '}'; + } +}
