You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by rm...@apache.org on 2018/05/29 12:42:28 UTC

svn commit: r1832442 - in /geronimo/components/config/trunk/impl/src: main/java/org/apache/geronimo/config/ main/java/org/apache/geronimo/config/cdi/ test/java/org/apache/geronimo/config/test/internal/

Author: rmannibucau
Date: Tue May 29 12:42:27 2018
New Revision: 1832442

URL: http://svn.apache.org/viewvc?rev=1832442&view=rev
Log:
extracting placeholder support to benefit from it the most

Added:
    geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/Placeholders.java
Modified:
    geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
    geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigValueImpl.java
    geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigurationHandler.java
    geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ArrayTypeTest.java

Modified: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java?rev=1832442&r1=1832441&r2=1832442&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java (original)
+++ geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java Tue May 29 12:42:27 2018
@@ -102,14 +102,14 @@ public class ConfigImpl implements Confi
 
     public <T> T convert(String value, Class<T> asType) {
         if (value != null) {
-            return getConverter(asType).convert(value);
+            return getConverter(asType).convert(Placeholders.replace(this, value));
         }
         return null;
     }
 
     public <T> List<T> convertList(String rawValue, Class<T> arrayElementType) {
         MicroProfileTypedConverter<T> converter = getConverter(arrayElementType);
-        String[] parts = rawValue.split(ARRAY_SEPARATOR_REGEX);
+        String[] parts = Placeholders.replace(this, rawValue).split(ARRAY_SEPARATOR_REGEX);
         if(parts.length == 0) {
             return Collections.emptyList();
         }

Modified: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigValueImpl.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigValueImpl.java?rev=1832442&r1=1832441&r2=1832442&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigValueImpl.java (original)
+++ geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/ConfigValueImpl.java Tue May 29 12:42:27 2018
@@ -174,33 +174,7 @@ public class ConfigValueImpl<T> {
 
     private String resolveStringValue() {
         //X TODO implement lookupChain
-
-        String value = config.getValue(keyOriginal);
-        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 = config.access(varName).evaluateVariables(true).get();
-                if (variableValue != null)
-                {
-                    value = value.replace("${" + varName + "}", variableValue);
-                }
-                startVar++;
-            }
-        }
-        return value;
+        return Placeholders.replace(config, config.getValue(keyOriginal));
     }
 
     //X @Override

Added: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/Placeholders.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/Placeholders.java?rev=1832442&view=auto
==============================================================================
--- geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/Placeholders.java (added)
+++ geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/Placeholders.java Tue May 29 12:42:27 2018
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.geronimo.config;
+
+import org.eclipse.microprofile.config.Config;
+
+public final class Placeholders {
+    private Placeholders() {
+        // no-op
+    }
+
+    public static String replace(final Config config, final String originalValue) {
+        String value = originalValue;
+        // 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 = config.getOptionalValue(varName, String.class).orElse(null);
+            if (variableValue != null)
+            {
+                value = value.replace("${" + varName + "}", variableValue);
+            }
+            startVar++;
+        }
+        return value;
+    }
+}

Modified: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigurationHandler.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigurationHandler.java?rev=1832442&r1=1832441&r2=1832442&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigurationHandler.java (original)
+++ geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigurationHandler.java Tue May 29 12:42:27 2018
@@ -35,6 +35,7 @@ import java.util.stream.Collector;
 import java.util.stream.Stream;
 
 import org.apache.geronimo.config.ConfigImpl;
+import org.apache.geronimo.config.Placeholders;
 import org.eclipse.microprofile.config.Config;
 import org.eclipse.microprofile.config.ConfigProvider;
 import org.eclipse.microprofile.config.inject.ConfigProperty;
@@ -118,27 +119,35 @@ public class ConfigurationHandler implem
             }
 
             key = prefix + (annotation.name().isEmpty() ? m.getDeclaringClass().getName() + "." + m.getName() : annotation.name());
