geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1818954 - in /geronimo/components/config/branches/ConfigJSR/impl: ./ src/main/java/org/apache/geronimo/config/ src/main/java/org/apache/geronimo/config/cdi/ src/main/java/org/apache/geronimo/config/converters/ src/main/resources/META-INF/s...
Date Thu, 21 Dec 2017 17:01:39 GMT
Author: struberg
Date: Thu Dec 21 17:01:39 2017
New Revision: 1818954

URL: http://svn.apache.org/viewvc?rev=1818954&view=rev
Log:
support Arrays with implicit converters

Added:
    geronimo/components/config/branches/ConfigJSR/impl/src/main/resources/META-INF/services/javax.config.spi.ConfigProviderResolver
      - copied unchanged from r1818953, geronimo/components/config/branches/ConfigJSR/impl/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigProviderResolver
Removed:
    geronimo/components/config/branches/ConfigJSR/impl/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigProviderResolver
Modified:
    geronimo/components/config/branches/ConfigJSR/impl/debug-suite.xml
    geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
    geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/DefaultConfigBuilder.java
    geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java
    geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java

Modified: geronimo/components/config/branches/ConfigJSR/impl/debug-suite.xml
URL: http://svn.apache.org/viewvc/geronimo/components/config/branches/ConfigJSR/impl/debug-suite.xml?rev=1818954&r1=1818953&r2=1818954&view=diff
==============================================================================
--- geronimo/components/config/branches/ConfigJSR/impl/debug-suite.xml (original)
+++ geronimo/components/config/branches/ConfigJSR/impl/debug-suite.xml Thu Dec 21 17:01:39
2017
@@ -23,7 +23,7 @@
 
     <classes>
         <!-- Issues in the spec -->
-        <class name="org.eclipse.javaconfig.tck.CDIPlainInjectionTest">
+        <class name="org.eclipse.configjsr.ArrayConverterTest">
             <methods>
                 <include name=".*"/>
             </methods>

Modified: geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java?rev=1818954&r1=1818953&r2=1818954&view=diff
==============================================================================
--- geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
(original)
+++ geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/ConfigImpl.java
Thu Dec 21 17:01:39 2017
@@ -167,10 +167,22 @@ public class ConfigImpl implements Confi
             synchronized (implicitConverters) {
                 converter = implicitConverters.get(asType);
                 if (converter == null) {
-                    // try to check whether the class is an 'implicit converter'
-                    converter = ImplicitConverter.getImplicitConverter(asType);
-                    if (converter != null) {
-                        implicitConverters.putIfAbsent(asType, converter);
+                    if (asType.isArray()) {
+                        Converter singleItemConverter = getConverter(asType.getComponentType());
+                        if (singleItemConverter == null) {
+                            return null;
+                        }
+                        else {
+                            converter = new ImplicitConverter.ImplicitArrayConverter(singleItemConverter,
asType.getComponentType());
+                            implicitConverters.putIfAbsent(asType, converter);
+                        }
+                    }
+                    else {
+                        // try to check whether the class is an 'implicit converter'
+                        converter = ImplicitConverter.getImplicitConverter(asType);
+                        if (converter != null) {
+                            implicitConverters.putIfAbsent(asType, converter);
+                        }
                     }
                 }
             }
@@ -217,6 +229,14 @@ public class ConfigImpl implements Confi
         }
     }
 
+    public void addPrioritisedConverter(DefaultConfigBuilder.PrioritisedConverter prioritisedConverter)
{
+        Converter oldConverter = converters.get(prioritisedConverter.getType());
+        if (oldConverter == null || prioritisedConverter.getPriority() >= getPriority(oldConverter))
{
+            converters.put(prioritisedConverter.getType(), prioritisedConverter.getConverter());
+        }
+    }
+
+
     private int getPriority(Converter<?> converter) {
         int priority = 100;
         Priority priorityAnnotation = converter.getClass().getAnnotation(Priority.class);
@@ -260,4 +280,5 @@ public class ConfigImpl implements Confi
 
         return getTypeOfConverter(clazz.getSuperclass());
     }
+
 }
\ No newline at end of file

