TAMAYA-60 Automatic loading of provided PropertySourceProviders is now configurable.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/0a1befb1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/0a1befb1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/0a1befb1 Branch: refs/heads/master Commit: 0a1befb10e3ca5f15e7ad814857bf2dca39ebc92 Parents: 662a557 Author: Oliver B. Fischer <[email protected]> Authored: Sun Feb 8 13:16:14 2015 +0100 Committer: Oliver B. Fischer <[email protected]> Committed: Sun Feb 8 13:16:14 2015 +0100 ---------------------------------------------------------------------- .../tamaya/builder/ConfigurationBuilder.java | 21 ++++++ .../ProgrammaticConfigurationContext.java | 16 +++- .../builder/ConfigurationBuilderTest.java | 53 +++++++++++++ .../builder/TestPropertySourceProvider.java | 79 ++++++++++++++++++++ ...org.apache.tamaya.spi.PropertySourceProvider | 19 +++++ 5 files changed, 187 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/0a1befb1/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 00198f1..f3fcde3 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 @@ -66,6 +66,7 @@ public class ConfigurationBuilder { * will be loaded if the configuration is build. */ private boolean loadProvidedPropertySources = false; + private boolean loadProvidedPropertySourceProviders = false; /** @@ -175,6 +176,25 @@ public class ConfigurationBuilder { return this; } + public ConfigurationBuilder enableProvidedPropertySourceProviders() { + checkBuilderState(); + + loadProvidedPropertySourceProviders = true; + + return this; + } + + public ConfigurationBuilder disableProvidedPropertySourceProviders() { + checkBuilderState(); + + loadProvidedPropertySourceProviders = false; + + return this; + } + + public boolean isPropertySourceProvidersLoadingEnabled() { + return loadProvidedPropertySourceProviders; + } //X TODO think on a functonality/API for using the default PropertyConverters and use the configured ones here //X TODO as overrides used first. @@ -191,6 +211,7 @@ public class ConfigurationBuilder { contextBuilder.loadProvidedPropertyConverters(isPropertyConverterLoadingEnabled()); contextBuilder.loadProvidedPropertySources(isPropertySourcesLoadingEnabled()); + contextBuilder.loadProvidedPropertySourceProviders(isPropertySourceProvidersLoadingEnabled()); return new DefaultConfiguration(contextBuilder.build()); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/0a1befb1/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 75fe389..f61c561 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 @@ -122,7 +122,16 @@ class ProgrammaticConfigurationContext implements ConfigurationContext { private List<PropertySource> getAllPropertySources(Builder builder) { List<PropertySource> provided = builder.loadProvidedPropertySources ? ServiceContext.getInstance().getServices(PropertySource.class) - : emptyList(); + : new ArrayList<>(); + + if (builder.loadProvidedPropertySourceProviders) { + List<PropertySourceProvider> providers = ServiceContext.getInstance() + .getServices(PropertySourceProvider.class); + for (PropertySourceProvider provider : providers) { + Collection<PropertySource> sources = provider.getPropertySources(); + provided.addAll(sources); + } + } List<PropertySource> configured = builder.propertySources; @@ -265,6 +274,7 @@ class ProgrammaticConfigurationContext implements ConfigurationContext { private boolean loadProvidedPropertyConverters; private boolean loadProvidedPropertySources; + private boolean loadProvidedPropertySourceProviders; public Builder setPropertyValueCombinationPolicy(PropertyValueCombinationPolicy policy) { this.propertyValueCombinationPolicy = Objects.requireNonNull(policy); @@ -342,6 +352,10 @@ class ProgrammaticConfigurationContext implements ConfigurationContext { public void loadProvidedPropertySources(boolean state) { loadProvidedPropertySources = state; } + + public void loadProvidedPropertySourceProviders(boolean state) { + loadProvidedPropertySourceProviders = state; + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/0a1befb1/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 85628c1..8d710f1 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 @@ -32,6 +32,7 @@ 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; @@ -444,4 +445,56 @@ public class ConfigurationBuilderTest { assertThat(config.get("tps_c"), Matchers.nullValue()); } + + /********************************************************************* + * Tests for enabling and disabling of automatic loading of + * P r o p e r t y S o u r c e P r o v i d e r s + */ + + @Test + public void disablingOfPropertySourceProvidersIsOk() { + ConfigurationBuilder builder = new ConfigurationBuilder(); + + builder.enableProvidedPropertySourceProviders() + .disableProvidedPropertySourceProviders() + .build(); + + assertThat(builder.isPropertySourceProvidersLoadingEnabled(), is(false)); + } + + @Test + public void enablingOfPropertySourceProvidersIsOk() { + + ConfigurationBuilder builder = new ConfigurationBuilder(); + + builder.disableProvidedPropertySourceProviders() + .enableProvidedPropertySourceProviders() + .build(); + + assertThat(builder.isPropertySourceProvidersLoadingEnabled(), is(true)); + } + + @Test + public void loadingOfPropertySourceProvidersCanBeEnabled() { + ConfigurationBuilder builder = new ConfigurationBuilder(); + + Configuration config = builder.disableProvidedPropertySourceProviders() + .enableProvidedPropertySourceProviders() + .build(); + + assertThat(config.get("tpsp_x"), Matchers.equalTo("X")); + assertThat(config.get("tpsp_y"), Matchers.equalTo("Y")); + } + + @Test + public void loadingOfPropertySourceProvidersCanBeDisabled() { + ConfigurationBuilder builder = new ConfigurationBuilder(); + + Configuration config = builder.enableProvidedPropertySourceProviders() + .disableProvidedPropertySourceProviders() + .build(); + + assertThat(config.get("tpsp_x"), nullValue()); + assertThat(config.get("tpsp_x"), nullValue()); + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/0a1befb1/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProvider.java b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProvider.java new file mode 100644 index 0000000..f82a8e7 --- /dev/null +++ b/modules/builder/src/test/java/org/apache/tamaya/builder/TestPropertySourceProvider.java @@ -0,0 +1,79 @@ +/* + * 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 org.apache.tamaya.spi.PropertySourceProvider; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + +public class TestPropertySourceProvider + implements PropertySourceProvider +{ + @Override + public Collection<PropertySource> getPropertySources() { + ArrayList<PropertySource> sources = new ArrayList<>(2); + + sources.add(new XProvidingPropertySource()); + sources.add(new YProvidingPropertySource()); + + return sources; + } + + private class YProvidingPropertySource implements PropertySource { + private Map<String, String> props = Collections.singletonMap("tpsp_x", "X"); + + @Override + public int getOrdinal() { + return 100; + } + + @Override + public String get(String key) { + return getProperties().get(key); + } + + @Override + public Map<String, String> getProperties() { + return props; + } + } + + private class XProvidingPropertySource implements PropertySource { + private Map<String, String> props = Collections.singletonMap("tpsp_y", "Y"); + + @Override + public Map<String, String> getProperties() { + return props; + } + + @Override + public String get(String key) { + return getProperties().get(key); + } + + @Override + public int getOrdinal() { + return 100; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/0a1befb1/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider ---------------------------------------------------------------------- diff --git a/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider new file mode 100644 index 0000000..9a19ea0 --- /dev/null +++ b/modules/builder/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider @@ -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.TestPropertySourceProvider
