cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [1/2] git commit: CXF-5576: Initital support for CDI integration. Refactored JAXRSServerFactoryBean/Bus instantiation flow.
Date Wed, 19 Mar 2014 14:07:59 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 1e9e67d77 -> 635e5acb7


CXF-5576: Initital support for CDI integration. Refactored JAXRSServerFactoryBean/Bus instantiation
flow.


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/675c9b2f
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/675c9b2f
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/675c9b2f

Branch: refs/heads/master
Commit: 675c9b2f77541f7e36d97e8887ceb04ef24dcf3b
Parents: d71c706
Author: reta <drreta@gmail.com>
Authored: Wed Mar 19 10:07:09 2014 -0400
Committer: reta <drreta@gmail.com>
Committed: Wed Mar 19 10:07:09 2014 -0400

----------------------------------------------------------------------
 .../java/org/apache/cxf/cdi/CXFCdiServlet.java  |  33 +++--
 .../java/org/apache/cxf/cdi/CdiBusBean.java     | 120 +++++++++++++++++++
 .../cxf/cdi/JAXRSCdiResourceExtension.java      |  79 ++++++------
 .../cxf/cdi/JAXRSCdiServerFactoryBean.java      | 117 ------------------
 .../apache/cxf/jaxrs/utils/ResourceUtils.java   |  10 +-
 5 files changed, 181 insertions(+), 178 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/675c9b2f/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
index 2cae17d..869ef88 100644
--- a/integration/cdi/src/main/java/org/apache/cxf/cdi/CXFCdiServlet.java
+++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/CXFCdiServlet.java
@@ -21,9 +21,8 @@ package org.apache.cxf.cdi;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.CDI;
 import javax.servlet.ServletConfig;
-import javax.servlet.ServletException;
 
