TAMAYA-214: Rendered provider into property source for better OSGI support.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/ff03e511 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/ff03e511 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/ff03e511 Branch: refs/heads/master Commit: ff03e511babeccf37f4fc8ddb2ae9e774f5e6f16 Parents: 0c7ed25 Author: anatole <anat...@apache.org> Authored: Thu Dec 22 22:00:14 2016 +0100 Committer: anatole <anat...@apache.org> Committed: Thu Dec 22 22:00:14 2016 +0100 ---------------------------------------------------------------------- .../core/propertysource/BasePropertySource.java | 13 ++ .../JavaConfigurationPropertySource.java | 135 +++++++++++++++++++ .../propertysource/SimplePropertySource.java | 2 +- .../provider/JavaConfigurationProvider.java | 116 ---------------- .../org.apache.tamaya.spi.PropertySource | 3 +- ...org.apache.tamaya.spi.PropertySourceProvider | 19 --- .../DefaultConfigurationContextTest.java | 2 +- .../provider/JavaConfigurationProviderTest.java | 14 +- ...org.apache.tamaya.spi.PropertySourceProvider | 1 - 9 files changed, 155 insertions(+), 150 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ff03e511/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 75c4893..c59f78f 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 @@ -155,4 +155,17 @@ public abstract class BasePropertySource implements PropertySource { public boolean isScannable(){ return true; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof BasePropertySource)) return false; + BasePropertySource that = (BasePropertySource) o; + return Objects.equals(getName(), that.getName()); + } + + @Override + public int hashCode() { + return Objects.hash(getName()); + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ff03e511/code/core/src/main/java/org/apache/tamaya/core/propertysource/JavaConfigurationPropertySource.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/propertysource/JavaConfigurationPropertySource.java b/code/core/src/main/java/org/apache/tamaya/core/propertysource/JavaConfigurationPropertySource.java new file mode 100644 index 0000000..90abb72 --- /dev/null +++ b/code/core/src/main/java/org/apache/tamaya/core/propertysource/JavaConfigurationPropertySource.java @@ -0,0 +1,135 @@ +/* + * 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.propertysource; + +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.core.internal.PropertySourceComparator; +import org.apache.tamaya.core.propertysource.SimplePropertySource; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertySourceProvider; +import org.apache.tamaya.spi.ServiceContextManager; + +import java.io.IOException; +import java.net.URL; +import java.util.*; + +import static java.lang.String.format; +import static java.lang.Thread.currentThread; + +/** + * Provider which reads all {@value DEFAULT_SIMPLE_PROPERTIES_FILE_NAME} and + * {@value DEFAULT_XML_PROPERTIES_FILE_NAME} files found in the + * classpath. By setting + * {@code tamaya.defaultprops.disable} or {@code tamaya.defaults.disable} + * as system or environment property this feature can be disabled. + */ +public class JavaConfigurationPropertySource extends BasePropertySource { + /** + * Default location in the classpath, where Tamaya looks for simple line based configuration by default. + */ + public static final String DEFAULT_SIMPLE_PROPERTIES_FILE_NAME="META-INF/javaconfiguration.properties"; + + /** + * Default location in the classpath, where Tamaya looks for XML based configuration by default. + */ + public static final String DEFAULT_XML_PROPERTIES_FILE_NAME = "META-INF/javaconfiguration.xml"; + + private static final int DEFAULT_ORDINAL = 900; + + private boolean enabled = evaluateEnabled(); + + public JavaConfigurationPropertySource(){ + super("resource:META-INF/javaconfiguration.*", DEFAULT_ORDINAL); + } + + private boolean evaluateEnabled() { + String value = System.getProperty("tamaya.defaultprops.disable"); + if(value==null){ + value = System.getenv("tamaya.defaultprops.disable"); + } + if(value==null){ + value = System.getProperty("tamaya.defaults.disable"); + } + if(value==null){ + value = System.getenv("tamaya.defaults.disable"); + } + if(value==null){ + return true; + } + return value.isEmpty() || !Boolean.parseBoolean(value); + } + + private List<PropertySource> getPropertySources() { + List<PropertySource> propertySources = new ArrayList<>(); + propertySources.addAll(loadPropertySourcesByName(DEFAULT_SIMPLE_PROPERTIES_FILE_NAME)); + propertySources.addAll(loadPropertySourcesByName(DEFAULT_XML_PROPERTIES_FILE_NAME)); + Collections.sort(propertySources, PropertySourceComparator.getInstance()); + return propertySources; + } + + private Collection<? extends PropertySource> loadPropertySourcesByName(String filename) { + List<PropertySource> propertySources = new ArrayList<>(); + Enumeration<URL> propertyLocations; + try { + propertyLocations = ServiceContextManager.getServiceContext() + .getResources(filename, currentThread().getContextClassLoader()); + } catch (IOException e) { + String msg = format("Error while searching for %s", filename); + + throw new ConfigException(msg, e); + } + + while (propertyLocations.hasMoreElements()) { + URL currentUrl = propertyLocations.nextElement(); + SimplePropertySource sps = new SimplePropertySource(currentUrl); + + propertySources.add(sps); + } + + return propertySources; + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled){ + this.enabled = enabled; + } + + + @Override + public Map<String, String> getProperties() { + if (!isEnabled()) { + return Collections.emptyMap(); + } + Map<String,String> result = new HashMap<>(); + for(PropertySource ps:getPropertySources()){ + result.putAll(ps.getProperties()); + } + return result; + } + + @Override + public String toString() { + return "JavaConfigurationPropertySource{" + + "enabled=" + enabled + + '}'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ff03e511/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java b/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java index bce68da..6a06b62 100644 --- a/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java +++ b/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java @@ -132,7 +132,7 @@ public class SimplePropertySource extends BasePropertySource { for (String key : props.stringPropertyNames()) { properties.put(key, props.getProperty(key)); if (getName() == null){ - LOG.warning("No property source name found for " + this +", ommitting source meta-entries."); + LOG.finest("No property source name found for " + this +", ommitting source meta-entries."); } else { properties.put("_" + key + ".source", getName()); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ff03e511/code/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/code/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java b/code/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java deleted file mode 100644 index 7d14ed8..0000000 --- a/code/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java +++ /dev/null @@ -1,116 +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.core.provider; - -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.core.propertysource.SimplePropertySource; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertySourceProvider; -import org.apache.tamaya.spi.ServiceContextManager; - -import java.io.IOException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Enumeration; -import java.util.List; - -import static java.lang.String.format; -import static java.lang.Thread.currentThread; - -/** - * Provider which reads all {@value DEFAULT_SIMPLE_PROPERTIES_FILE_NAME} and - * {@value DEFAULT_XML_PROPERTIES_FILE_NAME} files found in the - * classpath. By setting - * {@code tamaya.defaultprops.disable} or {@code tamaya.defaults.disable} - * as system or environment property this feature can be disabled. - */ -public class JavaConfigurationProvider implements PropertySourceProvider { - /** - * Default location in the classpath, where Tamaya looks for simple line based configuration by default. - */ - public static final String DEFAULT_SIMPLE_PROPERTIES_FILE_NAME="META-INF/javaconfiguration.properties"; - - /** - * Default location in the classpath, where Tamaya looks for XML based configuration by default. - */ - public static final String DEFAULT_XML_PROPERTIES_FILE_NAME = "META-INF/javaconfiguration.xml"; - - private final boolean disabled = evaluateDisabled(); - - private boolean evaluateDisabled() { - String value = System.getProperty("tamaya.defaultprops.disable"); - if(value==null){ - value = System.getenv("tamaya.defaultprops.disable"); - } - if(value==null){ - value = System.getProperty("tamaya.defaults.disable"); - } - if(value==null){ - value = System.getenv("tamaya.defaults.disable"); - } - if(value==null){ - return false; - } - return value.isEmpty() || Boolean.parseBoolean(value); - } - - @Override - public Collection<PropertySource> getPropertySources() { - if (isDisabled()) { - return Collections.emptySet(); - } - - List<PropertySource> propertySources = new ArrayList<>(); - - propertySources.addAll(loadPropertySourcesByName(DEFAULT_SIMPLE_PROPERTIES_FILE_NAME)); - propertySources.addAll(loadPropertySourcesByName(DEFAULT_XML_PROPERTIES_FILE_NAME)); - - return Collections.unmodifiableList(propertySources); - } - - private Collection<? extends PropertySource> loadPropertySourcesByName(String filename) { - List<PropertySource> propertySources = new ArrayList<>(); - Enumeration<URL> propertyLocations; - try { - propertyLocations = ServiceContextManager.getServiceContext() - .getResources(filename, currentThread().getContextClassLoader()); - } catch (IOException e) { - String msg = format("Error while searching for %s", filename); - - throw new ConfigException(msg, e); - } - - while (propertyLocations.hasMoreElements()) { - URL currentUrl = propertyLocations.nextElement(); - SimplePropertySource sps = new SimplePropertySource(currentUrl); - - propertySources.add(sps); - } - - return propertySources; - } - - protected boolean isDisabled() { - return disabled; - } - - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ff03e511/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource index 5bc940d..56c599c 100644 --- a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource +++ b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySource @@ -18,4 +18,5 @@ # org.apache.tamaya.core.propertysource.EnvironmentPropertySource org.apache.tamaya.core.propertysource.SystemPropertySource -org.apache.tamaya.core.propertysource.CLIPropertySource \ No newline at end of file +org.apache.tamaya.core.propertysource.CLIPropertySource +org.apache.tamaya.core.propertysource.JavaConfigurationPropertySource \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ff03e511/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider ---------------------------------------------------------------------- diff --git a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider b/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider deleted file mode 100644 index 4535a09..0000000 --- a/code/core/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider +++ /dev/null @@ -1,19 +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 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.core.provider.JavaConfigurationProvider \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ff03e511/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 e0f50d7..6ab5976 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 @@ -53,7 +53,7 @@ public class DefaultConfigurationContextTest { assertEquals(ctx.getPropertySources().size(), 0); ctx = new DefaultConfigurationContextBuilder().addDefaultPropertySources().build(); assertNotNull(ctx.getPropertySources()); - assertEquals(8, ctx.getPropertySources().size()); + assertEquals(7, ctx.getPropertySources().size()); } @Test http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ff03e511/code/core/src/test/java/org/apache/tamaya/core/provider/JavaConfigurationProviderTest.java ---------------------------------------------------------------------- diff --git a/code/core/src/test/java/org/apache/tamaya/core/provider/JavaConfigurationProviderTest.java b/code/core/src/test/java/org/apache/tamaya/core/provider/JavaConfigurationProviderTest.java index 41bd8a1..26e5291 100644 --- a/code/core/src/test/java/org/apache/tamaya/core/provider/JavaConfigurationProviderTest.java +++ b/code/core/src/test/java/org/apache/tamaya/core/provider/JavaConfigurationProviderTest.java @@ -18,17 +18,15 @@ */ package org.apache.tamaya.core.provider; +import org.apache.tamaya.core.propertysource.JavaConfigurationPropertySource; import org.apache.tamaya.spi.PropertySource; import org.junit.Test; -import java.util.Collection; - import static org.apache.tamaya.ConfigurationProvider.getConfiguration; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; public class JavaConfigurationProviderTest { @@ -37,14 +35,8 @@ public class JavaConfigurationProviderTest { @Test public void loadsSimpleAndXMLPropertyFilesProper() { - Collection<PropertySource> propertySources = new JavaConfigurationProvider().getPropertySources(); - - assertThat(propertySources, notNullValue()); - assertThat(propertySources, hasSize(2)); - - PropertySource propertySource = propertySources.iterator().next(); - - assertThat(propertySource.getProperties().keySet(), hasSize(5)); // double the size for .source values. + PropertySource propertySource = new JavaConfigurationPropertySource(); + assertThat(propertySource.getProperties().keySet(), hasSize(7)); // double the size for .source values. for (int i = 1; i < 6; i++) { String key = "confkey" + i; http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ff03e511/code/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider ---------------------------------------------------------------------- diff --git a/code/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider b/code/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider index 9c352f4..c9f255a 100644 --- a/code/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider +++ b/code/core/src/test/resources/META-INF/services/org.apache.tamaya.spi.PropertySourceProvider @@ -17,4 +17,3 @@ # under the License. # org.apache.tamaya.core.testdata.TestPropertySourceProvider -org.apache.tamaya.core.provider.JavaConfigurationProvider