You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2010/07/09 19:40:00 UTC

svn commit: r962617 - in /openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core: managed/ mdb/ singleton/ stateful/ stateless/

Author: dblevins
Date: Fri Jul  9 17:39:59 2010
New Revision: 962617

URL: http://svn.apache.org/viewvc?rev=962617&view=rev
Log:
Move the binding of EJBContext into the deploy methods and remove the synchronized lookup/bind blocks from instance creation. (yay!)

Modified:
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContainer.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/MdbInstanceFactory.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/singleton/SingletonInstanceManager.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContainer.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContainer.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContainer.java?rev=962617&r1=962616&r2=962617&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContainer.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/managed/ManagedContainer.java Fri Jul  9 17:39:59 2010
@@ -102,12 +102,14 @@ public class ManagedContainer implements
 
     protected final Cache<Object, Instance> cache;
     private final ConcurrentHashMap<Object, Instance> checkedOutInstances = new ConcurrentHashMap<Object, Instance>();
+    private final SessionContext sessionContext;
 
     public ManagedContainer(Object id, SecurityService securityService) throws SystemException {
         this.cache = new SimpleCache<Object, Instance>(null, new SimplePassivater(), 1000, 50, new Duration("1 hour"));
         this.containerID = id;
         this.securityService = securityService;
         cache.setListener(new StatefulCacheListener());
+        sessionContext = new ManagedContext(securityService, new ManagedUserTransaction(new EjbUserTransaction(), entityManagerRegistry));
     }
 
     private Map<Method, MethodType> getLifecycleMethodsOfInterface(CoreDeploymentInfo deploymentInfo) {
@@ -291,6 +293,12 @@ public class ManagedContainer implements
             logger.error("Unable to register MBean ", e);
         }
 
+        try {
+            final Context context = deploymentInfo.getJndiEnc();
+            context.bind("comp/EJBContext", sessionContext);
+        } catch (NamingException e) {
+            throw new OpenEJBException("Failed to bind EJBContext", e);
+        }
     }
 
     /**
@@ -586,18 +594,6 @@ public class ManagedContainer implements
             CoreDeploymentInfo deploymentInfo = callContext.getDeploymentInfo();
             Context ctx = deploymentInfo.getJndiEnc();
 
-            // Get or create the session context
-            SessionContext sessionContext;
-            synchronized (this) {
-                try {
-                    sessionContext = (SessionContext) ctx.lookup("comp/EJBContext");
-                } catch (NamingException e1) {
-                    ManagedUserTransaction userTransaction = new ManagedUserTransaction(new EjbUserTransaction(), entityManagerRegistry);
-                    sessionContext = new ManagedContext(securityService, userTransaction);
-                    ctx.bind("comp/EJBContext", sessionContext);
-                }
-            }
-
             // Create bean instance
             InjectionProcessor injectionProcessor = new InjectionProcessor(beanClass, deploymentInfo.getInjections(), null, null, unwrap(ctx));
             try {

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/MdbInstanceFactory.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/MdbInstanceFactory.java?rev=962617&r1=962616&r2=962617&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/MdbInstanceFactory.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/mdb/MdbInstanceFactory.java Fri Jul  9 17:39:59 2010
@@ -28,6 +28,7 @@ import javax.resource.spi.UnavailableExc
 
 import org.apache.openejb.Injection;
 import org.apache.openejb.InjectionProcessor;
+import org.apache.openejb.OpenEJBException;
 import static org.apache.openejb.InjectionProcessor.unwrap;
 import org.apache.openejb.core.BaseContext;
 import org.apache.openejb.core.CoreDeploymentInfo;
@@ -60,6 +61,7 @@ public class MdbInstanceFactory {
     private final SecurityService securityService;
     private final int instanceLimit;
     private int instanceCount;
+    private final MdbContext mdbContext;
 
     /**
      * Creates a MdbInstanceFactory for a single specific deployment.
@@ -67,10 +69,19 @@ public class MdbInstanceFactory {
      * @param securityService the transaction manager for this container system
      * @param instanceLimit the maximal number of instances or <= 0 if unlimited
      */
