TAMAYA-62: Unified Configuration access. Fixed some FindBugs and checkstyle issues.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/b9735e02 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/b9735e02 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/b9735e02 Branch: refs/heads/master Commit: b9735e025ba331f4c4cd011fa4e0c5eb2b898e32 Parents: b60af86 Author: anatole <[email protected]> Authored: Sat Jan 24 11:13:51 2015 +0100 Committer: anatole <[email protected]> Committed: Sat Jan 24 21:06:14 2015 +0100 ---------------------------------------------------------------------- .../java/org/apache/tamaya/ConfigQuery.java | 1 + .../java/org/apache/tamaya/Configuration.java | 67 ++++----- .../apache/tamaya/ConfigurationProvider.java | 25 ++-- .../org/apache/tamaya/PropertyConverter.java | 49 +++++++ .../java/org/apache/tamaya/TypeLiteral.java | 137 +++++++++++++++++++ .../apache/tamaya/spi/ConfigurationContext.java | 21 ++- .../tamaya/spi/ConfigurationProviderSpi.java | 45 ++++++ .../apache/tamaya/spi/PropertyConverter.java | 48 ------- .../org/apache/tamaya/spi/PropertySource.java | 2 +- .../org/apache/tamaya/ConfigurationTest.java | 34 ++--- .../org/apache/tamaya/TestConfiguration.java | 21 +-- .../tamaya/TestConfigurationProvider.java | 44 ++++++ .../services/org.apache.tamaya.Configuration | 19 --- ...g.apache.tamaya.spi.ConfigurationProviderSpi | 19 +++ .../core/internal/DefaultConfiguration.java | 17 ++- .../internal/DefaultConfigurationContext.java | 11 +- .../internal/DefaultConfigurationProvider.java | 44 ++++++ .../core/internal/PropertiesFileLoader.java | 3 + .../core/internal/PropertyConverterManager.java | 35 ++--- .../converters/BigDecimalConverter.java | 2 +- .../converters/BigIntegerConverter.java | 2 +- .../internal/converters/BooleanConverter.java | 2 +- .../core/internal/converters/ByteConverter.java | 2 +- .../core/internal/converters/CharConverter.java | 2 +- .../internal/converters/CurrencyConverter.java | 2 +- .../internal/converters/DoubleConverter.java | 2 +- .../core/internal/converters/EnumConverter.java | 3 +- .../internal/converters/FloatConverter.java | 2 +- .../internal/converters/IntegerConverter.java | 2 +- .../internal/converters/LocalDateConverter.java | 2 +- .../converters/LocalDateTimeConverter.java | 2 +- .../internal/converters/LocalTimeConverter.java | 2 +- .../core/internal/converters/LongConverter.java | 2 +- .../internal/converters/NumberConverter.java | 2 +- .../internal/converters/ShortConverter.java | 2 +- .../internal/converters/ZoneIdConverter.java | 2 +- .../org.apache.tamaya.PropertyConverter | 34 +++++ ...g.apache.tamaya.spi.ConfigurationProviderSpi | 19 +++ .../apache/tamaya/core/ConfigurationTest.java | 28 ++-- .../converters/BooleanConverterTest.java | 4 +- .../internal/converters/ByteConverterTest.java | 3 +- .../provider/JavaConfigurationProviderTest.java | 6 +- .../testdata/TestPropertyFilterRemoving.java | 4 +- .../tamaya/inject/WithPropertyConverter.java | 2 +- .../ConfigTemplateInvocationHandler.java | 3 +- .../tamaya/inject/internal/ConfiguredField.java | 3 +- .../inject/internal/ConfiguredSetterMethod.java | 3 +- .../tamaya/inject/internal/InjectionUtils.java | 20 +-- .../resolver/internal/ConfigResolver.java | 3 +- 49 files changed, 572 insertions(+), 237 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/main/java/org/apache/tamaya/ConfigQuery.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/ConfigQuery.java b/java8/api/src/main/java/org/apache/tamaya/ConfigQuery.java index 140ccba..6151d03 100644 --- a/java8/api/src/main/java/org/apache/tamaya/ConfigQuery.java +++ b/java8/api/src/main/java/org/apache/tamaya/ConfigQuery.java @@ -22,6 +22,7 @@ package org.apache.tamaya; * Models a function that maps a given {@link org.apache.tamaya.Configuration} to something else. This can be used * to model additional functionality and applying it to a given {@link org.apache.tamaya.Configuration} instance by * calling the {@link org.apache.tamaya.Configuration#query(org.apache.tamaya.ConfigQuery)} method. + * @param <T> the result type of the query. */ @FunctionalInterface public interface ConfigQuery<T> { http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/main/java/org/apache/tamaya/Configuration.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/Configuration.java b/java8/api/src/main/java/org/apache/tamaya/Configuration.java index ed1f918..4ee7f21 100644 --- a/java8/api/src/main/java/org/apache/tamaya/Configuration.java +++ b/java8/api/src/main/java/org/apache/tamaya/Configuration.java @@ -18,14 +18,11 @@ */ package org.apache.tamaya; -import org.apache.tamaya.spi.ServiceContext; - import java.util.Map; import java.util.Optional; import java.util.OptionalDouble; import java.util.OptionalInt; import java.util.OptionalLong; -import java.util.function.Function; /** @@ -48,52 +45,34 @@ import java.util.function.Function; public interface Configuration { /** - * Access a configuration. + * Access a property. * - * @return the corresponding Configuration instance, never null. - * @throws ConfigException if no such configuration is defined. + * @param key the property's key, not null. + * @return the property's value or {@code null}. */ - public static Configuration current() { - return ServiceContext.getInstance().getService(Configuration.class).get(); + default String get(String key) { + return get(key, String.class); } + /** - * Access a property. + * Get the property keys as type T. This will implicitly require a corresponding {@link + * PropertyConverter} to be available that is capable current providing type T + * fromMap the given String keys. * - * @param key the property's key, not null. - * @return the property's value or {@code null}. + * @param key the property's absolute, or relative path, e.g. @code + * a/b/c/d.myProperty}. + * @param type The target type required, not null. + * @return the property value, never null.. + * @throws ConfigException if the keys could not be converted to the required target type. */ - String get(String key); - -// /** -// * Get the property keys as type T. This will implicitly require a corresponding {@link -// * org.apache.tamaya.spi.PropertyConverter} to be available that is capable current providing type T -// * fromMap the given String keys. -// * -// * @param key the property's absolute, or relative path, e.g. @code -// * a/b/c/d.myProperty}. -// * @param type The target type required, not null. -// * @return the property value, never null.. -// * @throws ConfigException if the keys could not be converted to the required target type. -// */ -// <T> T getListProperty(String key, TypeLiteral<T> type); -// -// /** -// * Get the property keys as type T. This will implicitly require a corresponding {@link -// * org.apache.tamaya.spi.PropertyConverter} to be available that is capable current providing type T -// * fromMap the given String keys. -// * -// * @param key the property's absolute, or relative path, e.g. @code -// * a/b/c/d.myProperty}. -// * @param converter the converter to be used. -// * @return the property value, never null.. -// * @throws ConfigException if the keys could not be converted to the required target type. -// */ -// <T> T getListProperty(String key, Function<List<String>, T> converter); + 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 - * org.apache.tamaya.spi.PropertyConverter} to be available that is capable current providing type T + * PropertyConverter} to be available that is capable current providing type T * fromMap the given String keys. * * @param key the property's absolute, or relative path, e.g. @code @@ -102,7 +81,7 @@ public interface Configuration { * @return the property value, never null.. * @throws ConfigException if the keys could not be converted to the required target type. */ - <T> T get(String key, Class<T> type); + <T> T get(String key, TypeLiteral<T> type); /** * Access a property. @@ -116,7 +95,7 @@ public interface Configuration { /** * Get the property keys as type T. This will implicitly require a corresponding {@link - * org.apache.tamaya.spi.PropertyConverter} to be available that is capable current providing type T + * PropertyConverter} to be available that is capable current providing type T * fromMap the given String keys. * * @param key the property's absolute, or relative path, e.g. @code @@ -135,6 +114,7 @@ public interface Configuration { * instances may not be contained in the result, but nevertheless be accessible calling one of the * {@code get(...)} methods. */ + @SuppressWarnings("JavaDoc") Map<String, String> getProperties(); /** @@ -154,14 +134,15 @@ public interface Configuration { * @throws ConfigException if the keys could not be converted to the required target * type, or no such property exists. */ - default <T> Optional<T> getOptional(String key, Function<String, T> converter) { + default <T> Optional<T> getOptional(String key, PropertyConverter<T> converter) { Optional<String> value = getOptional(key); if (value.isPresent()) { - return Optional.ofNullable(converter.apply(value.get())); + return Optional.ofNullable(converter.convert(value.get())); } return Optional.empty(); } + /** * Get the property keys as {@link Boolean}. * http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java b/java8/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java index 7a64caa..183786c 100644 --- a/java8/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java +++ b/java8/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java @@ -16,17 +16,18 @@ */ package org.apache.tamaya; +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConfigurationProviderSpi; import org.apache.tamaya.spi.ServiceContextManager; /** * Static access to the {@link org.apache.tamaya.Configuration} for the very application. - * <p> - * Exists for Java7 backward compatibility only. - * - * @deprecated Since Java 8, you better use {@link org.apache.tamaya.Configuration#current()}. */ -@Deprecated public final class ConfigurationProvider { + + private static final ConfigurationProviderSpi PROVIDER_SPI = ServiceContextManager.getServiceContext() + .getService(ConfigurationProviderSpi.class).get(); + private ConfigurationProvider() { // just to prevent initialisation } @@ -35,10 +36,18 @@ public final class ConfigurationProvider { * Access the current configuration. * * @return the corresponding Configuration instance, never null. - * @deprecated Since Java 8, you better use {@link org.apache.tamaya.Configuration#current()}. */ - @Deprecated public static Configuration getConfiguration() { - return ServiceContextManager.getServiceContext().getService(Configuration.class).get(); + return PROVIDER_SPI.getConfiguration(); } + + /** + * Get access to the current ConfigurationContext. + * + * @return the current ConfigurationContext, never null. + */ + public static ConfigurationContext getConfigurationContext(){ + return PROVIDER_SPI.getConfigurationContext(); + } + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/main/java/org/apache/tamaya/PropertyConverter.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/PropertyConverter.java b/java8/api/src/main/java/org/apache/tamaya/PropertyConverter.java new file mode 100644 index 0000000..4f6ea80 --- /dev/null +++ b/java8/api/src/main/java/org/apache/tamaya/PropertyConverter.java @@ -0,0 +1,49 @@ +/* + * 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; + + +/** + * Interface for an property that converts a configured String into something else. + * This is used for implementing type conversion from a property (String) to a certain target + * type. Hereby the target type can be multivalued (eg collections) or complex if needed. + * @param <T> the result/target type of the converter. + */ +public interface PropertyConverter<T> { + + /** + * Convert the given configuration keys from it's String representation into the required target type. + * <b>IMPORTANT NOTE: </b> Multiple instances of this type are ordered in a chain of converters that + * try to parse/convert a configured value. The first non-null result returned by a converter is + * used as the final result of the conversion. As a consequence implementations of this class must + * only return non-null values that are the result of a successful conversion of an entry. + * + * @param value the configuration value, not null. + * @return the converted value, or {@code null} if the value could not be converted. Returning a non-null + * value will stop the conversion process and return the value as result (converted value). + */ + T convert(String value); + + //X TODO probably add some diagnostic info which explains what kind of + //X format(s) is supported. + //X This could be useful if e.g. no converter in the chain felt responsible + //X because a wrongly formatted configuration string had been used. + //X This could probably also be handled via an additional Annotation on the converter. + //X Collection<String> getSupportedFormats(); +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/main/java/org/apache/tamaya/TypeLiteral.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/TypeLiteral.java b/java8/api/src/main/java/org/apache/tamaya/TypeLiteral.java new file mode 100644 index 0000000..57e7ec7 --- /dev/null +++ b/java8/api/src/main/java/org/apache/tamaya/TypeLiteral.java @@ -0,0 +1,137 @@ +/* +* 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; + +import java.io.Serializable; +import java.lang.reflect.GenericArrayType; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + + +/** +* <p>Class for instantiation of objects that represent parameterized types +* with current parameters.</p> +* <p> +* <p>An object that represents a parameterized type may be obtained by +* subclassing <tt>TypeLiteral</tt>.</p> +* <p> +* <pre> +* TypeLiteral<List<Integer>> stringListType = new TypeLiteral<List<Integer>>() {}; +* </pre> +* +* @param <T> the type, including all type parameters +*/ +public class TypeLiteral<T> implements Serializable { + + private static final long serialVersionUID = 1L; + private Type type; + + protected TypeLiteral(Type type) { + this.type = type; + } + + protected TypeLiteral() { } + + public static <T> TypeLiteral<T> of(Type type){ + return new TypeLiteral<>(type); + } + + private static Class<?> getTypeLiteralSubclass(Class<?> clazz) { + Class<?> superclass = clazz.getSuperclass(); + if (superclass.equals(TypeLiteral.class)) { + return clazz; + } else if (superclass.equals(Object.class)) { + return null; + } else { + return (getTypeLiteralSubclass(superclass)); + } + } + + private static Type getTypeParameter(Class<?> superclass) { + Type type = superclass.getGenericSuperclass(); + if (type instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) type; + if (parameterizedType.getActualTypeArguments().length == 1) { + return parameterizedType.getActualTypeArguments()[0]; + } + } + return null; + } + + /** + * @return the actual type represented by this object + */ + public final Type getType() { + if (type == null) { + Class<?> typeLiteralSubclass = getTypeLiteralSubclass(this.getClass()); + if (typeLiteralSubclass == null) { + throw new RuntimeException(getClass() + " is not a subclass of TypeLiteral"); + } + type = getTypeParameter(typeLiteralSubclass); + if (type == null) { + throw new RuntimeException(getClass() + " does not specify the type parameter T of TypeLiteral<T>"); + } + } + return type; + } + + /** + * @return the raw type represented by this object + */ + @SuppressWarnings("unchecked") + public final Class<T> getRawType() { + Type type = getType(); + if (type instanceof Class) { + return (Class<T>) type; + } else if (type instanceof ParameterizedType) { + return (Class<T>) ((ParameterizedType) type).getRawType(); + } else if (type instanceof GenericArrayType) { + return (Class<T>) Object[].class; + } else { + throw new RuntimeException("Illegal type"); + } + } + + @Override + public boolean equals(Object o) { + if (this == o){ + return true; + } + if (!(o instanceof TypeLiteral)){ + return false; + } + TypeLiteral that = (TypeLiteral) o; + if (type != null ? !type.equals(that.type) : that.type != null){ + return false; + } + return true; + } + + @Override + public int hashCode() { + int result = type != null ? type.hashCode() : 0; + return result; + } + + @Override + public String toString() { + return "TypeLiteral{" + + "type=" + type + + '}'; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java b/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java index e5d9ece..1848428 100644 --- a/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java +++ b/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationContext.java @@ -19,22 +19,21 @@ package org.apache.tamaya.spi; +import org.apache.tamaya.PropertyConverter; +import org.apache.tamaya.TypeLiteral; + import java.util.List; import java.util.Map; /** * Central SPI for programmatically dealing with the setup of the configuration system. * This includes adding and enlisting {@link org.apache.tamaya.spi.PropertySource}s, - * managing {@link org.apache.tamaya.spi.PropertyConverter}s, ConfigFilters, etc. + * managing {@link org.apache.tamaya.PropertyConverter}s, ConfigFilters, etc. */ public interface ConfigurationContext { - public static ConfigurationContext context() { - return ServiceContext.getInstance().getService(ConfigurationContext.class).get(); - } - /** - * This method can be used for programmatically adding {@link PropertySource}s. + * This method can be used for programmatically adding {@link org.apache.tamaya.spi.PropertySource}s. * It is not needed for normal 'usage' by end users, but only for Extension Developers! * * @param propertySourcesToAdd the PropertySources to add @@ -56,20 +55,20 @@ public interface ConfigurationContext { List<PropertySource> getPropertySources(); /** - * This method can be used for programmatically adding {@link PropertyConverter}s. + * This method can be used for programmatically adding {@link org.apache.tamaya.PropertyConverter}s. * It is not needed for normal 'usage' by end users, but only for Extension Developers! * * @param typeToConvert the type which the converter is for * @param propertyConverter the PropertyConverters to add for this type */ - <T> void addPropertyConverter(Class<T> typeToConvert, PropertyConverter<T> propertyConverter); + <T> void addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter); /** * <p> * This method returns the Map of registered PropertyConverters * per type. * The List for each type is ordered via their {@link javax.annotation.Priority} and - * cladd name. Refer also to {@link #getPropertyConverters()}. + * class name. Refer also to {@link #getPropertyConverters()}. * </p> * <p> * A simplified scenario could be like: @@ -84,7 +83,7 @@ public interface ConfigurationContext { * * @return map with sorted list of registered PropertySources per type. */ - Map<Class<?>, List<PropertyConverter<?>>> getPropertyConverters(); + Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters(); /** * <p> @@ -133,7 +132,7 @@ public interface ConfigurationContext { * * @return a sorted list of registered PropertySources per type. */ - <T> List<PropertyConverter<T>> getPropertyConverters(Class<T> type); + <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> type); /** * Access the current PropertyFilter instances. http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java b/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java new file mode 100644 index 0000000..577163b --- /dev/null +++ b/java8/api/src/main/java/org/apache/tamaya/spi/ConfigurationProviderSpi.java @@ -0,0 +1,45 @@ +/* + * 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; + +/** + * SPI that must be implemented to provide the component that manages all {@link org.apache.tamaya.Configuration} + * instances in a system. In SE this may be a true singleton containing exact one {@link org.apache.tamaya.Configuration} + * instance, whereas in Java EE and other more complex environments instances may be returned depending the current + * runtime context. + */ +public interface ConfigurationProviderSpi { + + /** + * Access the current {@link org.apache.tamaya.Configuration}. + * + * @return the current {@link org.apache.tamaya.Configuration} instance, never null. + */ + Configuration getConfiguration(); + + /** + * Get access to the current {@link org.apache.tamaya.spi.ConfigurationContext}. + * + * @return the current {@link org.apache.tamaya.spi.ConfigurationContext}, never null. + */ + ConfigurationContext getConfigurationContext(); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/main/java/org/apache/tamaya/spi/PropertyConverter.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/spi/PropertyConverter.java b/java8/api/src/main/java/org/apache/tamaya/spi/PropertyConverter.java deleted file mode 100644 index 235eebc..0000000 --- a/java8/api/src/main/java/org/apache/tamaya/spi/PropertyConverter.java +++ /dev/null @@ -1,48 +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.spi; - - -/** - * Interface for an property that converts a configured String into something else. - * This is used for implementing type conversion from a property (String) to a certain target - * type. Hereby the target type can be multivalued (eg collections) or complex if needed. - */ -public interface PropertyConverter<T> { - - /** - * Convert the given configuration keys from it's String representation into the required target type. - * <b>IMPORTANT NOTE: </b> Multiple instances of this type are ordered in a chain of converters that - * try to parse/convert a configured value. The first non-null result returned by a converter is - * used as the final result of the conversion. As a consequence implementations of this class must - * only return non-null values that are the result of a successful conversion of an entry. - * - * @param value the configuration value - * @return the converted value, or {@code null} if the value could not be converted. Returning a non-null - * value will stop the conversion process and return the value as result (converted value). - */ - T convert(String value); - - //X TODO probably add some diagnostic info which explains what kind of - //X format(s) is supported. - //X This could be useful if e.g. no converter in the chain felt responsible - //X because a wrongly formatted configuration string had been used. - //X This could probably also be handled via an additional Annotation on the converter. - //X Collection<String> getSupportedFormats(); -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/main/java/org/apache/tamaya/spi/PropertySource.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/spi/PropertySource.java b/java8/api/src/main/java/org/apache/tamaya/spi/PropertySource.java index 270a1ff..b8622a9 100644 --- a/java8/api/src/main/java/org/apache/tamaya/spi/PropertySource.java +++ b/java8/api/src/main/java/org/apache/tamaya/spi/PropertySource.java @@ -85,7 +85,7 @@ public interface PropertySource { /** * Get the name of the property source. The name should be unique for the type of source, whereas multiple instances - * of the same type (and thus name) may exist in a system. Give a {@link org.apache.tamaya.spi.ConfigurationContext} + * of the same type (and thus name) may exist in a system. Give a {@link ConfigurationContext} * the name of a PropertySource is unique. * * @return the property source's name, never null. http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/test/java/org/apache/tamaya/ConfigurationTest.java ---------------------------------------------------------------------- diff --git a/java8/api/src/test/java/org/apache/tamaya/ConfigurationTest.java b/java8/api/src/test/java/org/apache/tamaya/ConfigurationTest.java index 1ca8d29..28d25ba 100644 --- a/java8/api/src/test/java/org/apache/tamaya/ConfigurationTest.java +++ b/java8/api/src/test/java/org/apache/tamaya/ConfigurationTest.java @@ -28,51 +28,51 @@ public class ConfigurationTest { @org.junit.Test public void testget() throws Exception { - assertEquals(Boolean.TRUE, Configuration.current().getOptional("booleanTrue", (s) -> Boolean.valueOf(s)).get()); - assertEquals(Boolean.FALSE, Configuration.current().getOptional("booleanFalse", (s) -> Boolean.valueOf(s)).get()); - assertEquals((int) Byte.MAX_VALUE, (int) Configuration.current().getOptional("byte", (s) -> Byte.valueOf(s)).get()); - assertEquals((int) Integer.MAX_VALUE, (int) Configuration.current().getOptional("int", (s) -> Integer.valueOf(s)).get()); - assertEquals((long) Long.MAX_VALUE, (long) Configuration.current().getOptional("long", (s) -> Long.valueOf(s)).get()); - assertEquals((double) Float.MAX_VALUE, (double) Configuration.current().getOptional("float", (s) -> Float.valueOf(s)).get(), 0.0d); - assertEquals((double) Double.MAX_VALUE, (double) Configuration.current().getOptional("double", (s) -> Double.valueOf(s)).get(), 0.0d); + assertEquals(Boolean.TRUE, ConfigurationProvider.getConfiguration().getOptional("booleanTrue", (s) -> Boolean.valueOf(s)).get()); + assertEquals(Boolean.FALSE, ConfigurationProvider.getConfiguration().getOptional("booleanFalse", (s) -> Boolean.valueOf(s)).get()); + assertEquals((int) Byte.MAX_VALUE, (int) ConfigurationProvider.getConfiguration().getOptional("byte", (s) -> Byte.valueOf(s)).get()); + assertEquals((int) Integer.MAX_VALUE, (int) ConfigurationProvider.getConfiguration().getOptional("int", (s) -> Integer.valueOf(s)).get()); + assertEquals((long) Long.MAX_VALUE, (long) ConfigurationProvider.getConfiguration().getOptional("long", (s) -> Long.valueOf(s)).get()); + assertEquals((double) Float.MAX_VALUE, (double) ConfigurationProvider.getConfiguration().getOptional("float", (s) -> Float.valueOf(s)).get(), 0.0d); + assertEquals((double) Double.MAX_VALUE, (double) ConfigurationProvider.getConfiguration().getOptional("double", (s) -> Double.valueOf(s)).get(), 0.0d); } @org.junit.Test public void testGetBoolean() throws Exception { - assertTrue(Configuration.current().getBoolean("booleanTrue")); - assertFalse(Configuration.current().getBoolean("booleanFalse")); - assertFalse(Configuration.current().getBoolean("foorBar")); + assertTrue(ConfigurationProvider.getConfiguration().getBoolean("booleanTrue")); + assertFalse(ConfigurationProvider.getConfiguration().getBoolean("booleanFalse")); + assertFalse(ConfigurationProvider.getConfiguration().getBoolean("foorBar")); } @org.junit.Test public void testGetInteger() throws Exception { - assertEquals(Integer.MAX_VALUE, Configuration.current().getInteger("int").getAsInt()); + assertEquals(Integer.MAX_VALUE, ConfigurationProvider.getConfiguration().getInteger("int").getAsInt()); } @org.junit.Test public void testGetLong() throws Exception { - assertEquals(Long.MAX_VALUE, Configuration.current().getLong("long").getAsLong()); + assertEquals(Long.MAX_VALUE, ConfigurationProvider.getConfiguration().getLong("long").getAsLong()); } @org.junit.Test public void testGetDouble() throws Exception { - assertEquals(Double.MAX_VALUE, Configuration.current().getDouble("double").getAsDouble(), 0.0d); + assertEquals(Double.MAX_VALUE, ConfigurationProvider.getConfiguration().getDouble("double").getAsDouble(), 0.0d); } @org.junit.Test public void testWith() throws Exception { - assertEquals(Configuration.current(), Configuration.current().with(c -> c)); + assertEquals(ConfigurationProvider.getConfiguration(), ConfigurationProvider.getConfiguration().with(c -> c)); } @org.junit.Test public void testQuery() throws Exception { - assertEquals("myFooResult", Configuration.current().query(c -> "myFooResult")); + assertEquals("myFooResult", ConfigurationProvider.getConfiguration().query(c -> "myFooResult")); } @org.junit.Test public void testGetAdapted() throws Exception { - assertEquals("yes", Configuration.current().getOptional("booleanTrue", (v) -> Boolean.parseBoolean(v) ? "yes" : "no").get()); - assertEquals("no", Configuration.current().getOptional("booleanFalse", (v) -> Boolean.parseBoolean(v) ? "yes" : "no").get()); + assertEquals("yes", ConfigurationProvider.getConfiguration().getOptional("booleanTrue", (v) -> Boolean.parseBoolean(v) ? "yes" : "no").get()); + assertEquals("no", ConfigurationProvider.getConfiguration().getOptional("booleanFalse", (v) -> Boolean.parseBoolean(v) ? "yes" : "no").get()); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/test/java/org/apache/tamaya/TestConfiguration.java ---------------------------------------------------------------------- diff --git a/java8/api/src/test/java/org/apache/tamaya/TestConfiguration.java b/java8/api/src/test/java/org/apache/tamaya/TestConfiguration.java index c572dff..2501b32 100644 --- a/java8/api/src/test/java/org/apache/tamaya/TestConfiguration.java +++ b/java8/api/src/test/java/org/apache/tamaya/TestConfiguration.java @@ -18,6 +18,8 @@ */ package org.apache.tamaya; +import org.apache.tamaya.spi.ConfigurationContext; + import java.util.HashMap; import java.util.Map; @@ -47,26 +49,26 @@ public class TestConfiguration implements Configuration { } @Override - public <T> T get(String key, Class<T> type) { - if (type.equals(Long.class)) { + public <T> T get(String key, TypeLiteral<T> type) { + if (type.getType().equals(Long.class)) { return (T) (Object) Long.MAX_VALUE; - } else if (type.equals(Integer.class)) { + } else if (type.getType().equals(Integer.class)) { return (T) (Object) Integer.MAX_VALUE; - } else if (type.equals(Double.class)) { + } else if (type.getType().equals(Double.class)) { return (T) (Object) Double.MAX_VALUE; - } else if (type.equals(Float.class)) { + } else if (type.getType().equals(Float.class)) { return (T) (Object) Float.MAX_VALUE; - } else if (type.equals(Short.class)) { + } else if (type.getType().equals(Short.class)) { return (T) (Object) Short.MAX_VALUE; - } else if (type.equals(Byte.class)) { + } else if (type.getType().equals(Byte.class)) { return (T) (Object) Byte.MAX_VALUE; - } else if (type.equals(Boolean.class)) { + } else if (type.getType().equals(Boolean.class)) { if ("booleanTrue".equals(key)) { return (T) (Object) Boolean.TRUE; } else { return (T) (Object) Boolean.FALSE; } - } else if (type.equals(String.class)) { + } else if (type.getType().equals(String.class)) { return (T) (Object) "aStringValue"; } throw new ConfigException("No such property: " + key); @@ -76,4 +78,5 @@ public class TestConfiguration implements Configuration { public Map<String, String> getProperties() { return null; } + } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/java8/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java b/java8/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java new file mode 100644 index 0000000..f2dac28 --- /dev/null +++ b/java8/api/src/test/java/org/apache/tamaya/TestConfigurationProvider.java @@ -0,0 +1,44 @@ +/* + * 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; + +import org.apache.tamaya.spi.ConfigurationContext; +import org.apache.tamaya.spi.ConfigurationProviderSpi; + +import java.util.HashMap; +import java.util.Map; + +/** + * Test Configuration class, that is used to testdata the default methods provided by the API. + */ +public class TestConfigurationProvider implements ConfigurationProviderSpi { + + private static final Configuration config = new TestConfiguration(); + + + @Override + public Configuration getConfiguration() { + return config; + } + + @Override + public ConfigurationContext getConfigurationContext() { + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/test/resources/META-INF/services/org.apache.tamaya.Configuration ---------------------------------------------------------------------- diff --git a/java8/api/src/test/resources/META-INF/services/org.apache.tamaya.Configuration b/java8/api/src/test/resources/META-INF/services/org.apache.tamaya.Configuration deleted file mode 100644 index 1f42438..0000000 --- a/java8/api/src/test/resources/META-INF/services/org.apache.tamaya.Configuration +++ /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.TestConfiguration \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/api/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi ---------------------------------------------------------------------- diff --git a/java8/api/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi b/java8/api/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi new file mode 100644 index 0000000..b9c5ba5 --- /dev/null +++ b/java8/api/src/test/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi @@ -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.TestConfigurationProvider http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java index e862d02..60fcac5 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfiguration.java @@ -20,17 +20,18 @@ package org.apache.tamaya.core.internal; import org.apache.tamaya.ConfigException; import org.apache.tamaya.Configuration; +import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import org.apache.tamaya.spi.PropertyFilter; import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.ServiceContext; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -55,7 +56,11 @@ public class DefaultConfiguration implements Configuration { /** * The current {@link org.apache.tamaya.spi.ConfigurationContext} of the current instance. */ - private final ConfigurationContext configurationContext = ServiceContext.getInstance().getService(ConfigurationContext.class).get(); + private final ConfigurationContext configurationContext; + + public DefaultConfiguration(ConfigurationContext context){ + this.configurationContext = Objects.requireNonNull(context); + } /** * This method evaluates the given configuration key. Hereby if goes down the chain or PropertySource instances @@ -194,7 +199,7 @@ public class DefaultConfiguration implements Configuration { /** * Accesses the current String value for the given key (see {@link #getOptional(String)}) and tries to convert it - * using the {@link org.apache.tamaya.spi.PropertyConverter} instances provided by the current + * using the {@link org.apache.tamaya.PropertyConverter} instances provided by the current * {@link org.apache.tamaya.spi.ConfigurationContext}. * * @param key the property's absolute, or relative path, e.g. @code @@ -204,7 +209,7 @@ public class DefaultConfiguration implements Configuration { * @return the converted value, never null. */ @Override - public <T> T get(String key, Class<T> type) { + public <T> T get(String key, TypeLiteral<T> type) { Optional<String> value = getOptional(key); if (value.isPresent()) { List<PropertyConverter<T>> converters = configurationContext.getPropertyConverters(type); @@ -219,7 +224,7 @@ public class DefaultConfiguration implements Configuration { " failed to convert value: " + value.get()); } } - throw new ConfigException("Unparseable config value for type: " + type.getName() + ": " + key); + throw new ConfigException("Unparseable config value for type: " + type.getType() + ": " + key); } return null; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java index b6acae5..8b17c77 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContext.java @@ -18,8 +18,9 @@ */ package org.apache.tamaya.core.internal; +import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConfigurationContext; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import org.apache.tamaya.spi.PropertyFilter; import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertySourceProvider; @@ -40,7 +41,7 @@ import java.util.concurrent.locks.StampedLock; */ public class DefaultConfigurationContext implements ConfigurationContext { /** - * Cubcomponent handling {@link org.apache.tamaya.spi.PropertyConverter} instances. + * Cubcomponent handling {@link org.apache.tamaya.PropertyConverter} instances. */ private PropertyConverterManager propertyConverterManager = new PropertyConverterManager(); @@ -161,17 +162,17 @@ public class DefaultConfigurationContext implements ConfigurationContext { } @Override - public <T> void addPropertyConverter(Class<T> typeToConvert, PropertyConverter<T> propertyConverter) { + public <T> void addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter) { propertyConverterManager.register(typeToConvert, propertyConverter); } @Override - public Map<Class<?>, List<PropertyConverter<?>>> getPropertyConverters() { + public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { return propertyConverterManager.getPropertyConverters(); } @Override - public <T> List<PropertyConverter<T>> getPropertyConverters(Class<T> targetType) { + public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) { return propertyConverterManager.getPropertyConverters(targetType); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java new file mode 100644 index 0000000..bcef9af --- /dev/null +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java @@ -0,0 +1,44 @@ +/* + * 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.ConfigurationProviderSpi; + +/** + * 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. + */ +public class DefaultConfigurationProvider implements ConfigurationProviderSpi { + + private ConfigurationContext context = new DefaultConfigurationContext(); + private Configuration config = new DefaultConfiguration(context); + + @Override + public Configuration getConfiguration() { + return config; + } + + @Override + public ConfigurationContext getConfigurationContext() { + return context; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java index b79a756..f1abdd7 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertiesFileLoader.java @@ -27,6 +27,9 @@ import java.util.Objects; import java.util.Properties; import java.util.Set; +/** + * Minimalistic loader for property files from the classpath. + */ public final class PropertiesFileLoader { http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java index 15af9ac..e698634 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java @@ -34,19 +34,20 @@ import java.util.concurrent.locks.StampedLock; import java.util.logging.Logger; import org.apache.tamaya.ConfigException; +import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.core.internal.converters.EnumConverter; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import org.apache.tamaya.spi.ServiceContext; /** - * Manager that deals with {@link org.apache.tamaya.spi.PropertyConverter} instances. + * Manager that deals with {@link org.apache.tamaya.PropertyConverter} instances. * This class is thread-safe. */ public class PropertyConverterManager { /** The logger used. */ private static final Logger LOG = Logger.getLogger(PropertyConverterManager.class.getName()); /** The registered converters. */ - private Map<Class<?>, List<PropertyConverter<?>>> converters = new ConcurrentHashMap<>(); + private Map<TypeLiteral<?>, List<PropertyConverter<?>>> converters = new ConcurrentHashMap<>(); /** The lock used. */ private StampedLock lock = new StampedLock(); private static final String CHAR_NULL_ERROR = "Cannot convert null property"; @@ -68,7 +69,7 @@ public class PropertyConverterManager { conv.getClass().getName()); } else { Type targetType = type.getActualTypeArguments()[0]; - register((Class<?>)targetType, conv); + register(TypeLiteral.of(targetType), conv); } } } @@ -80,7 +81,7 @@ public class PropertyConverterManager { * @param converter the converter, not null. * @param <T> the type. */ - public <T> void register(Class<T> targetType, PropertyConverter<T> converter) { + public <T> void register(TypeLiteral<T> targetType, PropertyConverter<T> converter) { Objects.requireNonNull(converter); Lock writeLock = lock.asWriteLock(); try { @@ -103,7 +104,7 @@ public class PropertyConverterManager { * @param targetType the target type, not null. * @return true, if a converter for the given type is registered, or a default one can be created. */ - public boolean isTargetTypeSupported(Class<?> targetType) { + public boolean isTargetTypeSupported(TypeLiteral<?> targetType) { return converters.containsKey(targetType) || createDefaultPropertyConverter(targetType) != null; } @@ -114,9 +115,9 @@ public class PropertyConverterManager { * factory methods taking a single String instance. * * @return the current map of instantiated and registered converters. - * @see #createDefaultPropertyConverter(Class) + * @see #createDefaultPropertyConverter(org.apache.tamaya.TypeLiteral) */ - public Map<Class<?>, List<PropertyConverter<?>>> getPropertyConverters() { + public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { Lock readLock = lock.asReadLock(); try { readLock.lock(); @@ -134,9 +135,9 @@ public class PropertyConverterManager { * @param targetType the target type, not null. * @param <T> the type class * @return the ordered list of converters (may be empty for not convertible types). - * @see #createDefaultPropertyConverter(Class) + * @see #createDefaultPropertyConverter(org.apache.tamaya.TypeLiteral) */ - public <T> List<PropertyConverter<T>> getPropertyConverters(Class<T> targetType) { + public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) { Lock readLock = lock.asReadLock(); List<PropertyConverter<T>> converters; try { @@ -170,17 +171,17 @@ public class PropertyConverterManager { * @param <T> the type class * @return a new converter, or null. */ - protected <T> PropertyConverter<T> createDefaultPropertyConverter(Class<T> targetType) { - if(Enum.class.isAssignableFrom(targetType)){ - return new EnumConverter<T>(targetType); + protected <T> PropertyConverter<T> createDefaultPropertyConverter(TypeLiteral<T> targetType) { + if(Enum.class.isAssignableFrom(targetType.getRawType())){ + return new EnumConverter<T>(targetType.getRawType()); } PropertyConverter<T> converter = null; - Method factoryMethod = getFactoryMethod(targetType, "of", "valueOf", "instanceOf", "getInstance", "from", "fromString", "parse"); + Method factoryMethod = getFactoryMethod(targetType.getRawType(), "of", "valueOf", "instanceOf", "getInstance", "from", "fromString", "parse"); if (factoryMethod != null) { converter = (value) -> { try { factoryMethod.setAccessible(true); - return targetType.cast(factoryMethod.invoke(value)); + return targetType.getRawType().cast(factoryMethod.invoke(value)); } catch (Exception e) { throw new ConfigException("Failed to decode '" + value + "'", e); } @@ -188,7 +189,7 @@ public class PropertyConverterManager { } if (converter == null) { try { - Constructor<T> constr = targetType.getDeclaredConstructor(String.class); + Constructor<T> constr = targetType.getRawType().getDeclaredConstructor(String.class); converter = (value) -> { try { constr.setAccessible(true); @@ -198,7 +199,7 @@ public class PropertyConverterManager { } }; } catch (Exception e) { - LOG.finest(() -> "Failed to construct instance of type: " + targetType.getName()+": " + e); + LOG.finest(() -> "Failed to construct instance of type: " + targetType.getRawType().getName()+": " + e); } } return converter; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java index f927e22..87f3d14 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.math.BigDecimal; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java index f0eba47..4db9bcd 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.math.BigInteger; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java index 76fd4d7..754b4b7 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import javax.annotation.CheckForNull; import java.util.Locale; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java index 514f2df..6e52ef1 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java index 829c16b..f3773bb 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java index c3c6e56..4d033eb 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.util.Currency; import java.util.Locale; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java index 8eb016b..1814cba 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java index 652c808..05f29ad 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java @@ -19,7 +19,7 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.ConfigException; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -28,6 +28,7 @@ import java.util.logging.Logger; /** * Converter, converting from String to tge given enum type. + * @param <T> the enumeration type. */ public class EnumConverter<T> implements PropertyConverter<T> { private Logger LOG = Logger.getLogger(EnumConverter.class.getName()); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java index 839dbe0..955197b 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java index c67e587..923b132 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java index 7c223dd..c652732 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.time.LocalDate; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java index 0dd3351..5125231 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalDateTimeConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.time.LocalDateTime; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java index 031b028..d46fcc0 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LocalTimeConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.time.LocalTime; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java index 600d875..67434d2 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java index 6c8094f..3fdc68d 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.math.BigDecimal; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java index 8a71ef7..e41c1d6 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ZoneIdConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ZoneIdConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ZoneIdConverter.java index d2134e0..aa9ee66 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ZoneIdConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ZoneIdConverter.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.internal.converters; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.time.ZoneId; import java.util.Objects; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/resources/META-INF/services/org.apache.tamaya.PropertyConverter ---------------------------------------------------------------------- diff --git a/java8/core/src/main/resources/META-INF/services/org.apache.tamaya.PropertyConverter b/java8/core/src/main/resources/META-INF/services/org.apache.tamaya.PropertyConverter new file mode 100644 index 0000000..964843c --- /dev/null +++ b/java8/core/src/main/resources/META-INF/services/org.apache.tamaya.PropertyConverter @@ -0,0 +1,34 @@ +# +# 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.converters.BooleanConverter +org.apache.tamaya.core.internal.converters.ByteConverter +org.apache.tamaya.core.internal.converters.CharConverter +org.apache.tamaya.core.internal.converters.DoubleConverter +org.apache.tamaya.core.internal.converters.FloatConverter +org.apache.tamaya.core.internal.converters.IntegerConverter +org.apache.tamaya.core.internal.converters.LongConverter +org.apache.tamaya.core.internal.converters.ShortConverter +org.apache.tamaya.core.internal.converters.BigDecimalConverter +org.apache.tamaya.core.internal.converters.BigIntegerConverter +org.apache.tamaya.core.internal.converters.CurrencyConverter +org.apache.tamaya.core.internal.converters.LocalDateConverter +org.apache.tamaya.core.internal.converters.LocalDateTimeConverter +org.apache.tamaya.core.internal.converters.LocalTimeConverter +org.apache.tamaya.core.internal.converters.ZoneIdConverter +org.apache.tamaya.core.internal.converters.NumberConverter http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi ---------------------------------------------------------------------- diff --git a/java8/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi b/java8/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi new file mode 100644 index 0000000..7c0e249 --- /dev/null +++ b/java8/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.ConfigurationProviderSpi @@ -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.DefaultConfigurationProvider http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java b/java8/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java index c2ddbf8..3e2b160 100644 --- a/java8/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java +++ b/java8/core/src/test/java/org/apache/tamaya/core/ConfigurationTest.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core; -import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -32,25 +32,25 @@ public class ConfigurationTest { @Test public void testAccess(){ - assertNotNull(Configuration.current()); + assertNotNull(ConfigurationProvider.getConfiguration()); } @Test public void testContent(){ - assertEquals("Robin", Configuration.current().getOptional("name").get()); - assertEquals("Sabine", Configuration.current().getOptional("name2").get()); // from default - assertEquals("Mapped to name: Robin", Configuration.current().getOptional("name3").get()); // oderridden default, mapped by filter to name property - assertEquals("Sereina(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)", Configuration.current().getOptional("name4").get()); // final only - assertNull(Configuration.current().getOptional("name5").orElse(null)); // final only, but removed from filter - - System.out.println("name : " + Configuration.current().getOptional("name").get()); - System.out.println("name2: " + Configuration.current().getOptional("name2").get()); - System.out.println("name3: " + Configuration.current().getOptional("name3").get()); - System.out.println("name4: " + Configuration.current().getOptional("name4").get()); - System.out.println("name5: " + Configuration.current().getOptional("name5").orElse(null)); + assertEquals("Robin", ConfigurationProvider.getConfiguration().getOptional("name").get()); + assertEquals("Sabine", ConfigurationProvider.getConfiguration().getOptional("name2").get()); // from default + assertEquals("Mapped to name: Robin", ConfigurationProvider.getConfiguration().getOptional("name3").get()); // oderridden default, mapped by filter to name property + assertEquals("Sereina(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)(filtered)", ConfigurationProvider.getConfiguration().getOptional("name4").get()); // final only + assertNull(ConfigurationProvider.getConfiguration().getOptional("name5").orElse(null)); // final only, but removed from filter + + System.out.println("name : " + ConfigurationProvider.getConfiguration().getOptional("name").get()); + System.out.println("name2: " + ConfigurationProvider.getConfiguration().getOptional("name2").get()); + System.out.println("name3: " + ConfigurationProvider.getConfiguration().getOptional("name3").get()); + System.out.println("name4: " + ConfigurationProvider.getConfiguration().getOptional("name4").get()); + System.out.println("name5: " + ConfigurationProvider.getConfiguration().getOptional("name5").orElse(null)); System.out.println("ALL :"); - Configuration.current().getProperties().entrySet().forEach(e -> + ConfigurationProvider.getConfiguration().getProperties().entrySet().forEach(e -> System.out.println(" " + e.getKey()+" = " + e.getValue())); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java index ba4279f..a9671f0 100644 --- a/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java +++ b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/BooleanConverterTest.java @@ -19,11 +19,11 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; import org.junit.Test; import java.util.Optional; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -39,7 +39,7 @@ public class BooleanConverterTest { */ @Test public void testConvert_Byte() throws Exception { - Configuration config = Configuration.current(); + Configuration config = ConfigurationProvider.getConfiguration(); // trues Optional<Boolean> valueRead = config.getOptional("tests.converter.boolean.y1", Boolean.class); assertTrue(valueRead.isPresent()); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java index 5bdee37..fd16d8b 100644 --- a/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java +++ b/java8/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java @@ -19,6 +19,7 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; import org.junit.Test; import java.util.Optional; @@ -37,7 +38,7 @@ public class ByteConverterTest { */ @Test public void testConvert_Byte() throws Exception { - Configuration config = Configuration.current(); + Configuration config = ConfigurationProvider.getConfiguration(); Optional<Byte> valueRead = config.getOptional("tests.converter.byte.decimal", Byte.class); assertTrue(valueRead.isPresent()); assertEquals(valueRead.get().byteValue(), 101); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/test/java/org/apache/tamaya/core/test/provider/JavaConfigurationProviderTest.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/test/provider/JavaConfigurationProviderTest.java b/java8/core/src/test/java/org/apache/tamaya/core/test/provider/JavaConfigurationProviderTest.java index bfd9fd0..dfb5ff1 100644 --- a/java8/core/src/test/java/org/apache/tamaya/core/test/provider/JavaConfigurationProviderTest.java +++ b/java8/core/src/test/java/org/apache/tamaya/core/test/provider/JavaConfigurationProviderTest.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.test.provider; -import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.core.provider.JavaConfigurationProvider; import org.apache.tamaya.spi.PropertySource; import org.junit.Assert; @@ -51,8 +51,8 @@ public class JavaConfigurationProviderTest { Assert.assertEquals(value, propertySource.get(key)); // check if we had our key in configuration.current - Assert.assertTrue(Configuration.current().getProperties().containsKey(key)); - Assert.assertEquals(value, Configuration.current().getOptional(key).get()); + Assert.assertTrue(ConfigurationProvider.getConfiguration().getProperties().containsKey(key)); + Assert.assertEquals(value, ConfigurationProvider.getConfiguration().getOptional(key).get()); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/java8/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilterRemoving.java ---------------------------------------------------------------------- diff --git a/java8/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilterRemoving.java b/java8/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilterRemoving.java index 4667ca6..2cf76f1 100644 --- a/java8/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilterRemoving.java +++ b/java8/core/src/test/java/org/apache/tamaya/core/testdata/TestPropertyFilterRemoving.java @@ -18,7 +18,7 @@ */ package org.apache.tamaya.core.testdata; -import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; import org.apache.tamaya.spi.PropertyFilter; import javax.annotation.Priority; @@ -34,7 +34,7 @@ public class TestPropertyFilterRemoving implements PropertyFilter{ return null; } else if("name3".equals(key)){ - return "Mapped to name: " + Configuration.current().getOptional("name").orElse("NoName found!"); + return "Mapped to name: " + ConfigurationProvider.getConfiguration().getOptional("name").orElse("NoName found!"); } return valueToBeFiltered; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/modules/injection/src/main/java/org/apache/tamaya/inject/WithPropertyConverter.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/WithPropertyConverter.java b/modules/injection/src/main/java/org/apache/tamaya/inject/WithPropertyConverter.java index f0c3796..2759cc3 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/WithPropertyConverter.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/WithPropertyConverter.java @@ -19,7 +19,7 @@ package org.apache.tamaya.inject; -import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.PropertyConverter; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/b9735e02/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigTemplateInvocationHandler.java ---------------------------------------------------------------------- diff --git a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigTemplateInvocationHandler.java b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigTemplateInvocationHandler.java index 2c62fdf..9e9ee73 100644 --- a/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigTemplateInvocationHandler.java +++ b/modules/injection/src/main/java/org/apache/tamaya/inject/internal/ConfigTemplateInvocationHandler.java @@ -19,6 +19,7 @@ package org.apache.tamaya.inject.internal; import org.apache.tamaya.Configuration; +import org.apache.tamaya.TypeLiteral; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @@ -66,6 +67,6 @@ public final class ConfigTemplateInvocationHandler implements InvocationHandler return "Configured Proxy -> " + this.type.getType().getName(); } String configValue = InjectionUtils.getConfigValue(method); - return InjectionUtils.adaptValue(method, method.getReturnType(), configValue); + return InjectionUtils.adaptValue(method, TypeLiteral.of(method.getReturnType()), configValue); } }
