You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jo...@apache.org on 2017/08/09 11:58:55 UTC

svn commit: r1804508 - in /geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi: ConfigExtension.java ConfigInjectionBean.java

Author: johndament
Date: Wed Aug  9 11:58:55 2017
New Revision: 1804508

URL: http://svn.apache.org/viewvc?rev=1804508&view=rev
Log:
TCK passing on OWB, OWB2, and Weld3.

Modified:
    geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java
    geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java

Modified: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java?rev=1804508&r1=1804507&r2=1804508&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java (original)
+++ geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java Wed Aug  9 11:58:55 2017
@@ -16,15 +16,10 @@
  */
 package org.apache.geronimo.config.cdi;
 
-import java.lang.reflect.Type;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
-import java.util.stream.Collectors;
+import org.apache.geronimo.config.DefaultConfigProvider;
+import org.eclipse.microprofile.config.Config;
+import org.eclipse.microprofile.config.ConfigProvider;
+import org.eclipse.microprofile.config.inject.ConfigProperty;
 
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.spi.AfterBeanDiscovery;
@@ -36,11 +31,16 @@ import javax.enterprise.inject.spi.Exten
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.inject.spi.ProcessInjectionPoint;
 import javax.inject.Provider;
-
-import org.apache.geronimo.config.DefaultConfigProvider;
-import org.eclipse.microprofile.config.Config;
-import org.eclipse.microprofile.config.ConfigProvider;
-import org.eclipse.microprofile.config.inject.ConfigProperty;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
 
 /**
  * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
@@ -48,6 +48,7 @@ import org.eclipse.microprofile.config.i
 public class ConfigExtension implements Extension {
     private Config config;
 
+    private static final Predicate<InjectionPoint> NOT_PROVIDERS = ip -> (ip.getType() instanceof Class) || (ip.getType() instanceof ParameterizedType && ((ParameterizedType)ip.getType()).getRawType() != Provider.class);
     private static final Map<Type, Type> REPLACED_TYPES = new HashMap<>();
 
     static {
@@ -70,14 +71,10 @@ public class ConfigExtension implements
 
     public void registerConfigProducer(@Observes AfterBeanDiscovery abd, BeanManager bm) {
         Set<Type> types = injectionPoints.stream()
-                .filter(ip -> ip.getType() instanceof Class)
+                .filter(NOT_PROVIDERS)
                 .map(ip -> REPLACED_TYPES.getOrDefault(ip.getType(), ip.getType()))
                 .collect(Collectors.toSet());
 
-        // Provider and Optional are ParameterizedTypes and not a Class, so we need to add them manually
-        types.add(Provider.class);
-        types.add(Optional.class);
-
         types.stream()
                 .map(type -> new ConfigInjectionBean(bm, type))
                 .forEach(abd::addBean);

Modified: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java?rev=1804508&r1=1804507&r2=1804508&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java (original)
+++ geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java Wed Aug  9 11:58:55 2017
@@ -16,8 +16,6 @@
  */
 package org.apache.geronimo.config.cdi;
 
-import java.io.IOException;
-import java.io.Serializable;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
@@ -42,7 +40,6 @@ import org.apache.geronimo.config.Config
 import org.eclipse.microprofile.config.Config;
 import org.eclipse.microprofile.config.ConfigProvider;
 import org.eclipse.microprofile.config.inject.ConfigProperty;
-import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
 
 /**
  * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
@@ -57,6 +54,7 @@ public class ConfigInjectionBean<T> impl
     private final BeanManager bm;
     private final Class rawType;
     private final Set<Type> types;
+    private final String id;
 
     /**
      * only access via {@link #getConfig(}
@@ -65,10 +63,10 @@ public class ConfigInjectionBean<T> impl
 
     public ConfigInjectionBean(BeanManager bm, Type type) {
         this.bm = bm;
-
         types = new HashSet<>();
         types.add(type);
         rawType = getRawType(type);
+        this.id = "ConfigInjectionBean_" + types;
     }
 
     private Class getRawType(Type type) {
@@ -117,7 +115,7 @@ public class ConfigInjectionBean<T> impl
             // 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 (T) new ConfigValueProvider(getConfig(), key, clazz);
+                return getConfigValue(key, defaultValue, clazz);
             }
 
             // handle Optional<T>
@@ -128,19 +126,23 @@ public class ConfigInjectionBean<T> impl
         }
         else {
             Class clazz = (Class) annotated.getBaseType();
-            if (ConfigExtension.isDefaultUnset(defaultValue)) {
-                return (T) getConfig().getValue(key, clazz);
-            }
-            else {
-                Config config = getConfig();
-                return (T) config.getOptionalValue(key, clazz)
-                        .orElse(((ConfigImpl) config).convert(defaultValue, clazz));
-            }
+            return getConfigValue(key, defaultValue, clazz);
         }
 
         throw new IllegalStateException("unhandled ConfigProperty");
     }
 
+    private T getConfigValue(String key, String defaultValue, Class clazz) {
+        if (ConfigExtension.isDefaultUnset(defaultValue)) {
+            return (T) getConfig().getValue(key, clazz);
+        }
+        else {
+            Config config = getConfig();
+            return (T) config.getOptionalValue(key, clazz)
+                    .orElse(((ConfigImpl) config).convert(defaultValue, clazz));
+        }
+    }
+
     /**
      * Get the property key to use.
      * In case the {@link ConfigProperty#name()} is empty we will try to determine the key name from the InjectionPoint.
@@ -210,12 +212,12 @@ public class ConfigInjectionBean<T> impl
 
     @Override
     public boolean isAlternative() {
-        return true;
+        return false;
     }
 
     @Override
     public String getId() {
-        return "ConfigInjectionBean_" + rawType.getName();
+        return id;
     }
 
     private static class ConfigPropertyLiteral extends AnnotationLiteral<ConfigProperty> implements ConfigProperty {
@@ -229,32 +231,4 @@ public class ConfigInjectionBean<T> impl
             return "";
         }
     }
-
-    /**
-     * A special Provider&lt;T&gt;
-     * This concrete class is needed because we need the injected Provider for the ConfigProperty
-     * to be Serializable. A Lambda would not work in this case
-     */
-    public static class ConfigValueProvider<T> implements Provider<T>, Serializable {
-        private transient Config config;
-        private final String key;
-        private final Class<T> type;
-
-        ConfigValueProvider(Config config, String key, Class<T> type) {
-            this.config = config;
-            this.key = key;
-            this.type = type;
-        }
-
-        @Override
-        public T get() {
-            return (T) config.getValue(key, type);
-        }
-
-        private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
-            in.defaultReadObject();
-            config = ConfigProviderResolver.instance().getConfig();
-        }
-
-    }
 }