cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject cxf git commit: CXF-7097: application leaks a creational context in CDI
Date Tue, 08 Nov 2016 22:45:59 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes c06cc6dcf -> 1a670208b


CXF-7097: application leaks a creational context in CDI


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

Branch: refs/heads/3.1.x-fixes
Commit: 1a670208b139fee709869178b8e32f0c4d86c9a4
Parents: c06cc6d
Author: reta <drreta@gmail.com>
Authored: Tue Nov 8 17:45:22 2016 -0500
Committer: reta <drreta@gmail.com>
Committed: Tue Nov 8 17:45:22 2016 -0500

----------------------------------------------------------------------
 .../apache/cxf/cdi/DefaultApplicationBean.java  |  2 +-
 .../cxf/cdi/JAXRSCdiResourceExtension.java      | 40 ++++++++++++++++++--
 .../cdi/src/main/resources/META-INF/beans.xml   |  3 ++
 .../cdi/base/AbstractCdiSingleAppTest.java      |  2 +-
 4 files changed, 41 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/1a670208/integration/cdi/src/main/java/org/apache/cxf/cdi/DefaultApplicationBean.java
----------------------------------------------------------------------
diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/DefaultApplicationBean.java
b/integration/cdi/src/main/java/org/apache/cxf/cdi/DefaultApplicationBean.java
index af83bbe..ed5afa6 100644
--- a/integration/cdi/src/main/java/org/apache/cxf/cdi/DefaultApplicationBean.java
+++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/DefaultApplicationBean.java
@@ -49,7 +49,7 @@ class DefaultApplicationBean extends AbstractCXFBean<DefaultApplication>
{
     @Override
     public void destroy(DefaultApplication defaultApplication,
                         CreationalContext<DefaultApplication> creationalContext) {
-
+        creationalContext.release();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cxf/blob/1a670208/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 2c03595..968424e 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,17 +22,20 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.ServiceLoader;
 import java.util.Set;
 
+import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.spi.AfterBeanDiscovery;
 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.BeforeShutdown;
 import javax.enterprise.inject.spi.Extension;
 import javax.enterprise.inject.spi.InjectionTarget;
 import javax.enterprise.inject.spi.ProcessBean;
@@ -57,10 +60,12 @@ public class JAXRSCdiResourceExtension implements Extension {
     private boolean hasBus;
     private Bus bus;
 
-    private final List< Bean< ? > > applicationBeans = new ArrayList< Bean<
? > >();
+    private final Set< Bean< ? > > applicationBeans = new LinkedHashSet< Bean<
? > >();
     private final List< Bean< ? > > serviceBeans = new ArrayList< Bean<
? > >();
     private final List< Bean< ? > > providerBeans = new ArrayList< Bean<
? > >();
     private final List< Bean< ? extends Feature > > featureBeans = new ArrayList<
Bean< ? extends Feature > >();
+    private final List< CreationalContext< ? > > disposableCreationalContexts
= 
+        new ArrayList< CreationalContext< ? > >();
 
     @SuppressWarnings("unchecked")
     public <T> void collect(@Observes final ProcessBean< T > event) {
@@ -81,15 +86,17 @@ public class JAXRSCdiResourceExtension implements Extension {
     public void load(@Observes final AfterDeploymentValidation event, final BeanManager beanManager)
{
         // no need of creational context, it only works for app scoped instances anyway
         final Bean<?> busBean = beanManager.resolve(beanManager.getBeans(CdiBusBean.CXF));
+        
         bus = (Bus)beanManager.getReference(
-                busBean, Bus.class,
+                busBean, 
+                Bus.class,
                 beanManager.createCreationalContext(busBean));
 
         for (final Bean< ? > application: applicationBeans) {
             final Application instance = (Application)beanManager.getReference(
                 application,
                 application.getBeanClass(),
-                beanManager.createCreationalContext(application)
+                createCreationalContext(beanManager, application)
             );
 
             // If there is an application without any singletons and classes defined, we
will
@@ -119,7 +126,18 @@ public class JAXRSCdiResourceExtension implements Extension {
             event.addBean(new CdiBusBean(busInjectionTarget));
         }
         if (applicationBeans.isEmpty() && !serviceBeans.isEmpty()) {
-            event.addBean(new DefaultApplicationBean());
+            final DefaultApplicationBean applicationBean = new DefaultApplicationBean();
+            applicationBeans.add(applicationBean);
+            event.addBean(applicationBean);
+        }
+    }
+    
+    /**
+     * Releases created CreationalContext instances 
+     */
+    public void release(@Observes final BeforeShutdown event) {
+        for (final CreationalContext<?> disposableCreationalContext: disposableCreationalContexts)
{
+            disposableCreationalContext.release();
         }
     }
     
@@ -284,4 +302,18 @@ public class JAXRSCdiResourceExtension implements Extension {
         
         return services;
     }
+    
+    /**
+     * Creates and collects the CreationalContext instances for future releasing. 
+     * @param beanManager bean manager instance
+     * @param bean bean instance to create CreationalContext for
+     * @return CreationalContext instance
+     */
+    private<T> CreationalContext< T > createCreationalContext(final BeanManager
beanManager, Bean< T > bean) {
+        final CreationalContext< T > creationalContext = beanManager.createCreationalContext(bean);
+        if (!(bean instanceof DefaultApplicationBean)) {
+            disposableCreationalContexts.add(creationalContext);
+        }
+        return creationalContext;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/1a670208/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
index aff28d9..70c89e6 100644
--- a/integration/cdi/src/main/resources/META-INF/beans.xml
+++ b/integration/cdi/src/main/resources/META-INF/beans.xml
@@ -2,4 +2,7 @@
 <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">
+	<scan>
+		<exclude name="org.apache.cxf.cdi.DefaultApplication" />
+	</scan>
 </beans>

http://git-wip-us.apache.org/repos/asf/cxf/blob/1a670208/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiSingleAppTest.java
----------------------------------------------------------------------
diff --git a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiSingleAppTest.java
b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiSingleAppTest.java
index f690d06..0dd6ce4 100644
--- a/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiSingleAppTest.java
+++ b/systests/cdi/base/src/main/java/org/apache/cxf/systests/cdi/base/AbstractCdiSingleAppTest.java
@@ -35,7 +35,7 @@ import org.junit.Test;
 public abstract class AbstractCdiSingleAppTest extends AbstractBusClientServerTestBase {
     @Test
     public void testInjectedVersionIsProperlyReturned() {
-        Response r = createWebClient("/rest/bookstore/version", MediaType.TEXT_PLAIN).get();
+        Response r = createWebClient(getBasePath() + "/version", MediaType.TEXT_PLAIN).get();
         assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
         assertEquals("1.0", r.readEntity(String.class));
     }


Mime
View raw message