Repository: incubator-tamaya-extensions Updated Branches: refs/heads/master 14b3abbd5 -> dc2cd7d38
TAMAYA-196: Added mutable name support to BaseProperty. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/df3978aa Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/df3978aa Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/df3978aa Branch: refs/heads/master Commit: df3978aa09fbb38142d68cc40461fc76a8aefe82 Parents: 14b3abb Author: anatole <[email protected]> Authored: Sun Nov 13 23:05:59 2016 +0100 Committer: anatole <[email protected]> Committed: Sun Nov 13 23:05:59 2016 +0100 ---------------------------------------------------------------------- .../tamaya/spisupport/BasePropertySource.java | 46 ++++- .../tamaya/spisupport/CLIPropertySource.java | 1 + .../spisupport/EnvironmentPropertySource.java | 7 +- .../tamaya/spisupport/MapPropertySource.java | 13 +- .../tamaya/spisupport/SimplePropertySource.java | 185 ++++++++++++++++--- .../tamaya/spisupport/SystemPropertySource.java | 12 +- .../spisupport/BasePropertySourceTest.java | 21 +-- .../spisupport/SimplePropertySourceTest.java | 4 +- 8 files changed, 216 insertions(+), 73 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/df3978aa/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java index a79df5e..30ff90d 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BasePropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/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; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/df3978aa/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java index edcbafe..4b0e249 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/CLIPropertySource.java @@ -52,6 +52,7 @@ public class CLIPropertySource extends BasePropertySource{ * @param args the args, or null. */ public CLIPropertySource(String... args){ + super("CLI"); if(args!=null){ initMainArgs(args); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/df3978aa/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java index 7717c11..1dd4ce7 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/EnvironmentPropertySource.java @@ -37,7 +37,7 @@ public class EnvironmentPropertySource extends BasePropertySource { private static final Logger LOG = Logger.getLogger(EnvironmentPropertySource.class.getName()); /** - * default ordinal for {@link org.apache.tamaya.core.propertysource.EnvironmentPropertySource} + * Default ordinal used. */ public static final int DEFAULT_ORDINAL = 300; @@ -108,6 +108,7 @@ public class EnvironmentPropertySource extends BasePropertySource { * @param ordinal the ordinal to be used. */ public EnvironmentPropertySource(String prefix, int ordinal){ + super("environment-properties"); this.prefix = prefix; setOrdinal(ordinal); } @@ -128,9 +129,9 @@ public class EnvironmentPropertySource extends BasePropertySource { @Override public String getName() { if(disabled){ - return "environment-properties(disabled)"; + return super.getName() + "(disabled)"; } - return "environment-properties"; + return super.getName(); } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/df3978aa/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java index 5580c24..de2beef 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/MapPropertySource.java @@ -19,7 +19,6 @@ package org.apache.tamaya.spisupport; import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Objects; import java.util.Properties; /** @@ -28,9 +27,6 @@ import java.util.Properties; */ public class MapPropertySource extends BasePropertySource { - /** The unique name of the PropertySource. */ - private final String name; - /** * The current properties. */ @@ -56,7 +52,7 @@ public class MapPropertySource extends BasePropertySource { * @param prefix the prefix context mapping, or null (for no mapping). */ public MapPropertySource(String name, Map<String, String> props, String prefix) { - this.name = Objects.requireNonNull(name); + super(name); if (prefix == null) { this.props.putAll(props); } else { @@ -93,11 +89,6 @@ public class MapPropertySource extends BasePropertySource { @Override - public String getName() { - return name; - } - - @Override public Map<String, String> getProperties() { return Collections.unmodifiableMap(this.props); } @@ -105,7 +96,7 @@ public class MapPropertySource extends BasePropertySource { @Override public String toString() { return "MapPropertySource{" + - "name=" + name + ", " + + "name=" + getName() + ", " + "ordinal=" + getOrdinal() + '}'; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/df3978aa/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java index 641e25d..37e3a7a 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SimplePropertySource.java @@ -23,11 +23,13 @@ import org.apache.tamaya.ConfigException; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.Properties; +import java.util.UUID; import java.util.logging.Logger; /** @@ -39,11 +41,6 @@ public class SimplePropertySource extends BasePropertySource { private static final Logger LOG = Logger.getLogger(SimplePropertySource.class.getName()); /** - * The property source name. - */ - private String name; - - /** * The current properties. */ private Map<String, String> properties; @@ -54,9 +51,8 @@ public class SimplePropertySource extends BasePropertySource { * @param propertiesLocation the URL encoded location, not null. */ public SimplePropertySource(File propertiesLocation) { - super(0); + super(propertiesLocation.toString(), 0); try { - this.name = propertiesLocation.toString(); this.properties = load(propertiesLocation.toURI().toURL()); } catch (IOException e) { throw new ConfigException("Failed to load properties from " + propertiesLocation, e); @@ -69,9 +65,8 @@ public class SimplePropertySource extends BasePropertySource { * @param propertiesLocation the URL encoded location, not null. */ public SimplePropertySource(URL propertiesLocation) { - super(0); + super(propertiesLocation.toString(), 0); this.properties = load(Objects.requireNonNull(propertiesLocation)); - this.name = propertiesLocation.toString(); } /** @@ -82,9 +77,8 @@ public class SimplePropertySource extends BasePropertySource { * @param defaultOrdinal the default ordinal */ public SimplePropertySource(String name, Map<String, String> properties, int defaultOrdinal){ - super(defaultOrdinal); + super(name, defaultOrdinal); this.properties = new HashMap<>(properties); - this.name = Objects.requireNonNull(name); } /** @@ -94,9 +88,8 @@ public class SimplePropertySource extends BasePropertySource { * @param properties the properties, not null. */ public SimplePropertySource(String name, Map<String, String> properties) { - super(0); + super(name, 0); this.properties = new HashMap<>(properties); - this.name = Objects.requireNonNull(name); } /** @@ -106,14 +99,23 @@ public class SimplePropertySource extends BasePropertySource { * @param propertiesLocation the URL encoded location, not null. */ public SimplePropertySource(String name, URL propertiesLocation) { - super(0); + super(name, 0); this.properties = load(propertiesLocation); - this.name = Objects.requireNonNull(name); } - @Override - public String getName() { - return name; + private SimplePropertySource(Builder builder) { + properties = builder.properties; + if(builder.defaultOrdinal!=null){ + setDefaultOrdinal(builder.defaultOrdinal); + } + if(builder.ordinal!=null){ + setOrdinal(builder.ordinal); + } + setName(builder.name); + } + + public static Builder newBuilder() { + return new Builder(); } @Override @@ -124,11 +126,11 @@ public class SimplePropertySource extends BasePropertySource { /** * loads the Properties from the given URL * - * @param propertiesFile {@link java.net.URL} to load Properties from - * @return loaded {@link java.util.Properties} + * @param propertiesFile {@link URL} to load Properties from + * @return loaded {@link Properties} * @throws IllegalStateException in case of an error while reading properties-file */ - private Map<String, String> load(URL propertiesFile) { + private static Map<String, String> load(URL propertiesFile) { boolean isXML = isXMLPropertieFiles(propertiesFile); Map<String, String> properties = new HashMap<>(); @@ -141,14 +143,10 @@ public class SimplePropertySource extends BasePropertySource { props.load(stream); } } - + String source = propertiesFile.toString(); 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."); - } else { - properties.put("_" + key + ".source", getName()); - } + properties.put("_" + key + ".source", source); } } catch (IOException e) { throw new ConfigException("Error loading properties from " + propertiesFile, e); @@ -157,8 +155,139 @@ public class SimplePropertySource extends BasePropertySource { return properties; } - private boolean isXMLPropertieFiles(URL url) { + private static boolean isXMLPropertieFiles(URL url) { return url.getFile().endsWith(".xml"); } + + /** + * {@code SimplePropertySource} builder static inner class. + */ + public static final class Builder { + private String name; + private Integer defaultOrdinal; + private Integer ordinal; + private Map<String, String> properties = new HashMap<>(); + + private Builder() { + } + + /** + * Sets the {@code name} to a new UUID and returns a reference to this Builder so that the methods + * can be chained together. + * + * @return a reference to this Builder + */ + public Builder withUuidName() { + this.name = UUID.randomUUID().toString(); + return this; + } + + /** + * Sets the {@code name} and returns a reference to this Builder so that the methods + * can be chained together. + * + * @param val the {@code name} to set, not null. + * @return a reference to this Builder + */ + public Builder withName(String val) { + this.name = Objects.requireNonNull(name); + return this; + } + + /** + * Sets the {@code ordinal} and returns a reference to this Builder so that the methods + * can be chained together. + * + * @param val the {@code ordinal} to set + * @return a reference to this Builder + */ + public Builder withOrdinal(int val) { + this.ordinal = val; + return this; + } + + /** + * Sets the {@code defaultOrdinal} and returns a reference to this Builder so that the methods + * can be chained together. + * + * @param val the {@code defaultOrdinal} to set + * @return a reference to this Builder + */ + public Builder withDefaultOrdinal(int val) { + this.defaultOrdinal = val; + return this; + } + + /** + * Reads the {@code properties} from the given resource and returns a reference + * to this Builder so that the methods can be chained together. + * + * @param resource the {@code resource} to read + * @return a reference to this Builder + */ + public Builder withProperties(URL resource) { + this.properties.putAll(load(resource)); + return this; + } + + /** + * Reads the {@code properties} from the given resource and returns a reference + * to this Builder so that the methods can be chained together. + * + * @param file the {@code file} to read from (xml or properties format). + * @return a reference to this Builder + */ + public Builder withProperties(File file) { + try { + this.properties.putAll(load(file.toURI().toURL())); + } catch (MalformedURLException e) { + throw new IllegalArgumentException("Failed to read file: " + file, e); + } + return this; + } + + /** + * Sets the {@code properties} and returns a reference to this Builder so that the methods can be chained together. + * + * @param val the {@code properties} to set + * @return a reference to this Builder + */ + public Builder withProperties(Map<String, String> val) { + this.properties.putAll(val); + return this; + } + + /** + * Sets the {@code properties} and returns a reference to this Builder so that the methods can be chained together. + * + * @param val the {@code properties} to set + * @return a reference to this Builder + */ + public Builder withProperty(String key, String val) { + this.properties.put(key, val); + return this; + } + + /** + * Sets the {@code properties} and returns a reference to this Builder so that the methods can be chained together. + * + * @param val the {@code properties} to set + * @return a reference to this Builder + */ + public Builder withSource(String val) { + this.properties.put("_source", val); + return this; + } + + /** + * Returns a {@code SimplePropertySource} built from the parameters previously set. + * + * @return a {@code SimplePropertySource} built with parameters of this {@code SimplePropertySource.Builder} + */ + public SimplePropertySource build() { + this.properties.put("_builtAt", String.valueOf(System.currentTimeMillis())); + return new SimplePropertySource(this); + } + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/df3978aa/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java index ea10e93..25863d6 100644 --- a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/SystemPropertySource.java @@ -32,7 +32,7 @@ import java.util.Properties; public class SystemPropertySource extends BasePropertySource { /** - * default ordinal for {@link org.apache.tamaya.core.propertysource.SystemPropertySource} + * default ordinal used. */ public static final int DEFAULT_ORDINAL = 1000; @@ -65,6 +65,7 @@ public class SystemPropertySource extends BasePropertySource { * </pre> */ public SystemPropertySource(){ + super("system-properties", DEFAULT_ORDINAL); initFromSystemProperties(); if(!disabled){ cachedProperties = Collections.unmodifiableMap(loadProperties()); @@ -125,11 +126,6 @@ public class SystemPropertySource extends BasePropertySource { this.prefix = prefix; } - @Override - public int getDefaultOrdinal() { - return DEFAULT_ORDINAL; - } - private Map<String, String> loadProperties() { Properties sysProps = System.getProperties(); @@ -151,9 +147,9 @@ public class SystemPropertySource extends BasePropertySource { @Override public String getName() { if(disabled){ - return "system-properties(disabled)"; + return super.getName() + "(disabled)"; } - return "system-properties"; + return super.getName(); } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/df3978aa/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BasePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BasePropertySourceTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BasePropertySourceTest.java index 96f1c7f..dc24165 100644 --- a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BasePropertySourceTest.java +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BasePropertySourceTest.java @@ -34,12 +34,7 @@ public class BasePropertySourceTest { @Test public void testGetOrdinal() { - PropertySource defaultPropertySource = new BasePropertySource(56) { - - @Override - public String getName() { - return "testWithDefault"; - } + PropertySource defaultPropertySource = new BasePropertySource("testWithDefault", 56) { @Override public PropertyValue get(String key) { @@ -67,12 +62,7 @@ public class BasePropertySourceTest { private static class OverriddenOrdinalPropertySource extends BasePropertySource { private OverriddenOrdinalPropertySource() { - super(250); - } - - @Override - public String getName() { - return "overriddenOrdinal"; + super("overriddenOrdinal", 250); } @Override @@ -86,12 +76,7 @@ public class BasePropertySourceTest { private static class OverriddenInvalidOrdinalPropertySource extends BasePropertySource { private OverriddenInvalidOrdinalPropertySource() { - super(1); - } - - @Override - public String getName() { - return "overriddenInvalidOrdinal"; + super("overriddenInvalidOrdinal", 1); } @Override http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/df3978aa/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SimplePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SimplePropertySourceTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SimplePropertySourceTest.java index e199609..a7409a4 100644 --- a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SimplePropertySourceTest.java +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/SimplePropertySourceTest.java @@ -36,7 +36,7 @@ public class SimplePropertySourceTest { SimplePropertySource source = new SimplePropertySource(resource); assertThat(source, notNullValue()); - assertThat(source.getProperties(), aMapWithSize(2)); + assertThat(source.getProperties(), aMapWithSize(4)); assertThat(source.getProperties(), hasEntry("a", "b")); assertThat(source.getProperties(), hasEntry("b", "1")); @@ -80,6 +80,6 @@ public class SimplePropertySourceTest { SimplePropertySource source = new SimplePropertySource(resource); assertThat(source, notNullValue()); - assertThat(source.getProperties(), aMapWithSize(5)); + assertThat(source.getProperties(), aMapWithSize(10)); // 5 * 2 meta entries. } }
