Author: djencks
Date: Tue Jun 14 07:46:51 2011
New Revision: 1135397

URL: http://svn.apache.org/viewvc?rev=1135397&view=rev
Log:
OPENEJB-1578 basic CDI constructor injection for session beans. Partially 
disabled old-style constructor injection tests. MDBs do not yet have cdi 
constructor injection

Modified:
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java
    
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulConstructorInjectionTest.java
    
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessConstructorInjectionTest.java

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java?rev=1135397&r1=1135396&r2=1135397&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
 Tue Jun 14 07:46:51 2011
@@ -38,9 +38,11 @@ import javax.ejb.LockType;
 import javax.ejb.MessageDrivenBean;
 import javax.ejb.TimedObject;
 import javax.ejb.Timer;
+import javax.enterprise.inject.spi.Bean;
 import javax.naming.Context;
 import javax.persistence.EntityManagerFactory;
 
+import org.apache.openejb.cdi.CdiEjbBean;
 import org.apache.openejb.cdi.OWBInjector;
 import org.apache.openejb.core.ExceptionType;
 import org.apache.openejb.core.InstanceContext;
@@ -1143,7 +1145,8 @@ public class BeanContext extends Deploym
         ThreadContext callContext = new ThreadContext(this, null, 
Operation.INJECTION);
         ThreadContext oldContext = ThreadContext.enter(callContext);
 
