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/30 15:15:17 UTC
svn commit: r1832553 - in /geronimo/components/config/trunk/impl/src:
main/java/org/apache/geronimo/config/cdi/
test/java/org/apache/geronimo/config/test/internal/
Author: rmannibucau
Date: Wed May 30 15:15:17 2018
New Revision: 1832553
URL: http://svn.apache.org/viewvc?rev=1832553&view=rev
Log:
ensure Config is injectable
Added:
geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigBean.java
- copied, changed from r1832552, geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java
geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ConfigInjectionTest.java
- copied, changed from r1832552, geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ProviderTest.java
Modified:
geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigExtension.java
Copied: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigBean.java (from r1832552, 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/ConfigBean.java?p2=geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigBean.java&p1=geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/cdi/ConfigInjectionBean.java&r1=1832552&r2=1832553&rev=1832553&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/ConfigBean.java Wed May 30 15:15:17 2018
@@ -1,12 +1,12 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
+ * 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
+ * the License. You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * 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,
@@ -16,76 +16,31 @@
*/
package org.apache.geronimo.config.cdi;
-import org.apache.geronimo.config.ConfigImpl;
-import org.apache.geronimo.config.ConfigValueImpl;
-import org.eclipse.microprofile.config.Config;
-import org.eclipse.microprofile.config.ConfigProvider;
-import org.eclipse.microprofile.config.inject.ConfigProperty;
+import static java.util.Arrays.asList;
+import static java.util.Collections.singleton;
-import javax.enterprise.context.Dependent;
-import javax.enterprise.context.spi.CreationalContext;
-import javax.enterprise.inject.spi.Annotated;
-import javax.enterprise.inject.spi.AnnotatedMember;
-import javax.enterprise.inject.spi.AnnotatedType;
-import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.BeanManager;
-import javax.enterprise.inject.spi.InjectionPoint;
-import javax.enterprise.inject.spi.PassivationCapable;
-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;
-/**
- * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
- */
-public class ConfigInjectionBean<T> implements Bean<T>, PassivationCapable {
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.PassivationCapable;
- private final static Set<Annotation> QUALIFIERS = new HashSet<>();
- static {
- QUALIFIERS.add(new ConfigPropertyLiteral());
- }
-
- private final BeanManager bm;
- private final Class rawType;
- private final Set<Type> types;
- private final String id;
-
- /**
- * only access via {@link #getConfig(}
- */
- private ConfigImpl _config;
-
- 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) {
- if (type instanceof Class) {
- return (Class) type;
- }
- else if (type instanceof ParameterizedType) {
- ParameterizedType paramType = (ParameterizedType) type;
+import org.eclipse.microprofile.config.Config;
+import org.eclipse.microprofile.config.ConfigProvider;
- return (Class) paramType.getRawType();
- }
+public class ConfigBean<T> implements Bean<T>, PassivationCapable {
- throw new UnsupportedOperationException("No idea how to handle " + type);
- }
+ private final String id = ConfigBean.class.getName();
+
+ private final Set<Annotation> qualifiers = singleton(new DefaultLiteral());
+
+ private final Set<Type> types = new HashSet<>(asList(Config.class, Object.class));
@Override
public Set<InjectionPoint> getInjectionPoints() {
@@ -94,7 +49,7 @@ public class ConfigInjectionBean<T> impl
@Override
public Class<?> getBeanClass() {
- return rawType;
+ return Config.class;
}
@Override
@@ -103,107 +58,13 @@ public class ConfigInjectionBean<T> impl
}
@Override
- public T create(CreationalContext<T> context) {
- InjectionPoint ip = (InjectionPoint)bm.getInjectableReference(new ConfigInjectionPoint(this),context);
- if (ip == null) {
- throw new IllegalStateException("Could not retrieve InjectionPoint");
- }
- Annotated annotated = ip.getAnnotated();
- ConfigProperty configProperty = annotated.getAnnotation(ConfigProperty.class);
- String key = getConfigKey(ip, configProperty);
- String defaultValue = configProperty.defaultValue();
-
- if (annotated.getBaseType() instanceof ParameterizedType) {
- 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) {
- return getConfigValue(key, defaultValue, clazzParam);
- }
-
- // handle Optional<T>
- if (rawType instanceof Class && ((Class) rawType).isAssignableFrom(Optional.class) && paramType.getActualTypeArguments().length == 1) {
- return (T) getConfig().getOptionalValue(key, clazzParam);
- }
-
- if (rawType instanceof Class && ((Class) rawType).isAssignableFrom(Supplier.class) && paramType.getActualTypeArguments().length == 1) {
- return (T) new ConfigSupplier(clazzParam, key, defaultValue, (ConfigImpl)getConfig());
- }
-
- if (Set.class.equals(rawType)) {
- return (T) new HashSet(getList(key, clazzParam, defaultValue));
- }
- if (List.class.equals(rawType)) {
- return (T) getList(key, clazzParam, defaultValue);
- }
- }
- else {
- Class clazz = (Class) annotated.getBaseType();
- return getConfigValue(key, defaultValue, clazz);
- }
-
- throw new IllegalStateException("unhandled ConfigProperty");
- }
-
- private List getList(String key, Class clazzParam, String defaultValue) {
- ConfigValueImpl configValue = getConfig()
- .access(key)
- .as(clazzParam)
- .asList()
- .evaluateVariables(true);
-
- if (!ConfigExtension.isDefaultUnset(defaultValue))
- {
- configValue.withStringDefault(defaultValue);
- }
-
- return (List) configValue.get();
- }
-
- 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.
- */
- static String getConfigKey(InjectionPoint ip, ConfigProperty configProperty) {
- String key = configProperty.name();
- if (key.length() > 0) {
- return key;
- }
- if (ip.getAnnotated() instanceof AnnotatedMember) {
- AnnotatedMember member = (AnnotatedMember) ip.getAnnotated();
- AnnotatedType declaringType = member.getDeclaringType();
- if (declaringType != null) {
- return declaringType.getJavaClass().getCanonicalName() + "." + member.getJavaMember().getName();
- }
- }
-
- throw new IllegalStateException("Could not find default name for @ConfigProperty InjectionPoint " + ip);
- }
-
- public ConfigImpl getConfig() {
- if (_config == null) {
- _config = (ConfigImpl) ConfigProvider.getConfig();
- }
- return _config;
+ public T create(final CreationalContext<T> context) {
+ return (T) ConfigProvider.getConfig();
}
@Override
- public void destroy(T instance, CreationalContext<T> context) {
-
+ public void destroy(final T instance, final CreationalContext<T> context) {
+ // no-op
}
@Override
@@ -213,12 +74,12 @@ public class ConfigInjectionBean<T> impl
@Override
public Set<Annotation> getQualifiers() {
- return QUALIFIERS;
+ return qualifiers;
}
@Override
public Class<? extends Annotation> getScope() {
- return Dependent.class;
+ return ApplicationScoped.class;
}
@Override
@@ -240,16 +101,4 @@ public class ConfigInjectionBean<T> impl
public String getId() {
return id;
}
-
- private static class ConfigPropertyLiteral extends AnnotationLiteral<ConfigProperty> implements ConfigProperty {
- @Override
- public String name() {
- return "";
- }
-
- @Override
- public String defaultValue() {
- return "";
- }
- }
}
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=1832553&r1=1832552&r2=1832553&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 May 30 15:15:17 2018
@@ -32,6 +32,7 @@ import javax.enterprise.inject.spi.Deplo
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
+import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessInjectionPoint;
import javax.inject.Provider;
@@ -71,6 +72,7 @@ public class ConfigExtension implements
private Set<Class<?>> proxies = new HashSet<>();
private List<Class<?>> validProxies;
private List<ProxyBean<?>> proxyBeans;
+ private boolean foundConfig;
public void findProxies(@Observes ProcessAnnotatedType<?> pat) {
@@ -89,6 +91,10 @@ public class ConfigExtension implements
}
}
+ void onConfig(@Observes final ProcessAnnotatedType<Config> configProcessBean) {
+ foundConfig = true;
+ }
+
public void registerConfigProducer(@Observes AfterBeanDiscovery abd, BeanManager bm) {
Set<Type> types = injectionPoints.stream()
.filter(NOT_PROVIDERS)
@@ -115,6 +121,10 @@ public class ConfigExtension implements
.collect(toList());
proxyBeans.forEach(abd::addBean);
} // else there are errors
+
+ if (!foundConfig) {
+ abd.addBean(new ConfigInjectionBean<>(bm, Config.class));
+ }
}
public void validate(@Observes AfterDeploymentValidation add) {
Copied: geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ConfigInjectionTest.java (from r1832552, geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ProviderTest.java)
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ConfigInjectionTest.java?p2=geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ConfigInjectionTest.java&p1=geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ProviderTest.java&r1=1832552&r2=1832553&rev=1832553&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ProviderTest.java (original)
+++ geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/test/internal/ConfigInjectionTest.java Wed May 30 15:15:17 2018
@@ -16,11 +16,9 @@
*/
package org.apache.geronimo.config.test.internal;
-import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
-import javax.inject.Provider;
-import org.eclipse.microprofile.config.inject.ConfigProperty;
+import org.eclipse.microprofile.config.Config;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
@@ -30,15 +28,12 @@ import org.jboss.shrinkwrap.api.spec.Web
import org.testng.Assert;
import org.testng.annotations.Test;
-public class ProviderTest extends Arquillian {
- private static final String SOME_KEY = "org.apache.geronimo.config.test.internal.somekey";
-
+public class ConfigInjectionTest extends Arquillian {
@Deployment
public static WebArchive deploy() {
- System.setProperty(SOME_KEY, "someval");
JavaArchive testJar = ShrinkWrap
.create(JavaArchive.class, "configProviderTest.jar")
- .addClasses(ProviderTest.class, SomeBean.class)
+ .addClasses(ConfigInjectionTest.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
return ShrinkWrap
@@ -46,31 +41,12 @@ public class ProviderTest extends Arquil
.addAsLibrary(testJar);
}
- private @Inject SomeBean someBean;
-
+ @Inject
+ private Config config;
@Test
public void testConfigProvider() {
- System.setProperty(SOME_KEY, "someval");
- String myconfig = someBean.getMyconfig();
- Assert.assertEquals(myconfig, "someval");
-
- System.setProperty(SOME_KEY, "otherval");
- myconfig = someBean.getMyconfig();
- Assert.assertEquals(myconfig, "otherval");
- }
-
-
- @RequestScoped
- public static class SomeBean {
-
- @Inject
- @ConfigProperty(name=SOME_KEY)
- private Provider<String> myconfig;
-
- public String getMyconfig() {
- return myconfig.get();
- }
-
+ Assert.assertNotNull(config); // is injected
+ Assert.assertNotNull(config.getValue("java.version", String.class)); // is usable
}
}