deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject [1/2] deltaspike git commit: DELTASPIKE-1240 add list support for DeltaSpike config
Date Mon, 24 Apr 2017 12:05:32 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 774887693 -> 135c59f05


DELTASPIKE-1240 add list support for DeltaSpike config


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

Branch: refs/heads/master
Commit: 83a93da2a5dd423fe26ef55447b51406c8a85ee3
Parents: 7748876
Author: Mark Struberg <struberg@apache.org>
Authored: Mon Apr 24 13:59:55 2017 +0200
Committer: Mark Struberg <struberg@apache.org>
Committed: Mon Apr 24 14:00:34 2017 +0200

----------------------------------------------------------------------
 .../resources/deltaspike/default-checks.xml     |  1 -
 .../core/api/config/ConfigResolver.java         | 77 +++++++++++++++++++-
 .../test/api/config/ConfigResolverTest.java     | 32 ++++++++
 .../test/api/config/TestConfigSource.java       |  5 ++
 .../src/main/asciidoc/configuration.adoc        | 19 +++++
 5 files changed, 131 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/83a93da2/deltaspike/checkstyle-rules/src/main/resources/deltaspike/default-checks.xml
----------------------------------------------------------------------
diff --git a/deltaspike/checkstyle-rules/src/main/resources/deltaspike/default-checks.xml
b/deltaspike/checkstyle-rules/src/main/resources/deltaspike/default-checks.xml
index 54484d2..93e8d8f 100644
--- a/deltaspike/checkstyle-rules/src/main/resources/deltaspike/default-checks.xml
+++ b/deltaspike/checkstyle-rules/src/main/resources/deltaspike/default-checks.xml
@@ -190,7 +190,6 @@
         <module name="MissingOverride"/>
         <module name="HideUtilityClassConstructor"/>
         <module name="EqualsAvoidNull"/>
