[jboss-cvs] JBossAS SVN: r92052 - in projects/ejb3/trunk/core/src: main/java/org/jboss/ejb3/service and 4 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Aug 6 01:44:51 EDT 2009


Author: ALRubinger
Date: 2009-08-06 01:44:51 -0400 (Thu, 06 Aug 2009)
New Revision: 92052

Added:
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/TransactionalServiceBean.java
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/TransactionalServiceLocal.java
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/unit/
   projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/unit/TransactionTestCase.java
Modified:
   projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/EJBContainer.java
   projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/service/ServiceContainer.java
   projects/ejb3/trunk/core/src/main/resources/ejb3-interceptors-aop.xml
Log:
[EJBTHREE-1738] Ensure start() lifecycle is called in the context of a Transaction; part one of the issue.  Still to resolve Security Context (@RunAs) and follow up with true integration tests as reported

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/EJBContainer.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/EJBContainer.java	2009-08-06 05:12:21 UTC (rev 92051)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/EJBContainer.java	2009-08-06 05:44:51 UTC (rev 92052)
@@ -890,7 +890,10 @@
       */
    }
 
-   public final void start() throws Exception
+   /**
+    * This should have been final, but ServiceContainer wants to butt in.
+    */
+   public void start() throws Exception
    {
       this.lockedStart();
       
@@ -926,7 +929,10 @@
       log.info("STARTED EJB: " + beanClass.getName() + " ejbName: " + ejbName);
    }
 
-   public final void stop() throws Exception
+   /**
+    * This should have been final, but ServiceContainer wants to butt in.
+    */
+   public void stop() throws Exception
    {
       // Wait for active invocations to complete - and block new invocations
       this.blockInvocations();

Modified: projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/service/ServiceContainer.java
===================================================================
--- projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/service/ServiceContainer.java	2009-08-06 05:12:21 UTC (rev 92051)
+++ projects/ejb3/trunk/core/src/main/java/org/jboss/ejb3/service/ServiceContainer.java	2009-08-06 05:44:51 UTC (rev 92052)
@@ -260,8 +260,9 @@
 
          injectDependencies(beanContext);
 
+         invokePostConstruct(beanContext);
+         
          timerServiceFactory.restoreTimerService(timerService);
-         invokeOptionalMethod(METHOD_NAME_LIFECYCLE_CALLBACK_START);
       }
       catch (Exception e)
       {
@@ -281,9 +282,8 @@
    @Override
    protected void lockedStop() throws Exception
    {
-      // Make the lifecycle callback
-      invokeOptionalMethod(METHOD_NAME_LIFECYCLE_CALLBACK_STOP);
-
+      invokePreDestroy(beanContext);
+      
       if (timerService != null)
       {
          timerServiceFactory.suspendTimerService(timerService);
@@ -338,6 +338,31 @@
       });
    }
 
