Repository: incubator-tamaya
Updated Branches:
  refs/heads/master 492ff2ac1 -> 57df9012d


TAMAYA-182: Added additional methods for loading defaults with the builder.


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

Branch: refs/heads/master
Commit: bf38044d285e4e4722199b0e6177f0be92255f12
Parents: 492ff2a
Author: anatole <anat...@apache.org>
Authored: Sun Nov 13 23:07:08 2016 +0100
Committer: anatole <anat...@apache.org>
Committed: Sun Nov 13 23:07:08 2016 +0100

----------------------------------------------------------------------
 .../apache/tamaya/ConfigurationProvider.java    | 17 +++++++
 .../tamaya/spi/ConfigurationContextBuilder.java | 26 ++++++++++-
 .../DefaultConfigurationContextBuilder.java     | 33 +++++++-------
 .../internal/DefaultConfigurationProvider.java  |  6 +--
 .../core/propertysource/BasePropertySource.java | 48 ++++++++++++++++++--
 .../DefaultConfigurationContextTest.java        |  2 +-
 6 files changed, 106 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bf38044d/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
----------------------------------------------------------------------
diff --git 
a/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java 
b/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
index 875f8fc..9f4baa0 100644
--- a/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
+++ b/code/api/src/main/java/org/apache/tamaya/ConfigurationProvider.java
@@ -118,12 +118,29 @@ public final class ConfigurationProvider {
      * @param context the new ConfigurationContext to be applied.
      * @throws java.lang.UnsupportedOperationException if the current provider 
is read-only and does not support
      *                                                 applying a new 
ConfigurationContext.
+     * @deprecated Use #setConfiguration(Configuration) instead of.
      */
+    @Deprecated
     public static void setConfigurationContext(ConfigurationContext context) {
         PROVIDER_SPI.setConfigurationContext(context);
     }
 
     /**
+     * This method allows to replace the current default {@link 
org.apache.tamaya.Configuration} with a new
+     * instance. It is the responsibility of the ConfigurationProvider to 
trigger
+     * corresponding update events for the current {@link 
org.apache.tamaya.Configuration}, so observing
+     * listeners can do whatever is appropriate to react to any given 
configuration change.
+     *
+     * @param config the new Configuration to be applied, not null..
+     * @throws java.lang.UnsupportedOperationException if the current provider 
is read-only and
+     *                                                 does not support
+     *                                                 applying a new 
Configuration.
+     */
+    public static void setConfiguration(Configuration config) {
+        PROVIDER_SPI.setConfiguration(config);
+    }
+
+    /**
      * Create a new {@link org.apache.tamaya.spi.ConfigurationContextBuilder} 
instance. This method creates
      * a new builder instance that is not related to any concrete {@link 
org.apache.tamaya.spi.ConfigurationContext}.
      * You can use {@link 
#setConfigurationContext(org.apache.tamaya.spi.ConfigurationContext)} to change 
the

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bf38044d/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java
----------------------------------------------------------------------
diff --git 
a/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java 
b/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java
index a57234e..9f37118 100644
--- 
a/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java
+++ 
b/code/api/src/main/java/org/apache/tamaya/spi/ConfigurationContextBuilder.java
@@ -20,7 +20,10 @@ package org.apache.tamaya.spi;
 
 import org.apache.tamaya.TypeLiteral;
 
-import java.util.*;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
 
 /**
  * A builder for creating new or adapting instances of {@link 
ConfigurationContext}.
@@ -76,6 +79,14 @@ public interface ConfigurationContextBuilder {
     ConfigurationContextBuilder addPropertySources(Collection<PropertySource> 
propertySources);
 
     /**
+     * Add all registered (default) property sources to the context built. The 
sources are ordered
+     * based on their ordinal values and added to the chain of property 
sources with
+     * higher priority.
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder addDefaultPropertySources();
+
+    /**
      * Removes the given property sources, if existing. The existing order of 
property
      * sources is preserved.
      *
@@ -189,6 +200,13 @@ public interface ConfigurationContextBuilder {
     ConfigurationContextBuilder addPropertyFilters(Collection<PropertyFilter> 
filters);
 
     /**
+     * Add all registered (default) property filters to the context built.
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder addDefaultPropertyFilters();
+
+
+    /**
      * Removes the given PropertyFilter instances, if existing. The order of 
the remaining
      * filters is preserved.
      *
@@ -233,6 +251,12 @@ public interface ConfigurationContextBuilder {
                                                           
Collection<PropertyConverter<T>> propertyConverters);
 
     /**
+     * Add all registered (default) property converters to the context built.
+     * @return this builder, for chaining, never null.
+     */
+    ConfigurationContextBuilder addDefaultPropertyConverters();
+
+    /**
      * Removes the given PropertyConverter instances for the given type,
      * if existing.
      *

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bf38044d/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilder.java
----------------------------------------------------------------------
diff --git 
a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilder.java
 
b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilder.java
index 486fb05..e8c1ace 100644
--- 
a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilder.java
+++ 
b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationContextBuilder.java
@@ -129,28 +129,27 @@ public class DefaultConfigurationContextBuilder 
implements ConfigurationContextB
         return this;
     }
 
-    protected DefaultConfigurationContextBuilder loadDefaultPropertyFilters() {
+    @Override
+    public ConfigurationContextBuilder addDefaultPropertySources() {
         checkBuilderState();
-        for(PropertyFilter 
pf:ServiceContextManager.getServiceContext().getServices(PropertyFilter.class)){
-            addPropertyFilters(pf);
-        }
-        return this;
+        List<PropertySource> propertySources = new ArrayList<>();
+        
propertySources.addAll(ServiceContextManager.getServiceContext().getServices(PropertySource.class));
+        Collections.sort(propertySources, DEFAULT_PROPERTYSOURCE_COMPARATOR);
+        return addPropertySources(propertySources);
     }
 
-    protected DefaultConfigurationContextBuilder loadDefaultPropertySources() {
+    @Override
+    public ConfigurationContextBuilder addDefaultPropertyFilters() {
         checkBuilderState();
-        for(PropertySource 
ps:ServiceContextManager.getServiceContext().getServices(PropertySource.class)){
-            addPropertySources(ps);
-        }
-        for(PropertySourceProvider 
pv:ServiceContextManager.getServiceContext().getServices(PropertySourceProvider.class)){
-            for(PropertySource ps:pv.getPropertySources()){
-                addPropertySources(ps);
-            }
+        for(PropertyFilter 
pf:ServiceContextManager.getServiceContext().getServices(PropertyFilter.class)){
+            addPropertyFilters(pf);
         }
         return this;
     }
 
-    protected DefaultConfigurationContextBuilder 
loadDefaultPropertyConverters() {
+
+    @Override
+    public DefaultConfigurationContextBuilder addDefaultPropertyConverters() {
         checkBuilderState();
         for(Map.Entry<TypeLiteral, Collection<PropertyConverter>> 
en:getDefaultPropertyConverters().entrySet()){
             for(PropertyConverter pc: en.getValue()) {
@@ -350,9 +349,9 @@ public class DefaultConfigurationContextBuilder implements 
ConfigurationContextB
     protected ConfigurationContextBuilder loadDefaults() {
         checkBuilderState();
         this.combinationPolicy = 
PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR;
-        loadDefaultPropertySources();
-        loadDefaultPropertyFilters();
-        loadDefaultPropertyConverters();
+        addDefaultPropertySources();
+        addDefaultPropertyFilters();
+        addDefaultPropertyConverters();
         return this;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bf38044d/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java
----------------------------------------------------------------------
diff --git 
a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java
 
b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java
index be04a6e..c0e173c 100644
--- 
a/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java
+++ 
b/code/core/src/main/java/org/apache/tamaya/core/internal/DefaultConfigurationProvider.java
@@ -35,9 +35,9 @@ import java.util.Objects;
 public class DefaultConfigurationProvider implements ConfigurationProviderSpi {
 
     ConfigurationContext context = new DefaultConfigurationContextBuilder()
-            .loadDefaultPropertyConverters()
-            .loadDefaultPropertyFilters()
-            .loadDefaultPropertySources().build();
+            .addDefaultPropertyConverters()
+            .addDefaultPropertyFilters()
+            .addDefaultPropertySources().build();
 
     private Configuration config = new DefaultConfiguration(context);
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bf38044d/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
----------------------------------------------------------------------
diff --git 
a/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
 
b/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
index fd38818..ff0622b 100644
--- 
a/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
+++ 
b/code/core/src/main/java/org/apache/tamaya/core/propertysource/BasePropertySource.java
@@ -23,6 +23,7 @@ import org.apache.tamaya.spi.PropertyValue;
 import org.apache.tamaya.spi.PropertyValueBuilder;
 
 import java.util.Map;
+import java.util.Objects;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -32,20 +33,42 @@ import java.util.logging.Logger;
  */
 public abstract class BasePropertySource implements PropertySource{
     /** default ordinal that will be used, if no ordinal is provided with the 
config. */
-    private final int defaultOrdinal;
-
+    private int defaultOrdinal;
     /** Used if the ordinal has been set explicitly. */
     private volatile Integer ordinal;
+    /** The name of the property source. */
+    private String name;
+
+    /**
+     * Constructor.
+     * @param name the (unique) property source name, not null.
+     */
+    protected BasePropertySource(String name){
+        this.name = Objects.requireNonNull(name);
+        this.defaultOrdinal = 0;
+    }
 
     /**
      * Constructor.
      * @param defaultOrdinal default ordinal that will be used, if no ordinal 
is provided with the config.
      */
     protected BasePropertySource(int defaultOrdinal){
+        this.name = getClass().getSimpleName();
         this.defaultOrdinal = defaultOrdinal;
     }
 
     /**
+     * Constructor.
+     * @param name the (unique) property source name, not null.
+     * @param defaultOrdinal default ordinal that will be used, if no ordinal 
is provided with the config.
+     */
+    protected BasePropertySource(String name, int defaultOrdinal){
+        this.name = Objects.requireNonNull(name);
+        this.defaultOrdinal = defaultOrdinal;
+    }
+
+
+    /**
      * Constructor, using a default ordinal of 0.
      */
     protected BasePropertySource(){
@@ -54,7 +77,15 @@ public abstract class BasePropertySource implements 
PropertySource{
 
     @Override
     public String getName() {
-        return getClass().getSimpleName();
+        return name;
+    }
+
+    /**
+     * Sets the property source's (unique) name.
+     * @param name the name, not null.
+     */
+    public void setName(String name){
+        this.name = Objects.requireNonNull(name);
     }
 
     /**
@@ -66,6 +97,15 @@ public abstract class BasePropertySource implements 
PropertySource{
         this.ordinal = ordinal;
     }
 
+    /**
+     * Allows to set the ordinal of this property source explcitly. This will 
override any evaluated
+     * ordinal, or default ordinal. To reset an explcit ordinal call {@code 
setOrdinal(null);}.
+     * @param defaultOrdinal the default ordinal, or null.
+     */
+    public void setDefaultOrdinal(Integer defaultOrdinal){
+        this.defaultOrdinal = defaultOrdinal;
+    }
+
     @Override
     public int getOrdinal() {
         Integer ordinal = this.ordinal;
@@ -80,7 +120,7 @@ public abstract class BasePropertySource implements 
PropertySource{
                 return Integer.parseInt(configuredOrdinal.getValue());
             } catch (Exception e) {
                 Logger.getLogger(getClass().getName()).log(Level.WARNING,
-                        "Configured Ordinal is not an int number: " + 
configuredOrdinal.getValue(), e);
+                        "Configured Ordinal is not an int number: " + 
configuredOrdinal, e);
             }
         }
         return getDefaultOrdinal();

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/bf38044d/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java
----------------------------------------------------------------------
diff --git 
a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java
 
b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java
index ac18b32..e0f50d7 100644
--- 
a/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java
+++ 
b/code/core/src/test/java/org/apache/tamaya/core/internal/DefaultConfigurationContextTest.java
@@ -51,7 +51,7 @@ public class DefaultConfigurationContextTest {
         ConfigurationContext ctx = new 
DefaultConfigurationContextBuilder().build();
         assertNotNull(ctx.getPropertySources());
         assertEquals(ctx.getPropertySources().size(), 0);
-        ctx = new 
DefaultConfigurationContextBuilder().loadDefaultPropertySources().build();
+        ctx = new 
DefaultConfigurationContextBuilder().addDefaultPropertySources().build();
         assertNotNull(ctx.getPropertySources());
         assertEquals(8, ctx.getPropertySources().size());
     }

Reply via email to