-        <module name="ModifiedControlVariable"/>
         <module name="DeclarationOrder"/>
         <!--module name="MultipleStringLiterals"/-->
         <module name="OneStatementPerLine"/>

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/83a93da2/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 effdd4a..2963ad8 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
@@ -637,6 +637,19 @@ public final class ConfigResolver
     {
 
         /**
+         * Declare the Resolver to return a List of the given Type.
+         * When getting value it will be split on each comma (',') character.
+         * If a comma is contained in the values it must get escaped with a preceding backslash
(&quot;\,&quot;).
+         * Any backslash needs to get escaped via double-backslash (&quot;\\&quot;).
+         * Note that in property files this leads to &quot;\\\\&quot; as properties
escape themselves.
+         *
+         * @return a TypedResolver for a list of configured comma separated values
+         *
+         * @since 1.8.0
+         */
+        TypedResolver<List<T>> asList();
+
+        /**
          * Appends the resolved value of the given property to the key of this builder. This
is described in more detail
          * in {@link ConfigResolver#getPropertyAwarePropertyValue(String, String)}.
          * @param propertyName The name of the parameter property
@@ -816,6 +829,8 @@ public final class ConfigResolver
 
         private boolean strictly = false;
 
+        private boolean isList = false;
+
         private Converter<?> converter;
 
         private boolean evaluateVariables = false;
@@ -845,6 +860,15 @@ public final class ConfigResolver
 
         @Override
         @SuppressWarnings("unchecked")
+        public TypedResolver<List<T>> asList()
+        {
+            isList = true;
+
+            return (TypedResolver<List<T>>) this;
+        }
+
+        @Override
+        @SuppressWarnings("unchecked")
         public <N> TypedResolver<N> as(Class<N> clazz, Converter<N>
converter)
         {
             configEntryType = clazz;
@@ -955,7 +979,15 @@ public final class ConfigResolver
             }
 
             String valueStr = resolveStringValue();
-            T value = convert(valueStr);
+            T value;
+            if (isList)
+            {
+                value = splitAndConvertListValue(valueStr);
+            }
+            else
+            {
+                value = convert(valueStr);
+            }
 
             if (withDefault)
             {
@@ -978,6 +1010,48 @@ public final class ConfigResolver
             return value;
         }
 
+        private T splitAndConvertListValue(String valueStr)
+        {
+            List list = new ArrayList();
+            StringBuilder currentValue = new StringBuilder();
+            int length = valueStr.length();
+            for (int i = 0; i < length; i++)
+            {
+                char c = valueStr.charAt(i);
+                if (c == '\\')
+                {
+                    if (i < length - 1)
+                    {
+                        char nextC = valueStr.charAt(i + 1);
+                        currentValue.append(nextC);
+                        i++;
+                    }
+                }
+                else if (c == ',')
+                {
+                    String trimedVal = currentValue.toString().trim();
+                    if (trimedVal.length() > 0)
+                    {
+                        list.add(convert(trimedVal));
+                    }
+
+                    currentValue.setLength(0);
+                }
+                else
+                {
+                    currentValue.append(c);
+                }
+            }
+
+            String trimedVal = currentValue.toString().trim();
+            if (trimedVal.length() > 0)
+            {
+                list.add(convert(trimedVal));
+            }
+
+            return (T) list;
+        }
+
         @Override
         public String getKey()
         {
@@ -1082,7 +1156,6 @@ public final class ConfigResolver
          */
         private T convert(String value)
         {
-
             if (value == null)
             {
                 return null;

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/83a93da2/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 be24c62..9911ae0 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
@@ -29,6 +29,7 @@ import org.junit.Test;
 
 import java.util.List;
 
+@SuppressWarnings("Duplicates")
 public class ConfigResolverTest
 {
     private static final String DEFAULT_VALUE = "defaultValue";
@@ -221,6 +222,37 @@ public class ConfigResolverTest
         Assert.assertEquals(expected, projectStageAwareExampleEntry1);
     }
 
+    @Test
+    public void testConfiguredListValues_WithWhitespace() {
+        List<String> emails = ConfigResolver.resolve("test.list.value.emails").asList().getValue();
+        Assert.assertNotNull(emails);
+        Assert.assertEquals(3, emails.size());
+        Assert.assertTrue(emails.contains("test1@apache.org"));
+        Assert.assertTrue(emails.contains("test2@apache.org"));
+        Assert.assertTrue(emails.contains("test3@apache.org"));
+    }
+
+    @Test
+    public void testConfiguredListValues_WithEscaping() {
+        List<String> escapedValues = ConfigResolver.resolve("test.list.value.escaped.list").asList().getValue();
+        Assert.assertNotNull(escapedValues);
+        Assert.assertEquals(3, escapedValues.size());
+        Assert.assertTrue(escapedValues.contains("val,ue1"));
+        Assert.assertTrue(escapedValues.contains("value2"));
+        Assert.assertTrue(escapedValues.contains("val\\ue3"));
+    }
+
+    @Test
+    public void testConfiguredListValues_OtherType() {
+        List<Integer> intValues = ConfigResolver.resolve("test.list.intvalues").as(Integer.class).asList().getValue();
+        Assert.assertNotNull(intValues);
+        Assert.assertEquals(4, intValues.size());
+        Assert.assertTrue(intValues.contains(3));
+        Assert.assertTrue(intValues.contains(7));
+        Assert.assertTrue(intValues.contains(11));
+        Assert.assertTrue(intValues.contains(17));
+    }
+
     private void setTestConfigSourceValue(String key, String value)
     {
         ConfigSource[] configSources = ConfigResolver.getConfigSources();

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/83a93da2/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 ad177b0..f18f7d5 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
@@ -102,6 +102,11 @@ public class TestConfigSource implements ConfigSource
         props.put("deltaspike.test.exampleEntry-1.UnitTest", "tomato-UnitTest");
         props.put("deltaspike.test.exampleEntry-2", "default-exampleEntry-1-is-${deltaspike.test.exampleEntry-1}");
         props.put("deltaspike.test.exampleEntry-2.UnitTest", "projectStageAware-exampleEntry-1-is-${deltaspike.test.exampleEntry-1}");
+
+        // values for testing the list handling
+        props.put("test.list.value.emails", "test1@apache.org, test2@apache.org, \n  test3@apache.org");
+        props.put("test.list.value.escaped.list","val\\,ue1,value2, val\\\\ue3");
+        props.put("test.list.intvalues","3,7, 11 ,\t 17\n");
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/83a93da2/documentation/src/main/asciidoc/configuration.adoc
----------------------------------------------------------------------
diff --git a/documentation/src/main/asciidoc/configuration.adoc b/documentation/src/main/asciidoc/configuration.adoc
index adedaa3..688f944 100644
--- a/documentation/src/main/asciidoc/configuration.adoc
+++ b/documentation/src/main/asciidoc/configuration.adoc
@@ -250,6 +250,25 @@ Only after that time the configured value will get evaluate again.
 TIP: Note that the 'cache' is only held within the very TypedResolver instance.
 If you use different `TypedResolver` instances (e.g. in different classes) then you might
get different cache timeouts.
 
+==== List Values handling
+
+As of DeltaSpike-1.8.0, the `Typed Resolver` is also able to handle list values.
+Resolve a list of e.g. retry interval values via:
+
+[source,java]
+----
+List<Integer> retryIntervals
+    = ConfigResolver.resolve("myapp.retry.intervalls")
+        .as(Integer.class)
+        .asList()
+        .getValue();
+----
+
+The values get configured as comma (`','`) separated value String.
+A Comma inside a value can be escaped with a backslash (`\,`), backslashes should be escaped
with double-backslash (`\\`).
+Trailing and leading whitespaces get trimmed for each value.
+
+
 === Injection of configured values into beans using @ConfigProperty
 
 DeltaSpike provides a way to inject configured values into your code via the qualifier `@ConfigProperty`.


Mime
View raw message