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: TAMAYA-63: Aligned API with Java 7. Fixed issues. TAMAYA 60: Implemented builder, though core depenmdency should be removed.
Date Mon, 26 Jan 2015 00:17:19 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master 4c265e080 -> 8169c4e18


TAMAYA-63: Aligned API with Java 7. Fixed issues.
TAMAYA 60: Implemented builder, though core depenmdency should be removed.


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

Branch: refs/heads/master
Commit: 8169c4e18483ef852862b78f31f72ebd8c42ee60
Parents: 4c265e0
Author: anatole <anatole@apache.org>
Authored: Mon Jan 26 01:17:03 2015 +0100
Committer: anatole <anatole@apache.org>
Committed: Mon Jan 26 01:17:03 2015 +0100

----------------------------------------------------------------------
 .../resources/findbugs/findbugs-exclude.xml     |  24 ++
 .../tamaya/builder/ConfigurationBuilder.java    |  46 ++--
 .../builder/ProgrammaticConfiguration.java      |  41 ---
 .../ProgrammaticConfigurationContext.java       | 264 ++++++++++++++++++-
 4 files changed, 303 insertions(+), 72 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8169c4e1/buildconfigurations/src/main/resources/findbugs/findbugs-exclude.xml
----------------------------------------------------------------------
diff --git a/buildconfigurations/src/main/resources/findbugs/findbugs-exclude.xml b/buildconfigurations/src/main/resources/findbugs/findbugs-exclude.xml
index 6a79c99..9cdc594 100644
--- a/buildconfigurations/src/main/resources/findbugs/findbugs-exclude.xml
+++ b/buildconfigurations/src/main/resources/findbugs/findbugs-exclude.xml
@@ -43,6 +43,30 @@ under the License.
                 returns="int"/>
         <Bug pattern="UPM_UNCALLED_PRIVATE_METHOD"/>
     </Match>
+    <Match>
+        <!-- Note:
+          The current version of FindBugs (version 3.0.0) is not able to detect
+          the usage of this method via a method reference.
+          Oliver B. Fischer, 17.01.2015
+        -->
+        <Class name=" org.apache.tamaya.builder.ProgrammaticConfigurationContext"/>
+        <Method name="comparePropertyFilters"
+                params="org.apache.tamaya.spi.PropertyFilter,org.apache.tamaya.spi.PropertyFilter"
+                returns="int"/>
+        <Bug pattern="UPM_UNCALLED_PRIVATE_METHOD"/>
+    </Match>
+    <Match>
+        <!-- Note:
+          The current version of FindBugs (version 3.0.0) is not able to detect
+          the usage of this method via a method reference.
+          Oliver B. Fischer, 17.01.2015
+        -->
+        <Class name=" org.apache.tamaya.builder.ProgrammaticConfigurationContext"/>
+        <Method name="comparePropertySources"
+                params="org.apache.tamaya.spi.PropertySource,org.apache.tamaya.spi.PropertySource"
+                returns="int"/>
+        <Bug pattern="UPM_UNCALLED_PRIVATE_METHOD"/>
+    </Match>
 
     <Match>
         <!-- Note:

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8169c4e1/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
b/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
index 04d0e8f..ff4a3f7 100644
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
+++ b/modules/builder/src/main/java/org/apache/tamaya/builder/ConfigurationBuilder.java
@@ -21,6 +21,7 @@ package org.apache.tamaya.builder;
 import org.apache.tamaya.Configuration;
 import org.apache.tamaya.PropertyConverter;
 import org.apache.tamaya.TypeLiteral;
+import org.apache.tamaya.core.internal.DefaultConfiguration;
 import org.apache.tamaya.spi.ConfigurationContext;
 import org.apache.tamaya.spi.PropertyFilter;
 import org.apache.tamaya.spi.PropertySource;
