You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ro...@apache.org on 2019/01/23 21:00:44 UTC
svn commit: r1851982 - in /aries/trunk/cdi:
cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/
cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/
cdi-extender/src/main/java/org/apache/aries/cdi/container/inte...
Author: rotty3000
Date: Wed Jan 23 21:00:44 2019
New Revision: 1851982
URL: http://svn.apache.org/viewvc?rev=1851982&view=rev
Log:
[CDI] initial support for servlet annotations to http extension
Added:
aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Annotates.java
- copied, changed from r1851981, aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Types.java
Removed:
aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Types.java
Modified:
aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/Activator.java
aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/DiscoveryExtension.java
aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/RuntimeExtension.java
aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Maps.java
aries/trunk/cdi/cdi-extension-http/pom.xml
aries/trunk/cdi/cdi-extension-http/src/main/java/org/apache/aries/cdi/extension/http/HttpExtension.java
aries/trunk/cdi/cdi-itests/pom.xml
aries/trunk/cdi/cdi-itests/src/main/java/org/apache/aries/cdi/test/tb6/FooServlet.java
Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/Activator.java
URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/Activator.java?rev=1851982&r1=1851981&r2=1851982&view=diff
==============================================================================
--- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/Activator.java (original)
+++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/Activator.java Wed Jan 23 21:00:44 2019
@@ -14,8 +14,9 @@
package org.apache.aries.cdi.container.internal;
-import static org.osgi.namespace.extender.ExtenderNamespace.*;
-import static org.osgi.service.cdi.CDIConstants.*;
+import static org.osgi.namespace.extender.ExtenderNamespace.EXTENDER_NAMESPACE;
+import static org.osgi.service.cdi.CDIConstants.CDI_CAPABILITY_NAME;
+import static org.osgi.service.cdi.CDIConstants.REQUIREMENT_BEANS_ATTRIBUTE;
import java.util.Dictionary;
import java.util.Hashtable;
@@ -200,8 +201,8 @@ public class Activator extends AbstractE
@Override
protected void debug(Bundle bundle, String msg) {
- if (_log.isDebugEnabled()) {
- _log.debug(msg, bundle);
+ if (_log.isTraceEnabled()) {
+ _log.trace(msg, bundle);
}
}
Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/DiscoveryExtension.java
URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/DiscoveryExtension.java?rev=1851982&r1=1851981&r2=1851982&view=diff
==============================================================================
--- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/DiscoveryExtension.java (original)
+++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/DiscoveryExtension.java Wed Jan 23 21:00:44 2019
@@ -15,17 +15,14 @@
package org.apache.aries.cdi.container.internal.container;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Executable;
-import java.lang.reflect.Parameter;
-import java.util.AbstractMap.SimpleEntry;
+import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
-import java.util.Map.Entry;
+import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.stream.Collectors;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
@@ -34,7 +31,6 @@ import javax.enterprise.inject.spi.After
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedField;
import javax.enterprise.inject.spi.AnnotatedParameter;
-import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.DefinitionException;
@@ -43,11 +39,8 @@ import javax.enterprise.inject.spi.Injec
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessInjectionPoint;
-import javax.enterprise.inject.spi.ProcessManagedBean;
import javax.enterprise.inject.spi.ProcessProducerField;
import javax.enterprise.inject.spi.ProcessProducerMethod;
-import javax.enterprise.inject.spi.ProcessSessionBean;
-import javax.enterprise.inject.spi.ProcessSyntheticBean;
import org.apache.aries.cdi.container.internal.model.BeansModel;
import org.apache.aries.cdi.container.internal.model.ComponentPropertiesModel;
@@ -57,8 +50,7 @@ import org.apache.aries.cdi.container.in
import org.apache.aries.cdi.container.internal.model.OSGiBean;
import org.apache.aries.cdi.container.internal.model.ReferenceModel;
import org.apache.aries.cdi.container.internal.model.ReferenceModel.Builder;
-import org.apache.aries.cdi.container.internal.util.Maps;
-import org.apache.aries.cdi.container.internal.util.Types;
+import org.apache.aries.cdi.container.internal.util.Annotates;
import org.osgi.service.cdi.ComponentType;
import org.osgi.service.cdi.ConfigurationPolicy;
import org.osgi.service.cdi.MaximumCardinality;
@@ -68,7 +60,6 @@ import org.osgi.service.cdi.annotations.
import org.osgi.service.cdi.annotations.FactoryComponent;
import org.osgi.service.cdi.annotations.PID;
import org.osgi.service.cdi.annotations.Reference;
-import org.osgi.service.cdi.annotations.ServiceInstance;
import org.osgi.service.cdi.annotations.SingleComponent;
import org.osgi.service.cdi.reference.BindBeanServiceObjects;
import org.osgi.service.cdi.reference.BindService;
@@ -83,69 +74,95 @@ public class DiscoveryExtension implemen
_containerTemplate = _containerState.containerDTO().template.components.get(0);
}
- static Entry<Class<?>, Annotated> getBeanClassAndAnnotated(ProcessBean<?> pb) {
- Annotated annotated = null;
- Class<?> annotatedClass = null;
+ <X> void processAnnotatedType(@Observes ProcessAnnotatedType<X> pat) {
+ Class<X> declaringClass = Annotates.declaringClass(pat.getAnnotatedType());
- if (pb instanceof ProcessManagedBean) {
- ProcessManagedBean<?> bean = (ProcessManagedBean<?>)pb;
+ final String className = declaringClass.getName();
- annotated = bean.getAnnotated();
- annotatedClass = bean.getAnnotatedBeanClass().getJavaClass();
- }
- else if (pb instanceof ProcessSessionBean) {
- ProcessSessionBean<?> bean = (ProcessSessionBean<?>)pb;
+ OSGiBean osgiBean = _beansModel.getOSGiBean(className);
- annotated = bean.getAnnotated();
- annotatedClass = bean.getAnnotatedBeanClass().getJavaClass();
+ if (osgiBean == null) {
+ return;
}
- else if (pb instanceof ProcessProducerMethod) {
- ProcessProducerMethod<?, ?> producer = (ProcessProducerMethod<?, ?>)pb;
- annotated = producer.getAnnotated();
- annotatedClass = producer.getAnnotatedProducerMethod().getDeclaringType().getJavaClass();
- }
- else if (pb instanceof ProcessProducerField) {
- ProcessProducerField<?, ?> producer = (ProcessProducerField<?, ?>)pb;
+ osgiBean.found(true);
+ }
- annotated = producer.getAnnotated();
- annotatedClass = producer.getAnnotatedProducerField().getDeclaringType().getJavaClass();
- }
- else if (pb instanceof ProcessSyntheticBean) {
- ProcessSyntheticBean<?> synthetic = (ProcessSyntheticBean<?>)pb;
+ <X> void processBindObject(@Observes ProcessInjectionPoint<X, BindService<?>> pip) {
+ processInjectionPoint0(pip, true);
+ }
- annotated = synthetic.getAnnotated();
- annotatedClass = synthetic.getBean().getBeanClass();
- }
- else {
- annotated = pb.getAnnotated();
- annotatedClass = pb.getBean().getBeanClass();
- }
+ <X> void processBindServiceObjects(@Observes ProcessInjectionPoint<X, BindBeanServiceObjects<?>> pip) {
+ processInjectionPoint0(pip, true);
+ }
- return new SimpleEntry<>(annotatedClass, annotated);
+ <X> void processBindServiceReference(@Observes ProcessInjectionPoint<X, BindServiceReference<?>> pip) {
+ processInjectionPoint0(pip, true);
}
- static Class<?> getDeclaringClass(InjectionPoint injectionPoint) {
+ <X, T> void processInjectionPoint(@Observes ProcessInjectionPoint<X, T> pip) {
+ processInjectionPoint0(pip, false);
+ }
+
+ <X, T> void processInjectionPoint0(ProcessInjectionPoint<X, T> pip, boolean special) {
+ InjectionPoint injectionPoint = pip.getInjectionPoint();
+
Annotated annotated = injectionPoint.getAnnotated();
- Class<?> declaringClass = null;
+ Class<X> declaringClass = Annotates.declaringClass(annotated);
- if (annotated instanceof AnnotatedParameter) {
- AnnotatedParameter<?> ap = (AnnotatedParameter<?>)annotated;
+ String className = declaringClass.getName();
- Parameter javaParameter = ap.getJavaParameter();
+ OSGiBean osgiBean = _beansModel.getOSGiBean(className);
- Executable executable = javaParameter.getDeclaringExecutable();
+ if (osgiBean == null) {
+ return;
+ }
- declaringClass = executable.getDeclaringClass();
+ if (special) {
+ doSpecial(osgiBean, annotated, injectionPoint.getType());
}
else {
- AnnotatedField<?> af = (AnnotatedField<?>)annotated;
+ doOther(osgiBean, declaringClass, annotated, injectionPoint);
+ }
+ }
+
+ <X> void processBean(@Observes ProcessBean<X> pb) {
+ final Class<X> declaringClass = Annotates.declaringClass(pb);
- declaringClass = af.getDeclaringType().getJavaClass();
+ String className = declaringClass.getName();
+
+ OSGiBean osgiBean = _beansModel.getOSGiBean(className);
+
+ if (osgiBean == null) {
+ return;
}
- return declaringClass;
+ osgiBean.found(true);
+
+ final Annotated annotated = pb.getAnnotated();
+
+ try {
+ List<String> serviceTypes = Annotates.serviceClassNames(annotated);
+ Map<String, Object> componentProperties = Annotates.componentProperties(annotated);
+ ServiceScope serviceScope = Annotates.serviceScope(annotated);
+
+ if (annotated.isAnnotationPresent(SingleComponent.class)) {
+ doSingleComponent(osgiBean, declaringClass, annotated, pb.getBean(), serviceTypes, serviceScope, componentProperties);
+ }
+ else if (annotated.isAnnotationPresent(FactoryComponent.class)) {
+ doFactoryComponent(osgiBean, declaringClass, annotated, pb.getBean(), serviceTypes, serviceScope, componentProperties);
+ }
+ else if (annotated.isAnnotationPresent(ComponentScoped.class)) {
+ // Explicitly ignore this case
+ }
+ else {
+ doContainerBean(osgiBean, declaringClass, annotated, pb, pb.getBean().getScope(), serviceTypes, serviceScope, componentProperties);
+ }
+ }
+ catch (Exception e) {
+ pb.addDefinitionError(e);
+ }
}
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) {
@@ -173,245 +190,148 @@ public class DiscoveryExtension implemen
);
}
- <X> void processAnnotatedType(@Observes ProcessAnnotatedType<X> pat, BeanManager beanManager) {
- final AnnotatedType<X> at = pat.getAnnotatedType();
-
- Class<X> annotatedClass = at.getJavaClass();
-
- final String className = annotatedClass.getName();
-
- OSGiBean osgiBean = _beansModel.getOSGiBean(className);
+ void doComponentProperties(OSGiBean osgiBean, Class<?> declaringClass, InjectionPoint injectionPoint) {
+ try {
+ ComponentPropertiesModel configurationModel = new ComponentPropertiesModel.Builder(
+ injectionPoint.getType()
+ ).declaringClass(
+ declaringClass
+ ).injectionPoint(
+ injectionPoint
+ ).build();
- if (osgiBean == null) {
- return;
+ osgiBean.addConfiguration(_containerState, configurationModel.toDTO());
+ }
+ catch (Exception e) {
+ _containerState.error(e);
}
-
- osgiBean.found(true);
}
- @SuppressWarnings("rawtypes")
- void processBean(@Observes ProcessBean<?> pb) {
- Entry<Class<?>, Annotated> beanClassAndAnnotated = getBeanClassAndAnnotated(pb);
-
- final Class<?> annotatedClass = beanClassAndAnnotated.getKey();
-
- String className = annotatedClass.getName();
-
- OSGiBean osgiBean = _beansModel.getOSGiBean(className);
+ void doContainerBean(OSGiBean osgiBean, Class<?> declaringClass, Annotated annotated, ProcessBean<?> pb, Class<? extends Annotation> scope, List<String> serviceTypeNames, ServiceScope serviceScope, Map<String, Object> componentProperties) {
+ String className = declaringClass.getName();
- if (osgiBean == null) {
- return;
+ if (!_containerTemplate.beans.contains(className)) {
+ _containerTemplate.beans.add(className);
}
- osgiBean.found(true);
-
- final Annotated annotated = beanClassAndAnnotated.getValue();
-
- try {
- List<Class<?>> serviceTypes = Types.collectServiceTypes(annotated);
-
- if ((annotated instanceof AnnotatedType) &&
- Optional.ofNullable(
- annotated.getAnnotation(SingleComponent.class)).isPresent()) {
-
- ExtendedComponentTemplateDTO componentTemplate = new ExtendedComponentTemplateDTO();
- componentTemplate.activations = new CopyOnWriteArrayList<>();
-
- ExtendedActivationTemplateDTO activationTemplate = new ExtendedActivationTemplateDTO();
- activationTemplate.declaringClass = annotatedClass;
- activationTemplate.properties = Collections.emptyMap();
- activationTemplate.scope = getScope(annotated);
- activationTemplate.serviceClasses = serviceTypes.stream().map(
- st -> st.getName()
- ).collect(Collectors.toList());
-
- componentTemplate.activations.add(activationTemplate);
-
- componentTemplate.bean = pb.getBean();
- componentTemplate.beans = new CopyOnWriteArrayList<>();
- componentTemplate.configurations = new CopyOnWriteArrayList<>();
- componentTemplate.name = pb.getBean().getName();
- componentTemplate.properties = Maps.componentProperties(annotated);
- componentTemplate.references = new CopyOnWriteArrayList<>();
- componentTemplate.type = ComponentType.SINGLE;
-
- annotated.getAnnotations(PID.class).stream().forEach(
- PID -> {
- ExtendedConfigurationTemplateDTO configurationTemplate = new ExtendedConfigurationTemplateDTO();
-
- configurationTemplate.declaringClass = annotatedClass;
- configurationTemplate.maximumCardinality = MaximumCardinality.ONE;
- configurationTemplate.pid = Optional.of(PID.value()).map(
- s -> {
- if (s.equals("$") || s.equals("")) {
- return componentTemplate.name;
- }
- return s;
- }
- ).orElse(componentTemplate.name);
-
- if (PID.value().equals("$") || PID.value().equals("")) {
- configurationTemplate.pid = componentTemplate.name;
- }
- else {
- configurationTemplate.pid = PID.value();
- }
-
- configurationTemplate.policy = PID.policy();
-
- componentTemplate.configurations.add(configurationTemplate);
- }
- );
+ if (!serviceTypeNames.isEmpty()) {
+ if (!scope.equals(ApplicationScoped.class) &&
+ !scope.equals(Dependent.class)) {
- if (componentTemplate.configurations.isEmpty()) {
- ExtendedConfigurationTemplateDTO configurationTemplate = new ExtendedConfigurationTemplateDTO();
+ pb.addDefinitionError(
+ new IllegalStateException(
+ String.format(
+ "@Service can only be used on @ApplicationScoped, @Dependent, @SingleComponent, and @FactoryComponent: %s",
+ pb.getBean())));
+ return;
+ }
- configurationTemplate.declaringClass = annotatedClass;
- configurationTemplate.maximumCardinality = MaximumCardinality.ONE;
- configurationTemplate.pid = componentTemplate.name;
- configurationTemplate.policy = ConfigurationPolicy.OPTIONAL;
+ ExtendedActivationTemplateDTO activationTemplate = new ExtendedActivationTemplateDTO();
+ activationTemplate.cdiScope = scope;
+ activationTemplate.declaringClass = declaringClass;
+ if (pb instanceof ProcessProducerField) {
+ activationTemplate.producer = ((ProcessProducerField<?, ?>) pb).getAnnotatedProducerField();
+ }
+ else if (pb instanceof ProcessProducerMethod) {
+ activationTemplate.producer = ((ProcessProducerMethod<?, ?>) pb).getAnnotatedProducerMethod();
+ }
+ activationTemplate.properties = componentProperties;
+ activationTemplate.scope = serviceScope;
+ activationTemplate.serviceClasses = serviceTypeNames;
- componentTemplate.configurations.add(configurationTemplate);
- }
+ _containerTemplate.activations.add(activationTemplate);
+ }
- componentTemplate.beans.add(className);
+ osgiBean.setComponent(_containerState, _containerTemplate);
+ }
- _containerState.containerDTO().template.components.add(componentTemplate);
+ void doFactoryComponent(OSGiBean osgiBean, Class<?> declaringClass, Annotated annotated, Bean<?> bean, List<String> serviceTypeNames, ServiceScope serviceScope, Map<String, Object> componentProperties) {
+ ExtendedComponentTemplateDTO componentTemplate = new ExtendedComponentTemplateDTO();
+ componentTemplate.activations = new CopyOnWriteArrayList<>();
- osgiBean.setComponent(_containerState, componentTemplate);
- }
- else if ((annotated instanceof AnnotatedType) &&
- Optional.ofNullable(
- annotated.getAnnotation(FactoryComponent.class)).isPresent()) {
-
- ExtendedComponentTemplateDTO componentTemplate = new ExtendedComponentTemplateDTO();
- componentTemplate.activations = new CopyOnWriteArrayList<>();
-
- ExtendedActivationTemplateDTO activationTemplate = new ExtendedActivationTemplateDTO();
- activationTemplate.declaringClass = annotatedClass;
- activationTemplate.properties = Collections.emptyMap();
- activationTemplate.scope = getScope(annotated);
- activationTemplate.serviceClasses = serviceTypes.stream().map(
- st -> st.getName()
- ).collect(Collectors.toList());
-
- componentTemplate.activations.add(activationTemplate);
-
- componentTemplate.bean = pb.getBean();
- componentTemplate.beans = new CopyOnWriteArrayList<>();
- componentTemplate.configurations = new CopyOnWriteArrayList<>();
- componentTemplate.name = pb.getBean().getName();
- componentTemplate.properties = Maps.componentProperties(annotated);
- componentTemplate.references = new CopyOnWriteArrayList<>();
- componentTemplate.type = ComponentType.FACTORY;
-
- annotated.getAnnotations(PID.class).stream().forEach(
- PID -> {
- ExtendedConfigurationTemplateDTO configurationTemplate = new ExtendedConfigurationTemplateDTO();
-
- configurationTemplate.declaringClass = annotatedClass;
- configurationTemplate.maximumCardinality = MaximumCardinality.ONE;
- configurationTemplate.pid = Optional.of(PID.value()).map(
- s -> {
- if (s.equals("$") || s.equals("")) {
- return componentTemplate.name;
- }
- return s;
- }
- ).orElse(componentTemplate.name);
+ ExtendedActivationTemplateDTO activationTemplate = new ExtendedActivationTemplateDTO();
+ activationTemplate.declaringClass = declaringClass;
+ activationTemplate.properties = Collections.emptyMap();
+ activationTemplate.scope = serviceScope;
+ activationTemplate.serviceClasses = serviceTypeNames;
- configurationTemplate.policy = PID.policy();
+ componentTemplate.activations.add(activationTemplate);
- componentTemplate.configurations.add(configurationTemplate);
- }
- );
+ componentTemplate.bean = bean;
+ componentTemplate.beans = new CopyOnWriteArrayList<>();
+ componentTemplate.configurations = new CopyOnWriteArrayList<>();
+ componentTemplate.name = bean.getName();
+ componentTemplate.properties = componentProperties;
+ componentTemplate.references = new CopyOnWriteArrayList<>();
+ componentTemplate.type = ComponentType.FACTORY;
+ annotated.getAnnotations(PID.class).stream().forEach(
+ PID -> {
ExtendedConfigurationTemplateDTO configurationTemplate = new ExtendedConfigurationTemplateDTO();
- configurationTemplate.declaringClass = annotatedClass;
- configurationTemplate.maximumCardinality = MaximumCardinality.MANY;
- configurationTemplate.pid = Optional.ofNullable(
- annotated.getAnnotation(FactoryComponent.class)
- ).map(fc -> {
- if (fc.value().equals("$") || fc.value().equals("")) {
- return componentTemplate.name;
+ configurationTemplate.declaringClass = declaringClass;
+ configurationTemplate.maximumCardinality = MaximumCardinality.ONE;
+ configurationTemplate.pid = Optional.of(PID.value()).map(
+ s -> {
+ if (s.equals("$") || s.equals("")) {
+ return componentTemplate.name;
+ }
+ return s;
}
- return fc.value();
- }).orElse(componentTemplate.name);
- configurationTemplate.policy = ConfigurationPolicy.REQUIRED;
+ ).orElse(componentTemplate.name);
+
+ configurationTemplate.policy = PID.policy();
componentTemplate.configurations.add(configurationTemplate);
- componentTemplate.beans.add(className);
+ }
+ );
- _containerState.containerDTO().template.components.add(componentTemplate);
+ ExtendedConfigurationTemplateDTO configurationTemplate = new ExtendedConfigurationTemplateDTO();
- osgiBean.setComponent(_containerState, componentTemplate);
+ configurationTemplate.declaringClass = declaringClass;
+ configurationTemplate.maximumCardinality = MaximumCardinality.MANY;
+ configurationTemplate.pid = Optional.ofNullable(
+ annotated.getAnnotation(FactoryComponent.class)
+ ).map(fc -> {
+ if (fc.value().equals("$") || fc.value().equals("")) {
+ return componentTemplate.name;
}
- else if ((annotated instanceof AnnotatedType) &&
- Optional.ofNullable(
- annotated.getAnnotation(ComponentScoped.class)).isPresent()) {
+ return fc.value();
+ }).orElse(componentTemplate.name);
+ configurationTemplate.policy = ConfigurationPolicy.REQUIRED;
- // Explicitly ignore this case
- }
- else {
- if (!_containerTemplate.beans.contains(className)) {
- _containerTemplate.beans.add(className);
- }
+ componentTemplate.configurations.add(configurationTemplate);
+ componentTemplate.beans.add(declaringClass.getName());
- if (!serviceTypes.isEmpty()) {
- Class<? extends Annotation> scope = pb.getBean().getScope();
- if (!scope.equals(ApplicationScoped.class) &&
- !scope.equals(Dependent.class)) {
-
- pb.addDefinitionError(
- new IllegalStateException(
- String.format(
- "@Service can only be used on @ApplicationScoped, @Dependent, @SingleComponent, and @FactoryComponent: %s",
- pb.getBean())));
- return;
- }
+ _containerState.containerDTO().template.components.add(componentTemplate);
- ExtendedActivationTemplateDTO activationTemplate = new ExtendedActivationTemplateDTO();
- activationTemplate.cdiScope = scope;
- activationTemplate.declaringClass = annotatedClass;
- if (pb instanceof ProcessProducerField) {
- activationTemplate.producer = ((ProcessProducerField) pb).getAnnotatedProducerField();
- }
- else if (pb instanceof ProcessProducerMethod) {
- activationTemplate.producer = ((ProcessProducerMethod) pb).getAnnotatedProducerMethod();
- }
- activationTemplate.properties = Maps.componentProperties(annotated);
- activationTemplate.scope = getScope(annotated);
- activationTemplate.serviceClasses = serviceTypes.stream().map(
- st -> st.getName()
- ).collect(Collectors.toList());
+ osgiBean.setComponent(_containerState, componentTemplate);
+ }
- _containerTemplate.activations.add(activationTemplate);
- }
+ void doOther(OSGiBean osgiBean, Class<?> declaringClass, Annotated annotated, InjectionPoint injectionPoint) {
+ Reference reference = annotated.getAnnotation(Reference.class);
+ ComponentProperties componentProperties = annotated.getAnnotation(ComponentProperties.class);
- osgiBean.setComponent(_containerState, _containerTemplate);
- }
+ if (reference != null) {
+ doReference(osgiBean, annotated, injectionPoint, reference, componentProperties);
}
- catch (Exception e) {
- pb.addDefinitionError(e);
+ else if (componentProperties != null) {
+ doComponentProperties(osgiBean, declaringClass, injectionPoint);
}
}
- void processBindObject(@Observes ProcessInjectionPoint<?, BindService<?>> pip) {
- InjectionPoint injectionPoint = pip.getInjectionPoint();
-
- Class<?> declaringClass = getDeclaringClass(injectionPoint);
-
- String className = declaringClass.getName();
-
- OSGiBean osgiBean = _beansModel.getOSGiBean(className);
+ void doReference(OSGiBean osgiBean, Annotated annotated, InjectionPoint injectionPoint, Reference reference, ComponentProperties componentProperties) {
+ if (componentProperties != null) {
+ _containerState.error(
+ new IllegalArgumentException(
+ String.format(
+ "Cannot use @Reference and @Configuration on the same injection point {}",
+ injectionPoint))
+ );
- if (osgiBean == null) {
return;
}
- Annotated annotated = injectionPoint.getAnnotated();
-
Builder builder = null;
if (annotated instanceof AnnotatedParameter) {
@@ -431,55 +351,73 @@ public class DiscoveryExtension implemen
}
}
- void processBindServiceObjects(@Observes ProcessInjectionPoint<?, BindBeanServiceObjects<?>> pip) {
- InjectionPoint injectionPoint = pip.getInjectionPoint();
+ void doSingleComponent(OSGiBean osgiBean, Class<?> declaringClass, Annotated annotated, Bean<?> bean, List<String> serviceTypes, ServiceScope serviceScope, Map<String, Object> componentProperties) {
+ ExtendedComponentTemplateDTO componentTemplate = new ExtendedComponentTemplateDTO();
+ componentTemplate.activations = new CopyOnWriteArrayList<>();
+
+ ExtendedActivationTemplateDTO activationTemplate = new ExtendedActivationTemplateDTO();
+ activationTemplate.declaringClass = declaringClass;
+ activationTemplate.properties = Collections.emptyMap();
+ activationTemplate.scope = serviceScope;
+ activationTemplate.serviceClasses = serviceTypes;
+
+ componentTemplate.activations.add(activationTemplate);
+
+ componentTemplate.bean = bean;
+ componentTemplate.beans = new CopyOnWriteArrayList<>();
+ componentTemplate.configurations = new CopyOnWriteArrayList<>();
+ componentTemplate.name = bean.getName();
+ componentTemplate.properties = componentProperties;
+ componentTemplate.references = new CopyOnWriteArrayList<>();
+ componentTemplate.type = ComponentType.SINGLE;
- Class<?> declaringClass = getDeclaringClass(injectionPoint);
-
- String className = declaringClass.getName();
+ annotated.getAnnotations(PID.class).stream().forEach(
+ PID -> {
+ ExtendedConfigurationTemplateDTO configurationTemplate = new ExtendedConfigurationTemplateDTO();
- OSGiBean osgiBean = _beansModel.getOSGiBean(className);
+ configurationTemplate.declaringClass = declaringClass;
+ configurationTemplate.maximumCardinality = MaximumCardinality.ONE;
+ configurationTemplate.pid = Optional.of(PID.value()).map(
+ s -> {
+ if (s.equals("$") || s.equals("")) {
+ return componentTemplate.name;
+ }
+ return s;
+ }
+ ).orElse(componentTemplate.name);
- if (osgiBean == null) {
- return;
- }
+ if (PID.value().equals("$") || PID.value().equals("")) {
+ configurationTemplate.pid = componentTemplate.name;
+ }
+ else {
+ configurationTemplate.pid = PID.value();
+ }
- Annotated annotated = injectionPoint.getAnnotated();
+ configurationTemplate.policy = PID.policy();
- Builder builder = null;
+ componentTemplate.configurations.add(configurationTemplate);
+ }
+ );
- if (annotated instanceof AnnotatedParameter) {
- builder = new ReferenceModel.Builder((AnnotatedParameter<?>)annotated);
- }
- else {
- builder = new ReferenceModel.Builder((AnnotatedField<?>)annotated);
- }
+ if (componentTemplate.configurations.isEmpty()) {
+ ExtendedConfigurationTemplateDTO configurationTemplate = new ExtendedConfigurationTemplateDTO();
- try {
- ReferenceModel referenceModel = builder.type(injectionPoint.getType()).build();
+ configurationTemplate.declaringClass = declaringClass;
+ configurationTemplate.maximumCardinality = MaximumCardinality.ONE;
+ configurationTemplate.pid = componentTemplate.name;
+ configurationTemplate.policy = ConfigurationPolicy.OPTIONAL;
- osgiBean.addReference(referenceModel.toDTO());
- }
- catch (Exception e) {
- _containerState.error(e);
+ componentTemplate.configurations.add(configurationTemplate);
}
- }
-
- void processBindServiceReference(@Observes ProcessInjectionPoint<?, BindServiceReference<?>> pip) {
- InjectionPoint injectionPoint = pip.getInjectionPoint();
- Class<?> declaringClass = getDeclaringClass(injectionPoint);
+ componentTemplate.beans.add(declaringClass.getName());
- String className = declaringClass.getName();
+ _containerState.containerDTO().template.components.add(componentTemplate);
- OSGiBean osgiBean = _beansModel.getOSGiBean(className);
-
- if (osgiBean == null) {
- return;
- }
-
- Annotated annotated = injectionPoint.getAnnotated();
+ osgiBean.setComponent(_containerState, componentTemplate);
+ }
+ void doSpecial(OSGiBean osgiBean, Annotated annotated, Type injectionPointType) {
Builder builder = null;
if (annotated instanceof AnnotatedParameter) {
@@ -490,7 +428,7 @@ public class DiscoveryExtension implemen
}
try {
- ReferenceModel referenceModel = builder.type(injectionPoint.getType()).build();
+ ReferenceModel referenceModel = builder.type(injectionPointType).build();
osgiBean.addReference(referenceModel.toDTO());
}
@@ -499,81 +437,6 @@ public class DiscoveryExtension implemen
}
}
- void processInjectionPoint(@Observes ProcessInjectionPoint<?, ?> pip) {
- InjectionPoint injectionPoint = pip.getInjectionPoint();
-
- Class<?> declaringClass = getDeclaringClass(injectionPoint);
-
- String className = declaringClass.getName();
-
- OSGiBean osgiBean = _beansModel.getOSGiBean(className);
-
- if (osgiBean == null) {
- return;
- }
-
- Annotated annotated = injectionPoint.getAnnotated();
- Reference reference = annotated.getAnnotation(Reference.class);
- ComponentProperties componentProperties = annotated.getAnnotation(ComponentProperties.class);
-
- if (reference != null) {
- if (componentProperties != null) {
- _containerState.error(
- new IllegalArgumentException(
- String.format(
- "Cannot use @Reference and @Configuration on the same injection point {}",
- injectionPoint))
- );
-
- return;
- }
-
- Builder builder = null;
-
- if (annotated instanceof AnnotatedParameter) {
- builder = new ReferenceModel.Builder((AnnotatedParameter<?>)annotated);
- }
- else {
- builder = new ReferenceModel.Builder((AnnotatedField<?>)annotated);
- }
-
- try {
- ReferenceModel referenceModel = builder.type(injectionPoint.getType()).build();
-
- osgiBean.addReference(referenceModel.toDTO());
- }
- catch (Exception e) {
- _containerState.error(e);
- }
- }
- else if (componentProperties != null) {
- try {
- ComponentPropertiesModel configurationModel = new ComponentPropertiesModel.Builder(
- injectionPoint.getType()
- ).declaringClass(
- declaringClass
- ).injectionPoint(
- injectionPoint
- ).build();
-
- osgiBean.addConfiguration(_containerState, configurationModel.toDTO());
- }
- catch (Exception e) {
- _containerState.error(e);
- }
- }
- }
-
- ServiceScope getScope(Annotated annotated) {
- ServiceInstance serviceInstance = annotated.getAnnotation(ServiceInstance.class);
-
- if (serviceInstance != null) {
- return serviceInstance.value();
- }
-
- return ServiceScope.SINGLETON;
- }
-
void scanComponentBean(
ExtendedComponentTemplateDTO template,
Bean<?> bean,
@@ -606,8 +469,8 @@ public class DiscoveryExtension implemen
}
for (InjectionPoint injectionPoint : bean.getInjectionPoints()) {
- if ((injectionPoint.getAnnotated().getAnnotation(ComponentProperties.class) != null) ||
- (injectionPoint.getAnnotated().getAnnotation(Reference.class) != null)) {
+ if (injectionPoint.getAnnotated().isAnnotationPresent(ComponentProperties.class) ||
+ injectionPoint.getAnnotated().isAnnotationPresent(Reference.class)) {
continue;
}
Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/RuntimeExtension.java
URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/RuntimeExtension.java?rev=1851982&r1=1851981&r2=1851982&view=diff
==============================================================================
--- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/RuntimeExtension.java (original)
+++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/RuntimeExtension.java Wed Jan 23 21:00:44 2019
@@ -14,11 +14,12 @@
package org.apache.aries.cdi.container.internal.container;
-import static javax.interceptor.Interceptor.Priority.*;
+import static javax.interceptor.Interceptor.Priority.PLATFORM_AFTER;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
+import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -40,7 +41,10 @@ import javax.enterprise.inject.spi.Befor
import javax.enterprise.inject.spi.BeforeShutdown;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessInjectionPoint;
+import javax.enterprise.inject.spi.ProcessProducerField;
+import javax.enterprise.inject.spi.ProcessProducerMethod;
import javax.enterprise.inject.spi.Producer;
import javax.enterprise.inject.spi.ProducerFactory;
@@ -60,6 +64,7 @@ import org.apache.aries.cdi.container.in
import org.apache.aries.cdi.container.internal.model.OSGiBean;
import org.apache.aries.cdi.container.internal.model.ReferenceModel;
import org.apache.aries.cdi.container.internal.model.SingleComponent;
+import org.apache.aries.cdi.container.internal.util.Annotates;
import org.apache.aries.cdi.container.internal.util.SRs;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
@@ -70,12 +75,12 @@ import org.osgi.service.cdi.CDIConstants
import org.osgi.service.cdi.ComponentType;
import org.osgi.service.cdi.ServiceScope;
import org.osgi.service.cdi.annotations.ComponentProperties;
-import org.osgi.service.cdi.annotations.ComponentScoped;
import org.osgi.service.cdi.annotations.Reference;
import org.osgi.service.cdi.reference.BindBeanServiceObjects;
import org.osgi.service.cdi.reference.BindService;
import org.osgi.service.cdi.reference.BindServiceReference;
import org.osgi.service.cdi.runtime.dto.ComponentDTO;
+import org.osgi.service.cdi.runtime.dto.template.ActivationTemplateDTO;
import org.osgi.service.cdi.runtime.dto.template.ComponentTemplateDTO;
import org.osgi.service.cdi.runtime.dto.template.ConfigurationTemplateDTO;
import org.osgi.service.log.Logger;
@@ -97,6 +102,7 @@ public class RuntimeExtension implements
_configurationBuilder = configurationBuilder;
_singleBuilder = singleBuilder;
_factoryBuilder = factoryBuilder;
+ _containerTemplate = _containerState.containerDTO().template.components.get(0);
}
void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) {
@@ -108,11 +114,87 @@ public class RuntimeExtension implements
bbd.addQualifier(org.osgi.service.cdi.annotations.Reluctant.class);
bbd.addQualifier(org.osgi.service.cdi.annotations.Service.class);
bbd.addQualifier(org.osgi.service.cdi.annotations.ServiceInstance.class);
- bbd.addScope(ComponentScoped.class, false, false);
+ bbd.addScope(org.osgi.service.cdi.annotations.ComponentScoped.class, false, false);
bbd.addStereotype(org.osgi.service.cdi.annotations.FactoryComponent.class);
bbd.addStereotype(org.osgi.service.cdi.annotations.SingleComponent.class);
}
+ void processBindObject(@Observes ProcessInjectionPoint<?, BindService<?>> pip) {
+ processInjectionPoint0(pip, true);
+ }
+
+ void processBindServiceObjects(@Observes ProcessInjectionPoint<?, BindBeanServiceObjects<?>> pip) {
+ processInjectionPoint0(pip, true);
+ }
+
+ void processBindServiceReference(@Observes ProcessInjectionPoint<?, BindServiceReference<?>> pip) {
+ processInjectionPoint0(pip, true);
+ }
+
+ void processInjectionPoint(@Observes ProcessInjectionPoint<?, ?> pip) {
+ processInjectionPoint0(pip, false);
+ }
+
+ <X> void processBean(@Observes ProcessBean<X> pb) {
+ final Class<X> declaringClass = Annotates.declaringClass(pb);
+
+ String className = declaringClass.getName();
+
+ OSGiBean osgiBean = _containerState.beansModel().getOSGiBean(className);
+
+ if (osgiBean == null) {
+ return;
+ }
+
+ final Annotated annotated = pb.getAnnotated();
+
+ try {
+ List<String> serviceTypes = Annotates.serviceClassNames(annotated);
+ Map<String, Object> componentProperties = Annotates.componentProperties(annotated);
+ ServiceScope serviceScope = Annotates.serviceScope(annotated);
+
+ if (annotated.isAnnotationPresent(org.osgi.service.cdi.annotations.SingleComponent.class) ||
+ annotated.isAnnotationPresent(org.osgi.service.cdi.annotations.FactoryComponent.class)) {
+
+ ActivationTemplateDTO activationTemplate = osgiBean.getComponent().activations.get(0);
+ activationTemplate.scope = serviceScope;
+ activationTemplate.serviceClasses = serviceTypes;
+ osgiBean.getComponent().properties = componentProperties;
+ }
+ else if (annotated.isAnnotationPresent(org.osgi.service.cdi.annotations.ComponentScoped.class)) {
+ // Explicitly ignore this case
+ }
+ else if (!serviceTypes.isEmpty()) {
+ ExtendedActivationTemplateDTO activationTemplate = _containerTemplate.activations.stream().map(
+ ExtendedActivationTemplateDTO.class::cast
+ ).filter(
+ at -> at.declaringClass.equals(declaringClass)
+ ).findFirst().orElseGet(
+ () -> {
+ ExtendedActivationTemplateDTO at = new ExtendedActivationTemplateDTO();
+ at.cdiScope = pb.getBean().getScope();
+ at.declaringClass = declaringClass;
+ if (pb instanceof ProcessProducerField) {
+ at.producer = ((ProcessProducerField<?, ?>) pb).getAnnotatedProducerField();
+ }
+ else if (pb instanceof ProcessProducerMethod) {
+ at.producer = ((ProcessProducerMethod<?, ?>) pb).getAnnotatedProducerMethod();
+ }
+ _containerTemplate.activations.add(at);
+ return at;
+ }
+ );
+
+ activationTemplate.properties = componentProperties;
+ activationTemplate.scope = serviceScope;
+ activationTemplate.serviceClasses = serviceTypes;
+ }
+ }
+ catch (Exception e) {
+ pb.addDefinitionError(e);
+ }
+ }
+
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) {
abd.addContext(_containerState.componentContext());
@@ -181,48 +263,6 @@ public class RuntimeExtension implements
);
}
- void processBindObject(@Observes ProcessInjectionPoint<?, BindService<?>> pip) {
- processInjectionPoint(pip, true);
- }
-
- void processBindServiceObjects(@Observes ProcessInjectionPoint<?, BindBeanServiceObjects<?>> pip) {
- processInjectionPoint(pip, true);
- }
-
- void processBindServiceReference(@Observes ProcessInjectionPoint<?, BindServiceReference<?>> pip) {
- processInjectionPoint(pip, true);
- }
-
- void processInjectionPoint(@Observes ProcessInjectionPoint<?, ?> pip) {
- processInjectionPoint(pip, false);
- }
-
- void processInjectionPoint(ProcessInjectionPoint<?, ?> pip, boolean special) {
- InjectionPoint injectionPoint = pip.getInjectionPoint();
-
- Class<?> declaringClass = DiscoveryExtension.getDeclaringClass(injectionPoint);
-
- String className = declaringClass.getName();
-
- OSGiBean osgiBean = _containerState.beansModel().getOSGiBean(className);
-
- if (osgiBean == null) {
- return;
- }
-
- Annotated annotated = injectionPoint.getAnnotated();
- ComponentProperties componentProperties = annotated.getAnnotation(ComponentProperties.class);
- Reference reference = annotated.getAnnotation(Reference.class);
-
- if (((reference != null) || special) && matchReference(osgiBean, pip)) {
- return;
- }
-
- if (componentProperties != null) {
- matchConfiguration(osgiBean, pip);
- }
- }
-
private void addBeans(ComponentTemplateDTO componentTemplate, AfterBeanDiscovery abd, BeanManager bm) {
ComponentDTO componentDTO = _containerState.containerDTO().components.get(0);
@@ -315,7 +355,7 @@ public class RuntimeExtension implements
private boolean matchConfiguration(OSGiBean osgiBean, ProcessInjectionPoint<?, ?> pip) {
InjectionPoint injectionPoint = pip.getInjectionPoint();
- Class<?> declaringClass = DiscoveryExtension.getDeclaringClass(injectionPoint);
+ Class<?> declaringClass = Annotates.declaringClass(injectionPoint.getAnnotated());
ConfigurationTemplateDTO current = new ComponentPropertiesModel.Builder(injectionPoint.getType()).declaringClass(
declaringClass
@@ -381,6 +421,32 @@ public class RuntimeExtension implements
).orElse(false);
}
+ private void processInjectionPoint0(ProcessInjectionPoint<?, ?> pip, boolean special) {
+ InjectionPoint injectionPoint = pip.getInjectionPoint();
+
+ Class<?> declaringClass = Annotates.declaringClass(injectionPoint.getAnnotated());
+
+ String className = declaringClass.getName();
+
+ OSGiBean osgiBean = _containerState.beansModel().getOSGiBean(className);
+
+ if (osgiBean == null) {
+ return;
+ }
+
+ Annotated annotated = injectionPoint.getAnnotated();
+ ComponentProperties componentProperties = annotated.getAnnotation(ComponentProperties.class);
+ Reference reference = annotated.getAnnotation(Reference.class);
+
+ if (((reference != null) || special) && matchReference(osgiBean, pip)) {
+ return;
+ }
+
+ if (componentProperties != null) {
+ matchConfiguration(osgiBean, pip);
+ }
+ }
+
@SuppressWarnings({ "rawtypes", "unchecked" })
private void registerService(
ExtendedComponentInstanceDTO componentInstance,
@@ -480,6 +546,7 @@ public class RuntimeExtension implements
private final ConfigurationListener.Builder _configurationBuilder;
private final List<ConfigurationListener> _configurationListeners = new CopyOnWriteArrayList<>();
private final ContainerState _containerState;
+ private final ComponentTemplateDTO _containerTemplate;
private final FactoryComponent.Builder _factoryBuilder;
private final Logger _log;
private final List<ServiceRegistration<?>> _registrations = new CopyOnWriteArrayList<>();
Copied: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Annotates.java (from r1851981, aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Types.java)
URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Annotates.java?p2=aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Annotates.java&p1=aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Types.java&r1=1851981&r2=1851982&rev=1851982&view=diff
==============================================================================
--- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Types.java (original)
+++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Annotates.java Wed Jan 23 21:00:44 2019
@@ -14,26 +14,107 @@
package org.apache.aries.cdi.container.internal.util;
+import java.lang.reflect.Executable;
+import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.Map;
import java.util.Optional;
+import java.util.stream.Collectors;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedField;
+import javax.enterprise.inject.spi.AnnotatedMember;
import javax.enterprise.inject.spi.AnnotatedMethod;
+import javax.enterprise.inject.spi.AnnotatedParameter;
import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.ProcessBean;
+import javax.enterprise.inject.spi.ProcessManagedBean;
+import javax.enterprise.inject.spi.ProcessProducerField;
+import javax.enterprise.inject.spi.ProcessProducerMethod;
+import javax.enterprise.inject.spi.ProcessSessionBean;
+import javax.enterprise.inject.spi.ProcessSyntheticBean;
+import org.jboss.weld.util.Types;
+import org.osgi.service.cdi.ServiceScope;
import org.osgi.service.cdi.annotations.Service;
+import org.osgi.service.cdi.annotations.ServiceInstance;
-public class Types {
+public class Annotates {
- private Types() {
+ private Annotates() {
// no instances
}
- public static List<Class<?>> collectServiceTypes(Annotated annotated) {
+ public static Map<String, Object> componentProperties(Annotated annotated) {
+ return Maps.merge(annotated.getAnnotations());
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <X> Class<X> declaringClass(Object instance) {
+ Class<?> declaringClass = null;
+
+ if (instance instanceof AnnotatedMember) {
+ AnnotatedMember<?> af = (AnnotatedMember<?>)instance;
+
+ declaringClass = af.getDeclaringType().getJavaClass();
+ }
+ else if (instance instanceof AnnotatedParameter) {
+ AnnotatedParameter<?> ap = (AnnotatedParameter<?>)instance;
+
+ Parameter javaParameter = ap.getJavaParameter();
+
+ Executable executable = javaParameter.getDeclaringExecutable();
+
+ declaringClass = executable.getDeclaringClass();
+ }
+ else if (instance instanceof AnnotatedType) {
+ AnnotatedType<?> annotatedType = (AnnotatedType<?>)instance;
+
+ declaringClass = annotatedType.getJavaClass();
+ }
+ else if (instance instanceof Annotated) {
+ Annotated annotated = (Annotated)instance;
+
+ declaringClass = Types.getRawTypes(new Type[] {annotated.getBaseType()})[0];
+ }
+ else if (instance instanceof ProcessManagedBean) {
+ ProcessManagedBean<?> bean = (ProcessManagedBean<?>)instance;
+
+ declaringClass = bean.getAnnotatedBeanClass().getJavaClass();
+ }
+ else if (instance instanceof ProcessSessionBean) {
+ ProcessSessionBean<?> bean = (ProcessSessionBean<?>)instance;
+
+ declaringClass = bean.getAnnotatedBeanClass().getJavaClass();
+ }
+ else if (instance instanceof ProcessProducerMethod) {
+ ProcessProducerMethod<?, ?> producer = (ProcessProducerMethod<?, ?>)instance;
+
+ declaringClass = producer.getAnnotatedProducerMethod().getDeclaringType().getJavaClass();
+ }
+ else if (instance instanceof ProcessProducerField) {
+ ProcessProducerField<?, ?> producer = (ProcessProducerField<?, ?>)instance;
+
+ declaringClass = producer.getAnnotatedProducerField().getDeclaringType().getJavaClass();
+ }
+ else if (instance instanceof ProcessSyntheticBean) {
+ ProcessSyntheticBean<?> synthetic = (ProcessSyntheticBean<?>)instance;
+
+ declaringClass = synthetic.getBean().getBeanClass();
+ }
+ else if (instance instanceof ProcessBean) {
+ ProcessBean<?> processBean = (ProcessBean<?>)instance;
+
+ declaringClass = processBean.getBean().getBeanClass();
+ }
+
+ return (Class<X>)declaringClass;
+ }
+
+ public static List<Class<?>> serviceClasses(Annotated annotated) {
List<Class<?>> serviceTypes = new ArrayList<>();
List<java.lang.reflect.AnnotatedType> ats = new ArrayList<>();
@@ -132,4 +213,20 @@ public class Types {
return serviceTypes;
}
+ public static List<String> serviceClassNames(Annotated annotated) {
+ return serviceClasses(annotated).stream().map(
+ st -> st.getName()
+ ).sorted().collect(Collectors.toList());
+ }
+
+ public static ServiceScope serviceScope(Annotated annotated) {
+ ServiceInstance serviceInstance = annotated.getAnnotation(ServiceInstance.class);
+
+ if (serviceInstance != null) {
+ return serviceInstance.value();
+ }
+
+ return ServiceScope.SINGLETON;
+ }
+
}
\ No newline at end of file
Modified: aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Maps.java
URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Maps.java?rev=1851982&r1=1851981&r2=1851982&view=diff
==============================================================================
--- aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Maps.java (original)
+++ aries/trunk/cdi/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/util/Maps.java Wed Jan 23 21:00:44 2019
@@ -16,7 +16,6 @@ package org.apache.aries.cdi.container.i
import java.lang.annotation.Annotation;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
@@ -29,13 +28,6 @@ import java.util.Map.Entry;
import java.util.Objects;
import java.util.stream.Collectors;
-import javax.enterprise.inject.spi.Annotated;
-import javax.enterprise.inject.spi.AnnotatedConstructor;
-import javax.enterprise.inject.spi.AnnotatedField;
-import javax.enterprise.inject.spi.AnnotatedMethod;
-import javax.enterprise.inject.spi.AnnotatedParameter;
-import javax.enterprise.inject.spi.AnnotatedType;
-
import org.osgi.service.cdi.annotations.BeanPropertyType;
import org.osgi.util.converter.TypeReference;
@@ -107,26 +99,7 @@ public class Maps {
return map;
}
- public static Map<String, Object> componentProperties(Annotated annotated) {
- if (annotated instanceof AnnotatedType) {
- return merge(Arrays.asList(((AnnotatedType<?>)annotated).getJavaClass().getAnnotations()));
- }
- else if (annotated instanceof AnnotatedParameter) {
- return merge(Arrays.asList(((AnnotatedParameter<?>)annotated).getJavaParameter().getAnnotations()));
- }
- else if (annotated instanceof AnnotatedField) {
- return merge(Arrays.asList(((AnnotatedField<?>)annotated).getJavaMember().getAnnotations()));
- }
- else if (annotated instanceof AnnotatedConstructor) {
- return merge(Arrays.asList(((AnnotatedConstructor<?>)annotated).getJavaMember().getAnnotations()));
- }
- else if (annotated instanceof AnnotatedMethod) {
- return merge(Arrays.asList(((AnnotatedMethod<?>)annotated).getJavaMember().getAnnotations()));
- }
- return merge(new ArrayList<>(annotated.getAnnotations()));
- }
-
- public static Map<String, Object> merge(List<Annotation> annotations) {
+ public static Map<String, Object> merge(Collection<Annotation> annotations) {
return annotations.stream().filter(
ann -> Objects.nonNull(ann.annotationType().getAnnotation(BeanPropertyType.class))
).map(
Modified: aries/trunk/cdi/cdi-extension-http/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extension-http/pom.xml?rev=1851982&r1=1851981&r2=1851982&view=diff
==============================================================================
--- aries/trunk/cdi/cdi-extension-http/pom.xml (original)
+++ aries/trunk/cdi/cdi-extension-http/pom.xml Wed Jan 23 21:00:44 2019
@@ -44,6 +44,11 @@
<dependencies>
<dependency>
+ <groupId>org.apache.aries.cdi</groupId>
+ <artifactId>org.apache.aries.cdi.extra</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.http.servlet-api</artifactId>
</dependency>
Modified: aries/trunk/cdi/cdi-extension-http/src/main/java/org/apache/aries/cdi/extension/http/HttpExtension.java
URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-extension-http/src/main/java/org/apache/aries/cdi/extension/http/HttpExtension.java?rev=1851982&r1=1851981&r2=1851982&view=diff
==============================================================================
--- aries/trunk/cdi/cdi-extension-http/src/main/java/org/apache/aries/cdi/extension/http/HttpExtension.java (original)
+++ aries/trunk/cdi/cdi-extension-http/src/main/java/org/apache/aries/cdi/extension/http/HttpExtension.java Wed Jan 23 21:00:44 2019
@@ -23,25 +23,54 @@ import static org.osgi.service.cdi.CDICo
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT;
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
+import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import javax.annotation.Priority;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
+import javax.enterprise.inject.spi.AnnotatedConstructor;
+import javax.enterprise.inject.spi.AnnotatedField;
+import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeShutdown;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionTargetFactory;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+import javax.enterprise.inject.spi.WithAnnotations;
+import javax.servlet.Filter;
+import javax.servlet.Servlet;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestListener;
+import javax.servlet.annotation.MultipartConfig;
+import javax.servlet.annotation.WebFilter;
+import javax.servlet.annotation.WebListener;
+import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSessionListener;
+import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardContextSelect;
+import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardFilterAsyncSupported;
+import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardFilterDispatcher;
+import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardFilterName;
+import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardFilterPattern;
+import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardFilterServlet;
+import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardListener;
+import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardServletAsyncSupported;
+import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardServletMultipart;
+import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardServletName;
+import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardServletPattern;
+import org.apache.aries.cdi.extra.propertytypes.ServiceDescription;
+import org.apache.aries.cdi.extra.propertytypes.ServiceRanking;
import org.jboss.weld.module.web.servlet.WeldInitialListener;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceRegistration;
@@ -49,6 +78,7 @@ import org.osgi.framework.wiring.BundleC
import org.osgi.framework.wiring.BundleRequirement;
import org.osgi.framework.wiring.BundleWire;
import org.osgi.framework.wiring.BundleWiring;
+import org.osgi.service.cdi.annotations.Service;
public class HttpExtension implements Extension {
@@ -56,7 +86,145 @@ public class HttpExtension implements Ex
_bundle = bundle;
}
- // TODO process javax.servlet.annotations annotations
+ <X> void processWebFilter(@Observes @WithAnnotations(WebFilter.class) ProcessAnnotatedType<X> pat) {
+ final AnnotatedType<X> annotatedType = pat.getAnnotatedType();
+
+ WebFilter webFilter = annotatedType.getAnnotation(WebFilter.class);
+
+ final Set<Annotation> annotations = new HashSet<>(annotatedType.getAnnotations());
+
+ if (!annotatedType.isAnnotationPresent(Service.class)) {
+ annotations.add(Service.Literal.of(new Class[] {Filter.class}));
+ }
+
+ if(!annotatedType.isAnnotationPresent(HttpWhiteboardContextSelect.class)) {
+ annotations.add(HttpWhiteboardContextSelect.Literal.of(getSelectedContext()));
+ }
+
+ if (!webFilter.description().isEmpty()) {
+ annotations.add(ServiceDescription.Literal.of(webFilter.description()));
+ }
+
+ if (!webFilter.filterName().isEmpty()) {
+ annotations.add(HttpWhiteboardFilterName.Literal.of(webFilter.filterName()));
+ }
+
+ if (webFilter.servletNames().length > 0) {
+ annotations.add(HttpWhiteboardFilterServlet.Literal.of(webFilter.servletNames()));
+ }
+
+ if (webFilter.value().length > 0) {
+ annotations.add(HttpWhiteboardFilterPattern.Literal.of(webFilter.value()));
+ }
+ else if (webFilter.urlPatterns().length > 0) {
+ annotations.add(HttpWhiteboardFilterPattern.Literal.of(webFilter.urlPatterns()));
+ }
+
+ if (webFilter.dispatcherTypes().length > 0) {
+ annotations.add(HttpWhiteboardFilterDispatcher.Literal.of(webFilter.dispatcherTypes()));
+ }
+
+ annotations.add(HttpWhiteboardFilterAsyncSupported.Literal.of(webFilter.asyncSupported()));
+
+ pat.setAnnotatedType(new WebAnnotated<>(annotatedType, annotations));
+ }
+
+ <X> void processWebListener(@Observes @WithAnnotations(WebListener.class) ProcessAnnotatedType<X> pat) {
+ final AnnotatedType<X> annotatedType = pat.getAnnotatedType();
+
+ WebListener webListener = annotatedType.getAnnotation(WebListener.class);
+
+ final Set<Annotation> annotations = new HashSet<>(annotatedType.getAnnotations());
+
+ if (!annotatedType.isAnnotationPresent(Service.class)) {
+ List<Class<?>> listenerTypes = new ArrayList<>();
+
+ Class<X> javaClass = annotatedType.getJavaClass();
+
+ if (javax.servlet.ServletContextListener.class.isAssignableFrom(javaClass)) {
+ listenerTypes.add(javax.servlet.ServletContextListener.class);
+ }
+ if (javax.servlet.ServletContextAttributeListener.class.isAssignableFrom(javaClass)) {
+ listenerTypes.add(javax.servlet.ServletContextAttributeListener.class);
+ }
+ if (javax.servlet.ServletRequestListener.class.isAssignableFrom(javaClass)) {
+ listenerTypes.add(javax.servlet.ServletRequestListener.class);
+ }
+ if (javax.servlet.ServletRequestAttributeListener.class.isAssignableFrom(javaClass)) {
+ listenerTypes.add(javax.servlet.ServletRequestAttributeListener.class);
+ }
+ if (javax.servlet.http.HttpSessionListener.class.isAssignableFrom(javaClass)) {
+ listenerTypes.add(javax.servlet.http.HttpSessionListener.class);
+ }
+ if (javax.servlet.http.HttpSessionAttributeListener.class.isAssignableFrom(javaClass)) {
+ listenerTypes.add(javax.servlet.http.HttpSessionAttributeListener.class);
+ }
+ if (javax.servlet.http.HttpSessionIdListener.class.isAssignableFrom(javaClass)) {
+ listenerTypes.add(javax.servlet.http.HttpSessionIdListener.class);
+ }
+
+ annotations.add(Service.Literal.of(listenerTypes.toArray(new Class<?>[0])));
+ }
+
+ if(!annotatedType.isAnnotationPresent(HttpWhiteboardContextSelect.class)) {
+ annotations.add(HttpWhiteboardContextSelect.Literal.of(getSelectedContext()));
+ }
+
+ annotations.add(HttpWhiteboardListener.Literal.INSTANCE);
+
+ if (!webListener.value().isEmpty()) {
+ annotations.add(ServiceDescription.Literal.of(webListener.value()));
+ }
+
+ pat.setAnnotatedType(new WebAnnotated<>(annotatedType, annotations));
+ }
+
+ <X> void processWebServlet(@Observes @WithAnnotations(WebServlet.class) ProcessAnnotatedType<X> pat) {
+ final AnnotatedType<X> annotatedType = pat.getAnnotatedType();
+
+ WebServlet webServlet = annotatedType.getAnnotation(WebServlet.class);
+
+ final Set<Annotation> annotations = new HashSet<>(annotatedType.getAnnotations());
+
+ if (!annotatedType.isAnnotationPresent(Service.class)) {
+ annotations.add(Service.Literal.of(new Class[] {Servlet.class}));
+ }
+
+ if(!annotatedType.isAnnotationPresent(HttpWhiteboardContextSelect.class)) {
+ annotations.add(HttpWhiteboardContextSelect.Literal.of(getSelectedContext()));
+ }
+
+ if (!webServlet.name().isEmpty()) {
+ annotations.add(HttpWhiteboardServletName.Literal.of(webServlet.name()));
+ }
+
+ if (webServlet.value().length > 0) {
+ annotations.add(HttpWhiteboardServletPattern.Literal.of(webServlet.value()));
+ }
+ else if (webServlet.urlPatterns().length > 0) {
+ annotations.add(HttpWhiteboardServletPattern.Literal.of(webServlet.urlPatterns()));
+ }
+
+ annotations.add(ServiceRanking.Literal.of(webServlet.loadOnStartup()));
+
+ // TODO Howto: INIT PARAMS ???
+
+ annotations.add(HttpWhiteboardServletAsyncSupported.Literal.of(webServlet.asyncSupported()));
+
+ if (!webServlet.description().isEmpty()) {
+ annotations.add(ServiceDescription.Literal.of(webServlet.description()));
+ }
+
+ MultipartConfig multipartConfig = annotatedType.getAnnotation(MultipartConfig.class);
+
+ if (multipartConfig != null) {
+ annotations.add(HttpWhiteboardServletMultipart.Literal.of(true, multipartConfig.fileSizeThreshold(), multipartConfig.location(), multipartConfig.maxFileSize(), multipartConfig.maxRequestSize()));
+ }
+
+ // TODO HowTo: ServletSecurity ???
+
+ pat.setAnnotatedType(new WebAnnotated<>(annotatedType, annotations));
+ }
void afterDeploymentValidation(
@Observes @Priority(LIBRARY_AFTER + 800)
@@ -145,4 +313,65 @@ public class HttpExtension implements Ex
private String _contextSelect;
private ServiceRegistration<?> _listenerRegistration;
+ private class WebAnnotated<X> implements AnnotatedType<X> {
+
+ private final AnnotatedType<X> annotatedType;
+ private final Set<Annotation> annotations;
+
+ public WebAnnotated(AnnotatedType<X> annotatedType, Set<Annotation> annotations) {
+ this.annotatedType = annotatedType;
+ this.annotations = annotations;
+ }
+
+ @Override
+ public Type getBaseType() {
+ return annotatedType.getBaseType();
+ }
+
+ @Override
+ public Set<Type> getTypeClosure() {
+ return annotatedType.getTypeClosure();
+ }
+
+ @Override
+ public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
+ return annotations.stream().filter(
+ ann -> annotationType.isAssignableFrom(ann.annotationType())
+ ).map(
+ ann -> annotationType.cast(ann)
+ ).findFirst().orElse(null);
+ }
+
+ @Override
+ public Set<Annotation> getAnnotations() {
+ return annotations;
+ }
+
+ @Override
+ public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
+ return annotations.stream().anyMatch(
+ ann -> annotationType.isAssignableFrom(ann.annotationType())
+ );
+ }
+
+ @Override
+ public Class<X> getJavaClass() {
+ return annotatedType.getJavaClass();
+ }
+
+ @Override
+ public Set<AnnotatedConstructor<X>> getConstructors() {
+ return annotatedType.getConstructors();
+ }
+
+ @Override
+ public Set<AnnotatedMethod<? super X>> getMethods() {
+ return annotatedType.getMethods();
+ }
+
+ @Override
+ public Set<AnnotatedField<? super X>> getFields() {
+ return annotatedType.getFields();
+ }
+ }
}
Modified: aries/trunk/cdi/cdi-itests/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-itests/pom.xml?rev=1851982&r1=1851981&r2=1851982&view=diff
==============================================================================
--- aries/trunk/cdi/cdi-itests/pom.xml (original)
+++ aries/trunk/cdi/cdi-itests/pom.xml Wed Jan 23 21:00:44 2019
@@ -47,6 +47,10 @@
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-annotation_1.3_spec</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-atinject_1.0_spec</artifactId>
</dependency>
<dependency>
Modified: aries/trunk/cdi/cdi-itests/src/main/java/org/apache/aries/cdi/test/tb6/FooServlet.java
URL: http://svn.apache.org/viewvc/aries/trunk/cdi/cdi-itests/src/main/java/org/apache/aries/cdi/test/tb6/FooServlet.java?rev=1851982&r1=1851981&r2=1851982&view=diff
==============================================================================
--- aries/trunk/cdi/cdi-itests/src/main/java/org/apache/aries/cdi/test/tb6/FooServlet.java (original)
+++ aries/trunk/cdi/cdi-itests/src/main/java/org/apache/aries/cdi/test/tb6/FooServlet.java Wed Jan 23 21:00:44 2019
@@ -18,21 +18,13 @@ import java.io.IOException;
import java.io.PrintWriter;
import javax.inject.Inject;
-import javax.servlet.Servlet;
import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardServletName;
-import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardServletPattern;
-import org.osgi.service.cdi.annotations.Service;
-import org.osgi.service.cdi.annotations.SingleComponent;
-
-@HttpWhiteboardServletName("foo")
-@HttpWhiteboardServletPattern("/foo")
-@SingleComponent
-@Service(Servlet.class)
+@WebServlet(name = "foo", urlPatterns = "/foo")
@SuppressWarnings("serial")
public class FooServlet extends HttpServlet {