TAMAYA-60 It is now possible to enable and disable the automatic loading of 
service providers for PropertySource instances.


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

Branch: refs/heads/master
Commit: 6fee4134534d52acd27cfea93125359e82fc2856
Parents: 5f6a14f
Author: Oliver B. Fischer <[email protected]>
Authored: Sun Feb 8 12:41:24 2015 +0100
Committer: Oliver B. Fischer <[email protected]>
Committed: Sun Feb 8 12:41:24 2015 +0100

----------------------------------------------------------------------
 .../tamaya/builder/ConfigurationBuilder.java    | 31 +++++++++-
 .../ProgrammaticConfigurationContext.java       | 31 +++++++++-
 .../builder/ConfigurationBuilderTest.java       | 60 ++++++++++++++++++--
 .../tamaya/builder/TestPropertySource.java      | 53 +++++++++++++++++
 .../org.apache.tamaya.spi.PropertySource        | 19 +++++++
 5 files changed, 187 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/6fee4134/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 9ea41d5..00198f1 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
@@ -33,7 +33,7 @@ import java.util.Objects;
 /* TODO LIST FOR TAMAYA-60
  *
  * - configurable loading of provided PropertyConverter DONE
- * - configurable loading of provided PropertySources
+ * - configurable loading of provided PropertySources DONE
  * - configurable loading of provided PropertySourceProviders
  * - adding sources via URL
  *
@@ -61,6 +61,12 @@ public class ConfigurationBuilder {
      */
     private boolean loadProvidedPropertyConverters = true;
 