-            final boolean hasDefault = !annotation.defaultValue().equals(ConfigProperty.UNCONFIGURED_VALUE);
-            if (lookupType == long.class || lookupType == Long.class) {
-                defaultValue = hasDefault ? Long.parseLong(annotation.defaultValue()) : 0L;
-            } else if (lookupType == int.class || lookupType == Integer.class) {
-                defaultValue = hasDefault ? Integer.parseInt(annotation.defaultValue()) : 0;
-            } else if (lookupType == double.class || lookupType == Double.class) {
-                defaultValue = hasDefault ? Double.parseDouble(annotation.defaultValue()) : 0.;
-            } else if (lookupType == float.class || lookupType == Float.class) {
-                defaultValue = hasDefault ? Float.parseFloat(annotation.defaultValue()) : 0f;
-            } else if (lookupType == short.class || lookupType == Short.class) {
-                defaultValue = hasDefault ? Short.parseShort(annotation.defaultValue()) : (short) 0;
-            } else if (lookupType == char.class || lookupType == Character.class) {
-                defaultValue = hasDefault ? annotation.defaultValue().charAt(0) : (lookupType == char.class ? (char) 0 : null);
-            } else if (collectionCollector != null) {
-                defaultValue = hasDefault ? convert(annotation.defaultValue(), ConfigProvider.getConfig()) : null;
-            } else if (lookupType == String.class) {
-                defaultValue = hasDefault ? annotation.defaultValue() : null;
-            } else if (hasDefault) {
-                throw new IllegalArgumentException("Unsupported default for " + m);
+
+            final String defaultValue = annotation.defaultValue();
+            final boolean hasDefault = !defaultValue.equals(ConfigProperty.UNCONFIGURED_VALUE);
+
+            if (hasDefault) {
+                final Config config = ConfigProvider.getConfig();
+                final String finalDefaultValue = Placeholders.replace(config, defaultValue);
+                if (lookupType == long.class || lookupType == Long.class) {
+                    this.defaultValue = Long.parseLong(finalDefaultValue);
+                } else if (lookupType == int.class || lookupType == Integer.class) {
+                    this.defaultValue = Integer.parseInt(finalDefaultValue);
+                } else if (lookupType == double.class || lookupType == Double.class) {
+                    this.defaultValue = Double.parseDouble(finalDefaultValue);
+                } else if (lookupType == float.class || lookupType == Float.class) {
+                    this.defaultValue = Float.parseFloat(finalDefaultValue);
+                } else if (lookupType == short.class || lookupType == Short.class) {
+                    this.defaultValue = Short.parseShort(finalDefaultValue);
+                } else if (lookupType == char.class || lookupType == Character.class) {
+                    this.defaultValue = finalDefaultValue
+                                                  .charAt(0);
+                } else if (collectionCollector != null) {
+                    this.defaultValue = convert(finalDefaultValue, config);
+                } else if (lookupType == String.class) {
+                    this.defaultValue = finalDefaultValue;
+                } else {
+                    throw new IllegalArgumentException("Unsupported default for " + m);
+                }
             } else {
-                defaultValue = null;
+                this.defaultValue = null;
             }
         }
 

Modified: geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ArrayTypeTest.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ArrayTypeTest.java?rev=1832442&r1=1832441&r2=1832442&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ArrayTypeTest.java (original)
+++ geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ArrayTypeTest.java Tue May 29 12:42:27 2018
@@ -46,6 +46,7 @@ public class ArrayTypeTest extends Arqui
     public static WebArchive deploy() {
         System.setProperty(SOME_KEY, "1,2,3");
         System.setProperty(SOME_OTHER_KEY, "1,2\\,3");
+        System.setProperty("placeholder", "4,5,6");
         JavaArchive testJar = ShrinkWrap
                 .create(JavaArchive.class, "arrayTest.jar")
                 .addClasses(ArrayTypeTest.class, SomeBean.class)
@@ -66,6 +67,7 @@ public class ArrayTypeTest extends Arqui
         Assert.assertEquals(someBean.getIntValues(), asList(1,2,3));
         Assert.assertEquals(someBean.getIntSet(), new LinkedHashSet<>(asList(1,2,3)));
         Assert.assertEquals(someBean.getIntSetDefault(), new LinkedHashSet<>(asList(1,2,3)));
+        Assert.assertEquals(someBean.getIntSetPlaceholderDefault(), new LinkedHashSet<>(asList(4,5,6)));
     }
 
     @Test
@@ -89,10 +91,14 @@ public class ArrayTypeTest extends Arqui
         private Set<Integer> intSet;
 
         @Inject
-        @ConfigProperty(name=SOME_KEY, defaultValue = "1,2,3")
+        @ConfigProperty(name=SOME_KEY + ".missing", defaultValue = "1,2,3")
         private Set<Integer> intSetDefault;
 
         @Inject
+        @ConfigProperty(name=SOME_KEY + ".missing", defaultValue = "${placeholder}")
+        private Set<Integer> intSetPlaceholderDefault;
+
+        @Inject
         @ConfigProperty(name=SOME_KEY)
         private String stringValue;
 
@@ -100,6 +106,10 @@ public class ArrayTypeTest extends Arqui
         @ConfigProperty(name=SOME_OTHER_KEY)
         private List<String> values;
 
+        public Set<Integer> getIntSetPlaceholderDefault() {
+            return intSetPlaceholderDefault;
+        }
+
         public Set<Integer> getIntSetDefault() {
             return intSetDefault;
         }