tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject incubator-tamaya git commit: Moved context impl classes into separate directory.
Date Thu, 17 Sep 2015 01:41:52 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master b87049004 -> ab14f55cc


Moved context impl classes into separate directory.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/ab14f55c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/ab14f55c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/ab14f55c

Branch: refs/heads/master
Commit: ab14f55cc201220e994a47d0d1018dc475c92d37
Parents: b870490
Author: anatole <anatole@apache.org>
Authored: Thu Sep 17 03:41:33 2015 +0200
Committer: anatole <anatole@apache.org>
Committed: Thu Sep 17 03:41:33 2015 +0200

----------------------------------------------------------------------
 .../internal/CLAwareConfigurationContext.java   |   1 +
 .../internal/DefaultConfigurationContext.java   | 250 -----------
 .../clsupport/internal/EnumConverter.java       |  66 ---
 .../internal/PropertyConverterManager.java      | 429 -------------------
 .../ctx/DefaultConfigurationContext.java        | 250 +++++++++++
 .../clsupport/internal/ctx/EnumConverter.java   |  66 +++
 .../internal/ctx/PropertyConverterManager.java  | 429 +++++++++++++++++++
 7 files changed, 746 insertions(+), 745 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ab14f55c/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/CLAwareConfigurationContext.java
----------------------------------------------------------------------
diff --git a/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/CLAwareConfigurationContext.java b/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/CLAwareConfigurationContext.java
index ba86657..8575393 100644
--- a/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/CLAwareConfigurationContext.java
+++ b/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/CLAwareConfigurationContext.java
@@ -20,6 +20,7 @@ package org.apache.tamaya.clsupport.internal;
 
 import org.apache.tamaya.TypeLiteral;
 import org.apache.tamaya.clsupport.AbstractClassloaderAwareItemLoader;
