You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2018/03/21 16:40:28 UTC
[cxf] 01/02: CXF-7643 ensure to not conflict with default CDI beans
when possible + add a way for cdi container to integrate with cdi to
configure it
This is an automated email from the ASF dual-hosted git repository.
dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 4398d3a15c344281a6974d334311ec223a603e15
Author: Romain Manni-Bucau <rm...@apache.org>
AuthorDate: Sun Mar 18 21:00:42 2018 +0100
CXF-7643 ensure to not conflict with default CDI beans when possible + add a way for cdi container to integrate with cdi to configure it
---
.../org/apache/cxf/cdi/ContextProducerBean.java | 11 +++--
.../apache/cxf/cdi/JAXRSCdiResourceExtension.java | 55 +++++++++++++++++++++-
2 files changed, 61 insertions(+), 5 deletions(-)
diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/ContextProducerBean.java b/integration/cdi/src/main/java/org/apache/cxf/cdi/ContextProducerBean.java
index 7fab977..98bcca6 100644
--- a/integration/cdi/src/main/java/org/apache/cxf/cdi/ContextProducerBean.java
+++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/ContextProducerBean.java
@@ -34,16 +34,19 @@ import org.apache.cxf.phase.PhaseInterceptorChain;
public class ContextProducerBean extends AbstractCXFBean<Object> implements PassivationCapable {
private final Type type;
+ private final Set<Annotation> qualifiers;
- ContextProducerBean(Type type) {
+ ContextProducerBean(Type type, boolean defaultQualifier) {
this.type = type;
+ this.qualifiers = new HashSet<>(defaultQualifier ? 2 : 1);
+ this.qualifiers.add(ContextResolved.LITERAL);
+ if (defaultQualifier) {
+ this.qualifiers.add(DEFAULT);
+ }
}
@Override
public Set<Annotation> getQualifiers() {
- Set<Annotation> qualifiers = new HashSet<>(2);
- qualifiers.add(ContextResolved.LITERAL);
- qualifiers.add(DEFAULT);
return qualifiers;
}
diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java b/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
index bc7a251..04121a6 100644
--- a/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
+++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
@@ -22,6 +22,7 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
@@ -35,6 +36,7 @@ import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.BeforeShutdown;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionTarget;
@@ -63,6 +65,9 @@ import org.apache.cxf.jaxrs.utils.InjectionUtils;
import org.apache.cxf.jaxrs.utils.JAXRSServerFactoryCustomizationUtils;
import org.apache.cxf.jaxrs.utils.ResourceUtils;
+import static java.util.Arrays.asList;
+import static java.util.Optional.ofNullable;
+
/**
* Apache CXF portable CDI extension to support initialization of JAX-RS resources.
*/
@@ -78,6 +83,8 @@ public class JAXRSCdiResourceExtension implements Extension {
new ArrayList< CreationalContext< ? > >();
private final Set< Type > contextTypes = new LinkedHashSet<>();
+ private final Collection< String > existingStandardClasses = new HashSet<>();
+
/**
* Holder of the classified resource classes, converted to appropriate instance
* representations.
@@ -112,6 +119,46 @@ public class JAXRSCdiResourceExtension implements Extension {
}
}
+ // observing JAXRSCdiResourceExtension a "container" can customize that value to prevent some instances
+ // to be added with the default qualifier
+ public Collection<String> getExistingStandardClasses() {
+ return existingStandardClasses;
+ }
+
+ /**
+ * Fires itsels, allows other extensions to modify this one.
+ * Typical example can be to modify existingStandardClasses to prevent CXF
+ * to own some beans it shouldn't create with default classifier.
+ *
+ * @param beforeBeanDiscovery the corresponding cdi event.
+ * @param beanManager the cdi bean manager.
+ */
+ void onStartup(@Observes final BeforeBeanDiscovery beforeBeanDiscovery, final BeanManager beanManager) {
+ final ClassLoader loader = ofNullable(Thread.currentThread().getContextClassLoader())
+ .orElseGet(ClassLoader::getSystemClassLoader);
+ boolean webHandled = false;
+ try { // OWB
+ loader.loadClass("org.apache.webbeans.web.lifecycle.WebContainerLifecycle");
+ webHandled = true;
+ } catch (final NoClassDefFoundError | ClassNotFoundException e) {
+ // ok to keep them all
+ }
+ if (!webHandled) {
+ try { // Weld
+ loader.loadClass("org.jboss.weld.module.web.WeldWebModule");
+ webHandled = true;
+ } catch (final NoClassDefFoundError | ClassNotFoundException e) {
+ // ok to keep them all
+ }
+ }
+ if (webHandled) {
+ existingStandardClasses.addAll(asList(
+ "javax.servlet.http.HttpServletRequest",
+ "javax.servlet.ServletContext"));
+ }
+ beanManager.fireEvent(this);
+ }
+
/**
* For any {@link AnnotatedType} that includes a {@link Context} injection point, this method replaces
* the field with the following code:
@@ -149,6 +196,11 @@ public class JAXRSCdiResourceExtension implements Extension {
} else if (CdiBusBean.CXF.equals(event.getBean().getName())
&& Bus.class.isAssignableFrom(event.getBean().getBeanClass())) {
hasBus = true;
+ } else {
+ event.getBean().getTypes().stream()
+ .filter(e -> Object.class != e && InjectionUtils.STANDARD_CONTEXT_CLASSES.contains(e.getTypeName()))
+ .findFirst()
+ .ifPresent(type -> existingStandardClasses.add(type.getTypeName()));
}
}
@@ -224,7 +276,8 @@ public class JAXRSCdiResourceExtension implements Extension {
// add custom contexts
contextTypes.addAll(getCustomContextClasses());
// register all of the context types
- contextTypes.forEach(t -> event.addBean(new ContextProducerBean(t)));
+ contextTypes.forEach(
+ t -> event.addBean(new ContextProducerBean(t, !existingStandardClasses.contains(t.getTypeName()))));
}
/**
--
To stop receiving notification emails like this one, please contact
dkulp@apache.org.