TAMAYA-321 Add ConfigurationBuilder to core API.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/15f7cbbb Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/15f7cbbb Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/15f7cbbb Branch: refs/heads/master Commit: 15f7cbbb3ffac1e4cd2d2de0aabbb380fbc0b378 Parents: f255aa9 Author: Anatole Tresch <anat...@apache.org> Authored: Sun Nov 26 17:14:21 2017 +0100 Committer: Anatole Tresch <anat...@apache.org> Committed: Sun Nov 26 17:14:21 2017 +0100 ---------------------------------------------------------------------- .../java/org/apache/tamaya/Configuration.java | 26 +- .../apache/tamaya/ConfigurationProvider.java | 25 +- .../apache/tamaya/spi/ConfigurationBuilder.java | 347 +++++++++++++++ .../apache/tamaya/spi/ConfigurationContext.java | 10 +- .../tamaya/spi/ConfigurationContextBuilder.java | 17 +- .../tamaya/spi/ConfigurationProviderSpi.java | 13 +- .../apache/tamaya/spi/ConversionContext.java | 6 +- .../tamaya/TestConfigurationProvider.java | 6 + .../org/apache/tamaya/core/OSGIActivator.java | 5 +- .../core/internal/ConfigValueEvaluator.java | 47 -- .../tamaya/core/internal/CoreConfiguration.java | 43 ++ .../core/internal/CoreConfigurationBuilder.java | 91 ++++ .../core/internal/CoreConfigurationContext.java | 52 --- .../CoreConfigurationContextBuilder.java | 93 ---- .../internal/CoreConfigurationProvider.java | 101 +++++ .../internal/DefaultConfigurationProvider.java | 106 ----- .../core/internal/DefaultServiceContext.java | 205 --------- .../core/internal/OSGIServiceContext.java | 2 +- .../org.apache.tamaya.spi.ConfigurationBuilder | 19 + ...pache.tamaya.spi.ConfigurationContextBuilder | 19 - ...g.apache.tamaya.spi.ConfigurationProviderSpi | 2 +- .../org.apache.tamaya.spi.ServiceContext | 2 +- .../tamaya/core/ConfigurationBuilderTest.java | 440 +++++++++++++++++++ .../core/ConfigurationContextBuilderTest.java | 8 +- .../internal/CoreConfigurationBuilderTest.java | 219 +++++++++ .../CoreConfigurationContextBuilderTest.java | 200 --------- .../internal/CoreConfigurationContextTest.java | 176 -------- .../internal/CoreConfigurationProviderTest.java | 93 ++++ .../core/internal/CoreConfigurationTest.java | 177 ++++++++ .../DefaultConfigurationProviderTest.java | 72 --- .../internal/DefaultServiceContextTest.java | 138 ------ ...tServiceContextTest$InvalidPriorityInterface | 18 - ...efaultServiceContextTest$MultiImplsInterface | 20 - ...g.apache.tamaya.spi.ConfigurationProviderSpi | 18 + .../tamaya/spisupport/DefaultConfiguration.java | 19 +- .../spisupport/DefaultConfigurationBuilder.java | 21 +- .../DefaultConfigurationBuilderTest.java | 220 ++++++++++ .../spisupport/DefaultServiceContextTest.java | 138 ++++++ .../spisupport/TestConfigurationProvider.java | 92 ++++ ...g.apache.tamaya.spi.ConfigurationProviderSpi | 18 + ...tServiceContextTest$InvalidPriorityInterface | 18 + ...efaultServiceContextTest$MultiImplsInterface | 20 + jqassistant/serviceloader-rules.xml | 6 +- 43 files changed, 2174 insertions(+), 1194 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/api/src/main/java/org/apache/tamaya/Configuration.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/Configuration.java b/code/api/src/main/java/org/apache/tamaya/Configuration.java index 67aad7b..401eac7 100644 --- a/code/api/src/main/java/org/apache/tamaya/Configuration.java +++ b/code/api/src/main/java/org/apache/tamaya/Configuration.java @@ -18,6 +18,7 @@ */ package org.apache.tamaya; +import org.apache.tamaya.spi.ConfigurationBuilder; import org.apache.tamaya.spi.ConfigurationContext; import java.util.Map; @@ -53,7 +54,9 @@ public interface Configuration { * @param key the property's key, not {@code null}. * @return the property's value. */ - String get(String key); + default String get(String key){ + return get(key, TypeLiteral.of(String.class)); + } /** * Access a property. @@ -62,7 +65,9 @@ public interface Configuration { * @param defaultValue value to be returned, if no value is present, not {@code null} * @return the property's keys. */ - String getOrDefault(String key, String defaultValue); + default String getOrDefault(String key, String defaultValue){ + return getOrDefault(key, TypeLiteral.of(String.class), defaultValue); + } /** * Get the property keys as type T. This will implicitly require a corresponding {@link @@ -77,7 +82,9 @@ public interface Configuration { * @return the property value, never {@code null}. * @throws ConfigException if the keys could not be converted to the required target type. */ - <T> T getOrDefault(String key, Class<T> type, T defaultValue); + default <T> T getOrDefault(String key, Class<T> type, T defaultValue){ + return getOrDefault(key, TypeLiteral.of(type), defaultValue); + } /** * Get the property keys as type T. This will implicitly require a corresponding {@link @@ -91,7 +98,9 @@ public interface Configuration { * @return the property value, never {@code null}. * @throws ConfigException if the keys could not be converted to the required target type. */ - <T> T get(String key, Class<T> type); + default <T> T get(String key, Class<T> type){ + return get(key, TypeLiteral.of(type)); + } /** * Get the property keys as type T. This will implicitly require a corresponding {@link @@ -159,4 +168,13 @@ public interface Configuration { */ ConfigurationContext getContext(); + /** + * Create a new builder using this instance as it's base. + * @return a new builder, never null. + */ + default ConfigurationBuilder toBuilder() { + return ConfigurationProvider.getConfigurationBuilder().setConfiguration(this); + } + + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java b/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java index 564d5a1..bacb944 100644 --- a/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java +++ b/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java @@ -18,10 +18,7 @@ */ package org.apache.tamaya; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationContextBuilder; -import org.apache.tamaya.spi.ConfigurationProviderSpi; -import org.apache.tamaya.spi.ServiceContextManager; +import org.apache.tamaya.spi.*; import java.util.logging.Logger; @@ -109,17 +106,33 @@ public final class ConfigurationProvider { } /** - * Create a new {@link org.apache.tamaya.spi.ConfigurationContextBuilder} instance. This method creates + * Create a new {@link ConfigurationBuilder} instance. This method creates * a new builder instance that is not related to any concrete {@link org.apache.tamaya.spi.ConfigurationContext}. * You can use {@link #setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext)} to change the * current configuration context. * - * @return a new, empty {@link org.apache.tamaya.spi.ConfigurationContextBuilder}, never null. + * @return a new, empty {@link ConfigurationBuilder}, never null. * @see #setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext) * @see org.apache.tamaya.spi.ConfigurationContext + * @deprecated Will be removed. */ + @Deprecated public static ConfigurationContextBuilder getConfigurationContextBuilder() { return spi().getConfigurationContextBuilder(); } + /** + * Create a new {@link ConfigurationBuilder} instance. This method creates + * a new builder instance that is not related to any concrete {@link org.apache.tamaya.spi.ConfigurationContext}. + * You can use {@link #setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext)} to change the + * current configuration context. + * + * @return a new, empty {@link ConfigurationBuilder}, never null. + * @see #setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext) + * @see org.apache.tamaya.spi.ConfigurationContext + */ + public static ConfigurationBuilder getConfigurationBuilder() { + return spi().getConfigurationBuilder(); + } + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationBuilder.java b/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationBuilder.java new file mode 100644 index 0000000..c9afa0f --- /dev/null +++ b/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationBuilder.java @@ -0,0 +1,347 @@ +/* + * 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.spi; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.TypeLiteral; + +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.Map; + +/** + * A builder for creating new or adapting instances of {@link ConfigurationContext}. + * Builders can be obtained in exactly two ways: + * <ol> + * <li>By accessing a preinitialized builder from an existing {@link ConfigurationContext}, + * by calling {@link org.apache.tamaya.spi.ConfigurationContext#toBuilder()}.</li> + * <li>By accessing an empty builder instance from + * {@link org.apache.tamaya.ConfigurationProvider#getConfigurationContextBuilder()}.</li> + * </ol> + * After all changes are applied to a builder a new {@link ConfigurationContext} instance can + * be created and can be applied by calling + * {@link org.apache.tamaya.ConfigurationProvider#setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext)}. + * + */ +public interface ConfigurationBuilder { + + /** + * Init this builder instance with the given {@link Configuration} instance. This + * method will use any existing property sources, filters, converters and the combination + * policy of the given {@link Configuration} and initialize the current builder + * with them. + * + * @param config the {@link Configuration} instance to be used, not {@code null}. + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder setConfiguration(Configuration config); + + /** + * Init this builder instance with the given {@link ConfigurationContext} instance. This + * method will use any existing property sources, filters, converters and the combination + * policy of the given {@link ConfigurationContext} and initialize the current builder + * with them. + * + * @param context the {@link ConfigurationContext} instance to be used, not {@code null}. + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder setContext(ConfigurationContext context); + + /** + * This method can be used for adding {@link PropertySource}s. + * Hereby the property source is added to the tail of property sources with + * lowest priority regardless of its current ordinal value. To sort the property + * sources based on their ordinals call {@link #sortPropertySources}. + * + * @param propertySources the PropertySources to add + * @return this builder, for chaining, never null. + * @throws IllegalArgumentException If a property source with a given name already + * exists. + */ + ConfigurationBuilder addPropertySources(PropertySource... propertySources); + + /** + * This method can be used for programmatically adding {@link PropertySource}s. + * Hereby the property source is added to the tail of property sources with + * lowest priority regardless of its current ordinal value. To sort the property + * sources based on their ordinals call {@link #sortPropertySources}. + * + * @param propertySources the PropertySources to add + * @return this builder, for chaining, never null. + * @throws IllegalArgumentException If a property source with a given name already + * exists. + */ + ConfigurationBuilder addPropertySources(Collection<PropertySource> propertySources); + + /** + * Add all registered (default) property sources to the context built. The sources are ordered + * based on their ordinal values and added to the chain of property sources with + * higher priority. + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder addDefaultPropertySources(); + + /** + * Removes the given property sources, if existing. The existing order of property + * sources is preserved. + * + * @param propertySources the property sources to remove, not {@code null}. + * @return the builder for chaining. + */ + ConfigurationBuilder removePropertySources(PropertySource... propertySources); + + /** + * Removes the given property sources, if existing. The existing order of property + * sources is preserved. + * + * @param propertySources the property sources to remove, not {@code null}. + * @return the builder for chaining. + */ + ConfigurationBuilder removePropertySources(Collection<PropertySource> propertySources); + + /** + * Access the current chain of property sources. Items at the end of the list have + * precedence/more significance. + * + * @return the property source chain, never {@code null}. + */ + List<PropertySource> getPropertySources(); + + /** + * Access the current chain of property filters. Items at the end of the list have + * precedence/more significance. + * + * @return the property source chain, never {@code null}. + */ + List<PropertyFilter> getPropertyFilters(); + + /** + * Access the current registered property converters. + * + * @return the current registered property converters. + */ + Map<TypeLiteral<?>, Collection<PropertyConverter<?>>> getPropertyConverter(); + + /** + * Increases the priority of the given property source, by moving it towards the end + * of the chain of property sources. If the property source given is already at the end + * this method has no effect. This operation does not change any ordinal values. + * + * @param propertySource the property source to be incresed regarding its significance. + * @return the builder for chaining. + * @throws IllegalArgumentException If no such property source exists in the current + * chain. + */ + ConfigurationBuilder increasePriority(PropertySource propertySource); + + /** + * Decreases the priority of the given property source, by moving it towards the start + * of the chain of property sources. If the property source given is already the first + * this method has no effect. This operation does not change any ordinal values. + * + * @param propertySource the property source to be decresed regarding its significance. + * @return the builder for chaining. + * @throws IllegalArgumentException If no such property source exists in the current + * chain. + */ + ConfigurationBuilder decreasePriority(PropertySource propertySource); + + /** + * Increases the priority of the given property source to be maximal, by moving it to + * the tail of the of property source chain. If the property source given is + * already the last item this method has no effect. This operation does not change + * any ordinal values. + * + * @param propertySource the property source to be maximized regarding its significance. + * @return the builder for chaining. + * @throws IllegalArgumentException If no such property source exists in the current + * chain. + */ + ConfigurationBuilder highestPriority(PropertySource propertySource); + + /** + * Decreases the priority of the given property source to be minimal, by moving it to + * the start of the chain of property source chain. If the property source given is + * already the first item this method has no effect. This operation does not change + * any ordinal values. + * + * @param propertySource the property source to be minimized regarding its significance. + * @return the builder for chaining. + * @throws IllegalArgumentException If no such property source exists in the current + * chain. + */ + ConfigurationBuilder lowestPriority(PropertySource propertySource); + + /** + * Adds the given PropertyFilter instances, hereby the instances are added + * to the end of the list with highest priority. The ordering of existing + * property filters remains unchanged. To sort the property + * filters call {@link #sortPropertyFilter}. + * + * @param filters the filters to add + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder addPropertyFilters(PropertyFilter... filters); + + /** + * Adds the given PropertyFilter instances, hereby the instances are added + * to the end of the list with highest priority. The ordering of existing + * property filters remains unchanged. To sort the property + * filters call {@link #sortPropertyFilter}. + * + * @param filters the filters to add + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder addPropertyFilters(Collection<PropertyFilter> filters); + + /** + * Add all registered (default) property filters to the context built. + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder addDefaultPropertyFilters(); + + + /** + * Removes the given PropertyFilter instances, if existing. The order of the remaining + * filters is preserved. + * + * @param filters the filter to remove + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder removePropertyFilters(PropertyFilter... filters); + + /** + * Removes the given PropertyFilter instances, if existing. The order of the remaining + * filters is preserved. + * + * @param filters the filter to remove + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder removePropertyFilters(Collection<PropertyFilter> filters); + + /** + * This method can be used for adding {@link PropertyConverter}s. + * Converters are added at the end after any existing converters. + * For converters already registered for the current target type the + * method has no effect. + * + * @param typeToConvert the type for which the converter is for + * @param propertyConverters the PropertyConverters to add for this type + * @param <T> the target type. + * @return this builder, for chaining, never null. + */ + <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> typeToConvert, + @SuppressWarnings("unchecked") PropertyConverter<T>... propertyConverters); + + /** + * This method can be used for adding {@link PropertyConverter}s. + * Converters are added at the end after any existing converters. + * For converters already registered for the current target type the + * method has no effect. + * + * @param typeToConvert the type for which the converter is for + * @param propertyConverters the PropertyConverters to add for this type + * @param <T> the target type. + * @return this builder, for chaining, never null. + */ + <T> ConfigurationBuilder addPropertyConverters(TypeLiteral<T> typeToConvert, + Collection<PropertyConverter<T>> propertyConverters); + + /** + * Add all registered (default) property converters to the context built. + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder addDefaultPropertyConverters(); + + /** + * Removes the given PropertyConverter instances for the given type, + * if existing. + * + * @param typeToConvert the type which the converter is for + * @param propertyConverters the converter to remove + * @param <T> the target type. + * @return this builder, for chaining, never null. + */ + <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, + @SuppressWarnings("unchecked") PropertyConverter<T>... propertyConverters); + + /** + * Removes the given PropertyConverter instances for the given type, + * if existing. + * + * @param typeToConvert the type which the converter is for + * @param propertyConverters the converter to remove + * @param <T> the target type. + * @return this builder, for chaining, never null. + */ + <T> ConfigurationBuilder removePropertyConverters(TypeLiteral<T> typeToConvert, + Collection<PropertyConverter<T>> propertyConverters); + + /** + * Removes all converters for the given type, which actually renders a given type + * unsupported for type conversion. + * + * @param typeToConvert the type which the converter is for + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder removePropertyConverters(TypeLiteral<?> typeToConvert); + + /** + * Sorts the current registered property sources using the given comparator. + * + * NOTE: property sources at the beginning have minimal significance. + * + * @param comparator the comparator to be used, not {@code null}. + * @return this instance for chaining. + */ + ConfigurationBuilder sortPropertySources(Comparator<PropertySource> comparator); + + /** + * Sorts the current registered property filters using the given comparator. + * + * NOTE: property filters at the beginning have minimal significance. + * + * @param comparator the comparator to be used, not {@code null}. + * @return this instance for chaining. + */ + ConfigurationBuilder sortPropertyFilter(Comparator<PropertyFilter> comparator); + + /** + * Sets the {@link PropertyValueCombinationPolicy} used to evaluate the final + * property values. + * + * @param policy the {@link PropertyValueCombinationPolicy} used, not {@code null}. + * @return this builder, for chaining, never null. + */ + ConfigurationBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy); + + /** + * Builds a new {@link ConfigurationContext} based on the data in this builder. The ordering of property + * sources and property filters is not changed, regardless of their ordinals. For ensure a certain + * ordering/significance call {@link #sortPropertyFilter(Comparator)} and/or {@link #sortPropertySources(Comparator)} + * before building the context. + * + * @return the final context to be used to create a configuration. + * @see org.apache.tamaya.ConfigurationProvider#createConfiguration(ConfigurationContext) + */ + Configuration build(); + +} + http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java b/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java index c33cf9d..f077a1d 100644 --- a/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java +++ b/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java @@ -68,7 +68,7 @@ public interface ConfigurationContext { * It is not needed for normal 'usage' by end users, but only for Extension Developers! * * @param <T> the type of the type literal - * @param type the type which the converter is for + * @param type the type which the converters is for * @param propertyConverter the PropertyConverters to add for this type * @deprecated Use {@link ConfigurationContextBuilder} to create a new {@link ConfigurationContext}. * @see #toBuilder() @@ -139,13 +139,13 @@ public interface ConfigurationContext { * * <p> * The converters returned for a type should be used as a chain, whereas the result of the - * first converter that is able to convert the configured value, is taken as the chain's result. - * No more converters are called after a converter has successfully converted the input into + * first converters that is able to convert the configured value, is taken as the chain's result. + * No more converters are called after a converters has successfully converted the input into * the required target type. * </p> * * @param <T> the type of the type literal - * @param type type of the desired converter + * @param type type of the desired converters * @return a sorted list of registered PropertySources per type. */ <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> type); @@ -166,7 +166,9 @@ public interface ConfigurationContext { /** * Creates a {@link ConfigurationContextBuilder} preinitialized with the data from this instance. * @return a new builder instance, never null. + * @deprecated Will be removed. */ + @Deprecated ConfigurationContextBuilder toBuilder(); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java b/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java index 8aa0d05..17c1bf4 100644 --- a/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java +++ b/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java @@ -37,8 +37,9 @@ import java.util.Map; * After all changes are applied to a builder a new {@link ConfigurationContext} instance can * be created and can be applied by calling * {@link org.apache.tamaya.ConfigurationProvider#setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext)}. - * + * @deprecated Use {@link ConfigurationBuilder} instead. */ +@Deprecated public interface ConfigurationContextBuilder { /** @@ -230,7 +231,7 @@ public interface ConfigurationContextBuilder { * For converters already registered for the current target type the * method has no effect. * - * @param typeToConvert the type for which the converter is for + * @param typeToConvert the type for which the converters is for * @param propertyConverters the PropertyConverters to add for this type * @param <T> the target type. * @return this builder, for chaining, never null. @@ -244,7 +245,7 @@ public interface ConfigurationContextBuilder { * For converters already registered for the current target type the * method has no effect. * - * @param typeToConvert the type for which the converter is for + * @param typeToConvert the type for which the converters is for * @param propertyConverters the PropertyConverters to add for this type * @param <T> the target type. * @return this builder, for chaining, never null. @@ -262,8 +263,8 @@ public interface ConfigurationContextBuilder { * Removes the given PropertyConverter instances for the given type, * if existing. * - * @param typeToConvert the type which the converter is for - * @param propertyConverters the converter to remove + * @param typeToConvert the type which the converters is for + * @param propertyConverters the converters to remove * @param <T> the target type. * @return this builder, for chaining, never null. */ @@ -274,8 +275,8 @@ public interface ConfigurationContextBuilder { * Removes the given PropertyConverter instances for the given type, * if existing. * - * @param typeToConvert the type which the converter is for - * @param propertyConverters the converter to remove + * @param typeToConvert the type which the converters is for + * @param propertyConverters the converters to remove * @param <T> the target type. * @return this builder, for chaining, never null. */ @@ -286,7 +287,7 @@ public interface ConfigurationContextBuilder { * Removes all converters for the given type, which actually renders a given type * unsupported for type conversion. * - * @param typeToConvert the type which the converter is for + * @param typeToConvert the type which the converters is for * @return this builder, for chaining, never null. */ ConfigurationContextBuilder removePropertyConverters(TypeLiteral<?> typeToConvert); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java b/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java index b69bef6..fb93ab4 100644 --- a/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java +++ b/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java @@ -48,10 +48,19 @@ public interface ConfigurationProviderSpi { * Creates a new {@link org.apache.tamaya.spi.ConfigurationContextBuilder} instance. * * @return a new {@link org.apache.tamaya.spi.ConfigurationContextBuilder}, never null. + * @deprecated Will be removed */ + @Deprecated ConfigurationContextBuilder getConfigurationContextBuilder(); /** + * Creates a new {@link org.apache.tamaya.spi.ConfigurationBuilder} instance. + * + * @return a new {@link org.apache.tamaya.spi.ConfigurationBuilder}, never null. + */ + ConfigurationBuilder getConfigurationBuilder(); + + /** * This method allows to replace the current {@link org.apache.tamaya.Configuration} with a new * instance. This can be used to update the configuration with a new one, e.g. because some of the * data has changed and must be updated. It is the responsibility of the ConfigurationProvider to trigger @@ -70,7 +79,9 @@ public interface ConfigurationProviderSpi { * by the current implementation. * @see #setConfiguration(org.apache.tamaya.Configuration) */ - boolean isConfigurationSettable(); + default boolean isConfigurationSettable(){ + return true; + } /** * Get access to the current {@link ConfigurationContext}. http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/api/src/main/java/org/apache/tamaya/spi/ConversionContext.java ---------------------------------------------------------------------- diff --git a/code/api/src/main/java/org/apache/tamaya/spi/ConversionContext.java b/code/api/src/main/java/org/apache/tamaya/spi/ConversionContext.java index e039cf2..ac8de68 100644 --- a/code/api/src/main/java/org/apache/tamaya/spi/ConversionContext.java +++ b/code/api/src/main/java/org/apache/tamaya/spi/ConversionContext.java @@ -93,7 +93,7 @@ public class ConversionContext { * Allows to add information on the supported/tried formats, which can be shown to the user, especially when * conversion failed. Adding of formats is synchronized, all formats are added in order to the overall list. * This means formats should be passed in order of precedence. - * @param converterType the converter, which implements the formats provided. + * @param converterType the converters, which implements the formats provided. * @param formatDescriptors the format descriptions in a human readable form, e.g. as regular expressions. */ public void addSupportedFormats(@SuppressWarnings("rawtypes") Class<? extends PropertyConverter> converterType, String... formatDescriptors){ @@ -230,9 +230,9 @@ public class ConversionContext { } /** - * Add the formats provided by a {@link PropertyConverter}. This method should be called by each converter + * Add the formats provided by a {@link PropertyConverter}. This method should be called by each converters * performing/trying conversion, so the user can be given feedback on the supported formats on failure. - * @param converterType the converter type, not {@code null}. + * @param converterType the converters type, not {@code null}. * @param formatDescriptors the formats supported in a human readable form, e.g. as regular expressions. * @return the builder instance, for chaining */ http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java b/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java index eaef108..1ccce31 100644 --- a/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java +++ b/code/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java @@ -21,6 +21,7 @@ package org.apache.tamaya; import javax.annotation.Priority; import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConfigurationBuilder; import org.apache.tamaya.spi.ConfigurationContextBuilder; import org.apache.tamaya.spi.ConfigurationProviderSpi; @@ -58,6 +59,11 @@ public class TestConfigurationProvider implements ConfigurationProviderSpi { } @Override + public ConfigurationBuilder getConfigurationBuilder() { + return null; + } + + @Override public ConfigurationContextBuilder getConfigurationContextBuilder() { return null; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/java/org/apache/tamaya/core/OSGIActivator.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/OSGIActivator.java b/code/core/src/main/java/org/apache/tamaya/core/OSGIActivator.java index 3ddaf69..2ca51db 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/OSGIActivator.java +++ b/code/core/src/main/java/org/apache/tamaya/core/OSGIActivator.java @@ -24,7 +24,6 @@ import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.core.internal.*; import org.apache.tamaya.spi.ServiceContextManager; import org.apache.tamaya.spisupport.DefaultConfiguration; -import org.apache.tamaya.core.internal.CoreConfigurationContextBuilder; import org.apache.tamaya.spisupport.PropertyFilterComparator; import org.apache.tamaya.spisupport.PropertySourceComparator; import org.osgi.framework.BundleActivator; @@ -49,15 +48,13 @@ public class OSGIActivator implements BundleActivator { ServiceContextManager.set(new OSGIServiceContext(serviceLoader)); LOG.info("Registered Tamaya OSGI ServiceContext..."); ConfigurationProvider.setConfiguration( - new DefaultConfiguration( - new CoreConfigurationContextBuilder() + new CoreConfigurationBuilder() .addDefaultPropertyConverters() .addDefaultPropertyFilters() .addDefaultPropertySources() .sortPropertyFilter(PropertyFilterComparator.getInstance()) .sortPropertySources(PropertySourceComparator.getInstance()) .build() - ) ); LOG.info("Loaded default configuration from OSGI."); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/java/org/apache/tamaya/core/internal/ConfigValueEvaluator.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/ConfigValueEvaluator.java b/code/core/src/main/java/org/apache/tamaya/core/internal/ConfigValueEvaluator.java deleted file mode 100644 index f1c8bf2..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/ConfigValueEvaluator.java +++ /dev/null @@ -1,47 +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.core.internal; - -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.PropertyValue; - -import java.util.Map; - -/** - * Component SPI which encapsulates the evaluation of a single or full <b>raw</b>value - * for a {@link ConfigurationContext}. - */ -public interface ConfigValueEvaluator { - - /** - * Evaluates single value using a {@link ConfigurationContext}. - * @param key the config key, not {@code null}. - * @param context the context, not {@code null}. - * @return the value, or null. - */ - PropertyValue evaluteRawValue(String key, ConfigurationContext context); - - /** - * Evaluates all property values from a {@link ConfigurationContext}. - * @param context the context, not {@code null}. - * @return the value, or null. - */ - Map<String, PropertyValue> evaluateRawValues(ConfigurationContext context); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfiguration.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfiguration.java b/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfiguration.java new file mode 100644 index 0000000..5363b76 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfiguration.java @@ -0,0 +1,43 @@ +/* + * 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.core.internal; + +import org.apache.tamaya.spi.ConfigurationBuilder; +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spisupport.DefaultConfiguration; +import org.apache.tamaya.spisupport.DefaultConfigurationContext; +import org.apache.tamaya.spisupport.DefaultConfigurationContextBuilder; + +/** + * Default implementation of {@link ConfigurationBuilder}. + */ +public final class CoreConfiguration extends DefaultConfiguration { + + /** + * Creates a new builder instance. + */ + public CoreConfiguration(ConfigurationContext context) { + super(context); + } + + @Override + public ConfigurationBuilder toBuilder() { + return new CoreConfigurationBuilder(this); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationBuilder.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationBuilder.java b/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationBuilder.java new file mode 100644 index 0000000..4daacdf --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationBuilder.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.core.internal; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConfigurationBuilder; +import org.apache.tamaya.core.internal.converters.*; +import org.apache.tamaya.spisupport.DefaultConfigurationBuilder; +import org.apache.tamaya.spisupport.DefaultConfigurationContext; +import org.apache.tamaya.spisupport.DefaultConfigurationContextBuilder; + +import java.io.File; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.net.URI; +import java.net.URL; +import java.nio.file.Path; +import java.util.*; + +/** + * Default implementation of {@link ConfigurationBuilder}. + */ +public final class CoreConfigurationBuilder extends DefaultConfigurationBuilder { + + /** + * Creates a new builder instance. + */ + public CoreConfigurationBuilder() { + super(); + } + + /** + * Creates a new builder instance. + */ + public CoreConfigurationBuilder(Configuration config) { + super(config); + } + + /** + * Creates a new builder instance initializing it with the given context. + * @param context the context to be used, not null. + */ + public CoreConfigurationBuilder(ConfigurationContext context) { + super(context); + } + + @SuppressWarnings("unchecked") + protected void addCorePropertyConverters() { + addPropertyConverters(TypeLiteral.<BigDecimal>of(BigDecimal.class), new BigDecimalConverter()); + addPropertyConverters(TypeLiteral.<BigInteger>of(BigInteger.class), new BigIntegerConverter()); + addPropertyConverters(TypeLiteral.<Boolean>of(Boolean.class), new BooleanConverter()); + addPropertyConverters(TypeLiteral.<Byte>of(Byte.class), new ByteConverter()); + addPropertyConverters(TypeLiteral.<Character>of(Character.class), new CharConverter()); + addPropertyConverters(TypeLiteral.<Class<?>>of(Class.class), new ClassConverter()); + addPropertyConverters(TypeLiteral.<Currency>of(Currency.class), new CurrencyConverter()); + addPropertyConverters(TypeLiteral.<Double>of(Double.class), new DoubleConverter()); + addPropertyConverters(TypeLiteral.<File>of(File.class), new FileConverter()); + addPropertyConverters(TypeLiteral.<Float>of(Float.class), new FloatConverter()); + addPropertyConverters(TypeLiteral.<Integer>of(Integer.class), new IntegerConverter()); + addPropertyConverters(TypeLiteral.<Long>of(Long.class), new LongConverter()); + addPropertyConverters(TypeLiteral.<Number>of(Number.class), new NumberConverter()); + addPropertyConverters(TypeLiteral.<Path>of(Path.class), new PathConverter()); + addPropertyConverters(TypeLiteral.<Short>of(Short.class), new ShortConverter()); + addPropertyConverters(TypeLiteral.<URI>of(URI.class), new URIConverter()); + addPropertyConverters(TypeLiteral.<URL>of(URL.class), new URLConverter()); + } + + @Override + public Configuration build() { + return new CoreConfiguration(contextBuilder.build()); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationContext.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationContext.java b/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationContext.java deleted file mode 100644 index dd31b91..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationContext.java +++ /dev/null @@ -1,52 +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.core.internal; - -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.core.internal.converters.*; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationContextBuilder; -import org.apache.tamaya.spisupport.DefaultConfigurationContext; -import org.apache.tamaya.spisupport.DefaultConfigurationContextBuilder; - -import java.io.File; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.net.URI; -import java.net.URL; -import java.nio.file.Path; -import java.util.Currency; - -/** - * Default implementation of {@link ConfigurationContextBuilder}. - */ -public final class CoreConfigurationContext extends DefaultConfigurationContext { - - /** - * Creates a new builder instance. - */ - public CoreConfigurationContext(CoreConfigurationContextBuilder builder) { - super(builder); - } - - @Override - public ConfigurationContextBuilder toBuilder() { - return new CoreConfigurationContextBuilder(this); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationContextBuilder.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationContextBuilder.java b/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationContextBuilder.java deleted file mode 100644 index d440a88..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationContextBuilder.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.core.internal; - -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationContextBuilder; -import org.apache.tamaya.spi.PropertyConverter; -import org.apache.tamaya.spi.PropertyFilter; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; -import org.apache.tamaya.spi.PropertyValueCombinationPolicy; -import org.apache.tamaya.spi.ServiceContextManager; -import org.apache.tamaya.core.internal.converters.*; -import org.apache.tamaya.spisupport.DefaultConfigurationContextBuilder; -import org.apache.tamaya.spisupport.PropertySourceComparator; -import org.apache.tamaya.spisupport.propertysource.CLIPropertySource; -import org.apache.tamaya.spisupport.propertysource.EnvironmentPropertySource; -import org.apache.tamaya.spisupport.propertysource.JavaConfigurationPropertySource; -import org.apache.tamaya.spisupport.propertysource.SystemPropertySource; - -import java.io.File; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.net.URI; -import java.net.URL; -import java.nio.file.Path; -import java.util.*; -import java.util.logging.Logger; - -/** - * Default implementation of {@link ConfigurationContextBuilder}. - */ -public final class CoreConfigurationContextBuilder extends DefaultConfigurationContextBuilder { - - /** - * Creates a new builder instance. - */ - public CoreConfigurationContextBuilder() { - } - - /** - * Creates a new builder instance initializing it with the given context. - * @param context the context to be used, not null. - */ - public CoreConfigurationContextBuilder(ConfigurationContext context) { - super(context); - } - - @SuppressWarnings("unchecked") - protected void addCorePropertyConverters() { - addPropertyConverters(TypeLiteral.<BigDecimal>of(BigDecimal.class), new BigDecimalConverter()); - addPropertyConverters(TypeLiteral.<BigInteger>of(BigInteger.class), new BigIntegerConverter()); - addPropertyConverters(TypeLiteral.<Boolean>of(Boolean.class), new BooleanConverter()); - addPropertyConverters(TypeLiteral.<Byte>of(Byte.class), new ByteConverter()); - addPropertyConverters(TypeLiteral.<Character>of(Character.class), new CharConverter()); - addPropertyConverters(TypeLiteral.<Class<?>>of(Class.class), new ClassConverter()); - addPropertyConverters(TypeLiteral.<Currency>of(Currency.class), new CurrencyConverter()); - addPropertyConverters(TypeLiteral.<Double>of(Double.class), new DoubleConverter()); - addPropertyConverters(TypeLiteral.<File>of(File.class), new FileConverter()); - addPropertyConverters(TypeLiteral.<Float>of(Float.class), new FloatConverter()); - addPropertyConverters(TypeLiteral.<Integer>of(Integer.class), new IntegerConverter()); - addPropertyConverters(TypeLiteral.<Long>of(Long.class), new LongConverter()); - addPropertyConverters(TypeLiteral.<Number>of(Number.class), new NumberConverter()); - addPropertyConverters(TypeLiteral.<Path>of(Path.class), new PathConverter()); - addPropertyConverters(TypeLiteral.<Short>of(Short.class), new ShortConverter()); - addPropertyConverters(TypeLiteral.<URI>of(URI.class), new URIConverter()); - addPropertyConverters(TypeLiteral.<URL>of(URL.class), new URLConverter()); - } - - @Override - public ConfigurationContext build() { - return new CoreConfigurationContext(this); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationProvider.java b/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationProvider.java new file mode 100644 index 0000000..caf7fbb --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/CoreConfigurationProvider.java @@ -0,0 +1,101 @@ +/* + * 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.core.internal; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConfigurationBuilder; +import org.apache.tamaya.spi.ConfigurationContextBuilder; +import org.apache.tamaya.spi.ConfigurationProviderSpi; +import org.apache.tamaya.spisupport.DefaultConfiguration; +import org.apache.tamaya.spisupport.DefaultConfigurationContextBuilder; +import org.osgi.service.component.annotations.Component; + +import java.util.Objects; + +/** + * Implementation of the Configuration API. This class uses the current {@link org.apache.tamaya.spi.ConfigurationContext} to evaluate the + * chain of {@link org.apache.tamaya.spi.PropertySource} and {@link org.apache.tamaya.spi.PropertyFilter} + * instance to evaluate the current Configuration. + */ +@Component(service = ConfigurationProviderSpi.class) +public class CoreConfigurationProvider implements ConfigurationProviderSpi { + + private Configuration config = new CoreConfigurationBuilder() + .addDefaultPropertyConverters() + .addDefaultPropertyFilters() + .addDefaultPropertySources() + .build(); + { + String bannerConfig = config.getOrDefault("tamaya.banner", "OFF"); + + BannerManager bm = new BannerManager(bannerConfig); + bm.outputBanner(); + } + + @Override + public Configuration getConfiguration() { + return config; + } + + @Override + public Configuration createConfiguration(ConfigurationContext context) { + return new CoreConfiguration(context); + } + + @Override + public ConfigurationBuilder getConfigurationBuilder() { + return new CoreConfigurationBuilder(); + } + + @Override + public ConfigurationContextBuilder getConfigurationContextBuilder() { + return new DefaultConfigurationContextBuilder(); + } + + @Override + public void setConfiguration(Configuration config) { + Objects.requireNonNull(config.getContext()); + this.config = Objects.requireNonNull(config); + } + + @Override + public boolean isConfigurationSettable() { + return true; + } + + /** + * @deprecated use {@link Configuration#getContext()} instead. + */ + @Deprecated + @Override + public ConfigurationContext getConfigurationContext() { + return this.config.getContext(); + } + + /** + * @deprecated the context should be given upon creation of the {@link Configuration} + */ + @Deprecated + @Override + public void setConfigurationContext(ConfigurationContext context){ + this.config = new CoreConfigurationBuilder(context).build(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java deleted file mode 100644 index f37d4c3..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java +++ /dev/null @@ -1,106 +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.core.internal; - -import org.apache.tamaya.Configuration; -import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.ConfigurationContextBuilder; -import org.apache.tamaya.spi.ConfigurationProviderSpi; -import org.apache.tamaya.spisupport.DefaultConfiguration; -import org.osgi.service.component.annotations.Component; - -import java.util.Objects; - -/** - * Implementation of the Configuration API. This class uses the current {@link org.apache.tamaya.spi.ConfigurationContext} to evaluate the - * chain of {@link org.apache.tamaya.spi.PropertySource} and {@link org.apache.tamaya.spi.PropertyFilter} - * instance to evaluate the current Configuration. - */ -@Component(service = ConfigurationProviderSpi.class) -public class DefaultConfigurationProvider implements ConfigurationProviderSpi { - - ConfigurationContext context = new CoreConfigurationContextBuilder() - .addDefaultPropertyConverters() - .addDefaultPropertyFilters() - .addDefaultPropertySources() - .build(); - - private Configuration config = new DefaultConfiguration(context); - - { - String bannerConfig = config.getOrDefault("tamaya.banner", "OFF"); - - BannerManager bm = new BannerManager(bannerConfig); - bm.outputBanner(); - } - - @Override - public Configuration getConfiguration() { - return config; - } - - @Override - public Configuration createConfiguration(ConfigurationContext context) { - return new DefaultConfiguration(context); - } - - @Override - public ConfigurationContextBuilder getConfigurationContextBuilder() { - return new CoreConfigurationContextBuilder(); - } - - @Override - public void setConfiguration(Configuration config) { - Objects.requireNonNull(config.getContext()); - this.config = Objects.requireNonNull(config); - this.context = config.getContext(); - } - - @Override - public boolean isConfigurationSettable() { - return true; - } - - /** - * @deprecated use {@link Configuration#getContext()} instead. - */ - @Deprecated - @Override - public ConfigurationContext getConfigurationContext() { - return context; - } - - /** - * @deprecated the context should be given upon creation of the {@link Configuration} - */ - @Deprecated - @Override - public void setConfigurationContext(ConfigurationContext context){ - this.config = new DefaultConfiguration(context); - this.context = context; - } - - - @Deprecated - @Override - public boolean isConfigurationContextSettable() { - return true; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultServiceContext.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultServiceContext.java b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultServiceContext.java deleted file mode 100644 index 372ae2a..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultServiceContext.java +++ /dev/null @@ -1,205 +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.core.internal; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.spi.ServiceContext; -import org.apache.tamaya.spisupport.PriorityServiceComparator; - -import javax.annotation.Priority; -import java.io.IOException; -import java.net.URL; -import java.text.MessageFormat; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * This class implements the (default) {@link org.apache.tamaya.spi.ServiceContext} interface and hereby uses the JDK - * {@link java.util.ServiceLoader} to load the services required. - */ -public final class DefaultServiceContext implements ServiceContext { - private static final Logger LOG = Logger.getLogger(DefaultServiceContext.class.getName()); - /** - * List current services loaded, per class. - */ - private final ConcurrentHashMap<Class<?>, List<Object>> servicesLoaded = new ConcurrentHashMap<>(); - /** - * Singletons. - */ - private final Map<Class<?>, Object> singletons = new ConcurrentHashMap<>(); - @SuppressWarnings("rawtypes") - private Map<Class, Class> factoryTypes = new ConcurrentHashMap<>(); - - @Override - public <T> T getService(Class<T> serviceType) { - Object cached = singletons.get(serviceType); - if (cached == null) { - cached = create(serviceType); - if(cached!=null) { - singletons.put(serviceType, cached); - } - } - return serviceType.cast(cached); - } - - @Override - public <T> T create(Class<T> serviceType) { - @SuppressWarnings("unchecked") - Class<? extends T> implType = factoryTypes.get(serviceType); - if(implType==null) { - Collection<T> services = getServices(serviceType); - if (services.isEmpty()) { - return null; - } else { - return getServiceWithHighestPriority(services, serviceType); - } - } - try { - return implType.newInstance(); - } catch (Exception e) { - LOG.log(Level.SEVERE, "Failed to create instance of " + implType.getName(), e); - return null; - } - } - - /** - * Loads and registers services. - * - * @param <T> the concrete type. - * @param serviceType The service type. - * @return the items found, never {@code null}. - */ - @Override - public <T> List<T> getServices(final Class<T> serviceType) { - @SuppressWarnings("unchecked") - List<T> found = (List<T>) servicesLoaded.get(serviceType); - if (found != null) { - return found; - } - List<T> services = new ArrayList<>(); - try { - for (T t : ServiceLoader.load(serviceType)) { - services.add(t); - } - if(services.isEmpty()) { - for (T t : ServiceLoader.load(serviceType, serviceType.getClassLoader())) { - services.add(t); - } - } - Collections.sort(services, PriorityServiceComparator.getInstance()); - services = Collections.unmodifiableList(services); - } catch (ServiceConfigurationError e) { - LOG.log(Level.WARNING, - "Error loading services current type " + serviceType, e); - if(services==null){ - services = Collections.emptyList(); - } - } - @SuppressWarnings("unchecked") - final List<T> previousServices = List.class.cast(servicesLoaded.putIfAbsent(serviceType, (List<Object>) services)); - return previousServices != null ? previousServices : services; - } - - /** - * Checks the given instance for a @Priority annotation. If present the annotation's value is evaluated. If no such - * annotation is present, a default priority of {@code 1} is returned. - * @param o the instance, not {@code null}. - * @return a priority, by default 1. - */ - public static int getPriority(Object o){ - int prio = 1; //X TODO discuss default priority - Priority priority = o.getClass().getAnnotation(Priority.class); - if (priority != null) { - prio = priority.value(); - } - return prio; - } - - /** - * @param services to scan - * @param <T> type of the service - * - * @return the service with the highest {@link javax.annotation.Priority#value()} - * - * @throws ConfigException if there are multiple service implementations with the maximum priority - */ - private <T> T getServiceWithHighestPriority(Collection<T> services, Class<T> serviceType) { - T highestService = null; - // we do not need the priority stuff if the list contains only one element - if (services.size() == 1) { - highestService = services.iterator().next(); - this.factoryTypes.put(serviceType, highestService.getClass()); - return highestService; - } - - Integer highestPriority = null; - int highestPriorityServiceCount = 0; - - for (T service : services) { - int prio = getPriority(service); - if (highestPriority == null || highestPriority < prio) { - highestService = service; - highestPriorityServiceCount = 1; - highestPriority = prio; - } else if (highestPriority == prio) { - highestPriorityServiceCount++; - } - } - - if (highestPriorityServiceCount > 1) { - throw new ConfigException(MessageFormat.format("Found {0} implementations for Service {1} with Priority {2}: {3}", - highestPriorityServiceCount, - serviceType.getName(), - highestPriority, - services)); - } - this.factoryTypes.put(serviceType, highestService.getClass()); - return highestService; - } - - @Override - public int ordinal() { - return 1; - } - - @Override - public Enumeration<URL> getResources(String resource, ClassLoader cl) throws IOException { - if(cl==null){ - cl = Thread.currentThread().getContextClassLoader(); - } - if(cl==null){ - cl = getClass().getClassLoader(); - } - return cl.getResources(resource); - } - - @Override - public URL getResource(String resource, ClassLoader cl) { - if(cl==null){ - cl = Thread.currentThread().getContextClassLoader(); - } - if(cl==null){ - cl = getClass().getClassLoader(); - } - return cl.getResource(resource); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceContext.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceContext.java b/code/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceContext.java index ddbf692..b7328d9 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceContext.java +++ b/code/core/src/main/java/org/apache/tamaya/core/internal/OSGIServiceContext.java @@ -62,7 +62,7 @@ public class OSGIServiceContext implements ServiceContext{ } if(ConfigurationProviderSpi.class==serviceType){ @SuppressWarnings("unchecked") - T service = (T)new DefaultConfigurationProvider(); + T service = (T)new CoreConfigurationProvider(); this.osgiServiceLoader.getBundleContext().registerService( serviceType.getName(), service, http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationBuilder ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationBuilder b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationBuilder new file mode 100644 index 0000000..700b2b5 --- /dev/null +++ b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationBuilder @@ -0,0 +1,19 @@ +# +# 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. +# +org.apache.tamaya.core.internal.CoreConfigurationContextBuilder \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationContextBuilder ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationContextBuilder b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationContextBuilder deleted file mode 100644 index 700b2b5..0000000 --- a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationContextBuilder +++ /dev/null @@ -1,19 +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 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. -# -org.apache.tamaya.core.internal.CoreConfigurationContextBuilder \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi index 7c0e249..c4aa43f 100644 --- a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi +++ b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi @@ -16,4 +16,4 @@ # specific language governing permissions and limitations # under the License. # -org.apache.tamaya.core.internal.DefaultConfigurationProvider +org.apache.tamaya.core.internal.CoreConfigurationProvider http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/15f7cbbb/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext index f8e7e7f..135caa1 100644 --- a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext +++ b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ServiceContext @@ -16,4 +16,4 @@ # specific language governing permissions and limitations # under the License. # -org.apache.tamaya.core.internal.DefaultServiceContext +org.apache.tamaya.spisupport.DefaultServiceContext