geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1831984 - in /geronimo/components/config/trunk/impl/src: main/java/org/apache/geronimo/config/cdi/ConfigurationHandler.java test/java/org/apache/geronimo/config/test/internal/PoxyTest.java
Date Mon, 21 May 2018 18:50:46 GMT
Author: rmannibucau
Date: Mon May 21 18:50:46 2018
New Revision: 1831984

URL: http://svn.apache.org/viewvc?rev=1831984&view=rev
Log:
adding collection support to proxies

Modified:
    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/PoxyTest.java

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=1831984&r1=1831983&r2=1831984&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
Mon May 21 18:50:46 2018
@@ -17,17 +17,25 @@
 package org.apache.geronimo.config.cdi;
 
 import static java.util.function.Function.identity;
+import static java.util.stream.Collectors.toList;
 import static java.util.stream.Collectors.toMap;
+import static java.util.stream.Collectors.toSet;
 
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.Collection;
 import java.util.Map;
 import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collector;
 import java.util.stream.Stream;
 
+import org.apache.geronimo.config.ConfigImpl;
 import org.eclipse.microprofile.config.Config;
+import org.eclipse.microprofile.config.ConfigProvider;
 import org.eclipse.microprofile.config.inject.ConfigProperty;
 
 public class ConfigurationHandler implements InvocationHandler {
@@ -61,30 +69,63 @@ public class ConfigurationHandler implem
     private static class MethodMeta {
         private final String key;
         private final Object defaultValue;
-        private final Class type;
+        private final Class lookupType;
+        private final Class collectionConversionType;
+        private final Collector<Object, ?, ? extends Collection<Object>> collectionCollector;
+
         private final boolean optional;
 
         private MethodMeta(final Method m) {
             final ConfigProperty annotation = m.getAnnotation(ConfigProperty.class);
             optional = Optional.class == m.getReturnType();
-            type = optional ?
-                    Class.class.cast(ParameterizedType.class.cast(m.getGenericReturnType()).getActualTypeArguments()[0])
:
-                    m.getReturnType();
+            final Type type = optional ?
+                    ParameterizedType.class.cast(m.getGenericReturnType()).getActualTypeArguments()[0]
:
+                    m.getGenericReturnType();
+
+            if (Class.class.isInstance(type)) {
+                lookupType = Class.class.cast(type);
+                collectionCollector = null;
+                collectionConversionType = null;
+            } else if (ParameterizedType.class.isInstance(type)) {
+                final ParameterizedType pt = ParameterizedType.class.cast(type);
+                final Type rawType = pt.getRawType();
+                if (!Class.class.isInstance(rawType)) {
+                    throw new IllegalArgumentException("Unsupported parameterized type: "
+ type);
+                }
+
+                final Class<?> clazz = Class.class.cast(pt.getRawType());
+                if (Collection.class.isAssignableFrom(clazz)) {
+                    collectionConversionType = Class.class.cast(pt.getActualTypeArguments()[0]);
+                    lookupType = String.class;
+                    if (Set.class.isAssignableFrom(clazz)) {
+                        collectionCollector = toSet();
+                    } else {
+                        collectionCollector = toList();
+                    }
+                } else {
+                    throw new IllegalArgumentException("Unsupported parameterized type: "
+ type + ", did you want a Collection?");
+                }
+            } else {
+                throw new IllegalArgumentException("Unsupported type: " + type);
+            }
+
             key = annotation.name().isEmpty() ? m.getDeclaringClass().getName() + "." + m.getName()
: annotation.name();
             final boolean hasDefault = !annotation.defaultValue().equals(ConfigProperty.UNCONFIGURED_VALUE);
-            if (type == long.class || type == Long.class) {
+            if (lookupType == long.class || lookupType == Long.class) {
                 defaultValue = hasDefault ? Long.parseLong(annotation.defaultValue()) : 0L;
-            } else if (type == int.class || type == Integer.class) {
+            } else if (lookupType == int.class || lookupType == Integer.class) {
                 defaultValue = hasDefault ? Integer.parseInt(annotation.defaultValue()) :
0;
-            } else if (type == double.class || type == Double.class) {
+            } else if (lookupType == double.class || lookupType == Double.class) {
                 defaultValue = hasDefault ? Double.parseDouble(annotation.defaultValue())
: 0.;
-            } else if (type == float.class || type == Float.class) {
+            } else if (lookupType == float.class || lookupType == Float.class) {
                 defaultValue = hasDefault ? Float.parseFloat(annotation.defaultValue()) :
0f;
-            } else if (type == short.class || type == Short.class) {
+            } else if (lookupType == short.class || lookupType == Short.class) {
                 defaultValue = hasDefault ? Short.parseShort(annotation.defaultValue()) :
(short) 0;
-            } else if (type == char.class || type == Character.class) {
-                defaultValue = hasDefault ? annotation.defaultValue().charAt(0) : (type ==
char.class ? (char) 0 : null);
-            } else if (type == String.class) {
+            } 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);
@@ -94,11 +135,35 @@ public class ConfigurationHandler implem
         }
 
         Object read(final Config config) {
-            final Optional optionalValue = config.getOptionalValue(key, type);
+            final Optional optionalValue = config.getOptionalValue(key, lookupType);
             if (optional) {
-                return optionalValue;
+                return processOptional(optionalValue, config);
+            }
+            return processOptional(optionalValue, config).orElse(defaultValue);
+        }
+
+        private Optional processOptional(final Optional<?> optionalValue, final Config
config) {
+            if (collectionCollector != null) {
+                return optionalValue.map(String.class::cast).map(v -> convert(v, config));
+            }
+            return optionalValue;
+        }
+
+        private Collection<?> convert(final String o, final Config config) {
+            final String[] values = o.split(",");
+            return Stream.of(values)
+                    .map(v -> mapValue(v, config))
+                    .collect(collectionCollector);
+        }
+
+        private Object mapValue(final String raw, final Config config) {
+            if (String.class == collectionConversionType) {
+                return raw;
+            }
+            if (ConfigImpl.class.isInstance(config)) {
+                return ConfigImpl.class.cast(config).convert(raw, collectionConversionType);
             }
-            return optionalValue.orElse(defaultValue);
+            throw new IllegalArgumentException("Unsupported conversion if config instance
is not a ConfigImpl: " + collectionConversionType);
         }
     }
 }

Modified: geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/PoxyTest.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/PoxyTest.java?rev=1831984&r1=1831983&r2=1831984&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/PoxyTest.java
(original)
+++ geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/PoxyTest.java
Mon May 21 18:50:46 2018
@@ -16,8 +16,11 @@
  */
 package org.apache.geronimo.config.test.internal;
 
+import static java.util.Arrays.asList;
 import static org.testng.Assert.assertEquals;
 
+import java.util.Collection;
+
 import javax.inject.Inject;
 import javax.inject.Provider;
 
@@ -32,11 +35,13 @@ import org.testng.Assert;
 import org.testng.annotations.Test;
 
 public class PoxyTest extends Arquillian {
+    private static final String LIST_KEY = SomeProxy.class.getName() + ".list";
     private static final String SOME_KEY = SomeProxy.class.getName() + ".key";
     private static final String SOME_OTHER_KEY = SomeProxy.class.getName() + ".key2";
 
     @Deployment
     public static WebArchive deploy() {
+        System.setProperty(LIST_KEY, "a,b,1");
         System.setProperty(SOME_KEY, "yeah");
         System.setProperty(SOME_OTHER_KEY, "123");
         JavaArchive testJar = ShrinkWrap
@@ -58,6 +63,8 @@ public class PoxyTest extends Arquillian
         assertEquals(proxy.renamed(), "yeah");
         assertEquals(proxy.key2(), 123);
         assertEquals(proxy.key3(), "def");
+        assertEquals(proxy.list(), asList("a", "b", "1"));
+        assertEquals(proxy.listDefaults(), asList(1, 2, 1));
     }
 
     public interface SomeProxy {
@@ -72,5 +79,11 @@ public class PoxyTest extends Arquillian
 
         @ConfigProperty(name = "org.apache.geronimo.config.test.internal.PoxyTest$SomeProxy.key")
         String renamed();
+
+        @ConfigProperty
+        Collection<String> list();
+
+        @ConfigProperty(defaultValue = "1,2,1")
+        Collection<Integer> listDefaults();
     }
 }



Mime
View raw message