Modified: geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/DefaultConfigBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/DefaultConfigBuilder.java?rev=1818954&r1=1818953&r2=1818954&view=diff
==============================================================================
--- geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/DefaultConfigBuilder.java
(original)
+++ geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/DefaultConfigBuilder.java
Thu Dec 21 17:01:39 2017
@@ -27,8 +27,12 @@ import org.apache.geronimo.config.config
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.ServiceLoader;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
 
 import javax.enterprise.inject.Typed;
 import javax.enterprise.inject.Vetoed;
@@ -42,9 +46,12 @@ import static java.util.Arrays.asList;
 @Typed
 @Vetoed
 public class DefaultConfigBuilder implements ConfigBuilder {
+
+
     protected ClassLoader forClassLoader;
     private final List<ConfigSource> sources = new ArrayList<>();
     private final List<Converter<?>> converters = new ArrayList<>();
+    private final Map<Class<?>, PrioritisedConverter> prioritisedConverters =
new HashMap<>();
     private boolean ignoreDefaultSources = true;
     private boolean ignoreDiscoveredSources = true;
     private boolean ignoreDiscoveredConverters = true;
@@ -80,6 +87,25 @@ public class DefaultConfigBuilder implem
     }
 
     @Override
+    public <T> ConfigBuilder withConverter(Class<T> type, int priority, Converter<T>
converter) {
+        PrioritisedConverter oldPrioritisedConverter = prioritisedConverters.get(type);
+        if (oldPrioritisedConverter != null) {
+            if (oldPrioritisedConverter.priority == priority) {
+                throw new IllegalStateException("Found 2 converters with the same priority
for type " + type
+                    + ". This will result in random behaviour -> aborting! Previous Converter:
"
+                    + oldPrioritisedConverter.converter.getClass() + " 2nd Converter: " +
converter.getClass());
+            }
+            if (oldPrioritisedConverter.priority > priority) {
+                return this;
+            }
+        }
+
+        prioritisedConverters.put(type, new PrioritisedConverter(type, priority, converter));
+
+        return this;
+    }
+
+    @Override
     public ConfigBuilder addDiscoveredConverters() {
         ignoreDiscoveredConverters = false;
         return this;
@@ -124,6 +150,9 @@ public class DefaultConfigBuilder implem
             config.addConverter(converter);
         }
 
+        for (PrioritisedConverter prioritisedConverter : prioritisedConverters.values())
{
+            config.addPrioritisedConverter(prioritisedConverter);
+        }
         return config;
     }
 
@@ -136,4 +165,28 @@ public class DefaultConfigBuilder implem
 
         return configSources;
     }
+
+    static class PrioritisedConverter {
+        private final Class<?> clazz;
+        private final int priority;
+        private final Converter converter;
+
+        public PrioritisedConverter(Class<?> clazz, int priority, Converter converter)
{
+            this.clazz = clazz;
+            this.priority = priority;
+            this.converter = converter;
+        }
+
+        public Class<?> getType() {
+            return clazz;
+        }
+
+        public int getPriority() {
+            return priority;
+        }
+
+        public Converter getConverter() {
+            return converter;
+        }
+    }
 }

Modified: geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java?rev=1818954&r1=1818953&r2=1818954&view=diff
==============================================================================
--- geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java
(original)
+++ geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java
Thu Dec 21 17:01:39 2017
@@ -33,10 +33,14 @@ import javax.enterprise.inject.spi.Passi
 import javax.enterprise.util.AnnotationLiteral;
 import javax.inject.Provider;
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Array;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Optional;
 import java.util.Set;
 import java.util.function.Supplier;
@@ -112,21 +116,27 @@ public class ConfigInjectionBean<T> impl
             ParameterizedType paramType = (ParameterizedType) annotated.getBaseType();
             Type rawType = paramType.getRawType();
 
