tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [3/8] incubator-tamaya git commit: TAMAY-68: Added ConfigurationContextBuilder and all related stuff needed.
Date Fri, 27 Feb 2015 02:40:48 GMT
TAMAY-68: Added ConfigurationContextBuilder and all related stuff needed.


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

Branch: refs/heads/master
Commit: 3267be090d3da171b0aab8189af162059553b4c3
Parents: 7f7bfd5
Author: anatole <anatole@apache.org>
Authored: Fri Feb 27 02:18:31 2015 +0100
Committer: anatole <anatole@apache.org>
Committed: Fri Feb 27 03:40:34 2015 +0100

----------------------------------------------------------------------
 .../internal/DefaultConfigurationContext.java   |  77 ++++++----
 .../DefaultConfigurationContextBuilder.java     | 145 +++++++++++++++++++
 .../internal/DefaultConfigurationProvider.java  |  15 ++
 3 files changed, 208 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3267be09/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 8e6f5a4..a74fb55 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
@@ -21,6 +21,7 @@ package org.apache.tamaya.core.internal;
 import org.apache.tamaya.TypeLiteral;
 import org.apache.tamaya.spi.ConfigurationContext;
 import org.apache.tamaya.PropertyConverter;
+import org.apache.tamaya.spi.ConfigurationContextBuilder;
 import org.apache.tamaya.spi.PropertyFilter;
 import org.apache.tamaya.spi.PropertySource;
 import org.apache.tamaya.spi.PropertySourceProvider;
@@ -29,14 +30,11 @@ import org.apache.tamaya.spi.ServiceContext;
 
 import javax.annotation.Priority;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.StringJoiner;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.StampedLock;
 import java.util.function.Function;
 import java.util.logging.Logger;
 
