proposal for adapted code style
diff --git a/cdi-extension-jaxrs/src/main/java/org/apache/aries/cdi/extension/jaxrs/JaxrsCDIExtension.java b/cdi-extension-jaxrs/src/main/java/org/apache/aries/cdi/extension/jaxrs/JaxrsCDIExtension.java
index 8915d70..f7dba2f 100644
--- a/cdi-extension-jaxrs/src/main/java/org/apache/aries/cdi/extension/jaxrs/JaxrsCDIExtension.java
+++ b/cdi-extension-jaxrs/src/main/java/org/apache/aries/cdi/extension/jaxrs/JaxrsCDIExtension.java
@@ -14,10 +14,12 @@
package org.apache.aries.cdi.extension.jaxrs;
+import static java.util.Optional.of;
import static java.util.Optional.ofNullable;
import java.lang.annotation.Annotation;
import java.util.List;
+import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.enterprise.context.Dependent;
@@ -51,7 +53,7 @@
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.WriterInterceptor;
-import org.apache.aries.cdi.extension.spi.adapt.Adapted;
+import org.apache.aries.cdi.extension.spi.adapt.IfNotAService;
import org.apache.aries.cdi.extra.propertytypes.JaxrsApplicationBase;
import org.apache.aries.cdi.extra.propertytypes.JaxrsApplicationSelect;
import org.apache.aries.cdi.extra.propertytypes.JaxrsExtension;
@@ -82,17 +84,13 @@
applications.add(annotatedType);
- AnnotatedTypeConfigurator<? extends Application> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, Application.class, true)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsApplicationBase.class)) {
- configurator.add(
- JaxrsApplicationBase.Literal.of(
- annotatedType.getAnnotation(ApplicationPath.class).value()));
- }
+ commonProperties(pat, Application.class, true).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsApplicationBase.class)) {
+ configurator.add(
+ JaxrsApplicationBase.Literal.of(
+ annotatedType.getAnnotation(ApplicationPath.class).value()));
+ }
+ });
}
<X> void resource(
@@ -101,15 +99,11 @@
AnnotatedType<X> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<X> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, Object.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsResource.class)) {
- configurator.add(JaxrsResource.Literal.INSTANCE);
- }
+ commonProperties(pat, Object.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsResource.class)) {
+ configurator.add(JaxrsResource.Literal.INSTANCE);
+ }
+ });
}
void containerRequestFilter(
@@ -117,15 +111,11 @@
AnnotatedType<? extends ContainerRequestFilter> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<? extends ContainerRequestFilter> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, ContainerRequestFilter.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
- configurator.add(JaxrsExtension.Literal.INSTANCE);
- }
+ commonProperties(pat, ContainerRequestFilter.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
+ configurator.add(JaxrsExtension.Literal.INSTANCE);
+ }
+ });
}
void containerResponseFilter(
@@ -133,15 +123,11 @@
AnnotatedType<? extends ContainerResponseFilter> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<? extends ContainerResponseFilter> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, ContainerResponseFilter.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
- configurator.add(JaxrsExtension.Literal.INSTANCE);
- }
+ commonProperties(pat, ContainerResponseFilter.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
+ configurator.add(JaxrsExtension.Literal.INSTANCE);
+ }
+ });
}
void readerInterceptor(
@@ -149,15 +135,11 @@
AnnotatedType<? extends ReaderInterceptor> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<? extends ReaderInterceptor> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, ReaderInterceptor.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
- configurator.add(JaxrsExtension.Literal.INSTANCE);
- }
+ commonProperties(pat, ReaderInterceptor.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
+ configurator.add(JaxrsExtension.Literal.INSTANCE);
+ }
+ });
}
void writerInterceptor(
@@ -165,15 +147,11 @@
AnnotatedType<? extends WriterInterceptor> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<? extends WriterInterceptor> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, WriterInterceptor.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
- configurator.add(JaxrsExtension.Literal.INSTANCE);
- }
+ commonProperties(pat, WriterInterceptor.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
+ configurator.add(JaxrsExtension.Literal.INSTANCE);
+ }
+ });
}
@SuppressWarnings("rawtypes")
@@ -182,15 +160,11 @@
AnnotatedType<? extends MessageBodyReader> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<? extends MessageBodyReader> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, MessageBodyReader.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
- configurator.add(JaxrsExtension.Literal.INSTANCE);
- }
+ commonProperties(pat, MessageBodyReader.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
+ configurator.add(JaxrsExtension.Literal.INSTANCE);
+ }
+ });
}
@SuppressWarnings("rawtypes")
@@ -199,15 +173,12 @@
AnnotatedType<? extends MessageBodyWriter> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<? extends MessageBodyWriter> configurator = pat.configureAnnotatedType();
+ commonProperties(pat, MessageBodyWriter.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
+ configurator.add(JaxrsExtension.Literal.INSTANCE);
+ }
- if (!commonProperties(annotatedType, configurator, MessageBodyWriter.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
- configurator.add(JaxrsExtension.Literal.INSTANCE);
- }
+ });
}
@SuppressWarnings("rawtypes")
@@ -216,15 +187,11 @@
AnnotatedType<? extends ContextResolver> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<? extends ContextResolver> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, ContextResolver.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
- configurator.add(JaxrsExtension.Literal.INSTANCE);
- }
+ commonProperties(pat, ContextResolver.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
+ configurator.add(JaxrsExtension.Literal.INSTANCE);
+ }
+ });
}
@SuppressWarnings("rawtypes")
@@ -233,15 +200,11 @@
AnnotatedType<? extends ExceptionMapper> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<? extends ExceptionMapper> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, ExceptionMapper.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
- configurator.add(JaxrsExtension.Literal.INSTANCE);
- }
+ commonProperties(pat, ExceptionMapper.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
+ configurator.add(JaxrsExtension.Literal.INSTANCE);
+ }
+ });
}
void paramConverterProvider(
@@ -249,15 +212,11 @@
AnnotatedType<? extends ParamConverterProvider> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<? extends ParamConverterProvider> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, ParamConverterProvider.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
- configurator.add(JaxrsExtension.Literal.INSTANCE);
- }
+ commonProperties(pat, ParamConverterProvider.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
+ configurator.add(JaxrsExtension.Literal.INSTANCE);
+ }
+ });
}
void feature(
@@ -265,15 +224,11 @@
AnnotatedType<? extends Feature> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<? extends Feature> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, Feature.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
- configurator.add(JaxrsExtension.Literal.INSTANCE);
- }
+ commonProperties(pat, Feature.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
+ configurator.add(JaxrsExtension.Literal.INSTANCE);
+ }
+ });
}
void dynamicFeature(
@@ -281,72 +236,65 @@
AnnotatedType<? extends DynamicFeature> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<? extends DynamicFeature> configurator = pat.configureAnnotatedType();
-
- if (!commonProperties(annotatedType, configurator, DynamicFeature.class, false)) {
- return;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
- configurator.add(JaxrsExtension.Literal.INSTANCE);
- }
+ commonProperties(pat, DynamicFeature.class, false).ifPresent(configurator -> {
+ if (!annotatedType.isAnnotationPresent(JaxrsExtension.class)) {
+ configurator.add(JaxrsExtension.Literal.INSTANCE);
+ }
+ });
}
/*
* @return true if common properties were added (i.e. if no @Service was found)
*/
- boolean commonProperties(
- AnnotatedType<?> annotatedType, AnnotatedTypeConfigurator<?> configurator,
- Class<?> serviceType, boolean application) {
+ private <X> Optional<AnnotatedTypeConfigurator<X>> commonProperties(
+ ProcessAnnotatedType<X> pat, Class<?> serviceType, boolean application) {
+ return IfNotAService.run(pat, mgr -> {
+ final AnnotatedTypeConfigurator<X> configurator = mgr.mergeWith(serviceType);
+ final AnnotatedType<?> annotatedType = pat.getAnnotatedType();
+ if (!annotatedType.isAnnotationPresent(JaxrsName.class)) {
+ if (application) {
+ configurator.add(
+ JaxrsName.Literal.of(
+ ofNullable((String)configuration.get(JaxrsWhiteboardConstants.JAX_RS_NAME)).orElse(
+ JaxrsWhiteboardConstants.JAX_RS_DEFAULT_APPLICATION
+ )
+ )
+ );
+ }
+ else {
+ configurator.add(JaxrsName.Literal.of(annotatedType.getJavaClass().getSimpleName()));
+ }
+ }
- if (!Adapted.withServiceTypes(configurator, serviceType)) {
- return false;
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsName.class)) {
- if (application) {
- configurator.add(
- JaxrsName.Literal.of(
- ofNullable((String)configuration.get(JaxrsWhiteboardConstants.JAX_RS_NAME)).orElse(
- JaxrsWhiteboardConstants.JAX_RS_DEFAULT_APPLICATION
- )
- )
+ if (!application && !annotatedType.isAnnotationPresent(JaxrsApplicationSelect.class)) {
+ ofNullable((String)configuration.get(JaxrsWhiteboardConstants.JAX_RS_APPLICATION_SELECT)).ifPresent(
+ select -> configurator.add(JaxrsApplicationSelect.Literal.of(select))
);
}
- else {
- configurator.add(JaxrsName.Literal.of(annotatedType.getJavaClass().getSimpleName()));
+
+ if (!annotatedType.isAnnotationPresent(JaxrsExtensionSelect.class)) {
+ ofNullable((String[])configuration.get(JaxrsWhiteboardConstants.JAX_RS_EXTENSION_SELECT)).ifPresent(selects -> {
+ if (selects.length > 0) {
+ configurator.add(JaxrsExtensionSelect.Literal.of(selects));
+ }
+ });
}
- }
- if (!application && !annotatedType.isAnnotationPresent(JaxrsApplicationSelect.class)) {
- ofNullable((String)configuration.get(JaxrsWhiteboardConstants.JAX_RS_APPLICATION_SELECT)).ifPresent(
- select -> configurator.add(JaxrsApplicationSelect.Literal.of(select))
- );
- }
+ if (!annotatedType.isAnnotationPresent(JaxrsWhiteboardTarget.class)) {
+ ofNullable((String)configuration.get(JaxrsWhiteboardConstants.JAX_RS_WHITEBOARD_TARGET)).ifPresent(
+ target -> configurator.add(JaxrsWhiteboardTarget.Literal.of(target))
+ );
+ }
- if (!annotatedType.isAnnotationPresent(JaxrsExtensionSelect.class)) {
- ofNullable((String[])configuration.get(JaxrsWhiteboardConstants.JAX_RS_EXTENSION_SELECT)).ifPresent(selects -> {
- if (selects.length > 0) {
- configurator.add(JaxrsExtensionSelect.Literal.of(selects));
+ if (!annotatedType.isAnnotationPresent(ServiceInstance.class)) {
+ Class<? extends Annotation> beanScope = Util.beanScope(annotatedType, Dependent.class);
+
+ if (Dependent.class.equals(beanScope)) {
+ configurator.add(ServiceInstance.Literal.of(ServiceScope.PROTOTYPE));
}
- });
- }
-
- if (!annotatedType.isAnnotationPresent(JaxrsWhiteboardTarget.class)) {
- ofNullable((String)configuration.get(JaxrsWhiteboardConstants.JAX_RS_WHITEBOARD_TARGET)).ifPresent(
- target -> configurator.add(JaxrsWhiteboardTarget.Literal.of(target))
- );
- }
-
- if (!annotatedType.isAnnotationPresent(ServiceInstance.class)) {
- Class<? extends Annotation> beanScope = Util.beanScope(annotatedType, Dependent.class);
-
- if (Dependent.class.equals(beanScope)) {
- configurator.add(ServiceInstance.Literal.of(ServiceScope.PROTOTYPE));
}
- }
-
- return true;
+ return of(configurator);
+ }, Optional::empty);
}
void afterDeploymentValidation(@Observes AfterDeploymentValidation adv) {
diff --git a/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebFilterProcessor.java b/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebFilterProcessor.java
index 6bb425b..00aab47 100644
--- a/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebFilterProcessor.java
+++ b/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebFilterProcessor.java
@@ -23,7 +23,7 @@
import javax.servlet.Filter;
import javax.servlet.annotation.WebFilter;
-import org.apache.aries.cdi.extension.spi.adapt.Adapted;
+import org.apache.aries.cdi.extension.spi.adapt.IfNotAService;
import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardContextSelect;
import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardFilterAsyncSupported;
import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardFilterDispatcher;
@@ -48,50 +48,47 @@
public <X> void process(
Configuration configuration, ProcessAnnotatedType<X> pat) {
- final AnnotatedType<X> annotatedType = pat.getAnnotatedType();
+ IfNotAService.run(pat, mgr -> {
+ final AnnotatedTypeConfigurator<X> configurator = mgr.mergeWith(Filter.class);
+ final AnnotatedType<X> annotatedType = pat.getAnnotatedType();
- AnnotatedTypeConfigurator<X> configurator = pat.configureAnnotatedType();
+ WebFilter webFilter = annotatedType.getAnnotation(WebFilter.class);
- if (!Adapted.withServiceTypes(configurator, Filter.class)) {
- return;
- }
-
- WebFilter webFilter = annotatedType.getAnnotation(WebFilter.class);
-
- if(!annotatedType.isAnnotationPresent(HttpWhiteboardContextSelect.class)) {
- ofNullable((String)configuration.get(HTTP_WHITEBOARD_CONTEXT_SELECT)).ifPresent(
- select -> configurator.add(HttpWhiteboardContextSelect.Literal.of(select))
- );
- }
-
- if (!annotatedType.isAnnotationPresent(ServiceDescription.class) && !webFilter.description().isEmpty()) {
- configurator.add(ServiceDescription.Literal.of(webFilter.description()));
- }
-
- if (!annotatedType.isAnnotationPresent(HttpWhiteboardFilterName.class) && !webFilter.filterName().isEmpty()) {
- configurator.add(HttpWhiteboardFilterName.Literal.of(webFilter.filterName()));
- }
-
- if (!annotatedType.isAnnotationPresent(HttpWhiteboardFilterServlet.class) && webFilter.servletNames().length > 0) {
- configurator.add(HttpWhiteboardFilterServlet.Literal.of(webFilter.servletNames()));
- }
-
- if (!annotatedType.isAnnotationPresent(HttpWhiteboardFilterPattern.class)) {
- if (webFilter.value().length > 0) {
- configurator.add(HttpWhiteboardFilterPattern.Literal.of(webFilter.value()));
+ if(!annotatedType.isAnnotationPresent(HttpWhiteboardContextSelect.class)) {
+ ofNullable((String)configuration.get(HTTP_WHITEBOARD_CONTEXT_SELECT)).ifPresent(
+ select -> configurator.add(HttpWhiteboardContextSelect.Literal.of(select))
+ );
}
- else if (webFilter.urlPatterns().length > 0) {
- configurator.add(HttpWhiteboardFilterPattern.Literal.of(webFilter.urlPatterns()));
+
+ if (!annotatedType.isAnnotationPresent(ServiceDescription.class) && !webFilter.description().isEmpty()) {
+ configurator.add(ServiceDescription.Literal.of(webFilter.description()));
}
- }
- if (!annotatedType.isAnnotationPresent(HttpWhiteboardFilterDispatcher.class) && webFilter.dispatcherTypes().length > 0) {
- configurator.add(HttpWhiteboardFilterDispatcher.Literal.of(webFilter.dispatcherTypes()));
- }
+ if (!annotatedType.isAnnotationPresent(HttpWhiteboardFilterName.class) && !webFilter.filterName().isEmpty()) {
+ configurator.add(HttpWhiteboardFilterName.Literal.of(webFilter.filterName()));
+ }
- if(!annotatedType.isAnnotationPresent(HttpWhiteboardFilterAsyncSupported.class)) {
- configurator.add(HttpWhiteboardFilterAsyncSupported.Literal.of(webFilter.asyncSupported()));
- }
+ if (!annotatedType.isAnnotationPresent(HttpWhiteboardFilterServlet.class) && webFilter.servletNames().length > 0) {
+ configurator.add(HttpWhiteboardFilterServlet.Literal.of(webFilter.servletNames()));
+ }
+
+ if (!annotatedType.isAnnotationPresent(HttpWhiteboardFilterPattern.class)) {
+ if (webFilter.value().length > 0) {
+ configurator.add(HttpWhiteboardFilterPattern.Literal.of(webFilter.value()));
+ }
+ else if (webFilter.urlPatterns().length > 0) {
+ configurator.add(HttpWhiteboardFilterPattern.Literal.of(webFilter.urlPatterns()));
+ }
+ }
+
+ if (!annotatedType.isAnnotationPresent(HttpWhiteboardFilterDispatcher.class) && webFilter.dispatcherTypes().length > 0) {
+ configurator.add(HttpWhiteboardFilterDispatcher.Literal.of(webFilter.dispatcherTypes()));
+ }
+
+ if(!annotatedType.isAnnotationPresent(HttpWhiteboardFilterAsyncSupported.class)) {
+ configurator.add(HttpWhiteboardFilterAsyncSupported.Literal.of(webFilter.asyncSupported()));
+ }
+ });
}
}
diff --git a/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebListenerProcessor.java b/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebListenerProcessor.java
index ac2dfa0..918a2cc 100644
--- a/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebListenerProcessor.java
+++ b/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebListenerProcessor.java
@@ -17,15 +17,21 @@
import static java.util.Optional.ofNullable;
import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.stream.Stream;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator;
+import javax.servlet.ServletContextAttributeListener;
+import javax.servlet.ServletContextListener;
+import javax.servlet.ServletRequestAttributeListener;
+import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
+import javax.servlet.http.HttpSessionAttributeListener;
+import javax.servlet.http.HttpSessionIdListener;
+import javax.servlet.http.HttpSessionListener;
-import org.apache.aries.cdi.extension.spi.adapt.Adapted;
+import org.apache.aries.cdi.extension.spi.adapt.IfNotAService;
import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardContextSelect;
import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardListener;
import org.apache.aries.cdi.extra.propertytypes.ServiceDescription;
@@ -47,54 +53,37 @@
Configuration configuration, ProcessAnnotatedType<X> pat) {
final AnnotatedType<X> annotatedType = pat.getAnnotatedType();
+ IfNotAService.run(pat, mgr -> {
+ final Class<X> javaClass = annotatedType.getJavaClass();
+ final Class<?>[] serviceTypes = Stream.of(
+ ServletContextListener.class,
+ ServletContextAttributeListener.class,
+ ServletRequestListener.class,
+ ServletRequestAttributeListener.class,
+ HttpSessionListener.class,
+ HttpSessionAttributeListener.class,
+ HttpSessionIdListener.class)
+ .filter(c -> c.isAssignableFrom(javaClass))
+ .toArray(Class[]::new);
- AnnotatedTypeConfigurator<X> configurator = pat.configureAnnotatedType();
+ AnnotatedTypeConfigurator<X> configurator = mgr.mergeWith(serviceTypes);
- Set<Class<?>> serviceTypes = new HashSet<>();
+ WebListener webListener = annotatedType.getAnnotation(WebListener.class);
- Class<X> javaClass = annotatedType.getJavaClass();
+ if(!annotatedType.isAnnotationPresent(HttpWhiteboardContextSelect.class)) {
+ ofNullable((String)configuration.get(HTTP_WHITEBOARD_CONTEXT_SELECT)).ifPresent(
+ select -> configurator.add(HttpWhiteboardContextSelect.Literal.of(select))
+ );
+ }
- if (javax.servlet.ServletContextListener.class.isAssignableFrom(javaClass)) {
- serviceTypes.add(javax.servlet.ServletContextListener.class);
- }
- if (javax.servlet.ServletContextAttributeListener.class.isAssignableFrom(javaClass)) {
- serviceTypes.add(javax.servlet.ServletContextAttributeListener.class);
- }
- if (javax.servlet.ServletRequestListener.class.isAssignableFrom(javaClass)) {
- serviceTypes.add(javax.servlet.ServletRequestListener.class);
- }
- if (javax.servlet.ServletRequestAttributeListener.class.isAssignableFrom(javaClass)) {
- serviceTypes.add(javax.servlet.ServletRequestAttributeListener.class);
- }
- if (javax.servlet.http.HttpSessionListener.class.isAssignableFrom(javaClass)) {
- serviceTypes.add(javax.servlet.http.HttpSessionListener.class);
- }
- if (javax.servlet.http.HttpSessionAttributeListener.class.isAssignableFrom(javaClass)) {
- serviceTypes.add(javax.servlet.http.HttpSessionAttributeListener.class);
- }
- if (javax.servlet.http.HttpSessionIdListener.class.isAssignableFrom(javaClass)) {
- serviceTypes.add(javax.servlet.http.HttpSessionIdListener.class);
- }
+ if(!annotatedType.isAnnotationPresent(HttpWhiteboardListener.class)) {
+ configurator.add(HttpWhiteboardListener.Literal.INSTANCE);
+ }
- if (!Adapted.withServiceTypes(configurator, serviceTypes)) {
- return;
- }
-
- WebListener webListener = annotatedType.getAnnotation(WebListener.class);
-
- if(!annotatedType.isAnnotationPresent(HttpWhiteboardContextSelect.class)) {
- ofNullable((String)configuration.get(HTTP_WHITEBOARD_CONTEXT_SELECT)).ifPresent(
- select -> configurator.add(HttpWhiteboardContextSelect.Literal.of(select))
- );
- }
-
- if(!annotatedType.isAnnotationPresent(HttpWhiteboardListener.class)) {
- configurator.add(HttpWhiteboardListener.Literal.INSTANCE);
- }
-
- if (!annotatedType.isAnnotationPresent(ServiceDescription.class) && !webListener.value().isEmpty()) {
- configurator.add(ServiceDescription.Literal.of(webListener.value()));
- }
+ if (!annotatedType.isAnnotationPresent(ServiceDescription.class) && !webListener.value().isEmpty()) {
+ configurator.add(ServiceDescription.Literal.of(webListener.value()));
+ }
+ });
}
}
diff --git a/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebServletProcessor.java b/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebServletProcessor.java
index 590e805..de97eab 100644
--- a/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebServletProcessor.java
+++ b/cdi-extension-servlet-common/src/main/java/org/apache/aries/cdi/extension/servlet/common/WebServletProcessor.java
@@ -24,7 +24,7 @@
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
-import org.apache.aries.cdi.extension.spi.adapt.Adapted;
+import org.apache.aries.cdi.extension.spi.adapt.IfNotAService;
import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardContextSelect;
import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardServletAsyncSupported;
import org.apache.aries.cdi.extra.propertytypes.HttpWhiteboardServletMultipart;
@@ -49,58 +49,54 @@
public <X> void process(
Configuration configuration, ProcessAnnotatedType<X> pat) {
- final AnnotatedType<X> annotatedType = pat.getAnnotatedType();
+ IfNotAService.run(pat, mgr -> {
+ final AnnotatedTypeConfigurator<X> configurator = mgr.mergeWith(Servlet.class);
+ final AnnotatedType<X> annotatedType = pat.getAnnotatedType();
+ WebServlet webServlet = annotatedType.getAnnotation(WebServlet.class);
- AnnotatedTypeConfigurator<X> configurator = pat.configureAnnotatedType();
-
- if (!Adapted.withServiceTypes(configurator, Servlet.class)) {
- return;
- }
-
- WebServlet webServlet = annotatedType.getAnnotation(WebServlet.class);
-
- if(!annotatedType.isAnnotationPresent(HttpWhiteboardContextSelect.class)) {
- ofNullable((String)configuration.get(HTTP_WHITEBOARD_CONTEXT_SELECT)).ifPresent(
- select -> configurator.add(HttpWhiteboardContextSelect.Literal.of(select))
- );
- }
-
- if (!annotatedType.isAnnotationPresent(HttpWhiteboardServletName.class) && !webServlet.name().isEmpty()) {
- configurator.add(HttpWhiteboardServletName.Literal.of(webServlet.name()));
- }
-
- if(!annotatedType.isAnnotationPresent(HttpWhiteboardServletPattern.class)) {
- if (webServlet.value().length > 0) {
- configurator.add(HttpWhiteboardServletPattern.Literal.of(webServlet.value()));
+ if(!annotatedType.isAnnotationPresent(HttpWhiteboardContextSelect.class)) {
+ ofNullable((String)configuration.get(HTTP_WHITEBOARD_CONTEXT_SELECT)).ifPresent(
+ select -> configurator.add(HttpWhiteboardContextSelect.Literal.of(select))
+ );
}
- else if (webServlet.urlPatterns().length > 0) {
- configurator.add(HttpWhiteboardServletPattern.Literal.of(webServlet.urlPatterns()));
+
+ if (!annotatedType.isAnnotationPresent(HttpWhiteboardServletName.class) && !webServlet.name().isEmpty()) {
+ configurator.add(HttpWhiteboardServletName.Literal.of(webServlet.name()));
}
- }
- if (!annotatedType.isAnnotationPresent(ServiceRanking.class)) {
- configurator.add(ServiceRanking.Literal.of(webServlet.loadOnStartup()));
- }
-
- // TODO Howto: INIT PARAMS ???
-
- if (!annotatedType.isAnnotationPresent(HttpWhiteboardServletAsyncSupported.class)) {
- configurator.add(HttpWhiteboardServletAsyncSupported.Literal.of(webServlet.asyncSupported()));
- }
-
- if (!annotatedType.isAnnotationPresent(ServiceDescription.class) && !webServlet.description().isEmpty()) {
- configurator.add(ServiceDescription.Literal.of(webServlet.description()));
- }
-
- if (!annotatedType.isAnnotationPresent(HttpWhiteboardServletMultipart.class)) {
- MultipartConfig multipartConfig = annotatedType.getAnnotation(MultipartConfig.class);
-
- if (multipartConfig != null) {
- configurator.add(HttpWhiteboardServletMultipart.Literal.of(true, multipartConfig.fileSizeThreshold(), multipartConfig.location(), multipartConfig.maxFileSize(), multipartConfig.maxRequestSize()));
+ if(!annotatedType.isAnnotationPresent(HttpWhiteboardServletPattern.class)) {
+ if (webServlet.value().length > 0) {
+ configurator.add(HttpWhiteboardServletPattern.Literal.of(webServlet.value()));
+ }
+ else if (webServlet.urlPatterns().length > 0) {
+ configurator.add(HttpWhiteboardServletPattern.Literal.of(webServlet.urlPatterns()));
+ }
}
- }
- // TODO HowTo: ServletSecurity ???
+ if (!annotatedType.isAnnotationPresent(ServiceRanking.class)) {
+ configurator.add(ServiceRanking.Literal.of(webServlet.loadOnStartup()));
+ }
+
+ // TODO Howto: INIT PARAMS ???
+
+ if (!annotatedType.isAnnotationPresent(HttpWhiteboardServletAsyncSupported.class)) {
+ configurator.add(HttpWhiteboardServletAsyncSupported.Literal.of(webServlet.asyncSupported()));
+ }
+
+ if (!annotatedType.isAnnotationPresent(ServiceDescription.class) && !webServlet.description().isEmpty()) {
+ configurator.add(ServiceDescription.Literal.of(webServlet.description()));
+ }
+
+ if (!annotatedType.isAnnotationPresent(HttpWhiteboardServletMultipart.class)) {
+ MultipartConfig multipartConfig = annotatedType.getAnnotation(MultipartConfig.class);
+
+ if (multipartConfig != null) {
+ configurator.add(HttpWhiteboardServletMultipart.Literal.of(true, multipartConfig.fileSizeThreshold(), multipartConfig.location(), multipartConfig.maxFileSize(), multipartConfig.maxRequestSize()));
+ }
+ }
+
+ // TODO HowTo: ServletSecurity ???
+ });
}
}
diff --git a/cdi-extension-spi/src/main/java/org/apache/aries/cdi/extension/spi/adapt/Adapted.java b/cdi-extension-spi/src/main/java/org/apache/aries/cdi/extension/spi/adapt/Adapted.java
deleted file mode 100644
index 9f29f05..0000000
--- a/cdi-extension-spi/src/main/java/org/apache/aries/cdi/extension/spi/adapt/Adapted.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * Copyright (c) OSGi Alliance (2018). All Rights Reserved.
- *
- * Licensed 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
- *
- * 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,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.aries.cdi.extension.spi.adapt;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
-
-import javax.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator;
-
-import org.apache.aries.cdi.extension.spi.annotation.AdaptedService;
-import org.osgi.service.cdi.annotations.Service;
-
-public class Adapted {
-
- private Adapted() {
- //
- }
-
- /**
- * Adapt the annotated type associated with the configurator with additional
- * types in order to publish OSGi services with those types.
- * <p>
- * The annotated type will not be adapted if it is already annotated with {@link Service @Service}.
- *
- * @param <X> the type of the annotated type
- * @param configurator the configurator
- * @param serviceTypes the additional service types
- * @return true if the annotated type was adapted
- */
- public static <X> boolean withServiceTypes(AnnotatedTypeConfigurator<X> configurator, Collection<Class<?>> serviceTypes) {
- return withServiceTypes(configurator, serviceTypes, false);
- }
-
- /**
- * Adapt the annotated type associated with the configurator with additional
- * types in order to publish OSGi services with those types.
- * <p>
- * The annotated type will not be adapted if it is already annotated with {@link Service @Service}.
- *
- * @param <X> the type of the annotated type
- * @param configurator the configurator
- * @param serviceTypes the additional service types
- * @return true if the annotated type was adapted
- */
- public static <X> boolean withServiceTypes(AnnotatedTypeConfigurator<X> configurator, Class<?>... serviceTypes) {
- return withServiceTypes(configurator, Arrays.asList(serviceTypes), false);
- }
-
- /**
- * Adapt the annotated type associated with the configurator with additional
- * types in order to publish OSGi services with those types.
- * <p>
- * The annotated type will not be adapted if it is already annotated with {@link Service @Service}.
- *
- * @param <X> the type of the annotated type
- * @param configurator the configurator
- * @param serviceTypes the additional service types
- * @param replace if true do not merge with previous types
- * @return true if the annotated type was adapted
- */
- public static <X> boolean withServiceTypes(AnnotatedTypeConfigurator<X> configurator, Collection<Class<?>> serviceTypes, boolean replace) {
- if (configurator.getAnnotated().isAnnotationPresent(Service.class)) {
- return false;
- }
-
- Set<Class<?>> servicesSet = new HashSet<>(serviceTypes);
-
- AdaptedService adaptedService = configurator.getAnnotated().getAnnotation(AdaptedService.class);
-
- if (adaptedService != null) {
- configurator.remove(adaptedService::equals);
- if (!replace) {
- servicesSet.addAll(Arrays.asList(adaptedService.value()));
- }
- }
-
- configurator.add(
- AdaptedService.Literal.of(servicesSet.toArray(new Class<?>[0])));
-
- return true;
- }
-
-}
diff --git a/cdi-extension-spi/src/main/java/org/apache/aries/cdi/extension/spi/adapt/IfNotAService.java b/cdi-extension-spi/src/main/java/org/apache/aries/cdi/extension/spi/adapt/IfNotAService.java
new file mode 100644
index 0000000..03f1233
--- /dev/null
+++ b/cdi-extension-spi/src/main/java/org/apache/aries/cdi/extension/spi/adapt/IfNotAService.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) OSGi Alliance (2018). All Rights Reserved.
+ *
+ * Licensed 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
+ *
+ * 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,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.aries.cdi.extension.spi.adapt;
+
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Stream;
+
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+import javax.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator;
+
+import org.apache.aries.cdi.extension.spi.annotation.AdaptedService;
+import org.osgi.service.cdi.annotations.Service;
+
+public class IfNotAService {
+
+ private IfNotAService() {
+ //
+ }
+
+ public static <X, R> R run(final ProcessAnnotatedType<X> pat,
+ final Function<ServiceTypeManager<X>, R> task,
+ final Supplier<R> defaultValue) {
+ if (!pat.getAnnotatedType().isAnnotationPresent(Service.class)) {
+ return task.apply(serviceTypes -> {
+ final AdaptedService adaptedService = pat.getAnnotatedType().getAnnotation(AdaptedService.class);
+ final AnnotatedTypeConfigurator<X> configurator = pat.configureAnnotatedType();
+ final Class<?>[] services;
+ if (adaptedService != null) {
+ configurator.remove(a -> a.annotationType() == AdaptedService.class);
+ services = Stream.concat(
+ Stream.of(serviceTypes),
+ Stream.of(adaptedService.value()))
+ .toArray(Class[]::new);
+ } else {
+ services = serviceTypes;
+ }
+ configurator.add(AdaptedService.Literal.of(services));
+ return configurator;
+ });
+ }
+ return defaultValue.get();
+ }
+
+ public static <X> void run(final ProcessAnnotatedType<X> pat,
+ final Consumer<ServiceTypeManager<X>> task) {
+ run(pat, m -> {
+ task.accept(m);
+ return null;
+ }, () -> null);
+ }
+
+ public interface ServiceTypeManager<X> {
+ AnnotatedTypeConfigurator<X> mergeWith(Class<?>... serviceTypes);
+ }
+}