deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject deltaspike git commit: DELTASPIKE-1126 ConfigResolver variable evaluation now on by default
Date Mon, 18 Apr 2016 06:56:22 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 9c8c62d2c -> 4b09512b8


DELTASPIKE-1126 ConfigResolver variable evaluation now on by default


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

Branch: refs/heads/master
Commit: 4b09512b83850f078390e170921d6f4be826fbf2
Parents: 9c8c62d
Author: Mark Struberg <struberg@apache.org>
Authored: Mon Apr 18 08:26:21 2016 +0200
Committer: Mark Struberg <struberg@apache.org>
Committed: Mon Apr 18 08:53:40 2016 +0200

----------------------------------------------------------------------
 .../core/api/config/ConfigResolver.java         | 72 ++++++++++++--------
 .../test/api/config/ConfigResolverTest.java     | 21 ++++--
 .../src/main/asciidoc/configuration.adoc        | 16 +++++
 3 files changed, 77 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4b09512b/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 f2ad42c..7ef2c52 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
@@ -168,38 +168,14 @@ public final class ConfigResolver
      */
     public static String getPropertyValue(String key, String defaultValue)
     {
-        String value = getPropertyValue(key);
-
-        return fallbackToDefaultIfEmpty(key, value, defaultValue);
+        return getPropertyValue(key, defaultValue, true);
     }
 
     public static String getPropertyValue(String key, String defaultValue, boolean evaluateVariables)
     {
-        String value = getPropertyValue(key, defaultValue);
-        if (value != null && evaluateVariables)
-        {
-            int startVar = 0;
-            while ((startVar = value.indexOf("${", startVar)) >= 0)
-            {
-                int endVar = value.indexOf("}", startVar);
-                if (endVar <= 0)
-                {
-                    break;
-                }
-                String variable = value.substring(startVar + 2, endVar);
-                if (variable.isEmpty())
-                {
-                    break;
-                }
-                String variableValue = getPropertyValue(variable, null, true);
-                if (variableValue != null)
-                {
-                    value = value.replace("${" + variable + "}", variableValue);
-                }
-                startVar++;
-            }
-        }
-        return value;
+        String value = getPropertyValue(key, evaluateVariables);
+
+        return fallbackToDefaultIfEmpty(key, value, defaultValue);
     }
 
     /**
@@ -212,6 +188,20 @@ public final class ConfigResolver
      */
     public static String getPropertyValue(String key)
     {
+        return getPropertyValue(key, true);
+    }
+
+    /**
+     * Resolves the value configured for the given key.
+     *
+     * @param key the property key
+     * @param evaluateVariables whether to evaluate any '${variablename}' variable expressions
+     *
+     * @return the configured property value from the {@link ConfigSource} with the highest
ordinal or null if there is
+     *         no configured value for it
+     */
+    public static String getPropertyValue(String key, boolean evaluateVariables)
+    {
         ConfigSource[] appConfigSources = getConfigSources();
 
         String value;
@@ -223,6 +213,32 @@ public final class ConfigResolver
             {
                 LOG.log(Level.FINE, "found value {0} for key {1} in ConfigSource {2}.",
                         new Object[]{filterConfigValueForLog(key, value), key, configSource.getConfigName()});
+
+                if (evaluateVariables)
+                {
+                    // recursively resolve any ${varName} in the value
+                    int startVar = 0;
+                    while ((startVar = value.indexOf("${", startVar)) >= 0)
+                    {
+                        int endVar = value.indexOf("}", startVar);
+                        if (endVar <= 0)
+                        {
+                            break;
+                        }
+                        String varName = value.substring(startVar + 2, endVar);
+                        if (varName.isEmpty())
+                        {
+                            break;
+                        }
+                        String variableValue = getPropertyValue(varName, true);
+                        if (variableValue != null)
+                        {
+                            value = value.replace("${" + varName + "}", variableValue);
+                        }
+                        startVar++;
+                    }
+                }
+
                 return filterConfigValue(key, value);
             }
 

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4b09512b/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 50f996a..b2745f7 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
@@ -138,15 +138,28 @@ public class ConfigResolverTest
     @Test
     public void testConfigVariableReplacement()
     {
-        String url = ConfigResolver.getPropertyValue("deltaspike.test.someapp.soap.endpoint",
"", true);
-        Assert.assertEquals("http://localhost:12345/someservice/myendpoint", url);
+        {
+            String url = ConfigResolver.getPropertyValue("deltaspike.test.someapp.soap.endpoint",
"", true);
+            Assert.assertEquals("http://localhost:12345/someservice/myendpoint", url);
+        }
+
+        {
+            String url = ConfigResolver.getPropertyValue("deltaspike.test.someapp.soap.endpoint",
true);
+            Assert.assertEquals("http://localhost:12345/someservice/myendpoint", url);
+        }
     }
 
     @Test
     public void testConfigVariableNotExisting()
     {
-        String url = ConfigResolver.getPropertyValue("deltaspike.test.nonexisting.variable",
"", true);
-        Assert.assertEquals("${does.not.exist}/someservice/myendpoint", url);
+        {
+            String url = ConfigResolver.getPropertyValue("deltaspike.test.nonexisting.variable",
"", true);
+            Assert.assertEquals("${does.not.exist}/someservice/myendpoint", url);
+        }
+        {
+            String url = ConfigResolver.getPropertyValue("deltaspike.test.nonexisting.variable",
true);
+            Assert.assertEquals("${does.not.exist}/someservice/myendpoint", url);
+        }
 
     }
     @Test

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4b09512b/documentation/src/main/asciidoc/configuration.adoc
----------------------------------------------------------------------
diff --git a/documentation/src/main/asciidoc/configuration.adoc b/documentation/src/main/asciidoc/configuration.adoc
index 4b7f345..d59b85d 100644
--- a/documentation/src/main/asciidoc/configuration.adoc
+++ b/documentation/src/main/asciidoc/configuration.adoc
@@ -142,6 +142,22 @@ This makes the overall calculation a bit slower, but allows for values
 to change dynamically if someone likes to for example implement a
 `JmxConfigSource` (not yet part of DeltaSpike, but easily implementable).
 
+=== Variable Replacement in Configured Values
+
+Since version 1.6.1, DeltaSpike also supports using 'variables' inside configured values.
+You can e.g. define a single configuration key for your server and use it in other configuration
values
+-----------------------------------------------------------------
+document.server.url=http://localhost:8081
+myapp.document.lists=${document.server.url}/docapp/list
+myapp.document.admin=${document.server.url}/docadmin/app
+-----------------------------------------------------------------
+
+A variable name starts with `${` and ends with `}`.
+
+Variable support is enabled by default.
+If you like to use the `ConfigResolver` without variable support you need to use the methods
with the `evaluateVariables` parameter set to `false`.
+
+
 === TypedResolver API
 
 Very often the configured values represent more than just strings -- number types and booleans
are commonly used as


Mime
View raw message