deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject git commit: DELTASPIKE-378 add ProjectStageAware property handling
Date Mon, 03 Jun 2013 09:05:09 GMT
Updated Branches:
  refs/heads/master 5bd1d57bf -> d7929a405


DELTASPIKE-378 add ProjectStageAware property handling

Main entry point for this feature is
ConfigResolver#getProjectStageAwarePropertyValue


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/d7929a40
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/d7929a40
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/d7929a40

Branch: refs/heads/master
Commit: d7929a40521731c53f510996bf9918ff3b158e3d
Parents: 5bd1d57
Author: Mark Struberg <struberg@apache.org>
Authored: Mon Jun 3 11:03:55 2013 +0200
Committer: Mark Struberg <struberg@apache.org>
Committed: Mon Jun 3 11:03:55 2013 +0200

----------------------------------------------------------------------
 .../deltaspike/core/api/config/ConfigResolver.java |   46 +++++++++++++++
 .../deltaspike/core/util/ProjectStageProducer.java |    1 +
 .../test/api/config/ConfigResolverTest.java        |   10 +++
 .../test/api/config/TestConfigSource.java          |   15 ++++-
 4 files changed, 68 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d7929a40/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
index 43bb660..05cbc59 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/config/ConfigResolver.java
@@ -31,9 +31,11 @@ import java.util.logging.Logger;
 
 import javax.enterprise.inject.Typed;
 
+import org.apache.deltaspike.core.api.projectstage.ProjectStage;
 import org.apache.deltaspike.core.spi.config.ConfigSource;
 import org.apache.deltaspike.core.spi.config.ConfigSourceProvider;
 import org.apache.deltaspike.core.util.ClassUtils;
+import org.apache.deltaspike.core.util.ProjectStageProducer;
 import org.apache.deltaspike.core.util.ServiceUtils;
 
 /**
@@ -56,6 +58,8 @@ public final class ConfigResolver
     private static Map<ClassLoader, ConfigSource[]> configSources
         = new ConcurrentHashMap<ClassLoader, ConfigSource[]>();
 
+    private static volatile ProjectStage projectStage = null;
+
     private ConfigResolver()
     {
         // this is a utility class which doesn't get instantiated.
@@ -147,6 +151,35 @@ public final class ConfigResolver
     }
 
     /**
+     * <p>Search for the configured value in all {@link ConfigSource}s and take the
+     * current {@link org.apache.deltaspike.core.api.projectstage.ProjectStage}
+     * into account.</p>
+     *
+     * <p>It first will search if there is a configured value of the given key prefixed
+     * with the current ProjectStage (e.g. 'myproject.myconfig.Production') and if this didn't
+     * find anything it will lookup the given key without any prefix.</p>
+     *
+     * <p><b>Attention</b> This method must only be used after all ConfigSources
+     * got registered and it also must not be used to determine the ProjectStage itself.</p>
+     * @param key
+     * @param defaultValue
+     * @return the configured value or if non found the defaultValue
+     *
+     */
+    public static String getProjectStageAwarePropertyValue(String key, String defaultValue)
+    {
+        ProjectStage ps = getProjectStage();
+
+        String value = getPropertyValue(key + '.' + ps, defaultValue);
+        if (value == null)
+        {
+            value = getPropertyValue(key, defaultValue);
+        }
+
+        return value;
+    }
+
+    /**
      * Resolve all values for the given key, from all registered ConfigSources ordered by
their
      * ordinal value in ascending ways. If more {@link ConfigSource}s have the same ordinal,
their
      * order is undefined.
@@ -264,4 +297,17 @@ public final class ConfigResolver
         return configSources;
     }
 
+    private static ProjectStage getProjectStage()
+    {
+        if (projectStage == null)
+        {
+            synchronized (ConfigResolver.class)
+            {
+                projectStage = ProjectStageProducer.getInstance().getProjectStage();
+            }
+        }
+
+        return projectStage;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d7929a40/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ProjectStageProducer.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ProjectStageProducer.java
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ProjectStageProducer.java
index ba4ac6b..f2e39bf 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ProjectStageProducer.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/ProjectStageProducer.java
@@ -47,6 +47,7 @@ import java.util.logging.Logger;
  * }
  * </pre>
  *
+ * <p>Please note that there can only be one ProjectStage per EAR.</p>
  */
 @ApplicationScoped
 public class ProjectStageProducer implements Serializable

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d7929a40/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java
b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java
index 809ccc1..70c00e8 100644
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java
@@ -19,6 +19,8 @@
 package org.apache.deltaspike.test.api.config;
 
 import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.api.projectstage.ProjectStage;
+import org.apache.deltaspike.core.util.ProjectStageProducer;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -50,4 +52,12 @@ public class ConfigResolverTest
         Assert.assertNull(ConfigResolver.getPropertyValue("notexisting"));
         Assert.assertEquals("testvalue", ConfigResolver.getPropertyValue("testkey"));
     }
+
+    @Test
+    public void testGetProjectStageAwarePropertyValue()
+    {
+        ProjectStageProducer.setProjectStage(ProjectStage.UnitTest);
+        Assert.assertNull(ConfigResolver.getProjectStageAwarePropertyValue("notexisting",
null));
+        Assert.assertEquals("unittestvalue", ConfigResolver.getProjectStageAwarePropertyValue("testkey",
null));
+    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d7929a40/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
index 581c837..ed9dc86 100644
--- a/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
+++ b/deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/TestConfigSource.java
@@ -33,6 +33,15 @@ public class TestConfigSource implements ConfigSource
 
     private int ordinal = 700;
 
+    private Map<String, String> props = new HashMap<String, String>();
+
+
+    public TestConfigSource()
+    {
+        props.put("testkey", "testvalue");
+        props.put("testkey.UnitTest", "unittestvalue");
+    }
+
     @Override
     public String getConfigName()
     {
@@ -48,15 +57,13 @@ public class TestConfigSource implements ConfigSource
     @Override
     public String getPropertyValue(String key)
     {
-        return "testkey".equals(key) ? "testvalue" : null;
+        return props.get(key);
     }
 
     @Override
     public Map<String, String> getProperties()
     {
-        Map<String, String> map = new HashMap<String, String>();
-        map.put("testkey", "testvalue");
-        return map;
+        return props;
     }
 
 	@Override


Mime
View raw message