You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2014/03/07 03:40:44 UTC
git commit: CXF-5576: Initital support for CDI integration
Repository: cxf
Updated Branches:
refs/heads/master f1ff1df3f -> c2a1497ba
CXF-5576: Initital support for CDI integration
Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/c2a1497b
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/c2a1497b
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/c2a1497b
Branch: refs/heads/master
Commit: c2a1497bac42707a222df548ec0a8adc3a7899cd
Parents: f1ff1df
Author: reta <dr...@gmail.com>
Authored: Thu Mar 6 21:40:01 2014 -0500
Committer: reta <dr...@gmail.com>
Committed: Thu Mar 6 21:40:01 2014 -0500
----------------------------------------------------------------------
integration/cdi/pom.xml | 65 ++++++++++++
.../java/org/apache/cxf/cdi/CXFCdiServlet.java | 71 +++++++++++++
.../cxf/cdi/JAXRSCdiResourceExtension.java | 100 +++++++++++++++++++
.../cdi/src/main/resources/META-INF/beans.xml | 6 ++
.../javax.enterprise.inject.spi.Extension | 1 +
integration/pom.xml | 1 +
parent/pom.xml | 7 ++
7 files changed, 251 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cxf/blob/c2a1497b/integration/cdi/pom.xml
----------------------------------------------------------------------
diff --git a/integration/cdi/pom.xml b/integration/cdi/pom.xml
new file mode 100644
index 0000000..1cb72e0
--- /dev/null
+++ b/integration/cdi/pom.xml
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>cxf-integration-cdi</artifactId>
+ <packaging>jar</packaging>
+ <name>Apache CXF CDI Integration</name>
+ <description>Apache CXF CDI Integration</description>
+ <url>http://cxf.apache.org</url>
+ <parent>
+ <groupId>org.apache.cxf</groupId>
+ <artifactId>cxf-parent</artifactId>
+ <version>3.0.0-SNAPSHOT</version>
+ <relativePath>../../parent/pom.xml</relativePath>
+ </parent>
+ <dependencies>
+ <dependency>
+ <groupId>javax.enterprise</groupId>
+ <artifactId>cdi-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cxf</groupId>
+ <artifactId>cxf-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>${cxf.servlet-api.group}</groupId>
+ <artifactId>${cxf.servlet-api.artifact}</artifactId>
+ <scope>provided</scope>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cxf</groupId>
+ <artifactId>cxf-rt-frontend-jaxrs</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-jdk14</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
http://git-wip-us.apache.org/repos/asf/cxf/blob/c2a1497b/integration/cdi/src/main/java/org/apache/cxf/cdi/CXFCdiServlet.java
----------------------------------------------------------------------
diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/CXFCdiServlet.java b/integration/cdi/src/main/java/org/apache/cxf/cdi/CXFCdiServlet.java
new file mode 100644
index 0000000..b3867b9
--- /dev/null
+++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/CXFCdiServlet.java
@@ -0,0 +1,71 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * 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.cxf.cdi;
+
+import java.util.ArrayList;
+import java.util.ServiceLoader;
+
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.CDI;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.utils.ResourceUtils;
+import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
+
+/**
+ * Apache CXF servlet with CDI 1.1 integration support
+ */
+public class CXFCdiServlet extends CXFNonSpringServlet {
+ private static final long serialVersionUID = -2890970731778523861L;
+
+ @Override
+ @SuppressWarnings("rawtypes")
+ public void init(ServletConfig servletConfig) throws ServletException {
+ super.init(servletConfig);
+
+ final BeanManager beanManager = CDI.current().getBeanManager();
+ final JAXRSCdiResourceExtension extension = beanManager.getExtension(JAXRSCdiResourceExtension.class);
+ if (extension != null) {
+ for (final Application application: extension.getApplications()) {
+ final JAXRSServerFactoryBean bean = ResourceUtils.createApplication(application, false, false);
+
+ bean.setServiceBeans(new ArrayList< Object >(extension.getServices()));
+ bean.setProviders(extension.getProviders());
+ bean.setBus(getBus());
+
+ final ServiceLoader< MessageBodyWriter > writers = ServiceLoader.load(MessageBodyWriter.class);
+ for (final MessageBodyWriter< ? > writer: writers) {
+ bean.setProvider(writer);
+ }
+
+ final ServiceLoader< MessageBodyReader > readers = ServiceLoader.load(MessageBodyReader.class);
+ for (final MessageBodyReader< ? > reader: readers) {
+ bean.setProvider(reader);
+ }
+
+ bean.create();
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/cxf/blob/c2a1497b/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..cf537a4
--- /dev/null
+++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java
@@ -0,0 +1,100 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * 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.cxf.cdi;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.spi.AfterDeploymentValidation;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.ProcessBean;
+import javax.ws.rs.ApplicationPath;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.ext.Provider;
+
+/**
+ * Apache CXF portable CDI extension to support initialization of JAX-RS / JAX-WS resources.
+ */
+public class JAXRSCdiResourceExtension implements Extension {
+ private final List< Bean< ? > > applicationBeans = new ArrayList<Bean<?>>();
+ private final List< Bean< ? > > serviceBeans = new ArrayList<Bean<?>>();
+ private final List< Bean< ? > > providerBeans = new ArrayList<Bean<?>>();
+
+ private final List< Application > applications = new ArrayList< Application >();
+ private final List< Object > services = new ArrayList< Object >();
+ private final List< Object > providers = new ArrayList< Object >();
+
+ public <T> void collect(@Observes final ProcessBean< T > event) {
+ if (event.getAnnotated().isAnnotationPresent(ApplicationPath.class)) {
+ applicationBeans.add(event.getBean());
+ } else if (event.getAnnotated().isAnnotationPresent(Path.class)) {
+ serviceBeans.add(event.getBean());
+ } else if (event.getAnnotated().isAnnotationPresent(Provider.class)) {
+ providerBeans.add(event.getBean());
+ }
+ }
+
+ public void load(@Observes final AfterDeploymentValidation event, final BeanManager beanManager) {
+ for (final Bean< ? > bean: applicationBeans) {
+ applications.add(
+ (Application)beanManager.getReference(
+ bean,
+ bean.getBeanClass(),
+ beanManager.createCreationalContext(bean)
+ )
+ );
+ }
+
+ for (final Bean< ? > bean: serviceBeans) {
+ services.add(
+ beanManager.getReference(
+ bean,
+ bean.getBeanClass(),
+ beanManager.createCreationalContext(bean)
+ )
+ );
+ }
+
+ for (final Bean< ? > bean: providerBeans) {
+ providers.add(
+ beanManager.getReference(
+ bean,
+ bean.getBeanClass(),
+ beanManager.createCreationalContext(bean)
+ )
+ );
+ }
+ }
+
+ public List<Application> getApplications() {
+ return applications;
+ }
+
+ public List< Object > getServices() {
+ return services;
+ }
+
+ public List< Object > getProviders() {
+ return providers;
+ }
+}
http://git-wip-us.apache.org/repos/asf/cxf/blob/c2a1497b/integration/cdi/src/main/resources/META-INF/beans.xml
----------------------------------------------------------------------
diff --git a/integration/cdi/src/main/resources/META-INF/beans.xml b/integration/cdi/src/main/resources/META-INF/beans.xml
new file mode 100644
index 0000000..3306820
--- /dev/null
+++ b/integration/cdi/src/main/resources/META-INF/beans.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<beans xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_1.xsd"
+ version="1.1">
+</beans>
http://git-wip-us.apache.org/repos/asf/cxf/blob/c2a1497b/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
----------------------------------------------------------------------
diff --git a/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
new file mode 100644
index 0000000..685a8e6
--- /dev/null
+++ b/integration/cdi/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
@@ -0,0 +1 @@
+org.apache.cxf.cdi.JAXRSCdiResourceExtension
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cxf/blob/c2a1497b/integration/pom.xml
----------------------------------------------------------------------
diff --git a/integration/pom.xml b/integration/pom.xml
index 37ef314..8f15a4e 100644
--- a/integration/pom.xml
+++ b/integration/pom.xml
@@ -31,5 +31,6 @@
</parent>
<modules>
<module>jca</module>
+ <module>cdi</module>
</modules>
</project>
http://git-wip-us.apache.org/repos/asf/cxf/blob/c2a1497b/parent/pom.xml
----------------------------------------------------------------------
diff --git a/parent/pom.xml b/parent/pom.xml
index ee90a55..4f1a47c 100644
--- a/parent/pom.xml
+++ b/parent/pom.xml
@@ -156,6 +156,7 @@
<cxf.xmlschema.version>2.1.0</cxf.xmlschema.version>
<cxf.xpp3.bundle.version>1.1.4c_6</cxf.xpp3.bundle.version>
<cxf.validation.api.version>1.1.0.Final</cxf.validation.api.version>
+ <cxf.cdi.api.version>1.1</cxf.cdi.api.version>
<cxf.osgi.validation.api.version>1.1.0</cxf.osgi.validation.api.version>
<cxf.hibernate.validator.version>5.0.2.Final</cxf.hibernate.validator.version>
<cxf.javax.el.version>3.0.0</cxf.javax.el.version>
@@ -763,6 +764,12 @@
<optional>true</optional>
</dependency>
<dependency>
+ <groupId>javax.enterprise</groupId>
+ <artifactId>cdi-api</artifactId>
+ <version>${cxf.cdi.api.version}</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${cxf.slf4j.version}</version>