+    /**
+     * Flag if all existing property source service providers
+     * will be loaded if the configuration is build.
+     */
+    private boolean loadProvidedPropertySources = false;
+
 
     /**
      * Allows to set configuration context during unit tests.
@@ -148,6 +154,28 @@ public class ConfigurationBuilder {
         return this;
     }
 
+
+    public ConfigurationBuilder enableProvidedPropertySources() {
+        checkBuilderState();
+
+        loadProvidedPropertySources = true;
+
+        return this;
+    }
+
+    public boolean isPropertySourcesLoadingEnabled() {
+        return loadProvidedPropertySources;
+    }
+
+    public ConfigurationBuilder disableProvidedPropertySources() {
+        checkBuilderState();
+
+        loadProvidedPropertySources = false;
+
+        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.
 
@@ -162,6 +190,7 @@ public class ConfigurationBuilder {
         built = true;
 
         
contextBuilder.loadProvidedPropertyConverters(isPropertyConverterLoadingEnabled());
+        
contextBuilder.loadProvidedPropertySources(isPropertySourcesLoadingEnabled());
 
         return new DefaultConfiguration(contextBuilder.build());
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/6fee4134/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 dceabab..75fe389 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
@@ -27,6 +27,7 @@ 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.ServiceContext;
 
 import javax.annotation.Priority;
 import java.util.ArrayList;
@@ -44,6 +45,8 @@ import java.util.concurrent.locks.StampedLock;
 import java.util.function.Function;
 import java.util.logging.Logger;
 
+import static java.util.Collections.emptyList;
+
 /**
  * Implementation of the {@link org.apache.tamaya.spi.ConfigurationContext}
  * used by the {@link org.apache.tamaya.builder.ConfigurationBuilder}
@@ -89,7 +92,7 @@ class ProgrammaticConfigurationContext implements 
ConfigurationContext {
      */
     public ProgrammaticConfigurationContext(Builder builder) {
         propertyConverterManager = new 
PropertyConverterManager(builder.loadProvidedPropertyConverters);
-        immutablePropertySources.addAll(builder.propertySources);
+        immutablePropertySources = getAllPropertySources(builder);
         Collections.sort(immutablePropertySources, 
this::comparePropertySources);
         immutablePropertySources = 
Collections.unmodifiableList(immutablePropertySources);
         LOG.info(() -> "Using " + immutablePropertySources.size() + " property 
sources: " +
@@ -116,6 +119,27 @@ class ProgrammaticConfigurationContext implements 
ConfigurationContext {
         LOG.info(() -> "Using PropertyValueCombinationPolicy: " + 
propertyValueCombinationPolicy);
     }
 
+    private List<PropertySource> getAllPropertySources(Builder builder) {
+        List<PropertySource> provided = builder.loadProvidedPropertySources
+                ? 
ServiceContext.getInstance().getServices(PropertySource.class)
+                : emptyList();
+
+        List<PropertySource> configured = builder.propertySources;
+
+        return join(provided, configured);
+    }
+
+    private <T> List<T> join(List<T> a, List<T> b) {
+        int sizeA = a.size();
+        int sizeB = b.size();
+
+        ArrayList<T> result = new ArrayList<>(sizeA + sizeB);
+
+        result.addAll(a);
+        result.addAll(b);
+
+        return result;
+    }
 
     @Override
     public void addPropertySources(PropertySource... propertySourcesToAdd) {
@@ -240,6 +264,7 @@ class ProgrammaticConfigurationContext implements 
ConfigurationContext {
                 PropertyValueCombinationPolicy.DEFAULT_OVERRIDING_COLLECTOR;
 
         private boolean loadProvidedPropertyConverters;
+        private boolean loadProvidedPropertySources;
 
         public Builder 
setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy) {
             this.propertyValueCombinationPolicy = 
Objects.requireNonNull(policy);
@@ -313,6 +338,10 @@ class ProgrammaticConfigurationContext implements 
ConfigurationContext {
         public void loadProvidedPropertyConverters(boolean state) {
             loadProvidedPropertyConverters = state;
         }
+
+        public void loadProvidedPropertySources(boolean state) {
+            loadProvidedPropertySources = state;
+        }
     }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/6fee4134/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
----------------------------------------------------------------------
diff --git 
a/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
 
b/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
index f10f7eb..56b874a 100644
--- 
a/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
+++ 
b/modules/builder/src/test/java/org/apache/tamaya/builder/ConfigurationBuilderTest.java
@@ -20,26 +20,21 @@ package org.apache.tamaya.builder;
 
 import org.apache.tamaya.ConfigException;
 import org.apache.tamaya.Configuration;
-import org.apache.tamaya.PropertyConverter;
 import org.apache.tamaya.TypeLiteral;
-import org.apache.tamaya.builder.util.mockito.NotMockedAnswer;
 import org.apache.tamaya.builder.util.types.CustomTypeA;
 import org.apache.tamaya.builder.util.types.CustomTypeB;
 import org.apache.tamaya.builder.util.types.CustomTypeC;
 import org.apache.tamaya.spi.PropertySource;
 import org.hamcrest.CoreMatchers;
 import org.hamcrest.Matchers;
-import org.junit.Assert;
 import org.junit.Ignore;
 import org.junit.Test;
 
 import static 
org.apache.tamaya.builder.util.mockito.NotMockedAnswer.NOT_MOCKED_ANSWER;
 import static org.hamcrest.CoreMatchers.equalTo;
-import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.notNullValue;
-import static org.junit.Assert.fail;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
@@ -79,6 +74,7 @@ public class ConfigurationBuilderTest {
         PropertySource first = mock(PropertySource.class, NOT_MOCKED_ANSWER);
 
         doReturn("first").when(first).getName();
+        doReturn(100).when(first).getOrdinal();
 
         ConfigurationBuilder builder = new 
ConfigurationBuilder().addPropertySources(first);
 
@@ -97,6 +93,7 @@ public class ConfigurationBuilderTest {
 
         doReturn("one").when(source).getName();
         doReturn("a").when(source).get("keyOfA");
+        doReturn(100).when(source).getOrdinal();
 
         ConfigurationBuilder builder = new 
ConfigurationBuilder().addPropertySources(source);
 
@@ -251,6 +248,7 @@ public class ConfigurationBuilderTest {
 
         doReturn("source").when(source).getName();
         doReturn("A").when(source).get("key");
+        doReturn(100).when(source).getOrdinal();
 
         ConfigurationBuilder builder = new ConfigurationBuilder();
 
@@ -275,6 +273,7 @@ public class ConfigurationBuilderTest {
 
         doReturn("source").when(source).getName();
         doReturn("A").when(source).get("key");
+        doReturn(100).when(source).getOrdinal();
 
         ConfigurationBuilder builder = new ConfigurationBuilder();
 
@@ -299,6 +298,7 @@ public class ConfigurationBuilderTest {
 
         doReturn("source").when(source).getName();
         doReturn("A").when(source).get("key");
+        doReturn(100).when(source).getOrdinal();
 
         ConfigurationBuilder builder = new ConfigurationBuilder();
 
@@ -365,6 +365,7 @@ public class ConfigurationBuilderTest {
 
         doReturn("source").when(source).getName();
         doReturn("A").when(source).get("key");
+        doReturn(100).when(source).getOrdinal();
 
         ConfigurationBuilder builder = new 
ConfigurationBuilder().addPropertySources(source)
                                                                  
.enableProvidedPropertyConverters()
@@ -381,6 +382,7 @@ public class ConfigurationBuilderTest {
 
         doReturn("source").when(source).getName();
         doReturn("A").when(source).get("key");
+        doReturn(100).when(source).getOrdinal();
 
         ConfigurationBuilder builder = new 
ConfigurationBuilder().addPropertySources(source)
                                                                  
.disableProvidedPropertyConverters()
@@ -394,4 +396,52 @@ public class ConfigurationBuilderTest {
         assertThat(result.getValue(), equalTo("A"));
     }
 
+    /*********************************************************************
+     * Tests for enabling and disabling of automatic loading of
+     * P r o p e r t y S o u r c e s
+     */
+
+    @Test
+    public void enablingOfPropertySourceLoadingIsOk() {
+        ConfigurationBuilder builder = new ConfigurationBuilder();
+
+        builder.disableProvidedPropertySources()
+               .enableProvidedPropertySources();
+
+        assertThat(builder.isPropertySourcesLoadingEnabled(), is(true));
+    }
+
+    @Test
+    public void disablingPropertySourceLoadingIsOk() {
+        ConfigurationBuilder builder = new ConfigurationBuilder();
+
+        builder.enableProvidedPropertySources()
+               .disableProvidedPropertySources();
+
+        assertThat(builder.isPropertySourcesLoadingEnabled(), is(false));
+    }
+
+    @Test
+    public void loadingOfPropertySourcesCanBeEnabled() {
+        ConfigurationBuilder builder = new ConfigurationBuilder();
+
+        Configuration config = builder.disableProvidedPropertySources()
+                                      .enableProvidedPropertySources()
+                                      .build();
+
+
+        assertThat(config.get("tps_a"), Matchers.notNullValue());
+    }
+
+    @Test
+    public void loadingOfPropertySourcesCanBeDisabled() {
+        ConfigurationBuilder builder = new ConfigurationBuilder();
+
+        Configuration config = builder.enableProvidedPropertySources()
+                                      .disableProvidedPropertySources()
+                                      .build();
+
+
+        assertThat(config.get("tps_c"), Matchers.nullValue());
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/6fee4134/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySource.java
----------------------------------------------------------------------
diff --git 
a/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySource.java
 
b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySource.java
new file mode 100644
index 0000000..eb8f417
--- /dev/null
+++ 
b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySource.java
@@ -0,0 +1,53 @@
+/*
+ * 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.spi.PropertySource;
+
+import java.util.Collections;
+import java.util.Hashtable;
+import java.util.Map;
+
+public class TestPropertySource
+    implements PropertySource
+{
+    private Map<String, String> properties;
+
+    {
+        properties = new Hashtable<>(3);
+        properties.put("tps_a", "A");
+        properties.put("tps_b", "B");
+        properties.put("tps_c", "C");
+    }
+
+    @Override
+    public int getOrdinal() {
+        return 456;
+    }
+
+    @Override
+    public String get(String key) {
+        return getProperties().get(key);
+    }
+
+    @Override
+    public Map<String, String> getProperties() {
+        return Collections.unmodifiableMap(properties);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/6fee4134/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource
----------------------------------------------------------------------
diff --git 
a/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource
 
b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource
new file mode 100644
index 0000000..64d1b0d
--- /dev/null
+++ 
b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySource
@@ -0,0 +1,19 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy current the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+org.apache.tamaya.builder.TestPropertySource

Reply via email to