@@ -33,63 +34,52 @@ import java.util.Objects;
  * Builder that allows to build a Configuration completely manually.
  */
 public class ConfigurationBuilder {
-    /**
-     * Flag to indicate if this builder instance has already been used
-     * to build a configuration.
-     */
-    private boolean burned = false;
-
-    private ConfigurationContext context = new ProgrammaticConfigurationContext();
+    /** Builder used to create new ConfigurationContext instances. */
+    private ProgrammaticConfigurationContext.Builder contextBuilder = new ProgrammaticConfigurationContext.Builder();
 
     /**
      * Allows to set configuration context during unit tests.
      */
     ConfigurationBuilder setConfigurationContext(ConfigurationContext configurationContext)
{
-        context = configurationContext;
-
+        contextBuilder.setConfigurationContext(configurationContext);
         return this;
     }
 
     public ConfigurationBuilder addPropertySources(PropertySource... sources){
-        Objects.requireNonNull(sources);
-
-        context.addPropertySources(sources);
-
+        contextBuilder.addPropertySources(Objects.requireNonNull(sources));
         return this;
     }
 
     public ConfigurationBuilder addPropertySourceProviders(PropertySourceProvider... propertySourceProviders){
-
+        contextBuilder.addPropertySourceProviders(propertySourceProviders);
         return this;
     }
 
     public ConfigurationBuilder addPropertyFilters(PropertyFilter... propertyFilters){
+        contextBuilder.addPropertyFilters(propertyFilters);
         return this;
     }
 
     public ConfigurationBuilder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy
propertyValueCombinationPolicy){
-        return this;
-    }
-
-    public ConfigurationBuilder addPropertyConverter(PropertyConverter<?> propertyConverter){
-
+        contextBuilder.setPropertyValueCombinationPolicy(propertyValueCombinationPolicy);
         return this;
     }
 
     public <T> ConfigurationBuilder addPropertyConverter(TypeLiteral<T> type,
PropertyConverter<T> propertyConverter){
+        contextBuilder.addPropertyConverter(type, propertyConverter);
         return this;
     }
 
-    public Configuration build() {
-        checkStateOfBuilder();
-        burned = true;
+    //X TODO think on a functonality/API for using the default PropertyConverters and use
the configured ones here
+    //X TODO as overrides used first.
 
-        return new ProgrammaticConfiguration(context);
-    }
 
