deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject git commit: DELTASPIKE-398 add handling for @Dependent scoped beans.
Date Fri, 09 Aug 2013 21:41:37 GMT
Updated Branches:
  refs/heads/master 6bb833743 -> cef50b097


DELTASPIKE-398 add handling for @Dependent scoped beans.

This is important as we need a way to properly destroy them.


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

Branch: refs/heads/master
Commit: cef50b097b3a9613534405f03c3b1e6aaa404f09
Parents: 6bb8337
Author: Mark Struberg <struberg@apache.org>
Authored: Tue Aug 6 08:51:56 2013 +0200
Committer: Mark Struberg <struberg@apache.org>
Committed: Tue Aug 6 08:51:56 2013 +0200

----------------------------------------------------------------------
 .../core/api/provider/BeanProvider.java         | 27 ++++++++++
 .../core/api/provider/DependentProvider.java    | 55 ++++++++++++++++++++
 .../core/api/provider/BeanProviderTest.java     | 30 +++++++++--
 .../core/api/provider/DependentTestBean.java    | 14 +++++
 4 files changed, 123 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cef50b09/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
index ca7d0c7..daea20b 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
@@ -302,6 +302,33 @@ public final class BeanProvider
         return result;
     }
 
+    public static <T> DependentProvider<T> getDependent(Class<T> type,
Annotation... qualifiers)
+    {
+        BeanManager beanManager = getBeanManager();
+        Set<Bean<?>> beans = beanManager.getBeans(type, qualifiers);
+        Bean<?> bean = beanManager.resolve(beans);
+        return createDependentProvider(beanManager, type, (Bean<T>) bean);
+    }
+
+    public static <T> DependentProvider<T> getDependent(String name)
+    {
+        BeanManager beanManager = getBeanManager();
+        Set<Bean<?>> beans = beanManager.getBeans(name);
+        Bean<?> bean = beanManager.resolve(beans);
+        Class beanClass = bean.getBeanClass();
+
+        return createDependentProvider(beanManager, (Class<T>) beanClass, (Bean<T>)
bean);
+    }
+
+    private static <T> DependentProvider<T> createDependentProvider(BeanManager
beanManager, Class<T> type,
+                                                                    Bean<T> bean)
+    {
+        CreationalContext<T> cc = beanManager.createCreationalContext(bean);
+        T instance = (T) beanManager.getReference(bean, type, cc);
+
+        return new DependentProvider<T>(bean, cc, instance);
+    }
+
     /**
      * Get a set of {@link Bean} definitions by type independent of the qualifier.
      *

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cef50b09/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/DependentProvider.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/DependentProvider.java
b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/DependentProvider.java
new file mode 100644
index 0000000..a63fe83
--- /dev/null
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/DependentProvider.java
@@ -0,0 +1,55 @@
+/*
+ * 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.deltaspike.core.api.provider;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.inject.Provider;
+
+/**
+ * A {@link Provider} for &#064;Dependent scoped contextual instances.
+ * We need this to be apble to properly clean them up when they are not
+ * needed anymore via the {@link #destroy()} method.
+ *
+ * Instances of this class can be retrieved using the {@link BeanProvider}.
+ */
+public class DependentProvider<T> implements Provider<T>
+{
+    private T instance;
+    private CreationalContext<T> creationalContext;
+    private Bean<T> bean;
+
+    DependentProvider(Bean<T> bean, CreationalContext<T> creationalContext, T
instance)
+    {
+        this.bean = bean;
+        this.creationalContext = creationalContext;
+        this.instance = instance;
+    }
+
+    @Override
+    public T get()
+    {
+        return instance;
+    }
+
+    public void destroy()
+    {
+        bean.destroy(instance, creationalContext);
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cef50b09/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
index 010b44e..abbeb98 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
@@ -20,6 +20,7 @@ package org.apache.deltaspike.test.core.api.provider;
 
 
 import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.api.provider.DependentProvider;
 import org.apache.deltaspike.test.util.ArchiveUtils;
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.junit.Arquillian;
@@ -196,10 +197,33 @@ public class BeanProviderTest
     }
 
     @Test
-    public void dependentBeanResolving() throws Exception
+    public void testDependentBeanResolving() throws Exception
     {
-        DependentTestBean dependentTestBean = BeanProvider.getContextualReference(DependentTestBean.class);
-        Assert.assertNotNull(dependentTestBean);
+        DependentProvider<DependentTestBean> dependentTestBeanProvider = BeanProvider.getDependent(DependentTestBean.class);
+        checkDependentProvider(dependentTestBeanProvider);
+    }
+
+    @Test
+    public void testNamedDependentBeanResolving() throws Exception
+    {
+        DependentProvider<DependentTestBean> dependentTestBeanProvider = BeanProvider.getDependent("dependentTestBean");
+        checkDependentProvider(dependentTestBeanProvider);
+    }
+
+    private void checkDependentProvider(DependentProvider<DependentTestBean> dependentTestBeanProvider)
+    {
+        Assert.assertNotNull(dependentTestBeanProvider);
+
+        DependentTestBean instance = dependentTestBeanProvider.get();
+        Assert.assertNotNull(instance);
+
+        Assert.assertEquals(42, instance.getI());
+        instance.setI(21);
+        Assert.assertEquals(21, instance.getI());
+
+        Assert.assertFalse(instance.isDestroyed());
+        dependentTestBeanProvider.destroy();
+        Assert.assertTrue(instance.isDestroyed());
     }
 
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/cef50b09/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/DependentTestBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/DependentTestBean.java
b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/DependentTestBean.java
index 31630bb..1bda0b4 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/DependentTestBean.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/DependentTestBean.java
@@ -18,12 +18,16 @@
 */
 package org.apache.deltaspike.test.core.api.provider;
 
+import javax.annotation.PreDestroy;
 import javax.enterprise.context.Dependent;
+import javax.inject.Named;
 
 @Dependent
+@Named
 public class DependentTestBean
 {
     private int i = 42;
+    private boolean destroyed = false;
 
     public int getI()
     {
@@ -34,4 +38,14 @@ public class DependentTestBean
     {
         this.i = i;
     }
+
+    public boolean isDestroyed()
+    {
+        return destroyed;
+    }
+
+    @PreDestroy
+    public void cleanUp() {
+        destroyed = true;
+    }
 }


Mime
View raw message