+   private Object invokeOptionalBusinessMethod(String methodName) throws Exception
+   {
+      try
+      {
+         Method method = getBeanClass().getMethod(methodName);
+         try
+         {
+            return localInvoke(method, null);
+         }
+         catch(Throwable t)
+         {
+            throw sanitize(t);
+         }
+      }
+      catch (SecurityException e)
+      {
+         throw new RuntimeException(e);
+      }
+      catch (NoSuchMethodException e)
+      {
+         // ignore
+         return null;
+      }
+   }
+   
    /**
     * Invoke a method on the singleton without a specific security or transaction context.
     * 
@@ -400,16 +425,6 @@
 
    }
 
-   public void invokePostConstruct(BeanContext beanContext, Object[] params)
-   {
-      //Ignore
-   }
-
-   public void invokePreDestroy(BeanContext beanContext)
-   {
-      //Ignore
-   }
-
    public Object localInvoke(Object id, Method method, Object[] args) throws Throwable
    {
       return localInvoke(method, args);
@@ -797,9 +812,35 @@
       throw new RuntimeException("Don't do this");
    }
 
+   private Exception sanitize(Throwable t)
+   {
+      if(t instanceof Error)
+         throw (Error) t;
+      return (Exception) t;
+   }
+   
    @Inject
    public void setTimerServiceFactory(TimerServiceFactory factory)
    {
       this.timerServiceFactory = factory;
    }
+   
+   @Override
+   public void start() throws Exception
+   {
+      super.start();
+      
+      // EJBTHREE-1738: method start/stop need to have a tx context
+      invokeOptionalBusinessMethod(METHOD_NAME_LIFECYCLE_CALLBACK_START);
+   }
+   
+   @Override
+   public void stop() throws Exception
+   {
+      // Make the lifecycle callback
+      // EJBTHREE-1738: method start/stop need to have a tx context
+      invokeOptionalBusinessMethod(METHOD_NAME_LIFECYCLE_CALLBACK_STOP);
+
+      super.stop();
+   }
 }

Modified: projects/ejb3/trunk/core/src/main/resources/ejb3-interceptors-aop.xml
===================================================================
--- projects/ejb3/trunk/core/src/main/resources/ejb3-interceptors-aop.xml	2009-08-06 05:12:21 UTC (rev 92051)
+++ projects/ejb3/trunk/core/src/main/resources/ejb3-interceptors-aop.xml	2009-08-06 05:44:51 UTC (rev 92052)
@@ -383,6 +383,17 @@
    </domain>
 
    <domain name="Service Bean" extends="Intercepted Bean" inheritBindings="true">
+      <!-- Override the default LifecycleCallbackStack so a tx context is set -->
+      <stack name="LifecycleCallbackStack">
+         <interceptor-ref name="org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor"/>
+         <advice name="setup" aspect="InvocationContextInterceptor"/>
+         <!-- TODO: EJB 3.1 PFD 4.8.3: if REQUIRED then interpret as REQUIRES_NEW -->
+         <!-- 
+         <interceptor-ref name="org.jboss.ejb3.tx.CMTTxInterceptorFactory"/>
+         <interceptor-ref name="org.jboss.ejb3.tx.BMTTxInterceptorFactory"/>
+         -->
+      </stack>
+   
       <bind pointcut="execution(public * *->*(..))">
          <interceptor-ref name="org.jboss.ejb3.ENCPropagationInterceptor"/>
       </bind>

Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/TransactionalServiceBean.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/TransactionalServiceBean.java	                        (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/TransactionalServiceBean.java	2009-08-06 05:44:51 UTC (rev 92052)
@@ -0,0 +1,87 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.ejbthree1738;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+
+import org.jboss.ejb3.annotation.Service;
+import org.jboss.logging.Logger;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Service
+public class TransactionalServiceBean implements TransactionalServiceLocal
+{
+   private static final Logger log = Logger.getLogger(TransactionalServiceBean.class);
+   
+   @Resource(mappedName="java:/TransactionManager")
+   private TransactionManager tm;
+   
+   public static int postConstructs = 0;
+   
+   private boolean startCalledInTx = false;
+   
+   @PostConstruct
+   public void postConstruct()
+   {
+      log.info("postConstruct");
+      //new Exception("postConstruct").printStackTrace();
+      postConstructs++;
+   }
+   
+   /**
+    * magic start
+    */
+   public void start()
+   {
+      log.info("start");
+      //new Exception("start").printStackTrace();
+      try
+      {
+         final Transaction tx = tm.getTransaction();
+         log.info("tx = " + tx);
+         if(tx == null){
+            throw new IllegalStateException("EJBTHREE-1738: was expecting a tx to be present");
+         }
+         startCalledInTx = true;
+      }
+      catch(SystemException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+   
+   /*
+    * (non-Javadoc)
+    * @see org.jboss.ejb3.core.test.ejbthree1738.TransactionalServiceLocal#isStartCalledInTx()
+    */
+   public boolean isStartCalledInTx()
+   {
+      return this.startCalledInTx;
+   }
+}

Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/TransactionalServiceLocal.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/TransactionalServiceLocal.java	                        (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/TransactionalServiceLocal.java	2009-08-06 05:44:51 UTC (rev 92052)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.ejbthree1738;
+
+import javax.ejb.Local;
+
+/**
+ * 
+ * TransactionalServiceLocal
+ *
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Local
+public interface TransactionalServiceLocal
+{
+   /**
+    * Returns whether or not the start() lifecycle callback 
+    * was made in the presence of a transaction
+    * @return
+    */
+   boolean isStartCalledInTx();
+}

Added: projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/unit/TransactionTestCase.java
===================================================================
--- projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/unit/TransactionTestCase.java	                        (rev 0)
+++ projects/ejb3/trunk/core/src/test/java/org/jboss/ejb3/core/test/ejbthree1738/unit/TransactionTestCase.java	2009-08-06 05:44:51 UTC (rev 92052)
@@ -0,0 +1,53 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.core.test.ejbthree1738.unit;
+
+import org.jboss.ejb3.core.test.common.AbstractEJB3TestCase;
+import org.jboss.ejb3.core.test.ejbthree1738.TransactionalServiceBean;
+import org.jboss.ejb3.core.test.ejbthree1738.TransactionalServiceLocal;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class TransactionTestCase extends AbstractEJB3TestCase
+{
+   @BeforeClass
+   public static void beforeClass() throws Exception
+   {
+      AbstractEJB3TestCase.beforeClass();
+
+      deploySessionEjb(TransactionalServiceBean.class);
+   }
+
+   @Test
+   public void test1() throws Exception
+   {
+      TransactionalServiceLocal bean = lookup("TransactionalServiceBean/local", TransactionalServiceLocal.class);
+
+      // Ensure start() was called in the context of a transaction
+      Assert.assertTrue(bean.isStartCalledInTx());
+   }
+}




More information about the jboss-cvs-commits mailing list