-    private void checkStateOfBuilder() {
-        if (burned) {
-            throw new IllegalStateException("Configuration has been already build.");
-        }
+    /**
+     * Creates a new Configuration instance based on the configured data.
+     * @return a new Configuration instance.
+     */
+    public Configuration build() {
+        return new DefaultConfiguration(contextBuilder.build());
     }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8169c4e1/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfiguration.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfiguration.java
b/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfiguration.java
deleted file mode 100644
index fc2df80..0000000
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfiguration.java
+++ /dev/null
@@ -1,41 +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.builder;
-
-import org.apache.tamaya.core.internal.BaseConfiguration;
-import org.apache.tamaya.spi.ConfigurationContext;
-
-/**
- * Implementation of the {@link org.apache.tamaya.spi.ConfigurationContext}
- * used by the {@link org.apache.tamaya.builder.ConfigurationBuilder}
- * internally.
- */
-public class ProgrammaticConfiguration extends BaseConfiguration {
-    private final ConfigurationContext context;
-
-    public ProgrammaticConfiguration(ConfigurationContext configurationContext) {
-        context = configurationContext;
-    }
-
-    @Override
-    protected ConfigurationContext getConfigurationContext() {
-        return context;
-    }
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/8169c4e1/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
----------------------------------------------------------------------
diff --git a/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
b/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
index 1aa4228..750bf37 100644
--- a/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
+++ b/modules/builder/src/main/java/org/apache/tamaya/builder/ProgrammaticConfigurationContext.java
@@ -19,14 +19,272 @@
 package org.apache.tamaya.builder;
 
 
-import org.apache.tamaya.core.internal.DefaultConfigurationContext;
+import org.apache.tamaya.PropertyConverter;
+import org.apache.tamaya.TypeLiteral;
+import org.apache.tamaya.core.internal.PropertyConverterManager;
+import org.apache.tamaya.spi.ConfigurationContext;
+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 javax.annotation.Priority;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+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;
 
 /**
  * Implementation of the {@link org.apache.tamaya.spi.ConfigurationContext}
  * used by the {@link org.apache.tamaya.builder.ConfigurationBuilder}
  * internally.
  */
-public class ProgrammaticConfigurationContext
-    extends DefaultConfigurationContext {
+class ProgrammaticConfigurationContext implements ConfigurationContext {
+
+    /**
+     * The logger used.
+     */
+    private final static Logger LOG = Logger.getLogger(ProgrammaticConfigurationContext.class.getName());
+    /**
+     * Cubcomponent handling {@link org.apache.tamaya.PropertyConverter} instances.
+     */
+    private PropertyConverterManager propertyConverterManager = new PropertyConverterManager();
+
+    /**
+     * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertySource}
instances.
+     */
+    private List<PropertySource> immutablePropertySources = new ArrayList<>();
+
+    /**
+     * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertyFilter}
instances.
+     */
+    private List<PropertyFilter> immutablePropertyFilters = new ArrayList<>();
+
+    /**
+     * The overriding policy used when combining PropertySources registered to evalute the
final configuration
+     * values.
+     */
+    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.
+     */
+    public ProgrammaticConfigurationContext(Builder builder) {
+        immutablePropertySources.addAll(builder.propertySources);
+        Collections.sort(immutablePropertySources, this::comparePropertySources);
+        immutablePropertySources = Collections.unmodifiableList(immutablePropertySources);
+        LOG.info(() -> "Using " + immutablePropertySources.size() + " property sources:
" +
+                createStringList(immutablePropertySources, ps -> ps.getName() + '[' +
ps.getClass().getName() + ']'));
+
+        immutablePropertyFilters.addAll(builder.propertyFilters);
+        Collections.sort(immutablePropertyFilters, this::comparePropertyFilters);
+        immutablePropertyFilters = Collections.unmodifiableList(immutablePropertyFilters);
+        LOG.info(() -> "Using " + immutablePropertyFilters.size() + " property filters:
" +
+                createStringList(immutablePropertyFilters, f -> f.getClass().getName()));
+
+        propertyValueCombinationPolicy = builder.propertyValueCombinationPolicy;
+        LOG.info(() -> "Using PropertyValueCombinationPolicy: " + propertyValueCombinationPolicy);
+    }
+
+
+    @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.
+     *
+     * @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());
+        }
+    }
+
+    /**
+     * 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 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;
+    }
+
+    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();
+    }
+
+    public final static class Builder {
+        /**
+         * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertySource}
instances.
+         */
+        private List<PropertySource> propertySources = new ArrayList<>();
+
+        /**
+         * The current unmodifiable list of loaded {@link org.apache.tamaya.spi.PropertyFilter}
instances.
+         */
+        private List<PropertyFilter> propertyFilters = new ArrayList<>();
+
+        private Map<TypeLiteral<?>, List<PropertyConverter<?>>> propertyConverters
= new HashMap<>();
+
+        /**
+         * The overriding policy used when combining PropertySources registered to evalute
the final configuration
+         * values.
+         */
+        private PropertyValueCombinationPolicy propertyValueCombinationPolicy =
+                PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR;
+
+        public Builder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy)
{
+            this.propertyValueCombinationPolicy = Objects.requireNonNull(policy);
+            return this;
+        }
+
+        public Builder addPropertySources(PropertySource... propertySources) {
+            this.propertySources.addAll(Arrays.asList(propertySources));
+            return this;
+        }
+
+        public Builder addPropertySources(Collection<PropertySource> propertySources)
{
+            this.propertySources.addAll(propertySources);
+            return this;
+        }
+
+        public Builder addPropertySourceProviders(PropertySourceProvider... propertySourceProviders)
{
+            return addPropertySourceProviders(Arrays.asList(propertySourceProviders));
+        }
+
+        public Builder addPropertySourceProviders(Collection<PropertySourceProvider>
propertySourceProviders) {
+            for(PropertySourceProvider prov: propertySourceProviders) {
+                this.propertySources.addAll(evaluatePropertySourcesFromProviders(prov));
+            }
+            return this;
+        }
+
+        public Builder addPropertyFilters(PropertyFilter... propertySources) {
+            this.propertyFilters.addAll(Arrays.asList(propertySources));
+            return this;
+        }
+
+        public Builder addPropertyFilters(Collection<PropertyFilter> propertySources)
{
+            this.propertyFilters.addAll(propertySources);
+            return this;
+        }
+
+        /**
+         * 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.
+         */
+        private Collection<PropertySource> evaluatePropertySourcesFromProviders(PropertySourceProvider
provider) {
+            List<PropertySource> propertySources = new ArrayList<>();
+            Collection<PropertySource> sources = provider.getPropertySources();
+            propertySources.addAll(sources);
+            return propertySources;
+        }
+
+        public Builder setConfigurationContext(ConfigurationContext configurationContext)
{
+            this.addPropertySources(configurationContext.getPropertySources());
+            this.addPropertyFilters(configurationContext.getPropertyFilters());
+            this.propertyValueCombinationPolicy = Objects.requireNonNull(
+                    configurationContext.getPropertyValueCombinationPolicy());
+            return this;
+        }
+
+        //X TODO think on a functonality/API for using the default PropertyConverters and
use the configured ones here
+        //X TODO as overrides used first.
+
+        public <T> Builder addPropertyConverter(TypeLiteral<T> type, PropertyConverter<T>
propertyConverter) {
+            propertyConverters.computeIfAbsent(type, (t) -> new ArrayList<>())
+                    .add(propertyConverter);
+            return this;
+        }
+
+        public ConfigurationContext build() {
+            return new ProgrammaticConfigurationContext(this);
+        }
+
+
+
+    }
 
 }


Mime
View raw message