-    public MdbInstanceFactory(CoreDeploymentInfo deploymentInfo, SecurityService securityService, int instanceLimit) {
+    public MdbInstanceFactory(CoreDeploymentInfo deploymentInfo, SecurityService securityService, int instanceLimit) throws OpenEJBException {
         this.deploymentInfo = deploymentInfo;
         this.securityService = securityService;
         this.instanceLimit = instanceLimit;
+        mdbContext = new MdbContext(securityService);
+
+        try {
+            final Context context = deploymentInfo.getJndiEnc();
+            context.bind("comp/EJBContext", mdbContext);
+        } catch (NamingException e) {
+            throw new OpenEJBException("Failed to bind EJBContext", e);
+        }
+
     }
 
     /**
@@ -178,16 +189,6 @@ public class MdbInstanceFactory {
         ThreadContext oldContext = ThreadContext.enter(callContext);
         try {
             Context ctx = deploymentInfo.getJndiEnc();
-            // construct the bean instance
-            MdbContext mdbContext;
-            synchronized(this) {
-                try {
-                    mdbContext = (MdbContext) ctx.lookup("comp/EJBContext");
-                } catch (NamingException e) {
-                    mdbContext = new MdbContext(securityService);
-                    ctx.bind("comp/EJBContext",mdbContext);
-                }
-            }
 
             InjectionProcessor injectionProcessor = new InjectionProcessor(beanClass, deploymentInfo.getInjections(), null, null, unwrap(ctx));
 
@@ -252,24 +253,4 @@ public class MdbInstanceFactory {
         }
     }
 
-    private static void fillInjectionProperties(ObjectRecipe objectRecipe, Class clazz, CoreDeploymentInfo deploymentInfo, Context context) {
-        for (Injection injection : deploymentInfo.getInjections()) {
-            if (!injection.getTarget().isAssignableFrom(clazz)) continue;
-            try {
-                String jndiName = injection.getJndiName();
-                Object object = context.lookup("comp/env/" + jndiName);
-                if (object instanceof String) {
-                    String string = (String) object;
-                    // Pass it in raw so it could be potentially converted to
-                    // another data type by an xbean-reflect property editor
-                    objectRecipe.setProperty(injection.getTarget().getName() + "/" + injection.getName(), string);
-                } else {
-                    objectRecipe.setProperty(injection.getTarget().getName() + "/" + injection.getName(), object);
-                }
-            } catch (NamingException e) {
-                logger.warning("Injection data not found in enc: jndiName='" + injection.getJndiName() + "', target=" + injection.getTarget() + "/" + injection.getName());
-            }
-        }
-    }
-
 }

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/singleton/SingletonInstanceManager.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/singleton/SingletonInstanceManager.java?rev=962617&r1=962616&r2=962617&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/singleton/SingletonInstanceManager.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/singleton/SingletonInstanceManager.java Fri Jul  9 17:39:59 2010
@@ -21,7 +21,6 @@ import java.lang.management.ManagementFa
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.Lock;
@@ -42,7 +41,6 @@ import javax.xml.ws.WebServiceContext;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
-import org.apache.openejb.Injection;
 import org.apache.openejb.OpenEJBException;
 import org.apache.openejb.ApplicationException;
 import org.apache.openejb.InjectionProcessor;
@@ -62,17 +60,19 @@ import org.apache.openejb.util.LogCatego
 import org.apache.openejb.util.Logger;
 import org.apache.openejb.util.SafeToolkit;
 import org.apache.xbean.recipe.ConstructionException;
-import org.apache.xbean.recipe.ObjectRecipe;
-import org.apache.xbean.recipe.Option;
 
 public class SingletonInstanceManager {
     private static final Logger logger = Logger.getInstance(LogCategory.OPENEJB, "org.apache.openejb.util.resources");
 
     protected final SafeToolkit toolkit = SafeToolkit.getToolkit("SingletonInstanceManager");
     private SecurityService securityService;
+    private final SingletonContext sessionContext;
+    private final WebServiceContext webServiceContext;
 
     public SingletonInstanceManager(SecurityService securityService) {
         this.securityService = securityService;
+        sessionContext = new SingletonContext(this.securityService);
+        webServiceContext = (WebServiceContext) new EjbWsContext(sessionContext);
     }
 
     public Instance getInstance(final ThreadContext callContext) throws OpenEJBException {
@@ -134,30 +134,6 @@ public class SingletonInstanceManager {
 
         try {
             Context ctx = deploymentInfo.getJndiEnc();
-            SessionContext sessionContext;
-            // This needs to be synchronized as this code is multi-threaded.
-            // In between the lookup and the bind a bind may take place in another Thread.
-            // This is a fix for GERONIMO-3444
-            synchronized(this){
-                try {
-                    sessionContext = (SessionContext) ctx.lookup("comp/EJBContext");
-                } catch (NamingException e1) {
-                    sessionContext = createSessionContext();
-                    // TODO: This should work
-                    ctx.bind("comp/EJBContext", sessionContext);
-                }
-            }
-
-            // This is a fix for GERONIMO-3444
-            synchronized(this){
-                try {
-                    ctx.lookup("comp/WebServiceContext");
-                } catch (NamingException e) {
-                    WebServiceContext wsContext;
-                    wsContext = new EjbWsContext(sessionContext);
-                    ctx.bind("comp/WebServiceContext", wsContext);
-                }
-            }
 
             // Create bean instance
             InjectionProcessor injectionProcessor = new InjectionProcessor(beanClass, deploymentInfo.getInjections(), null, null, unwrap(ctx));
@@ -236,10 +212,6 @@ public class SingletonInstanceManager {
         }
     }
 
-    private SessionContext createSessionContext() {
-        return new SingletonContext(securityService);
-    }
-
     public void freeInstance(ThreadContext callContext) {
         CoreDeploymentInfo deploymentInfo = callContext.getDeploymentInfo();
         Data data = (Data) deploymentInfo.getContainerData();
@@ -288,7 +260,7 @@ public class SingletonInstanceManager {
 
     }
 
-    public void deploy(CoreDeploymentInfo deploymentInfo) {
+    public void deploy(CoreDeploymentInfo deploymentInfo) throws OpenEJBException {
         Data data = new Data();
         deploymentInfo.setContainerData(data);
 
@@ -315,6 +287,13 @@ public class SingletonInstanceManager {
             logger.error("Unable to register MBean ", e);
         }
 
+        try {
+            final Context context = deploymentInfo.getJndiEnc();
+            context.bind("comp/EJBContext", sessionContext);
+            context.bind("comp/WebServiceContext", webServiceContext);
+        } catch (NamingException e) {
+            throw new OpenEJBException("Failed to bind EJBContext", e);
+        }
     }
 
     public void undeploy(CoreDeploymentInfo deploymentInfo) {

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContainer.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContainer.java?rev=962617&r1=962616&r2=962617&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContainer.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateful/StatefulContainer.java Fri Jul  9 17:39:59 2010
@@ -106,6 +106,7 @@ public class StatefulContainer implement
 
     protected final Cache<Object, Instance> cache;
     private final ConcurrentHashMap<Object, Instance> checkedOutInstances = new ConcurrentHashMap<Object, Instance>();
+    private final SessionContext sessionContext;
 
     public StatefulContainer(Object id, SecurityService securityService, Cache<Object, Instance> cache) {
         this(id, securityService, cache, new Duration(0, TimeUnit.MILLISECONDS));
@@ -117,6 +118,7 @@ public class StatefulContainer implement
         this.cache = cache;
         cache.setListener(new StatefulCacheListener());
         this.accessTimeout = accessTimeout;
+        sessionContext = new StatefulContext(this.securityService, new StatefulUserTransaction(new EjbUserTransaction(), entityManagerRegistry));
     }
 
     private Map<Method, MethodType> getLifecycleMethodsOfInterface(CoreDeploymentInfo deploymentInfo) {
@@ -300,6 +302,12 @@ public class StatefulContainer implement
             logger.error("Unable to register MBean ", e);
         }
 
+        try {
+            final Context context = deploymentInfo.getJndiEnc();
+            context.bind("comp/EJBContext", sessionContext);
+        } catch (NamingException e) {
+            throw new OpenEJBException("Failed to bind EJBContext", e);
+        }
     }
 
     /**
@@ -595,24 +603,12 @@ public class StatefulContainer implement
             CoreDeploymentInfo deploymentInfo = callContext.getDeploymentInfo();
             Context ctx = deploymentInfo.getJndiEnc();
 
-            // Get or create the session context
-            SessionContext sessionContext;
-            synchronized (this) {
-                try {
-                    sessionContext = (SessionContext) ctx.lookup("comp/EJBContext");
-                } catch (NamingException e1) {
-                    StatefulUserTransaction userTransaction = new StatefulUserTransaction(new EjbUserTransaction(), entityManagerRegistry);
-                    sessionContext = new StatefulContext(securityService, userTransaction);
-                    ctx.bind("comp/EJBContext", sessionContext);
-                }
-            }
-
             // Create bean instance
             InjectionProcessor injectionProcessor = new InjectionProcessor(beanClass, deploymentInfo.getInjections(), null, null, unwrap(ctx));
             try {
                 if (SessionBean.class.isAssignableFrom(beanClass) || beanClass.getMethod("setSessionContext", SessionContext.class) != null) {
                     callContext.setCurrentOperation(Operation.INJECTION);
-                    injectionProcessor.setProperty("sessionContext", sessionContext);
+                    injectionProcessor.setProperty("sessionContext", this.sessionContext);
                 }
             } catch (NoSuchMethodException ignored) {
                 // bean doesn't have a setSessionContext method, so we don't need to inject one

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java?rev=962617&r1=962616&r2=962617&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/stateless/StatelessInstanceManager.java Fri Jul  9 17:39:59 2010
@@ -23,7 +23,6 @@ import java.rmi.RemoteException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.ThreadPoolExecutor;
@@ -37,11 +36,9 @@ import javax.ejb.SessionContext;
 import javax.ejb.ConcurrentAccessTimeoutException;
 import javax.naming.Context;
 import javax.naming.NamingException;
-import javax.xml.ws.WebServiceContext;
 import javax.management.ObjectName;
 import javax.management.MBeanServer;
 
-import org.apache.openejb.Injection;
 import org.apache.openejb.OpenEJBException;
 import org.apache.openejb.SystemException;
 import org.apache.openejb.ApplicationException;
@@ -168,38 +165,16 @@ public class StatelessInstanceManager {
         Operation originalOperation = callContext.getCurrentOperation();
         BaseContext.State[] originalAllowedStates = callContext.getCurrentAllowedStates();
 
+        final Data data = (Data) deploymentInfo.getContainerData();
         try {
             Context ctx = deploymentInfo.getJndiEnc();
-            SessionContext sessionContext;
-            // This needs to be synchronized as this code is multi-threaded.
-            // In between the lookup and the bind a bind may take place in another Thread.
-            // This is a fix for GERONIMO-3444
-            synchronized(this){
-                try {
-                    sessionContext = (SessionContext) ctx.lookup("comp/EJBContext");
-                } catch (NamingException e1) {
-                    sessionContext = createSessionContext(deploymentInfo);
-                    // TODO: This should work
-                    ctx.bind("comp/EJBContext", sessionContext);
-                }
-            }
-
-            // This is a fix for GERONIMO-3444
-            synchronized(this){
-                try {
-                    ctx.lookup("comp/WebServiceContext");
-                } catch (NamingException e) {
-                    WebServiceContext wsContext = new EjbWsContext(sessionContext);
-                    ctx.bind("comp/WebServiceContext", wsContext);
-                }
-            }
 
             // Create bean instance
             InjectionProcessor injectionProcessor = new InjectionProcessor(beanClass, deploymentInfo.getInjections(), null, null, unwrap(ctx));
             try {
                 if (SessionBean.class.isAssignableFrom(beanClass) || beanClass.getMethod("setSessionContext", SessionContext.class) != null) {
                     callContext.setCurrentOperation(Operation.INJECTION);
-                    injectionProcessor.setProperty("sessionContext", sessionContext);
+                    injectionProcessor.setProperty("sessionContext", data.sessionContext);
                 }
             } catch (NoSuchMethodException ignored) {
                 // bean doesn't have a setSessionContext method, so we don't need to inject one
@@ -260,16 +235,6 @@ public class StatelessInstanceManager {
         }
     }
 
-    private SessionContext createSessionContext(CoreDeploymentInfo deploymentInfo) {
-        final Data data = (Data) deploymentInfo.getContainerData();
-        
-        return new StatelessContext(securityService, new Flushable(){
-            public void flush() throws IOException {
-                data.getPool().flush();
-            }
-        });
-    }
-
     /**
      * All instances are removed from the pool in getInstance(...).  They are only
      * returned by the StatelessContainer via this method under two circumstances.
@@ -337,7 +302,7 @@ public class StatelessInstanceManager {
 
     }
 
-    public void deploy(CoreDeploymentInfo deploymentInfo) {
+    public void deploy(CoreDeploymentInfo deploymentInfo) throws OpenEJBException {
         Options options = new Options(deploymentInfo.getProperties());
 
         Duration accessTimeout = getDuration(options, "Timeout", this.accessTimeout, TimeUnit.MILLISECONDS);
@@ -357,10 +322,19 @@ public class StatelessInstanceManager {
 
         builder.setSupplier(new StatelessSupplier(deploymentInfo));
         builder.setExecutor(executor);
-        
+
+
         Data data = new Data(builder.build(), accessTimeout, closeTimeout);
         deploymentInfo.setContainerData(data);
 
+        try {
+            final Context context = deploymentInfo.getJndiEnc();
+            context.bind("comp/EJBContext", data.sessionContext);
+            context.bind("comp/WebServiceContext", new EjbWsContext(data.sessionContext));
+        } catch (NamingException e) {
+            throw new OpenEJBException("Failed to bind EJBContext", e);
+        }
+
         final int min = builder.getMin();
         long maxAge = builder.getMaxAge().getTime(TimeUnit.MILLISECONDS);
         double maxAgeOffset = builder.getMaxAgeOffset();
@@ -460,16 +434,22 @@ public class StatelessInstanceManager {
         deploymentInfo.setContainerData(null);
     }
 
-    private static final class Data {
+    private final class Data {
         private final Pool<Instance> pool;
         private final Duration accessTimeout;
         private final Duration closeTimeout;
         private final List<ObjectName> jmxNames = new ArrayList<ObjectName>();
+        private final SessionContext sessionContext;
 
         private Data(Pool<Instance> pool, Duration accessTimeout, Duration closeTimeout) {
             this.pool = pool;
             this.accessTimeout = accessTimeout;
             this.closeTimeout = closeTimeout;
+            this.sessionContext = new StatelessContext(securityService, new Flushable() {
+                public void flush() throws IOException {
+                    getPool().flush();
+                }
+            });
         }
 
         public Duration getAccessTimeout() {