TCK passing on OWB, OWB2, and Weld3.
git-svn-id: https://svn.apache.org/repos/asf/geronimo/components/config/trunk@1804508 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java b/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java
index 04604ef..ac8bfb5 100644
--- a/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java
+++ b/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java
@@ -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.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 @@
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 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);
diff --git a/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java b/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java
index ec8fda8..dde1eca 100644
--- a/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java
+++ b/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java
@@ -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.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 @@
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 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 @@
// 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 @@
}
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 @@
@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 @@
return "";
}
}
-
- /**
- * A special Provider<T>
- * 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();
- }
-
- }
}