Repository: incubator-tamaya
Updated Branches:
  refs/heads/master 924613847 -> 28988c323


TAMAYA-182: Added additional constructors for better support for programmtic 
configuration context composition using the builder API and explicit ordinal 
overriding.


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

Branch: refs/heads/master
Commit: 28988c323bd5fe3eb23ff2554ff3a345f037568c
Parents: 9246138
Author: anatole <[email protected]>
Authored: Sun Nov 6 18:00:11 2016 +0100
Committer: anatole <[email protected]>
Committed: Sun Nov 6 18:00:11 2016 +0100

----------------------------------------------------------------------
 .../core/propertysource/BasePropertySource.java |  19 +++-
 .../core/propertysource/CLIPropertySource.java  |  29 ++++-
 .../EnvironmentPropertySource.java              | 101 ++++++++++++++---
 .../propertysource/SystemPropertySource.java    | 113 +++++++++++++++----
 4 files changed, 221 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/28988c32/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 3745537..fd38818 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
@@ -34,6 +34,9 @@ public abstract class BasePropertySource implements 
PropertySource{
     /** default ordinal that will be used, if no ordinal is provided with the 
config. */
     private final int defaultOrdinal;
 
+    /** Used if the ordinal has been set explicitly. */
+    private volatile Integer ordinal;
+
     /**
      * Constructor.
      * @param defaultOrdinal default ordinal that will be used, if no ordinal 
is provided with the config.
@@ -54,10 +57,24 @@ public abstract class BasePropertySource implements 
PropertySource{
         return getClass().getSimpleName();
     }
 
+    /**
+     * 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 ordinal the explicit ordinal, or null.
+     */
+    public void setOrdinal(Integer ordinal){
+        this.ordinal = ordinal;
+    }
+
     @Override
     public int getOrdinal() {
+        Integer ordinal = this.ordinal;
+        if(ordinal!=null){
+            Logger.getLogger(getClass().getName()).finest(
+                    "Using explicit ordinal '"+ordinal+"' for property source: 
" + getName());
+            return ordinal;
+        }
         PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL);
-
         if(configuredOrdinal!=null){
             try {
                 return Integer.parseInt(configuredOrdinal.getValue());

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/28988c32/code/core/src/main/java/org/apache/tamaya/core/propertysource/CLIPropertySource.java
----------------------------------------------------------------------
diff --git 
a/code/core/src/main/java/org/apache/tamaya/core/propertysource/CLIPropertySource.java
 
b/code/core/src/main/java/org/apache/tamaya/core/propertysource/CLIPropertySource.java
index e2d5299..87bbceb 100644
--- 
a/code/core/src/main/java/org/apache/tamaya/core/propertysource/CLIPropertySource.java
+++ 
b/code/core/src/main/java/org/apache/tamaya/core/propertysource/CLIPropertySource.java
@@ -40,11 +40,36 @@ public class CLIPropertySource extends BasePropertySource{
         initMainArgs(args);
     }
 
-
     /**
      * Creates a new instance.
      */
-    public CLIPropertySource(){}
+    public CLIPropertySource(){
+        this(null);
+    }
+
+    /**
+     * Creates a new instance, allows optionally to pass the main arguments.
+     * @param args the args, or null.
+     */
+    public CLIPropertySource(String... args){
+        if(args!=null){
+            initMainArgs(args);
+        }
+    }
+
+    /**
+     * Creates a new instance, allows optionally to pass the main arguments.
+     * @param args the args, or null.
+     * @param ordinal the ordinal to be applied.
+     */
+    public CLIPropertySource(int ordinal, String... args){
+        if(args!=null){
+            initMainArgs(args);
+        }
+        setOrdinal(ordinal);
+    }
+
+
 
     /**
      * Configure the main arguments, hereby parsing and mapping the main 
arguments into

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/28988c32/code/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
----------------------------------------------------------------------
diff --git 
a/code/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
 
b/code/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
index ee5fda0..43e2449 100644
--- 
a/code/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
+++ 
b/code/core/src/main/java/org/apache/tamaya/core/propertysource/EnvironmentPropertySource.java
@@ -18,7 +18,6 @@
  */
 package org.apache.tamaya.core.propertysource;
 
-import org.apache.tamaya.spi.PropertySource;
 import org.apache.tamaya.spi.PropertyValue;
 
 import java.util.Collections;
@@ -33,7 +32,7 @@ import java.util.logging.Logger;
  * {@code set myprop=myval} on Windows. You can disable this feature by 
setting {@code tamaya.envprops.disable}
  * or {@code tamaya.defaults.disable}.
  */
-public class EnvironmentPropertySource implements PropertySource {
+public class EnvironmentPropertySource extends BasePropertySource {
 
     private static final Logger LOG = 
Logger.getLogger(EnvironmentPropertySource.class.getName());
 
@@ -42,10 +41,45 @@ public class EnvironmentPropertySource implements 
PropertySource {
      */
     public static final int DEFAULT_ORDINAL = 300;
 
-    private final boolean disabled = evaluateDisabled();
+    /**
+     * Prefix that allows environment properties to virtually be mapped on 
specified sub section.
+     */
+    private String prefix;
+
+    /**
+     * If true, this property source does not return any properties. This is 
useful since this
+     * property source is applied by default, but can be switched off by 
setting the
+     * {@code tamaya.envprops.disable} system/environment property to {@code 
true}.
+     */
+    private boolean disabled = false;
+
+
+    /**
+     * Creates a new instance. Also initializes the {@code prefix} and {@code 
disabled} properties
+     * from the system-/ environment properties:
+     * <pre>
+     *     tamaya.envprops.prefix
+     *     tamaya.envprops.disable
+     * </pre>
+     */
+    public EnvironmentPropertySource(){
+        initFromSystemProperties();
+    }
 
-    private boolean evaluateDisabled() {
-        String value = System.getProperty("tamaya.envprops.disable");
+    /**
+     * Initializes the {@code prefix} and {@code disabled} properties from the 
system-/
+     * environment properties:
+     * <pre>
+     *     tamaya.envprops.prefix
+     *     tamaya.envprops.disable
+     * </pre>
+     */
+    private void initFromSystemProperties() {
+        String value = System.getProperty("tamaya.envprops.prefix");
+        if(value==null){
+            prefix = System.getenv("tamaya.envprops.prefix");
+        }
+        value = System.getProperty("tamaya.envprops.disable");
         if(value==null){
             value = System.getenv("tamaya.envprops.disable");
         }
@@ -55,14 +89,39 @@ public class EnvironmentPropertySource implements 
PropertySource {
         if(value==null){
             value = System.getenv("tamaya.defaults.disable");
         }
-        if(value==null){
-            return false;
+        if(value!=null && !value.isEmpty()) {
+            this.disabled = Boolean.parseBoolean(value);
         }
-        return value.isEmpty() || Boolean.parseBoolean(value);
+    }
+
+    /**
+     * Creates a new instance using a fixed ordinal value.
+     * @param ordinal the ordinal number.
+     */
+    public EnvironmentPropertySource(int ordinal){
+        this(null, ordinal);
+    }
+
+    /**
+     * Creates a new instance.
+     * @param prefix the prefix to be used, or null.
+     * @param ordinal the ordinal to be used.
+     */
+    public EnvironmentPropertySource(String prefix, int ordinal){
+        this.prefix = prefix;
+        setOrdinal(ordinal);
+    }
+
+    /**
+     * Creates a new instance.
+     * @param prefix the prefix to be used, or null.
+     */
+    public EnvironmentPropertySource(String prefix){
+        this.prefix = prefix;
     }
 
     @Override
-    public int getOrdinal() {
+    public int getDefaultOrdinal() {
         return DEFAULT_ORDINAL;
     }
 
@@ -79,7 +138,11 @@ public class EnvironmentPropertySource implements 
PropertySource {
         if(disabled){
             return null;
         }
-        return PropertyValue.of(key, System.getenv(key), getName());
+        String prefix = this.prefix;
+        if(prefix==null) {
+            return PropertyValue.of(key, System.getenv(key), getName());
+        }
+        return PropertyValue.of(key, 
System.getenv(key.substring(prefix.length())), getName());
     }
 
     @Override
@@ -87,11 +150,21 @@ public class EnvironmentPropertySource implements 
PropertySource {
         if(disabled){
             return Collections.emptyMap();
         }
-        Map<String, String> entries = new HashMap<>(System.getenv());
-        for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
-            entries.put("_" + entry.getKey() + ".source", getName());
+        String prefix = this.prefix;
+        if(prefix==null) {
+            Map<String, String> entries = new HashMap<>(System.getenv());
+            for (Map.Entry<String, String> entry : System.getenv().entrySet()) 
{
+                entries.put("_" + entry.getKey() + ".source", getName());
+            }
+            return entries;
+        }else{
+            Map<String, String> entries = new HashMap<>();
+            for (Map.Entry<String, String> entry : System.getenv().entrySet()) 
{
+                entries.put(prefix + entry.getKey(), entry.getValue());
+                entries.put("_" + prefix + entry.getKey() + ".source", 
getName());
+            }
+            return entries;
         }
-        return entries;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/28988c32/code/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
----------------------------------------------------------------------
diff --git 
a/code/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
 
b/code/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
index bd568d4..5c7604e 100644
--- 
a/code/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
+++ 
b/code/core/src/main/java/org/apache/tamaya/core/propertysource/SystemPropertySource.java
@@ -18,7 +18,6 @@
  */
 package org.apache.tamaya.core.propertysource;
 
-import org.apache.tamaya.spi.PropertySource;
 import org.apache.tamaya.spi.PropertyValue;
 
 import java.util.Collections;
@@ -30,7 +29,7 @@ import java.util.Properties;
  * This {@link org.apache.tamaya.spi.PropertySource} manages the system 
properties. You can disable this feature by
  * setting {@code tamaya.envprops.disable} or {@code tamaya.defaults.disable}.
  */
-public class SystemPropertySource implements PropertySource {
+public class SystemPropertySource extends BasePropertySource {
 
     /**
      * default ordinal for {@link 
org.apache.tamaya.core.propertysource.SystemPropertySource}
@@ -43,12 +42,49 @@ public class SystemPropertySource implements PropertySource 
{
      * previous System.getProperties().hashCode()
      * so we can check if we need to reload
      */
-    private int previousHash;
+    private volatile int previousHash;
 
-    private final boolean disabled = evaluateDisabled();
+    /**
+     * Prefix that allows system properties to virtually be mapped on 
specified sub section.
+     */
+    private String prefix;
+
+    /**
+     * If true, this property source does not return any properties. This is 
useful since this
+     * property source is applied by default, but can be switched off by 
setting the
+     * {@code tamaya.envprops.disable} system/environment property to {@code 
true}.
+     */
+    private boolean disabled = false;
+
+    /**
+     * Creates a new instance. Also initializes the {@code prefix} and {@code 
disabled} properties
+     * from the system-/ environment properties:
+     * <pre>
+     *     tamaya.envprops.prefix
+     *     tamaya.envprops.disable
+     * </pre>
+     */
+    public SystemPropertySource(){
+        initFromSystemProperties();
+        if(!disabled){
+            cachedProperties = Collections.unmodifiableMap(loadProperties());
+        }
+    }
 
-    private boolean evaluateDisabled() {
-        String value = System.getProperty("tamaya.sysprops.disable");
+    /**
+     * Initializes the {@code prefix} and {@code disabled} properties from the 
system-/
+     * environment properties:
+     * <pre>
+     *     tamaya.envprops.prefix
+     *     tamaya.envprops.disable
+     * </pre>
+     */
+    private void initFromSystemProperties() {
+        String value = System.getProperty("tamaya.sysprops.prefix");
+        if(value==null){
+            prefix = System.getenv("tamaya.sysprops.prefix");
+        }
+        value = System.getProperty("tamaya.sysprops.disable");
         if(value==null){
             value = System.getenv("tamaya.sysprops.disable");
         }
@@ -58,34 +94,60 @@ public class SystemPropertySource implements PropertySource 
{
         if(value==null){
             value = System.getenv("tamaya.defaults.disable");
         }
-        if(value==null){
-            return false;
+        if(value!=null && !value.isEmpty()) {
+            this.disabled = Boolean.parseBoolean(value);
         }
-        return value.isEmpty() || Boolean.parseBoolean(value);
     }
 
+    /**
+     * Creates a new instance using a fixed ordinal value.
+     * @param ordinal the ordinal number.
+     */
+    public SystemPropertySource(int ordinal){
+        this(null, ordinal);
+    }
 
-
-    public SystemPropertySource() {
-        cachedProperties = loadProperties();
-        previousHash = System.getProperties().hashCode();
+    /**
+     * Creates a new instance.
+     * @param prefix the prefix to be used, or null.
+     * @param ordinal the ordinal to be used.
+     */
+    public SystemPropertySource(String prefix, int ordinal){
+        this.prefix = prefix;
+        setOrdinal(ordinal);
     }
 
-    private Map<String, String> loadProperties() {
-        Map<String,String> props = new HashMap<>();
-        Properties sysProps = System.getProperties();
-        for(String name: sysProps.stringPropertyNames()) {
-            props.put(name,sysProps.getProperty(name));
-            props.put("_"+name+".source",getName());
-        }
-        return props;
+    /**
+     * Creates a new instance.
+     * @param prefix the prefix to be used, or null.
+     */
+    public SystemPropertySource(String prefix){
+        this.prefix = prefix;
     }
 
     @Override
-    public int getOrdinal() {
+    public int getDefaultOrdinal() {
         return DEFAULT_ORDINAL;
     }
 
+
+    private Map<String, String> loadProperties() {
+        Properties sysProps = System.getProperties();
+        previousHash = System.getProperties().hashCode();
+        final String prefix = this.prefix;
+        Map<String, String> entries = new HashMap<>();
+        for (Map.Entry<Object,Object> entry : sysProps.entrySet()) {
+            if(prefix==null) {
+                entries.put("_" + entry.getKey() + ".source", getName());
+                entries.put((String) entry.getKey(), (String) 
entry.getValue());
+            }else {
+                entries.put(prefix + entry.getKey(), (String)entry.getValue());
+                entries.put("_" + prefix + entry.getKey() + ".source", 
getName());
+            }
+        }
+        return entries;
+    }
+
     @Override
     public String getName() {
         if(disabled){
@@ -99,7 +161,11 @@ public class SystemPropertySource implements PropertySource 
{
         if(disabled){
             return null;
         }
-        return PropertyValue.of(key, System.getProperty(key), getName());
+        String prefix = this.prefix;
+        if(prefix==null) {
+            return PropertyValue.of(key, System.getProperty(key), getName());
+        }
+        return PropertyValue.of(key, 
System.getProperty(key.substring(prefix.length())), getName());
     }
 
     @Override
@@ -113,7 +179,6 @@ public class SystemPropertySource implements PropertySource 
{
         if (previousHash != System.getProperties().hashCode()) {
             Map<String, String> properties = loadProperties();
             this.cachedProperties = Collections.unmodifiableMap(properties);
-            previousHash = System.getProperties().hashCode();
         }
         return this.cachedProperties;
     }

Reply via email to