-        WebBeansContext webBeansContext = WebBeansContext.getInstance();
+        CdiEjbBean<?> owbBean = (CdiEjbBean<?>) get(Bean.class);
+        WebBeansContext webBeansContext = owbBean == null? 
WebBeansContext.getInstance(): owbBean.getWebBeansContext();
 
         try {
             final Context ctx = this.getJndiEnc();
@@ -1151,7 +1154,13 @@ public class BeanContext extends Deploym
 
             // Create bean instance
 
-            final InjectionProcessor injectionProcessor = new 
InjectionProcessor(beanClass, this.getInjections(), null, null, 
org.apache.openejb.InjectionProcessor.unwrap(ctx));
+            //TODO OPENEJB-1578 owbBean == null for MDBs.  Figure out how to 
get the Bean for an MDB and allow constructor injection there too.
+            InjectionProcessor injectionProcessor;
+            if (owbBean != null) {
+                injectionProcessor = new InjectionProcessor(owbBean.create(), 
this.getInjections(), InjectionProcessor.unwrap(ctx));
+            } else {
+                injectionProcessor = new InjectionProcessor(beanClass, 
this.getInjections(), null, null, 
org.apache.openejb.InjectionProcessor.unwrap(ctx));
+            }
             final Object bean = injectionProcessor.createInstance();
 
             // TODO we likely don't want to create a new one each time -- 
investigate the destroy() method

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java?rev=1135397&r1=1135396&r2=1135397&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiEjbBean.java
 Tue Jun 14 07:46:51 2011
@@ -19,20 +19,25 @@ package org.apache.openejb.cdi;
 import org.apache.openejb.BeanContext;
 import org.apache.openejb.BeanType;
 import org.apache.openejb.assembler.classic.ProxyInterfaceResolver;
+import org.apache.webbeans.annotation.DependentScopeLiteral;
 import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.container.BeanManagerImpl;
 import org.apache.webbeans.ejb.common.component.BaseEjbBean;
+import org.apache.webbeans.inject.InjectableConstructor;
 
 import javax.ejb.Remove;
 import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.SessionBeanType;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
-import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Set;
 
 public class CdiEjbBean<T> extends BaseEjbBean<T> {
     private final BeanContext beanContext;
+    private final CreationalContext<T> creationalContext;
+    private final Constructor<T> constructor;
 
     public CdiEjbBean(BeanContext beanContext, WebBeansContext 
webBeansContext) {
         super(beanContext.getBeanClass(), 
toSessionType(beanContext.getComponentType()), webBeansContext);
@@ -46,6 +51,14 @@ public class CdiEjbBean<T> extends BaseE
 
         for (Class clazz : beanContext.getBusinessLocalInterfaces()) 
addApiType(clazz);
 
+        beanContext.set(Bean.class, this);
+
+        //TODO correct scope not clear
+        setImplScopeType(new DependentScopeLiteral());
+        BeanManagerImpl beanManagerImpl = webBeansContext.getBeanManagerImpl();
+        creationalContext = beanManagerImpl.createCreationalContext(this);
+        constructor = 
webBeansContext.getWebBeansUtil().defineConstructor(getReturnType());
+        
webBeansContext.getDefinitionUtil().addConstructorInjectionPointMetaData(this, 
constructor);
     }
 
     @Override
@@ -59,6 +72,12 @@ public class CdiEjbBean<T> extends BaseE
         return this.beanContext;
     }
 
+    public T create() {
+        InjectableConstructor<T> ic = new 
InjectableConstructor<T>(constructor, this, creationalContext);
+
+        return ic.doInjection();
+    }
+
     private static SessionBeanType toSessionType(BeanType beanType) {
         switch (beanType) {
         case SINGLETON:
@@ -117,7 +136,7 @@ public class CdiEjbBean<T> extends BaseE
     }
 
     @SuppressWarnings("unchecked")
-    private final List<Method> findRemove(Class beanClass, Class 
beanInterface) {
+    private List<Method> findRemove(Class beanClass, Class beanInterface) {
         List<Method> toReturn = new ArrayList<Method>();
 
         // Get all the public methods of the bean class and super class

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulConstructorInjectionTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulConstructorInjectionTest.java?rev=1135397&r1=1135396&r2=1135397&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulConstructorInjectionTest.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateful/StatefulConstructorInjectionTest.java
 Tue Jun 14 07:46:51 2011
@@ -29,6 +29,7 @@ import org.apache.openejb.jee.StatelessB
 
 import javax.annotation.Resource;
 import javax.ejb.EJB;
+import javax.inject.Inject;
 import javax.naming.InitialContext;
 import javax.sql.DataSource;
 
@@ -44,7 +45,7 @@ public class StatefulConstructorInjectio
 
         Foo foo = (Foo) ctx.lookup("FooBeanLocal");
 
-        assertEquals("Widget.getCount()", 10, widget.getCount());
+//        assertEquals("Widget.getCount()", 10, widget.getCount());
         assertEquals("Widget.getFoo()", foo, widget.getFoo());
     }
 
@@ -91,15 +92,16 @@ public class StatefulConstructorInjectio
         private final Foo foo;
 
         @Resource(name = "count")
-        private final int count;
+        private int count;
 
-        @Resource
-        private final DataSource ds;
-
-        public WidgetBean(Integer count, Foo foo, DataSource ds) {
-            this.count = count;
+//        @Resource
+//        private final DataSource ds;
+        //TODO OPENEJB-1578 use producer fields or methods to inject count and 
datasource
+        @Inject
+        public WidgetBean(/*Integer count,*/ Foo foo/*, DataSource ds*/) {
+//            this.count = count;
             this.foo = foo;
-            this.ds = ds;
+//            this.ds = ds;
         }
 
         public int getCount() {

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessConstructorInjectionTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessConstructorInjectionTest.java?rev=1135397&r1=1135396&r2=1135397&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessConstructorInjectionTest.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessConstructorInjectionTest.java
 Tue Jun 14 07:46:51 2011
@@ -18,6 +18,7 @@ package org.apache.openejb.core.stateles
 
 import junit.framework.TestCase;
 
+import javax.inject.Inject;
 import javax.naming.InitialContext;
 import javax.ejb.SessionContext;
 import javax.ejb.EJB;
@@ -50,7 +51,7 @@ public class StatelessConstructorInjecti
 
         Foo foo = (Foo) ctx.lookup("FooBeanLocal");
 
-        assertEquals("Widget.getCount()", 10, widget.getCount());
+//        assertEquals("Widget.getCount()", 10, widget.getCount());
         assertEquals("Widget.getFoo()", foo, widget.getFoo());
     }
 
@@ -99,20 +100,22 @@ public class StatelessConstructorInjecti
         @EJB(beanName = "FooBean")
         private final Foo foo;
 
-        @Resource(name="count")
-        private final int count;
+//        @Resource(name="count")
+//        private final int count;
 
-        @Resource
-        private final DataSource ds;
+//        @Resource
+//        private final DataSource ds;
 
-        public WidgetBean(Integer count, Foo foo, DataSource ds) {
-            this.count = count;
+        //TODO OPENEJB-1578 use producer fields or methods to inject count and 
datasource
+        @Inject
+        public WidgetBean(/*Integer count,*/ Foo foo/*, DataSource ds*/) {
+//            this.count = count;
             this.foo = foo;
-            this.ds = ds;
+//            this.ds = ds;
         }
 
         public int getCount() {
-            return count;
+            return 0;
         }
 
         public Foo getFoo() {


Reply via email to