@@ -68,12 +66,6 @@ public class DefaultConfigurationContext implements ConfigurationContext
{
     private PropertyValueCombinationPolicy propertyValueCombinationPolicy;
 
     /**
-     * Lock for internal synchronization.
-     */
-    private StampedLock propertySourceLock = new StampedLock();
-
-
-    /**
      * The first time the Configuration system gets invoked we do initialize
      * all our {@link org.apache.tamaya.spi.PropertySource}s and
      * {@link org.apache.tamaya.spi.PropertyFilter}s which are known at startup.
@@ -106,6 +98,29 @@ public class DefaultConfigurationContext implements ConfigurationContext
{
         LOG.info(() -> "Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy);
     }
 
+    DefaultConfigurationContext(DefaultConfigurationContextBuilder builder) {
+        List<PropertySource> propertySources = new ArrayList<>();
+        // first we load all PropertySources which got registered via java.util.ServiceLoader
+        propertySources.addAll(builder.propertySources);
+        // now sort them according to their ordinal values
+        Collections.sort(propertySources, this::comparePropertySources);
+        immutablePropertySources = Collections.unmodifiableList(propertySources);
+        LOG.info(() -> "Registered " + immutablePropertySources.size() + " property sources:
" +
+                createStringList(immutablePropertySources,ps -> ps.getName() + '[' + ps.getClass().getName()+']'));
+
+        // as next step we pick up the PropertyFilters pretty much the same way
+        List<PropertyFilter> propertyFilters = new ArrayList<>();
+        propertyFilters.addAll(ServiceContext.getInstance().getServices(PropertyFilter.class));
+        Collections.sort(propertyFilters, this::comparePropertyFilters);
+        immutablePropertyFilters = Collections.unmodifiableList(propertyFilters);
+        LOG.info(() -> "Registered " + immutablePropertyFilters.size() + " property filters:
" +
+                createStringList(immutablePropertyFilters,f -> f.getClass().getName()));
+
+        propertyValueCombinationPolicy = ServiceContext.getInstance().getService(PropertyValueCombinationPolicy.class)
+                .orElse(PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR);
+        LOG.info(() -> "Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy);
+    }
+
     /**
      * Pick up all {@link org.apache.tamaya.spi.PropertySourceProvider}s and return all the
      * {@link org.apache.tamaya.spi.PropertySource}s they like to register.
@@ -123,20 +138,20 @@ public class DefaultConfigurationContext implements ConfigurationContext
{
         return propertySources;
     }
 
-    @Override
-    public void addPropertySources(PropertySource... propertySourcesToAdd) {
-        Lock writeLock = propertySourceLock.asWriteLock();
-        try {
-            writeLock.lock();
-            List<PropertySource> newPropertySources = new ArrayList<>(this.immutablePropertySources);
-            newPropertySources.addAll(Arrays.asList(propertySourcesToAdd));
-            Collections.sort(newPropertySources, this::comparePropertySources);
-
-            this.immutablePropertySources = Collections.unmodifiableList(newPropertySources);
-        } finally {
-            writeLock.unlock();
-        }
-    }
+//    @Override
+//    public void addPropertySources(PropertySource... propertySourcesToAdd) {
+//        Lock writeLock = propertySourceLock.asWriteLock();
+//        try {
+//            writeLock.lock();
+//            List<PropertySource> newPropertySources = new ArrayList<>(this.immutablePropertySources);
+//            newPropertySources.addAll(Arrays.asList(propertySourcesToAdd));
+//            Collections.sort(newPropertySources, this::comparePropertySources);
+//
+//            this.immutablePropertySources = Collections.unmodifiableList(newPropertySources);
+//        } finally {
+//            writeLock.unlock();
+//        }
+//    }
 
     /**
      * Order property source reversely, the most important come first.
@@ -182,11 +197,11 @@ public class DefaultConfigurationContext implements ConfigurationContext
{
         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 <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()
{
@@ -208,10 +223,14 @@ public class DefaultConfigurationContext implements ConfigurationContext
{
         return propertyValueCombinationPolicy;
     }
 
+    @Override
+    public ConfigurationContextBuilder toBuilder() {
+        return new DefaultConfigurationContextBuilder().setContext(this);
+    }
+
     private <T> String createStringList(Collection<T> propertySources, Function<T,String>
mapper){
         StringJoiner joiner = new StringJoiner(", ");
         propertySources.forEach(t -> joiner.add(mapper.apply(t)));
         return joiner.toString();
     }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3267be09/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilder.java
----------------------------------------------------------------------
diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilder.java
b/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilder.java
new file mode 100644
index 0000000..122d4c7
--- /dev/null
+++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilder.java
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.core.internal;
+
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.PropertyConverter;
+import org.apache.tamaya.TypeLiteral;
+import org.apache.tamaya.spi.ConfigurationContext;
+import org.apache.tamaya.spi.ConfigurationContextBuilder;
+import org.apache.tamaya.spi.PropertyFilter;
+import org.apache.tamaya.spi.PropertySource;
+import org.apache.tamaya.spi.PropertyValueCombinationPolicy;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+/**
+ * Default implementation of {@link org.apache.tamaya.spi.ConfigurationContextBuilder}.
+ */
+class DefaultConfigurationContextBuilder implements ConfigurationContextBuilder {
+
+    List<PropertySource> propertySources = new ArrayList<>();
+    List<PropertyFilter> propertyFilters = new ArrayList<>();
+    Map<TypeLiteral<?>, List<PropertyConverter<?>>> propertyConverters
= new HashMap<>();
+    PropertyValueCombinationPolicy combinationPolicy;
+
+    DefaultConfigurationContextBuilder(){
+    }
+
+    @Override
+    public ConfigurationContextBuilder setContext(ConfigurationContext context) {
+        this.propertySources.clear();
+        this.propertySources.addAll(context.getPropertySources());
+        this.propertyFilters.clear();
+        this.propertyFilters.addAll(context.getPropertyFilters());
+        this.propertyConverters.clear();
+        this.propertyConverters.putAll(context.getPropertyConverters());
+        this.combinationPolicy = context.getPropertyValueCombinationPolicy();
+        return this;
+    }
+
+    @Override
+    public ConfigurationContextBuilder addPropertySources(Collection<PropertySource>
propertySourcesToAdd) {
+        Set<String> names = this.propertySources.stream().map(ps -> ps.getName()).collect(Collectors.toSet());
+        for(PropertySource ps:propertySourcesToAdd){
+            if(names.contains(ps.getName())){
+                throw new ConfigException("Duplicate PropertySource: " + ps.getName());
+            }
+        }
+        this.propertySources.addAll(propertySourcesToAdd);
+        return this;
+    }
+
+    @Override
+    public ConfigurationContextBuilder removePropertySources(Collection<String> propertySourcesToRemove)
{
+        this.propertySources = this.propertySources.stream().filter(ps -> !propertySourcesToRemove
+                .contains(ps.getName())).collect(Collectors.toList());
+        return this;
+    }
+
+    @Override
+    public ConfigurationContextBuilder addPropertyFilters(Collection<PropertyFilter>
filters) {
+        this.propertyFilters.addAll(filters);
+        return this;
+    }
+
+    @Override
+    public ConfigurationContextBuilder removePropertyFilters(Predicate<PropertyFilter>
selector) {
+        Collection<PropertyFilter> filtersToRemove = this.propertyFilters.stream().filter(selector)
+                .collect(Collectors.toList());
+        this.propertyFilters.removeAll(filtersToRemove);
+        return this;
+    }
+
+    @Override
+    public ConfigurationContextBuilder removePropertyFilters(Collection<PropertyFilter>
filters) {
+        this.propertyFilters.removeAll(filters);
+        return this;
+    }
+
+    @Override
+    public ConfigurationContextBuilder removePropertySources(Predicate<PropertySource>
selector) {
+        Collection<PropertySource> sourcesToRemove = this.propertySources.stream().filter(selector)
+                .collect(Collectors.toList());
+        this.propertySources.removeAll(sourcesToRemove);
+        return this;
+    }
+
+    @Override
+    public <T> ConfigurationContextBuilder addPropertyConverter(TypeLiteral<T>
typeToConvert, PropertyConverter<T> propertyConverter) {
+        List<PropertyConverter<?>> converters = this.propertyConverters.computeIfAbsent(typeToConvert,
(k) -> new ArrayList<>());
+        converters.add(propertyConverter);
+        return this;
+    }
+
+    @Override
+    public ConfigurationContextBuilder removePropertyConverters(TypeLiteral<?> typeToConvert,
PropertyConverter<?>... converters) {
+        return removePropertyConverters(typeToConvert, Arrays.asList(converters));
+    }
+
+    @Override
+    public ConfigurationContextBuilder removePropertyConverters(TypeLiteral<?> typeToConvert,
Collection<PropertyConverter<?>> converters) {
+        List<PropertyConverter<?>> existing = this.propertyConverters.get(typeToConvert);
+        if(existing!=null) {
+            existing.removeAll(converters);
+        }
+        return this;
+    }
+
+    @Override
+    public ConfigurationContextBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy
policy) {
+        this.combinationPolicy = Objects.requireNonNull(policy);
+        return this;
+    }
+
+    @Override
+    public ConfigurationContext build() {
+        return new DefaultConfigurationContext(this);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/3267be09/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
index bcef9af..859a4c2 100644
--- 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
@@ -20,6 +20,7 @@ package org.apache.tamaya.core.internal;
 
 import org.apache.tamaya.Configuration;
 import org.apache.tamaya.spi.ConfigurationContext;
+import org.apache.tamaya.spi.ConfigurationContextBuilder;
 import org.apache.tamaya.spi.ConfigurationProviderSpi;
 
 /**
@@ -41,4 +42,18 @@ public class DefaultConfigurationProvider implements ConfigurationProviderSpi
{
     public ConfigurationContext getConfigurationContext() {
         return context;
     }
+
+    @Override
+    public ConfigurationContextBuilder getConfigurationContextBuilder() {
+        return new DefaultConfigurationContextBuilder();
+    }
+
+    @Override
+    public void setConfigurationContext(ConfigurationContext context){
+        Configuration oldConfig = this.config;
+        Configuration newConfig = new DefaultConfiguration(context);
+        // TODO think on a SPI or move event part into API...
+        this.config = newConfig;
+        this.context = context;
+    }
 }


Mime
View raw message