+import org.apache.tamaya.clsupport.internal.ctx.DefaultConfigurationContext;
 import org.apache.tamaya.spi.ConfigurationContext;
 import org.apache.tamaya.spi.ConfigurationContextBuilder;
 import org.apache.tamaya.spi.PropertyConverter;

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ab14f55c/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/DefaultConfigurationContext.java
----------------------------------------------------------------------
diff --git a/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/DefaultConfigurationContext.java b/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/DefaultConfigurationContext.java
deleted file mode 100644
index 868d018..0000000
--- a/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/DefaultConfigurationContext.java
+++ /dev/null
@@ -1,250 +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.clsupport.internal;
-
-import org.apache.tamaya.ConfigurationProvider;
-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 javax.annotation.Priority;
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-import java.util.logging.Logger;
-
-/**
- * Default Implementation of a simple ConfigurationContext.
- */
-public class DefaultConfigurationContext implements ConfigurationContext {
-    /** The logger used. */
-    private final static Logger LOG = Logger.getLogger(DefaultConfigurationContext.class.getName());
-    /**
-     * Cubcomponent handling {@link PropertyConverter} instances.
-     */
-    private PropertyConverterManager propertyConverterManager = new PropertyConverterManager();
-
-    /**
-     * The current unmodifiable list of loaded {@link PropertySource} instances.
-     */
-    private List<PropertySource> immutablePropertySources;
-
-    /**
-     * The current unmodifiable list of loaded {@link PropertyFilter} instances.
-     */
-    private List<PropertyFilter> immutablePropertyFilters;
-
-    /**
-     * The overriding policy used when combining PropertySources registered to evalute the final configuration
-     * values.
-     */
-    private PropertyValueCombinationPolicy propertyValueCombinationPolicy;
-
-    /**
-     * Lock for internal synchronization.
-     */
-    private final ReentrantReadWriteLock propertySourceLock = new ReentrantReadWriteLock();
-
-    /** Comparator used for ordering property sources. */
-    private final PropertySourceComparator propertySourceComparator = new PropertySourceComparator();
-
-    /** Comparator used for ordering property filters. */
-    private final PropertyFilterComparator propertyFilterComparator = new PropertyFilterComparator();
-
-
-    /**
-     * The first time the Configuration system gets invoked we do initialize
-     * all our {@link PropertySource}s and
-     * {@link PropertyFilter}s which are known at startup.
-     */
-    public DefaultConfigurationContext() {
-        List<PropertySource> propertySources = new ArrayList<>();
-
-        // first we load all PropertySources which got registered via java.util.ServiceLoader
-        propertySources.addAll(ServiceContextManager.getServiceContext().getServices(PropertySource.class));
-
-        // after that we add all PropertySources which get dynamically registered via their PropertySourceProviders
-        propertySources.addAll(evaluatePropertySourcesFromProviders());
-
-        // now sort them according to their ordinal values
-        Collections.sort(propertySources, new PropertySourceComparator());
-
-        immutablePropertySources = Collections.unmodifiableList(propertySources);
-        LOG.info("Registered " + immutablePropertySources.size() + " property sources: " +
-                immutablePropertySources);
-
-        // as next step we pick up the PropertyFilters pretty much the same way
-        List<PropertyFilter> propertyFilters = new ArrayList<>();
-        propertyFilters.addAll(ServiceContextManager.getServiceContext().getServices(PropertyFilter.class));
-        Collections.sort(propertyFilters, new PropertyFilterComparator());
-        immutablePropertyFilters = Collections.unmodifiableList(propertyFilters);
-        LOG.info("Registered " + immutablePropertyFilters.size() + " property filters: " +
-                immutablePropertyFilters);
-
-        immutablePropertyFilters = Collections.unmodifiableList(propertyFilters);
-        LOG.info("Registered " + immutablePropertyFilters.size() + " property filters: " +
-                immutablePropertyFilters);
-        propertyValueCombinationPolicy = ServiceContextManager.getServiceContext().getService(PropertyValueCombinationPolicy.class);
-        if(propertyValueCombinationPolicy==null) {
-            propertyValueCombinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR;
-        }
-        LOG.info("Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy);
-    }
-
-    /**
-     * Pick up all {@link PropertySourceProvider}s and return all the
-     * {@link PropertySource}s they like to register.
-     */
-    private Collection<? extends PropertySource> evaluatePropertySourcesFromProviders() {
-        List<PropertySource> propertySources = new ArrayList<>();
-        Collection<PropertySourceProvider> propertySourceProviders = ServiceContextManager.getServiceContext().getServices(PropertySourceProvider.class);
-        for (PropertySourceProvider propertySourceProvider : propertySourceProviders) {
-            Collection<PropertySource> sources = propertySourceProvider.getPropertySources();
-            LOG.finer("PropertySourceProvider " + propertySourceProvider.getClass().getName() +
-                    " provided the following property sources: " + sources);
-                propertySources.addAll(sources);
-        }
-
-        return propertySources;
-    }
-
-    @Override
-    public void addPropertySources(PropertySource... propertySourcesToAdd) {
-        Lock writeLock = propertySourceLock.writeLock();
-        try {
-            writeLock.lock();
-            List<PropertySource> newPropertySources = new ArrayList<>(this.immutablePropertySources);
-            newPropertySources.addAll(Arrays.asList(propertySourcesToAdd));
-            Collections.sort(newPropertySources, new PropertySourceComparator());
-
-            this.immutablePropertySources = Collections.unmodifiableList(newPropertySources);
-        } finally {
-            writeLock.unlock();
-        }
-    }
-
-    private static class PropertySourceComparator implements Comparator<PropertySource>, Serializable {
-
-        private static final long serialVersionUID = 1L;
-
-        /**
-         * Order property source reversely, the most important come first.
-         *
-         * @param source1 the first PropertySource
-         * @param source2 the second PropertySource
-         * @return the comparison result.
-         */
-        private int comparePropertySources(PropertySource source1, PropertySource source2) {
-            if (source1.getOrdinal() < source2.getOrdinal()) {
-                return -1;
-            } else if (source1.getOrdinal() > source2.getOrdinal()) {
-                return 1;
-            } else {
-                return source1.getClass().getName().compareTo(source2.getClass().getName());
-            }
-        }
-
-        @Override
-        public int compare(PropertySource source1, PropertySource source2) {
-            return comparePropertySources(source1, source2);
-        }
-    }
-
-    private static class PropertyFilterComparator implements Comparator<PropertyFilter>, Serializable{
-
-        private static final long serialVersionUID = 1L;
-
-        /**
-         * Compare 2 filters for ordering the filter chain.
-         *
-         * @param filter1 the first filter
-         * @param filter2 the second filter
-         * @return the comparison result
-         */
-        private int comparePropertyFilters(PropertyFilter filter1, PropertyFilter filter2) {
-            Priority prio1 = filter1.getClass().getAnnotation(Priority.class);
-            Priority prio2 = filter2.getClass().getAnnotation(Priority.class);
-            int ord1 = prio1 != null ? prio1.value() : 0;
-            int ord2 = prio2 != null ? prio2.value() : 0;
-
-            if (ord1 < ord2) {
-                return -1;
-            } else if (ord1 > ord2) {
-                return 1;
-            } else {
-                return filter1.getClass().getName().compareTo(filter2.getClass().getName());
-            }
-        }
-
-        @Override
-        public int compare(PropertyFilter filter1, PropertyFilter filter2) {
-            return comparePropertyFilters(filter1, filter2);
-        }
-    }
-
-    @Override
-    public List<PropertySource> getPropertySources() {
-        return immutablePropertySources;
-    }
-
-    @Override
-    public <T> void addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter) {
-        propertyConverterManager.register(typeToConvert, propertyConverter);
-        LOG.info("Added PropertyConverter: " + propertyConverter.getClass().getName());
-    }
-
-    @Override
-    public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() {
-        return propertyConverterManager.getPropertyConverters();
-    }
-
-    @Override
-    public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) {
-        return propertyConverterManager.getPropertyConverters(targetType);
-    }
-
-    @Override
-    public List<PropertyFilter> getPropertyFilters() {
-        return immutablePropertyFilters;
-    }
-
-    @Override
-    public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy(){
-        return propertyValueCombinationPolicy;
-    }
-
-    @Override
-    public ConfigurationContextBuilder toBuilder() {
-        return ConfigurationProvider.getConfigurationContextBuilder().setContext(this);
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ab14f55c/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/EnumConverter.java
----------------------------------------------------------------------
diff --git a/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/EnumConverter.java b/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/EnumConverter.java
deleted file mode 100644
index b5b5cd9..0000000
--- a/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/EnumConverter.java
+++ /dev/null
@@ -1,66 +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.clsupport.internal;
-
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.spi.PropertyConverter;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Locale;
-import java.util.Objects;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-/**
- * Converter, converting from String to tge given enum type.
- */
-public class EnumConverter<T> implements PropertyConverter<T> {
-    private Logger LOG = Logger.getLogger(EnumConverter.class.getName());
-    private Class<T> enumType;
-    private Method factory;
-
-    public EnumConverter(Class<T> enumType) {
-        if (!Enum.class.isAssignableFrom(enumType)) {
-            throw new IllegalArgumentException("Not an Enum: " + enumType.getName());
-        }
-        this.enumType = Objects.requireNonNull(enumType);
-        try {
-            this.factory = enumType.getMethod("valueOf", String.class);
-        } catch (NoSuchMethodException e) {
-            throw new ConfigException("Uncovertible enum type without valueOf method found, please provide a custom " +
-                    "PropertyConverter for: " + enumType.getName());
-        }
-    }
-
-    @Override
-    public T convert(String value) {
-        try {
-            return (T) factory.invoke(null, value);
-        } catch (InvocationTargetException | IllegalAccessException e) {
-            LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(), e);
-        }
-        try {
-            return (T) factory.invoke(null, value.toUpperCase(Locale.ENGLISH));
-        } catch (InvocationTargetException | IllegalAccessException e) {
-            LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(), e);
-        }
-        return null;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ab14f55c/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/PropertyConverterManager.java
----------------------------------------------------------------------
diff --git a/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/PropertyConverterManager.java b/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/PropertyConverterManager.java
deleted file mode 100644
index d179e6b..0000000
--- a/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/PropertyConverterManager.java
+++ /dev/null
@@ -1,429 +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.clsupport.internal;
-
-import org.apache.tamaya.ConfigException;
-import org.apache.tamaya.TypeLiteral;
-import org.apache.tamaya.spi.PropertyConverter;
-import org.apache.tamaya.spi.ServiceContextManager;
-
-import javax.annotation.Priority;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.lang.reflect.Type;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-import java.util.logging.Logger;
-
-/**
- * Manager that deals with {@link 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<TypeLiteral<?>, List<PropertyConverter<?>>> converters = new ConcurrentHashMap<>();
-    /**
-     * The transitive converters.
-     */
-    private Map<TypeLiteral<?>, List<PropertyConverter<?>>> transitiveConverters = new ConcurrentHashMap<>();
-    /**
-     * The lock used.
-     */
-    private ReadWriteLock lock = new ReentrantReadWriteLock();
-
-    private static final Comparator<Object> PRIORITY_COMPARATOR = new Comparator<Object>() {
-
-        @Override
-        public int compare(Object o1, Object o2) {
-            int prio = getPriority(o1) - getPriority(o2);
-            if (prio < 0) {
-                return 1;
-            } else if (prio > 0) {
-                return -1;
-            } else {
-                return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName());
-            }
-        }
-    };
-
-    /**
-     * Checks the given instance for a @Priority annotation. If present the annotation's value s evaluated. If no such
-     * annotation is present, a default priority is returned (1);
-     * @param o the instance, not null.
-     * @return a priority, by default 1.
-     */
-    public static int getPriority(Object o){
-        int prio = 1;
-        Priority priority = o.getClass().getAnnotation(Priority.class);
-        if (priority != null) {
-            prio = priority.value();
-        }
-        return prio;
-    }
-
-    /**
-     * Constructor.
-     */
-    public PropertyConverterManager() {
-        this(true);
-    }
-
-    public PropertyConverterManager(boolean init) {
-        if (init) {
-            initConverters();
-        }
-    }
-
-    /**
-     * Registers the default converters provided out of the box.
-     */
-    protected void initConverters() {
-        for (PropertyConverter conv : ServiceContextManager.getServiceContext().getServices(PropertyConverter.class)) {
-            Type type = TypeLiteral.getGenericInterfaceTypeParameters(conv.getClass(), PropertyConverter.class)[0];
-            register(TypeLiteral.of(type), conv);
-        }
-    }
-
-    /**
-     * Registers a ew converter instance.
-     *
-     * @param targetType the target type, not null.
-     * @param converter  the converter, not null.
-     * @param <T>        the type.
-     */
-    public <T> void register(TypeLiteral<T> targetType, PropertyConverter<T> converter) {
-        Objects.requireNonNull(converter);
-        Lock writeLock = lock.writeLock();
-        try {
-            writeLock.lock();
-            List converters = List.class.cast(this.converters.get(targetType));
-            List<PropertyConverter<?>> newConverters = new ArrayList<>();
-            if (converters != null) {
-                newConverters.addAll(converters);
-            }
-            newConverters.add(converter);
-            Collections.sort(newConverters, PRIORITY_COMPARATOR);
-            this.converters.put(targetType, Collections.unmodifiableList(newConverters));
-            // evaluate transitive closure for all inherited supertypes and implemented interfaces
-            // direct implemented interfaces
-            for (Class<?> ifaceType : targetType.getRawType().getInterfaces()) {
-                converters = List.class.cast(this.transitiveConverters.get(ifaceType));
-                newConverters = new ArrayList<>();
-                if (converters != null) {
-                    newConverters.addAll(converters);
-                }
-                newConverters.add(converter);
-                Collections.sort(newConverters, PRIORITY_COMPARATOR);
-                this.transitiveConverters.put(TypeLiteral.of(ifaceType), Collections.unmodifiableList(newConverters));
-            }
-            Class<?> superClass = targetType.getRawType().getSuperclass();
-            while (superClass != null && !superClass.equals(Object.class)) {
-                converters = List.class.cast(this.transitiveConverters.get(superClass));
-                newConverters = new ArrayList<>();
-                if (converters != null) {
-                    newConverters.addAll(converters);
-                }
-                newConverters.add(converter);
-                Collections.sort(newConverters, PRIORITY_COMPARATOR);
-                this.transitiveConverters.put(TypeLiteral.of(superClass), Collections.unmodifiableList(newConverters));
-                for (Class<?> ifaceType : superClass.getInterfaces()) {
-                    converters = List.class.cast(this.transitiveConverters.get(ifaceType));
-                    newConverters = new ArrayList<>();
-                    if (converters != null) {
-                        newConverters.addAll(converters);
-                    }
-                    newConverters.add(converter);
-                    Collections.sort(newConverters, PRIORITY_COMPARATOR);
-                    this.transitiveConverters.put(TypeLiteral.of(ifaceType), Collections.unmodifiableList(newConverters));
-                }
-                superClass = superClass.getSuperclass();
-            }
-        } finally {
-            writeLock.unlock();
-        }
-    }
-
-    /**
-     * Allows to evaluate if a given target type is supported.
-     *
-     * @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(TypeLiteral<?> targetType) {
-        if (converters.containsKey(targetType) || transitiveConverters.containsKey(targetType)) {
-            return true;
-        }
-        return createDefaultPropertyConverter(targetType) != null;
-    }
-
-    /**
-     * Get a map of all property converters currently registered. This will not contain the converters that
-     * may be created, when an instance is adapted, which provides a String constructor or compatible
-     * factory methods taking a single String instance.
-     *
-     * @return the current map of instantiated and registered converters.
-     * @see #createDefaultPropertyConverter(TypeLiteral)
-     */
-    public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() {
-        Lock readLock = lock.readLock();
-        try {
-            readLock.lock();
-            return new HashMap<>(this.converters);
-        } finally {
-            readLock.unlock();
-        }
-    }
-
-    /**
-     * Get the list of all current registered converters for the given target type.
-     * If not converters are registered, they component tries to create and register a dynamic
-     * converter based on String costructor or static factory methods available.<br/>
-     * The converters provided are of the following type and returned in the following order:
-     * <ul>
-     *     <li>Converters mapped explicitly to the required target type are returned first, ordered
-     *     by decreasing priority. This means, if explicit converters are registered these are used
-     *     primarly for converting a value.</li>
-     *     <li>The target type of each explicitly registered converter also can be transitively mapped to
-     *     1) all directly implemented interfaces, 2) all its superclasses (except Object), 3) all the interfaces
-     *     implemented by its superclasses. These groups of transitive converters is returned similarly in the
-     *     order as mentioned, whereas also here a priority based decreasing ordering is applied.</li>
-     *     <li>java.lang wrapper classes and native types are automatically mapped.</li>
-     *     <li>If no explicit converters are registered, for Enum types a default implementation is provided that
-     *     compares the configuration values with the different enum members defined (cases sensitive mapping).</li>
-     * </ul>
-     *
-     * So given that list above directly registered mappings always are tried first, before any transitive mapping
-     * should be used. Also in all cases @Priority annotations are honored for ordering of the converters in place.
-     * Transitive conversion is supported for all directly implemented interfaces (including inherited ones) and
-     * the inheritance hierarchy (exception Object). Superinterfaces of implemented interfaces are ignored.
-     *
-     *
-     * @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(TypeLiteral)
-     */
-    public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) {
-        Lock readLock = lock.readLock();
-        List<PropertyConverter<T>> converterList = new ArrayList<>();
-        List<PropertyConverter<T>> converters;
-        // direct mapped converters
-        try {
-            readLock.lock();
-            converters = List.class.cast(this.converters.get(targetType));
-        } finally {
-            readLock.unlock();
-        }
-        if (converters != null) {
-            converterList.addAll(converters);
-        }
-        // transitive converter
-        try {
-            readLock.lock();
-            converters = List.class.cast(this.transitiveConverters.get(targetType));
-        } finally {
-            readLock.unlock();
-        }
-        if (converters != null) {
-            converterList.addAll(converters);
-        }
-        // handling of java.lang wrapper classes
-        TypeLiteral<T> boxedType = mapBoxedType(targetType);
-        if (boxedType != null) {
-            try {
-                readLock.lock();
-                converters = List.class.cast(this.converters.get(boxedType));
-            } finally {
-                readLock.unlock();
-            }
-            if (converters != null) {
-                converterList.addAll(converters);
-            }
-        }
-        if (converterList.isEmpty()) {
-            // adding any converters created on the fly, e.g. for enum types.
-            PropertyConverter<T> defaultConverter = createDefaultPropertyConverter(targetType);
-            if (defaultConverter != null) {
-                register(targetType, defaultConverter);
-                try {
-                    readLock.lock();
-                    converters = List.class.cast(this.converters.get(targetType));
-                } finally {
-                    readLock.unlock();
-                }
-            }
-            if (converters != null) {
-                converterList.addAll(converters);
-            }
-        }
-        return converterList;
-    }
-
-    /**
-     * Maps native types to the corresponding boxed types.
-     *
-     * @param targetType the native type.
-     * @param <T>        the type
-     * @return the boxed type, or null.
-     */
-    private <T> TypeLiteral<T> mapBoxedType(TypeLiteral<T> targetType) {
-        Type parameterType = targetType.getType();
-        if (parameterType == int.class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Integer.class));
-        }
-        if (parameterType == short.class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Short.class));
-        }
-        if (parameterType == byte.class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Byte.class));
-        }
-        if (parameterType == long.class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Long.class));
-        }
-        if (parameterType == boolean.class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Boolean.class));
-        }
-        if (parameterType == char.class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Character.class));
-        }
-        if (parameterType == float.class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Float.class));
-        }
-        if (parameterType == double.class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Double.class));
-        }
-        if (parameterType == int[].class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Integer[].class));
-        }
-        if (parameterType == short[].class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Short[].class));
-        }
-        if (parameterType == byte[].class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Byte[].class));
-        }
-        if (parameterType == long[].class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Long[].class));
-        }
-        if (parameterType == boolean.class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Boolean.class));
-        }
-        if (parameterType == char[].class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Character[].class));
-        }
-        if (parameterType == float[].class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Float[].class));
-        }
-        if (parameterType == double[].class) {
-            return TypeLiteral.class.cast(TypeLiteral.of(Double[].class));
-        }
-        return null;
-    }
-
-    /**
-     * Creates a dynamic PropertyConverter for the given target type.
-     *
-     * @param targetType the target type
-     * @param <T>        the type class
-     * @return a new converter, or null.
-     */
-    protected <T> PropertyConverter<T> createDefaultPropertyConverter(final TypeLiteral<T> targetType) {
-        if (Enum.class.isAssignableFrom(targetType.getRawType())) {
-            return new EnumConverter<T>(targetType.getRawType());
-        }
-        PropertyConverter<T> converter = null;
-        final Method factoryMethod = getFactoryMethod(targetType.getRawType(), "of", "valueOf", "instanceOf", "getInstance", "from", "fromString", "parse");
-        if (factoryMethod != null) {
-            converter = new PropertyConverter<T>() {
-                @Override
-                public T convert(String value) {
-                    try {
-                        if (!Modifier.isStatic(factoryMethod.getModifiers())) {
-                            throw new ConfigException(factoryMethod.toGenericString() +
-                                    " is not a static method. Only static " +
-                                    "methods can be used as factory methods.");
-                        }
-
-                        factoryMethod.setAccessible(true);
-
-                        Object invoke = factoryMethod.invoke(null, value);
-                        return targetType.getRawType().cast(invoke);
-                    } catch (Exception e) {
-                        throw new ConfigException("Failed to decode '" + value + "'", e);
-                    }
-                }
-            };
-        }
-        if (converter == null) {
-            try {
-                final Constructor<T> constr = targetType.getRawType().getDeclaredConstructor(String.class);
-                converter = new PropertyConverter<T>() {
-                    @Override
-                    public T convert(String value) {
-                        try {
-                            constr.setAccessible(true);
-                            return constr.newInstance(value);
-                        } catch (Exception e) {
-                            throw new ConfigException("Failed to decode '" + value + "'", e);
-                        }
-                    }
-                };
-            } catch (Exception e) {
-                LOG.finest("Failed to construct instance of type: " + targetType.getRawType().getName() + ": " + e);
-            }
-        }
-        return converter;
-    }
-
-    /**
-     * Tries to evaluate a factory method that can be used to create an instance based on a String.
-     *
-     * @param type        the target type
-     * @param methodNames the possible static method names
-     * @return the first method found, or null.
-     */
-    private Method getFactoryMethod(Class<?> type, String... methodNames) {
-        Method m;
-        for (String name : methodNames) {
-            try {
-                m = type.getDeclaredMethod(name, String.class);
-                return m;
-            } catch (NoSuchMethodException | RuntimeException e) {
-                LOG.finest("No such factory method found on type: " + type.getName() + ", methodName: " + name);
-            }
-        }
-        return null;
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ab14f55c/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/DefaultConfigurationContext.java
----------------------------------------------------------------------
diff --git a/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/DefaultConfigurationContext.java b/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/DefaultConfigurationContext.java
new file mode 100644
index 0000000..ea5b723
--- /dev/null
+++ b/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/DefaultConfigurationContext.java
@@ -0,0 +1,250 @@
+/*
+ * 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.clsupport.internal.ctx;
+
+import org.apache.tamaya.ConfigurationProvider;
+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 javax.annotation.Priority;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.logging.Logger;
+
+/**
+ * Default Implementation of a simple ConfigurationContext.
+ */
+public class DefaultConfigurationContext implements ConfigurationContext {
+    /** The logger used. */
+    private final static Logger LOG = Logger.getLogger(DefaultConfigurationContext.class.getName());
+    /**
+     * Cubcomponent handling {@link PropertyConverter} instances.
+     */
+    private PropertyConverterManager propertyConverterManager = new PropertyConverterManager();
+
+    /**
+     * The current unmodifiable list of loaded {@link PropertySource} instances.
+     */
+    private List<PropertySource> immutablePropertySources;
+
+    /**
+     * The current unmodifiable list of loaded {@link PropertyFilter} instances.
+     */
+    private List<PropertyFilter> immutablePropertyFilters;
+
+    /**
+     * The overriding policy used when combining PropertySources registered to evalute the final configuration
+     * values.
+     */
+    private PropertyValueCombinationPolicy propertyValueCombinationPolicy;
+
+    /**
+     * Lock for internal synchronization.
+     */
+    private final ReentrantReadWriteLock propertySourceLock = new ReentrantReadWriteLock();
+
+    /** Comparator used for ordering property sources. */
+    private final PropertySourceComparator propertySourceComparator = new PropertySourceComparator();
+
+    /** Comparator used for ordering property filters. */
+    private final PropertyFilterComparator propertyFilterComparator = new PropertyFilterComparator();
+
+
+    /**
+     * The first time the Configuration system gets invoked we do initialize
+     * all our {@link PropertySource}s and
+     * {@link PropertyFilter}s which are known at startup.
+     */
+    public DefaultConfigurationContext() {
+        List<PropertySource> propertySources = new ArrayList<>();
+
+        // first we load all PropertySources which got registered via java.util.ServiceLoader
+        propertySources.addAll(ServiceContextManager.getServiceContext().getServices(PropertySource.class));
+
+        // after that we add all PropertySources which get dynamically registered via their PropertySourceProviders
+        propertySources.addAll(evaluatePropertySourcesFromProviders());
+
+        // now sort them according to their ordinal values
+        Collections.sort(propertySources, new PropertySourceComparator());
+
+        immutablePropertySources = Collections.unmodifiableList(propertySources);
+        LOG.info("Registered " + immutablePropertySources.size() + " property sources: " +
+                immutablePropertySources);
+
+        // as next step we pick up the PropertyFilters pretty much the same way
+        List<PropertyFilter> propertyFilters = new ArrayList<>();
+        propertyFilters.addAll(ServiceContextManager.getServiceContext().getServices(PropertyFilter.class));
+        Collections.sort(propertyFilters, new PropertyFilterComparator());
+        immutablePropertyFilters = Collections.unmodifiableList(propertyFilters);
+        LOG.info("Registered " + immutablePropertyFilters.size() + " property filters: " +
+                immutablePropertyFilters);
+
+        immutablePropertyFilters = Collections.unmodifiableList(propertyFilters);
+        LOG.info("Registered " + immutablePropertyFilters.size() + " property filters: " +
+                immutablePropertyFilters);
+        propertyValueCombinationPolicy = ServiceContextManager.getServiceContext().getService(PropertyValueCombinationPolicy.class);
+        if(propertyValueCombinationPolicy==null) {
+            propertyValueCombinationPolicy = PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR;
+        }
+        LOG.info("Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy);
+    }
+
+    /**
+     * Pick up all {@link PropertySourceProvider}s and return all the
+     * {@link PropertySource}s they like to register.
+     */
+    private Collection<? extends PropertySource> evaluatePropertySourcesFromProviders() {
+        List<PropertySource> propertySources = new ArrayList<>();
+        Collection<PropertySourceProvider> propertySourceProviders = ServiceContextManager.getServiceContext().getServices(PropertySourceProvider.class);
+        for (PropertySourceProvider propertySourceProvider : propertySourceProviders) {
+            Collection<PropertySource> sources = propertySourceProvider.getPropertySources();
+            LOG.finer("PropertySourceProvider " + propertySourceProvider.getClass().getName() +
+                    " provided the following property sources: " + sources);
+                propertySources.addAll(sources);
+        }
+
+        return propertySources;
+    }
+
+    @Override
+    public void addPropertySources(PropertySource... propertySourcesToAdd) {
+        Lock writeLock = propertySourceLock.writeLock();
+        try {
+            writeLock.lock();
+            List<PropertySource> newPropertySources = new ArrayList<>(this.immutablePropertySources);
+            newPropertySources.addAll(Arrays.asList(propertySourcesToAdd));
+            Collections.sort(newPropertySources, new PropertySourceComparator());
+
+            this.immutablePropertySources = Collections.unmodifiableList(newPropertySources);
+        } finally {
+            writeLock.unlock();
+        }
+    }
+
+    private static class PropertySourceComparator implements Comparator<PropertySource>, Serializable {
+
+        private static final long serialVersionUID = 1L;
+
+        /**
+         * Order property source reversely, the most important come first.
+         *
+         * @param source1 the first PropertySource
+         * @param source2 the second PropertySource
+         * @return the comparison result.
+         */
+        private int comparePropertySources(PropertySource source1, PropertySource source2) {
+            if (source1.getOrdinal() < source2.getOrdinal()) {
+                return -1;
+            } else if (source1.getOrdinal() > source2.getOrdinal()) {
+                return 1;
+            } else {
+                return source1.getClass().getName().compareTo(source2.getClass().getName());
+            }
+        }
+
+        @Override
+        public int compare(PropertySource source1, PropertySource source2) {
+            return comparePropertySources(source1, source2);
+        }
+    }
+
+    private static class PropertyFilterComparator implements Comparator<PropertyFilter>, Serializable{
+
+        private static final long serialVersionUID = 1L;
+
+        /**
+         * Compare 2 filters for ordering the filter chain.
+         *
+         * @param filter1 the first filter
+         * @param filter2 the second filter
+         * @return the comparison result
+         */
+        private int comparePropertyFilters(PropertyFilter filter1, PropertyFilter filter2) {
+            Priority prio1 = filter1.getClass().getAnnotation(Priority.class);
+            Priority prio2 = filter2.getClass().getAnnotation(Priority.class);
+            int ord1 = prio1 != null ? prio1.value() : 0;
+            int ord2 = prio2 != null ? prio2.value() : 0;
+
+            if (ord1 < ord2) {
+                return -1;
+            } else if (ord1 > ord2) {
+                return 1;
+            } else {
+                return filter1.getClass().getName().compareTo(filter2.getClass().getName());
+            }
+        }
+
+        @Override
+        public int compare(PropertyFilter filter1, PropertyFilter filter2) {
+            return comparePropertyFilters(filter1, filter2);
+        }
+    }
+
+    @Override
+    public List<PropertySource> getPropertySources() {
+        return immutablePropertySources;
+    }
+
+    @Override
+    public <T> void addPropertyConverter(TypeLiteral<T> typeToConvert, PropertyConverter<T> propertyConverter) {
+        propertyConverterManager.register(typeToConvert, propertyConverter);
+        LOG.info("Added PropertyConverter: " + propertyConverter.getClass().getName());
+    }
+
+    @Override
+    public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() {
+        return propertyConverterManager.getPropertyConverters();
+    }
+
+    @Override
+    public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) {
+        return propertyConverterManager.getPropertyConverters(targetType);
+    }
+
+    @Override
+    public List<PropertyFilter> getPropertyFilters() {
+        return immutablePropertyFilters;
+    }
+
+    @Override
+    public PropertyValueCombinationPolicy getPropertyValueCombinationPolicy(){
+        return propertyValueCombinationPolicy;
+    }
+
+    @Override
+    public ConfigurationContextBuilder toBuilder() {
+        return ConfigurationProvider.getConfigurationContextBuilder().setContext(this);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ab14f55c/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/EnumConverter.java
----------------------------------------------------------------------
diff --git a/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/EnumConverter.java b/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/EnumConverter.java
new file mode 100644
index 0000000..5ccdca9
--- /dev/null
+++ b/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/EnumConverter.java
@@ -0,0 +1,66 @@
+/*
+ * 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.clsupport.internal.ctx;
+
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.spi.PropertyConverter;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Locale;
+import java.util.Objects;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Converter, converting from String to tge given enum type.
+ */
+class EnumConverter<T> implements PropertyConverter<T> {
+    private Logger LOG = Logger.getLogger(EnumConverter.class.getName());
+    private Class<T> enumType;
+    private Method factory;
+
+    public EnumConverter(Class<T> enumType) {
+        if (!Enum.class.isAssignableFrom(enumType)) {
+            throw new IllegalArgumentException("Not an Enum: " + enumType.getName());
+        }
+        this.enumType = Objects.requireNonNull(enumType);
+        try {
+            this.factory = enumType.getMethod("valueOf", String.class);
+        } catch (NoSuchMethodException e) {
+            throw new ConfigException("Uncovertible enum type without valueOf method found, please provide a custom " +
+                    "PropertyConverter for: " + enumType.getName());
+        }
+    }
+
+    @Override
+    public T convert(String value) {
+        try {
+            return (T) factory.invoke(null, value);
+        } catch (InvocationTargetException | IllegalAccessException e) {
+            LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(), e);
+        }
+        try {
+            return (T) factory.invoke(null, value.toUpperCase(Locale.ENGLISH));
+        } catch (InvocationTargetException | IllegalAccessException e) {
+            LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(), e);
+        }
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ab14f55c/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/PropertyConverterManager.java
----------------------------------------------------------------------
diff --git a/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/PropertyConverterManager.java b/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/PropertyConverterManager.java
new file mode 100644
index 0000000..41f5f31
--- /dev/null
+++ b/sandbox/classloader-support/src/main/java/org/apache/tamaya/clsupport/internal/ctx/PropertyConverterManager.java
@@ -0,0 +1,429 @@
+/*
+ * 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.clsupport.internal.ctx;
+
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.TypeLiteral;
+import org.apache.tamaya.spi.PropertyConverter;
+import org.apache.tamaya.spi.ServiceContextManager;
+
+import javax.annotation.Priority;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.logging.Logger;
+
+/**
+ * Manager that deals with {@link PropertyConverter} instances.
+ * This class is thread-safe.
+ */
+class PropertyConverterManager {
+    /**
+     * The logger used.
+     */
+    private static final Logger LOG = Logger.getLogger(PropertyConverterManager.class.getName());
+    /**
+     * The registered converters.
+     */
+    private Map<TypeLiteral<?>, List<PropertyConverter<?>>> converters = new ConcurrentHashMap<>();
+    /**
+     * The transitive converters.
+     */
+    private Map<TypeLiteral<?>, List<PropertyConverter<?>>> transitiveConverters = new ConcurrentHashMap<>();
+    /**
+     * The lock used.
+     */
+    private ReadWriteLock lock = new ReentrantReadWriteLock();
+
+    private static final Comparator<Object> PRIORITY_COMPARATOR = new Comparator<Object>() {
+
+        @Override
+        public int compare(Object o1, Object o2) {
+            int prio = getPriority(o1) - getPriority(o2);
+            if (prio < 0) {
+                return 1;
+            } else if (prio > 0) {
+                return -1;
+            } else {
+                return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName());
+            }
+        }
+    };
+
+    /**
+     * Checks the given instance for a @Priority annotation. If present the annotation's value s evaluated. If no such
+     * annotation is present, a default priority is returned (1);
+     * @param o the instance, not null.
+     * @return a priority, by default 1.
+     */
+    public static int getPriority(Object o){
+        int prio = 1;
+        Priority priority = o.getClass().getAnnotation(Priority.class);
+        if (priority != null) {
+            prio = priority.value();
+        }
+        return prio;
+    }
+
+    /**
+     * Constructor.
+     */
+    public PropertyConverterManager() {
+        this(true);
+    }
+
+    public PropertyConverterManager(boolean init) {
+        if (init) {
+            initConverters();
+        }
+    }
+
+    /**
+     * Registers the default converters provided out of the box.
+     */
+    protected void initConverters() {
+        for (PropertyConverter conv : ServiceContextManager.getServiceContext().getServices(PropertyConverter.class)) {
+            Type type = TypeLiteral.getGenericInterfaceTypeParameters(conv.getClass(), PropertyConverter.class)[0];
+            register(TypeLiteral.of(type), conv);
+        }
+    }
+
+    /**
+     * Registers a ew converter instance.
+     *
+     * @param targetType the target type, not null.
+     * @param converter  the converter, not null.
+     * @param <T>        the type.
+     */
+    public <T> void register(TypeLiteral<T> targetType, PropertyConverter<T> converter) {
+        Objects.requireNonNull(converter);
+        Lock writeLock = lock.writeLock();
+        try {
+            writeLock.lock();
+            List converters = List.class.cast(this.converters.get(targetType));
+            List<PropertyConverter<?>> newConverters = new ArrayList<>();
+            if (converters != null) {
+                newConverters.addAll(converters);
+            }
+            newConverters.add(converter);
+            Collections.sort(newConverters, PRIORITY_COMPARATOR);
+            this.converters.put(targetType, Collections.unmodifiableList(newConverters));
+            // evaluate transitive closure for all inherited supertypes and implemented interfaces
+            // direct implemented interfaces
+            for (Class<?> ifaceType : targetType.getRawType().getInterfaces()) {
+                converters = List.class.cast(this.transitiveConverters.get(ifaceType));
+                newConverters = new ArrayList<>();
+                if (converters != null) {
+                    newConverters.addAll(converters);
+                }
+                newConverters.add(converter);
+                Collections.sort(newConverters, PRIORITY_COMPARATOR);
+                this.transitiveConverters.put(TypeLiteral.of(ifaceType), Collections.unmodifiableList(newConverters));
+            }
+            Class<?> superClass = targetType.getRawType().getSuperclass();
+            while (superClass != null && !superClass.equals(Object.class)) {
+                converters = List.class.cast(this.transitiveConverters.get(superClass));
+                newConverters = new ArrayList<>();
+                if (converters != null) {
+                    newConverters.addAll(converters);
+                }
+                newConverters.add(converter);
+                Collections.sort(newConverters, PRIORITY_COMPARATOR);
+                this.transitiveConverters.put(TypeLiteral.of(superClass), Collections.unmodifiableList(newConverters));
+                for (Class<?> ifaceType : superClass.getInterfaces()) {
+                    converters = List.class.cast(this.transitiveConverters.get(ifaceType));
+                    newConverters = new ArrayList<>();
+                    if (converters != null) {
+                        newConverters.addAll(converters);
+                    }
+                    newConverters.add(converter);
+                    Collections.sort(newConverters, PRIORITY_COMPARATOR);
+                    this.transitiveConverters.put(TypeLiteral.of(ifaceType), Collections.unmodifiableList(newConverters));
+                }
+                superClass = superClass.getSuperclass();
+            }
+        } finally {
+            writeLock.unlock();
+        }
+    }
+
+    /**
+     * Allows to evaluate if a given target type is supported.
+     *
+     * @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(TypeLiteral<?> targetType) {
+        if (converters.containsKey(targetType) || transitiveConverters.containsKey(targetType)) {
+            return true;
+        }
+        return createDefaultPropertyConverter(targetType) != null;
+    }
+
+    /**
+     * Get a map of all property converters currently registered. This will not contain the converters that
+     * may be created, when an instance is adapted, which provides a String constructor or compatible
+     * factory methods taking a single String instance.
+     *
+     * @return the current map of instantiated and registered converters.
+     * @see #createDefaultPropertyConverter(TypeLiteral)
+     */
+    public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() {
+        Lock readLock = lock.readLock();
+        try {
+            readLock.lock();
+            return new HashMap<>(this.converters);
+        } finally {
+            readLock.unlock();
+        }
+    }
+
+    /**
+     * Get the list of all current registered converters for the given target type.
+     * If not converters are registered, they component tries to create and register a dynamic
+     * converter based on String costructor or static factory methods available.<br/>
+     * The converters provided are of the following type and returned in the following order:
+     * <ul>
+     *     <li>Converters mapped explicitly to the required target type are returned first, ordered
+     *     by decreasing priority. This means, if explicit converters are registered these are used
+     *     primarly for converting a value.</li>
+     *     <li>The target type of each explicitly registered converter also can be transitively mapped to
+     *     1) all directly implemented interfaces, 2) all its superclasses (except Object), 3) all the interfaces
+     *     implemented by its superclasses. These groups of transitive converters is returned similarly in the
+     *     order as mentioned, whereas also here a priority based decreasing ordering is applied.</li>
+     *     <li>java.lang wrapper classes and native types are automatically mapped.</li>
+     *     <li>If no explicit converters are registered, for Enum types a default implementation is provided that
+     *     compares the configuration values with the different enum members defined (cases sensitive mapping).</li>
+     * </ul>
+     *
+     * So given that list above directly registered mappings always are tried first, before any transitive mapping
+     * should be used. Also in all cases @Priority annotations are honored for ordering of the converters in place.
+     * Transitive conversion is supported for all directly implemented interfaces (including inherited ones) and
+     * the inheritance hierarchy (exception Object). Superinterfaces of implemented interfaces are ignored.
+     *
+     *
+     * @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(TypeLiteral)
+     */
+    public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) {
+        Lock readLock = lock.readLock();
+        List<PropertyConverter<T>> converterList = new ArrayList<>();
+        List<PropertyConverter<T>> converters;
+        // direct mapped converters
+        try {
+            readLock.lock();
+            converters = List.class.cast(this.converters.get(targetType));
+        } finally {
+            readLock.unlock();
+        }
+        if (converters != null) {
+            converterList.addAll(converters);
+        }
+        // transitive converter
+        try {
+            readLock.lock();
+            converters = List.class.cast(this.transitiveConverters.get(targetType));
+        } finally {
+            readLock.unlock();
+        }
+        if (converters != null) {
+            converterList.addAll(converters);
+        }
+        // handling of java.lang wrapper classes
+        TypeLiteral<T> boxedType = mapBoxedType(targetType);
+        if (boxedType != null) {
+            try {
+                readLock.lock();
+                converters = List.class.cast(this.converters.get(boxedType));
+            } finally {
+                readLock.unlock();
+            }
+            if (converters != null) {
+                converterList.addAll(converters);
+            }
+        }
+        if (converterList.isEmpty()) {
+            // adding any converters created on the fly, e.g. for enum types.
+            PropertyConverter<T> defaultConverter = createDefaultPropertyConverter(targetType);
+            if (defaultConverter != null) {
+                register(targetType, defaultConverter);
+                try {
+                    readLock.lock();
+                    converters = List.class.cast(this.converters.get(targetType));
+                } finally {
+                    readLock.unlock();
+                }
+            }
+            if (converters != null) {
+                converterList.addAll(converters);
+            }
+        }
+        return converterList;
+    }
+
+    /**
+     * Maps native types to the corresponding boxed types.
+     *
+     * @param targetType the native type.
+     * @param <T>        the type
+     * @return the boxed type, or null.
+     */
+    private <T> TypeLiteral<T> mapBoxedType(TypeLiteral<T> targetType) {
+        Type parameterType = targetType.getType();
+        if (parameterType == int.class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Integer.class));
+        }
+        if (parameterType == short.class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Short.class));
+        }
+        if (parameterType == byte.class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Byte.class));
+        }
+        if (parameterType == long.class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Long.class));
+        }
+        if (parameterType == boolean.class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Boolean.class));
+        }
+        if (parameterType == char.class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Character.class));
+        }
+        if (parameterType == float.class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Float.class));
+        }
+        if (parameterType == double.class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Double.class));
+        }
+        if (parameterType == int[].class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Integer[].class));
+        }
+        if (parameterType == short[].class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Short[].class));
+        }
+        if (parameterType == byte[].class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Byte[].class));
+        }
+        if (parameterType == long[].class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Long[].class));
+        }
+        if (parameterType == boolean.class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Boolean.class));
+        }
+        if (parameterType == char[].class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Character[].class));
+        }
+        if (parameterType == float[].class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Float[].class));
+        }
+        if (parameterType == double[].class) {
+            return TypeLiteral.class.cast(TypeLiteral.of(Double[].class));
+        }
+        return null;
+    }
+
+    /**
+     * Creates a dynamic PropertyConverter for the given target type.
+     *
+     * @param targetType the target type
+     * @param <T>        the type class
+     * @return a new converter, or null.
+     */
+    protected <T> PropertyConverter<T> createDefaultPropertyConverter(final TypeLiteral<T> targetType) {
+        if (Enum.class.isAssignableFrom(targetType.getRawType())) {
+            return new EnumConverter<T>(targetType.getRawType());
+        }
+        PropertyConverter<T> converter = null;
+        final Method factoryMethod = getFactoryMethod(targetType.getRawType(), "of", "valueOf", "instanceOf", "getInstance", "from", "fromString", "parse");
+        if (factoryMethod != null) {
+            converter = new PropertyConverter<T>() {
+                @Override
+                public T convert(String value) {
+                    try {
+                        if (!Modifier.isStatic(factoryMethod.getModifiers())) {
+                            throw new ConfigException(factoryMethod.toGenericString() +
+                                    " is not a static method. Only static " +
+                                    "methods can be used as factory methods.");
+                        }
+
+                        factoryMethod.setAccessible(true);
+
+                        Object invoke = factoryMethod.invoke(null, value);
+                        return targetType.getRawType().cast(invoke);
+                    } catch (Exception e) {
+                        throw new ConfigException("Failed to decode '" + value + "'", e);
+                    }
+                }
+            };
+        }
+        if (converter == null) {
+            try {
+                final Constructor<T> constr = targetType.getRawType().getDeclaredConstructor(String.class);
+                converter = new PropertyConverter<T>() {
+                    @Override
+                    public T convert(String value) {
+                        try {
+                            constr.setAccessible(true);
+                            return constr.newInstance(value);
+                        } catch (Exception e) {
+                            throw new ConfigException("Failed to decode '" + value + "'", e);
+                        }
+                    }
+                };
+            } catch (Exception e) {
+                LOG.finest("Failed to construct instance of type: " + targetType.getRawType().getName() + ": " + e);
+            }
+        }
+        return converter;
+    }
+
+    /**
+     * Tries to evaluate a factory method that can be used to create an instance based on a String.
+     *
+     * @param type        the target type
+     * @param methodNames the possible static method names
+     * @return the first method found, or null.
+     */
+    private Method getFactoryMethod(Class<?> type, String... methodNames) {
+        Method m;
+        for (String name : methodNames) {
+            try {
+                m = type.getDeclaredMethod(name, String.class);
+                return m;
+            } catch (NoSuchMethodException | RuntimeException e) {
+                LOG.finest("No such factory method found on type: " + type.getName() + ", methodName: " + name);
+            }
+        }
+        return null;
+    }
+
+}


Mime
View raw message