-import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.BusFactory;
 import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
 
 /**
@@ -31,18 +30,28 @@ import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
  */
 public class CXFCdiServlet extends CXFNonSpringServlet {
     private static final long serialVersionUID = -2890970731778523861L;
+    private boolean busCreated;
     
     @Override
-    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 JAXRSServerFactoryBean factory: extension.getFactories()) {
-                factory.setBus(getBus());    
-                factory.init();
-            }        
+    protected void loadBus(ServletConfig servletConfig) {
+        final BeanManager beanManager = CDI.current().getBeanManager();        
+        if (beanManager != null) {
+            final JAXRSCdiResourceExtension extension = beanManager.getExtension(JAXRSCdiResourceExtension.class);
+            if (extension != null) {
+                setBus(extension.getBus());
+            }
+        } else {
+            busCreated = true;
+            setBus(BusFactory.newInstance().createBus());
+        }
+    }
+
+    @Override
+    public void destroyBus() {
+        if (busCreated) {
+            //if we created the Bus, we need to destroy it.  Otherwise, spring will handle
it.
+            getBus().shutdown(true);
+            setBus(null);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/675c9b2f/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiBusBean.java
----------------------------------------------------------------------
diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiBusBean.java b/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiBusBean.java
new file mode 100644
index 0000000..faf350f
--- /dev/null
+++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiBusBean.java
@@ -0,0 +1,120 @@
+/**
+ * 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.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.Any;
+import javax.enterprise.inject.Default;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.InjectionTarget;
+import javax.enterprise.util.AnnotationLiteral;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.bus.CXFBusFactory;
+import org.apache.cxf.bus.extension.ExtensionManagerBus;
+
+public final class CdiBusBean implements Bean< ExtensionManagerBus > {
+    static final String CXF = "cxf";
+    
+    private final InjectionTarget<ExtensionManagerBus> injectionTarget;
+    
+    CdiBusBean(final InjectionTarget<ExtensionManagerBus> injectionTarget) {
+        this.injectionTarget = injectionTarget;
+    }
+    
+    @Override
+    public Class< ? > getBeanClass() {
+        return Bus.class;
+    }
+
+    @Override
+    public Set<InjectionPoint> getInjectionPoints() {
+        return injectionTarget.getInjectionPoints();
+    }
+
+    @Override
+    public String getName() {
+        return CXF;
+    }
+
+    @SuppressWarnings("serial")
+    @Override
+    public Set< Annotation > getQualifiers() {
+        Set<Annotation> qualifiers = new HashSet<Annotation>();
+        qualifiers.add(new AnnotationLiteral< Default >() { });
+        qualifiers.add(new AnnotationLiteral< Any >() { });
+        return qualifiers;
+    }
+
+    @Override
+    public Set<Type> getTypes() {
+        final Set< Type > types = new HashSet< Type >();
+        types.add(Bus.class);
+        types.add(Object.class);
+        return types;
+    }
+    
+    @Override
+    public Class<? extends Annotation> getScope() {
+        return ApplicationScoped.class;
+    }
+
+    @Override
+    public boolean isAlternative() {
+        return false;
+    }
+
+    @Override
+    public boolean isNullable() {
+        return false;
+    }
+    
+    @Override
+    public Set< Class< ? extends Annotation > > getStereotypes() {
+        return Collections.< Class< ? extends Annotation > >emptySet();
+    }
+
+    @Override
+    public ExtensionManagerBus create(final CreationalContext< ExtensionManagerBus >
ctx) {
+        final ExtensionManagerBus instance = injectionTarget.produce(ctx);
+        CXFBusFactory.possiblySetDefaultBus(instance);
+        instance.initialize();
+        
+        injectionTarget.inject(instance, ctx);
+        injectionTarget.postConstruct(instance);
+        return instance;
+    }
+
+    @Override
+    public void destroy(final ExtensionManagerBus instance, 
+            final CreationalContext< ExtensionManagerBus > ctx) {
+        injectionTarget.preDestroy(instance);
+        injectionTarget.dispose(instance);
+        instance.shutdown();
+        ctx.release();
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/675c9b2f/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
index 97c6e6f..ffe5421 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
@@ -19,10 +19,7 @@
 package org.apache.cxf.cdi;
 
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.ServiceLoader;
 
 import javax.enterprise.event.Observes;
@@ -41,21 +38,22 @@ import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Provider;
 
+import org.apache.cxf.Bus;
+import org.apache.cxf.bus.extension.ExtensionManagerBus;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
 
 /**
  * Apache CXF portable CDI extension to support initialization of JAX-RS / JAX-WS resources.
 
  */
-
-public class JAXRSCdiResourceExtension implements Extension {
+public class JAXRSCdiResourceExtension implements Extension {    
+    private Bean< ? > busBean;
+    private Bus bus;
+    
     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 Map< Bean< ? >, Bean< JAXRSServerFactoryBean > > factoryBeans
= 
-        new HashMap< Bean< ? >, Bean< JAXRSServerFactoryBean > >();
-    
-    private final List< JAXRSServerFactoryBean > factories = new ArrayList< JAXRSServerFactoryBean
>();
+        
     private final List< Object > services = new ArrayList< Object >();
     private final List< Object > providers = new ArrayList< Object >();
     
@@ -66,6 +64,9 @@ public class JAXRSCdiResourceExtension implements Extension {
             serviceBeans.add(event.getBean());
         } else if (event.getAnnotated().isAnnotationPresent(Provider.class)) {
             providerBeans.add(event.getBean());
+        } else if (CdiBusBean.CXF.equals(event.getBean().getName()) 
+                && Bus.class.isAssignableFrom(event.getBean().getBeanClass())) {
+            busBean = event.getBean();  
         }
     }
     
@@ -90,51 +91,45 @@ public class JAXRSCdiResourceExtension implements Extension {
             );    
         }
         
-        for (final Map.Entry< Bean< ? >, Bean< JAXRSServerFactoryBean > >
entry: factoryBeans.entrySet()) {
+        bus = (Bus)beanManager.getReference(
+            busBean, 
+            busBean.getBeanClass(), 
+            beanManager.createCreationalContext(busBean)
+        );
+        
+        for (final Bean< ? > application: applicationBeans) {
             final Application instance = (Application)beanManager.getReference(
-                entry.getKey(), 
-                entry.getKey().getBeanClass(), 
-                beanManager.createCreationalContext(entry.getKey()) 
+                application, 
+                application.getBeanClass(), 
+                beanManager.createCreationalContext(application) 
             );
             
             // Create the JAXRSServerFactoryBean for each application we have discovered
-            factories.add(createFactoryInstance(instance, entry.getValue(), beanManager));
+            final JAXRSServerFactoryBean factory = createFactoryInstance(instance);
+            factory.init();
         }
     }
     
     public void injectFactories(@Observes final AfterBeanDiscovery event, final BeanManager
beanManager) {
-        final AnnotatedType< JAXRSServerFactoryBean > factoryAnnotatedType = 
-             beanManager.createAnnotatedType(JAXRSServerFactoryBean.class);
-        
-        final InjectionTarget<JAXRSServerFactoryBean> injectionTarget = 
-             beanManager.createInjectionTarget(factoryAnnotatedType);
-        
-        for (final Bean< ? > applicationBean: applicationBeans) {
-            final Bean< JAXRSServerFactoryBean > factoryBean =
-                new JAXRSCdiServerFactoryBean(applicationBean, injectionTarget);   
-            
-            event.addBean(factoryBean);
-            factoryBeans.put(applicationBean, factoryBean);
-        }
-    }
-    
-    public List< JAXRSServerFactoryBean > getFactories() {
-        return Collections.< JAXRSServerFactoryBean > unmodifiableList(factories);
+        if (busBean == null) {
+            final AnnotatedType< ExtensionManagerBus > busAnnotatedType = 
+                beanManager.createAnnotatedType(ExtensionManagerBus.class);
+               
+            final InjectionTarget<ExtensionManagerBus> busInjectionTarget = 
+                beanManager.createInjectionTarget(busAnnotatedType);
+               
+            busBean = new CdiBusBean(busInjectionTarget);
+            event.addBean(busBean);
+        } 
     }
     
     @SuppressWarnings("rawtypes")
-    private JAXRSServerFactoryBean createFactoryInstance(final Application application, 
-            final Bean< ? > factoryBean, final BeanManager beanManager) {
-        
-        final JAXRSServerFactoryBean instance = (JAXRSServerFactoryBean)beanManager.getReference(
-            factoryBean, 
-            factoryBean.getBeanClass(), 
-            beanManager.createCreationalContext(factoryBean)
-        );
+    private JAXRSServerFactoryBean createFactoryInstance(final Application application) {
                         
-        ResourceUtils.initializeApplication(instance, application, false, false);       
  
+        JAXRSServerFactoryBean instance = ResourceUtils.createApplication(application, false,
false);          
         instance.setServiceBeans(new ArrayList< Object >(services));
         instance.setProviders(providers);
+        instance.setBus(bus);
               
         final ServiceLoader< MessageBodyWriter > writers = ServiceLoader.load(MessageBodyWriter.class);
         for (final MessageBodyWriter< ? > writer: writers) {
@@ -148,4 +143,8 @@ public class JAXRSCdiResourceExtension implements Extension {
         
         return instance; 
     }
+    
+    public Bus getBus() {
+        return bus;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/675c9b2f/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiServerFactoryBean.java
----------------------------------------------------------------------
diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiServerFactoryBean.java
b/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiServerFactoryBean.java
deleted file mode 100644
index 6a1b714..0000000
--- a/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiServerFactoryBean.java
+++ /dev/null
@@ -1,117 +0,0 @@
-/**
- * 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.lang.annotation.Annotation;
-import java.lang.reflect.Type;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
-
-import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.context.spi.CreationalContext;
-import javax.enterprise.inject.Any;
-import javax.enterprise.inject.Default;
-import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.InjectionPoint;
-import javax.enterprise.inject.spi.InjectionTarget;
-import javax.enterprise.util.AnnotationLiteral;
-
-import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
-
-public final class JAXRSCdiServerFactoryBean implements Bean< JAXRSServerFactoryBean >
{
-    private static final String FACTORY_BEAN = "factoryBean";
-    
-    private final Bean< ? > applicationBean;
-    private final InjectionTarget<JAXRSServerFactoryBean> injectionTarget;
-    
-    JAXRSCdiServerFactoryBean(final Bean< ? > applicationBean,
-            final InjectionTarget<JAXRSServerFactoryBean> injectionTarget) {
-        this.applicationBean = applicationBean;
-        this.injectionTarget = injectionTarget;
-    }
-    
-    @Override
-    public Class< ? > getBeanClass() {
-        return JAXRSServerFactoryBean.class;
-    }
-
-    @Override
-    public Set<InjectionPoint> getInjectionPoints() {
-        return injectionTarget.getInjectionPoints();
-    }
-
-    @Override
-    public String getName() {
-        return FACTORY_BEAN + applicationBean.getName();
-    }
-
-    @SuppressWarnings("serial")
-    @Override
-    public Set< Annotation > getQualifiers() {
-        Set<Annotation> qualifiers = new HashSet<Annotation>();
-        qualifiers.add(new AnnotationLiteral< Default >() { });
-        qualifiers.add(new AnnotationLiteral< Any >() { });
-        return qualifiers;
-    }
-
-    @Override
-    public Set<Type> getTypes() {
-        final Set< Type > types = new HashSet< Type >();
-        types.add(JAXRSServerFactoryBean.class);
-        types.add(Object.class);
-        return types;
-    }
-    
-    @Override
-    public Class<? extends Annotation> getScope() {
-        return ApplicationScoped.class;
-    }
-
-    @Override
-    public boolean isAlternative() {
-        return false;
-    }
-
-    @Override
-    public boolean isNullable() {
-        return false;
-    }
-    
-    @Override
-    public Set< Class< ? extends Annotation > > getStereotypes() {
-        return Collections.< Class< ? extends Annotation > >emptySet();
-    }
-
-    @Override
-    public JAXRSServerFactoryBean create(final CreationalContext< JAXRSServerFactoryBean
> ctx) {
-        final JAXRSServerFactoryBean instance = injectionTarget.produce(ctx);
-        injectionTarget.inject(instance, ctx);
-        injectionTarget.postConstruct(instance);
-        return instance;
-    }
-
-    @Override
-    public void destroy(final JAXRSServerFactoryBean instance, 
-            final CreationalContext< JAXRSServerFactoryBean > ctx) {
-        injectionTarget.preDestroy(instance);
-        injectionTarget.dispose(instance);
-        ctx.release();
-    }
-}

http://git-wip-us.apache.org/repos/asf/cxf/blob/675c9b2f/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
index b67fa79..67e3634 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ResourceUtils.java
@@ -722,19 +722,11 @@ public final class ResourceUtils {
         }
         return values;
     }
-    
     public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath)
{
         return createApplication(app, ignoreAppPath, false);
     }
-    
     public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath,
                                                            boolean staticSubresourceResolution)
{
-        return initializeApplication(new JAXRSServerFactoryBean(), app, 
-                ignoreAppPath, staticSubresourceResolution);
-    }
-    
-    public static JAXRSServerFactoryBean initializeApplication(JAXRSServerFactoryBean bean,
-            Application app, boolean ignoreAppPath, boolean staticSubresourceResolution)
{
         
         Set<Object> singletons = app.getSingletons();
         verifySingletons(singletons);
@@ -775,6 +767,7 @@ public final class ResourceUtils {
             }
         }
         
+        JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
         String address = "/";
         if (!ignoreAppPath) {
             ApplicationPath appPath = app.getClass().getAnnotation(ApplicationPath.class);
@@ -800,7 +793,6 @@ public final class ResourceUtils {
         
         return bean;
     }
-
     
     private static boolean isValidProvider(Class<?> c) {
         if (c == null || c == Object.class) {


Mime
View raw message