+            Class clazzParam = (Class) paramType.getActualTypeArguments()[0]; //X TODO check
type again, etc
+
             // handle Provider<T>
             if (rawType instanceof Class && ((Class) rawType).isAssignableFrom(Provider.class)
&& paramType.getActualTypeArguments().length == 1) {
-                Class clazz = (Class) paramType.getActualTypeArguments()[0]; //X TODO check
type again, etc
-                return getConfigValue(key, defaultValue, clazz);
+                return getConfigValue(key, defaultValue, clazzParam);
             }
 
             // handle Optional<T>
             if (rawType instanceof Class && ((Class) rawType).isAssignableFrom(Optional.class)
&& paramType.getActualTypeArguments().length == 1) {
-                Class clazz = (Class) paramType.getActualTypeArguments()[0]; //X TODO check
type again, etc
-                return (T) getConfig().getOptionalValue(key, clazz);
+                return (T) getConfig().getOptionalValue(key, clazzParam);
             }
 
             if (rawType instanceof Class && ((Class) rawType).isAssignableFrom(Supplier.class)
&& paramType.getActualTypeArguments().length == 1) {
-                Class clazz = (Class) paramType.getActualTypeArguments()[0]; //X TODO check
type again, etc
-                return (T) new ConfigSupplier(clazz, key, defaultValue, (ConfigImpl)getConfig());
+                return (T) new ConfigSupplier(clazzParam, key, defaultValue, (ConfigImpl)getConfig());
+            }
+
+            if (Set.class.equals(rawType)) {
+                return (T) new HashSet(getList(key, clazzParam));
+            }
+            if (List.class.equals(rawType)) {
+                return (T) getList(key, clazzParam);
             }
         }
         else {
@@ -137,6 +147,20 @@ public class ConfigInjectionBean<T> impl
         throw new IllegalStateException("unhandled ConfigProperty");
     }
 
+    private ArrayList getList(String key, Class clazzParam) {
+        Class arrayType = Array.newInstance(clazzParam, 0).getClass();
+
+        Object valueArray = getConfig().getValue(key, arrayType);
+        int length = Array.getLength(valueArray);
+
+        ArrayList list = new ArrayList(length);
+        for (int i = 0; i < length; i++) {
+            list.add(Array.get(valueArray, i));
+        }
+
+        return list;
+    }
+
     private T getConfigValue(String key, String defaultValue, Class clazz) {
         if (ConfigExtension.isDefaultUnset(defaultValue)) {
             return (T) getConfig().getValue(key, clazz);

Modified: geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java?rev=1818954&r1=1818953&r2=1818954&view=diff
==============================================================================
--- geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java
(original)
+++ geronimo/components/config/branches/ConfigJSR/impl/src/main/java/org/apache/geronimo/config/converters/ImplicitConverter.java
Thu Dec 21 17:01:39 2017
@@ -17,9 +17,12 @@
 package org.apache.geronimo.config.converters;
 
 
+import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
 
 import javax.config.spi.Converter;
 
@@ -31,7 +34,6 @@ import javax.config.spi.Converter;
 public abstract class ImplicitConverter {
 
     public static Converter getImplicitConverter(Class<?> clazz) {
-
         // handle ct with String param
         Converter converter = hasConverterCt(clazz, String.class);
         if (converter == null) {
@@ -99,4 +101,67 @@ public abstract class ImplicitConverter
         }
         return null;
     }
+
+    public static class ImplicitArrayConverter<T> implements Converter<T> {
+        private final Converter converter;
+        private final Class<?> type;
+
+        public ImplicitArrayConverter(Converter converter, Class<?> type) {
+            this.converter = converter;
+            this.type = type;
+        }
+
+        @Override
+        public T convert(String valueStr) {
+            if (valueStr == null)
+            {
+                return null;
+            }
+
+            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(converter.convert(trimedVal));
+                    }
+
+                    currentValue.setLength(0);
+                }
+                else
+                {
+                    currentValue.append(c);
+                }
+            }
+
+            String trimedVal = currentValue.toString().trim();
+            if (trimedVal.length() > 0)
+            {
+                list.add(converter.convert(trimedVal));
+            }
+
+            // everything else is an Object array
+            Object array = Array.newInstance(type, list.size());
+            for (int i=0; i < list.size(); i++) {
+                Array.set(array, i, list.get(i));
+            }
+            return (T) array;
+        }
+
+    }
 }



Mime
View raw message