[jboss-cvs] JBossAS SVN: r72624 - in projects/ejb3/trunk/transactions/src: test/java/org/jboss/ejb3/test/tx/bmt and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 23 10:25:06 EDT 2008


Author: wolfc
Date: 2008-04-23 10:25:05 -0400 (Wed, 23 Apr 2008)
New Revision: 72624

Modified:
   projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/TxUtil.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/bmt/StatefulBMTBean.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/bmt/unit/BMTUnitTestCase.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/MockEJBContext.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulBeanContext.java
   projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java
   projects/ejb3/trunk/transactions/src/test/resources/instance/jboss-aop.xml
Log:
EJBTHREE-1299: Some fixes for getUserTransaction allowance

Modified: projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/TxUtil.java
===================================================================
--- projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/TxUtil.java	2008-04-23 14:05:04 UTC (rev 72623)
+++ projects/ejb3/trunk/transactions/src/main/java/org/jboss/ejb3/tx/TxUtil.java	2008-04-23 14:25:05 UTC (rev 72624)
@@ -31,10 +31,13 @@
 import javax.transaction.Status;
 import javax.transaction.SystemException;
 import javax.transaction.TransactionManager;
+import javax.transaction.UserTransaction;
 
 import org.jboss.aop.Advisor;
 import org.jboss.aop.joinpoint.Invocation;
 import org.jboss.aop.joinpoint.MethodInvocation;
+import org.jboss.aspects.currentinvocation.CurrentInvocation;
+import org.jboss.ejb3.interceptors.container.BeanContext;
 import org.jboss.logging.Logger;
 import org.jboss.tm.TransactionManagerLocator;
 
@@ -48,12 +51,14 @@
 {
    private static final Logger log = Logger.getLogger(TxUtil.class);
    
-   protected static TransactionManager getTransactionManager()
+   // TODO: should really be protected
+   public static TransactionManager getTransactionManager()
    {
       return TransactionManagerLocator.getInstance().locate();
    }
 
-   protected static TransactionManagementType getTransactionManagementType(Advisor advisor)
+   // TODO: should really be protected
+   public static TransactionManagementType getTransactionManagementType(Advisor advisor)
    {
       TransactionManagement transactionManagement =  (TransactionManagement) advisor.resolveAnnotation(TransactionManagement.class);
       if (transactionManagement == null) return TransactionManagementType.CONTAINER;
@@ -92,12 +97,9 @@
       
    }
 
-   /**
-    * @param currentInvocation
-    * @return
-    */
-   public static boolean getRollbackOnly(Invocation currentInvocation)
+   public static boolean getRollbackOnly()
    {
+      Invocation currentInvocation = CurrentInvocation.getCurrentInvocation();
       Advisor advisor = currentInvocation.getAdvisor();
       // EJB1.1 11.6.1: Must throw IllegalStateException if BMT
       TransactionManagementType type = TxUtil.getTransactionManagementType(advisor);
@@ -151,4 +153,47 @@
       return getTxType(invocation.getAdvisor(), ((MethodInvocation) invocation).getActualMethod());
    }
 
+   public static UserTransaction getUserTransaction(BeanContext<?> ctx)
+   {
+      Invocation invocation = CurrentInvocation.getCurrentInvocation();
+      
+      // TODO: these checks are real ugly
+      // getUserTransaction is not allowed during construction and injection EJB 3 4.4.1 and EJB 3 4.5.2
+      // We're constructing the bean
+      if(invocation == null)
+         throw new IllegalStateException("It's not allowed to get the UserTransaction during construction and injection " + ctx);
+      // Is construction happening from within another bean?
+      if(ctx.getInstance() != invocation.getTargetObject())
+         throw new IllegalStateException("It's not allowed to get the UserTransaction during construction and injection " + ctx);
+      
+      Advisor advisor = invocation.getAdvisor();
+      TransactionManagementType type = TxUtil.getTransactionManagementType(advisor);
+      if (type != TransactionManagementType.BEAN) throw new IllegalStateException("Container " + advisor.getName() + ": it is illegal to inject UserTransaction into a CMT bean");
+
+      return new UserTransactionImpl();   
+   }
+   
+   public static void setRollbackOnly()
+   {
+      Advisor advisor = CurrentInvocation.getCurrentInvocation().getAdvisor();
+      // EJB1.1 11.6.1: Must throw IllegalStateException if BMT
+      TransactionManagementType type = TxUtil.getTransactionManagementType(advisor);
+      if (type != TransactionManagementType.CONTAINER) throw new IllegalStateException("Container " + advisor.getName() + ": it is illegal to call setRollbackOnly from BMT: " + type);
+
+      try
+      {
+         TransactionManager tm = TxUtil.getTransactionManager();
+
+         // The getRollbackOnly and setRollBackOnly method of the SessionContext interface should be used
+         // only in the session bean methods that execute in the context of a transaction.
+         if (tm.getTransaction() == null)
+            throw new IllegalStateException("setRollbackOnly() not allowed without a transaction.");
+
+         tm.setRollbackOnly();
+      }
+      catch (SystemException e)
+      {
+         log.warn("failed to set rollback only; ignoring", e);
+      }
+   }
 }

Modified: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/bmt/StatefulBMTBean.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/bmt/StatefulBMTBean.java	2008-04-23 14:05:04 UTC (rev 72623)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/bmt/StatefulBMTBean.java	2008-04-23 14:25:05 UTC (rev 72624)
@@ -21,13 +21,13 @@
  */
 package org.jboss.ejb3.test.tx.bmt;
 
+import javax.annotation.Resource;
+import javax.ejb.SessionContext;
 import javax.ejb.Stateful;
 import javax.ejb.TransactionManagement;
 import javax.ejb.TransactionManagementType;
 import javax.transaction.UserTransaction;
 
-import org.jboss.ejb3.tx.UserTransactionImpl;
-
 /**
  * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
  * @version $Revision: $
@@ -36,12 +36,19 @@
 @TransactionManagement(TransactionManagementType.BEAN)
 public class StatefulBMTBean
 {
+   private SessionContext ctx;
+   private Boolean isGetUserTransactionAllowed;
+   
    private UserTransaction getUserTransaction()
    {
-      // TODO: use the API
-      return new UserTransactionImpl();
+      return ctx.getUserTransaction();
    }
    
+   public boolean isGetUserTransactionAllowed()
+   {
+      return isGetUserTransactionAllowed;
+   }
+   
    public void normal() throws Exception
    {
       UserTransaction tx = getUserTransaction();
@@ -56,4 +63,24 @@
          tx.rollback();
       }
    }
+   
+   /**
+    * Note that the call to this method is hard-coded!
+    * @param ctx
+    */
+   @Resource
+   public void setSessionContext(SessionContext ctx)
+   {
+      this.ctx = ctx;
+      
+      try
+      {
+         ctx.getUserTransaction();
+         isGetUserTransactionAllowed = true;
+      }
+      catch(IllegalStateException e)
+      {
+         isGetUserTransactionAllowed = false;
+      }
+   }
 }

Modified: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/bmt/unit/BMTUnitTestCase.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/bmt/unit/BMTUnitTestCase.java	2008-04-23 14:05:04 UTC (rev 72623)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/bmt/unit/BMTUnitTestCase.java	2008-04-23 14:25:05 UTC (rev 72624)
@@ -29,6 +29,7 @@
 import org.jboss.ejb3.test.tx.mc.UnitTestBootstrap;
 import org.junit.After;
 import org.junit.AfterClass;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -71,6 +72,17 @@
    }
    
    @Test
+   public void testIsGetUserTransactionAllowed() throws Throwable
+   {
+      StatefulContainer<StatefulBMTBean> container = new StatefulContainer<StatefulBMTBean>("StatefulBMTBean", "Stateful Container", StatefulBMTBean.class);
+      
+      Object id = container.construct();
+      
+      Boolean isAllowed = container.invoke(id, "isGetUserTransactionAllowed");
+      Assert.assertFalse(isAllowed);
+   }
+   
+   @Test
    public void testNormal() throws Throwable
    {
       StatefulContainer<StatefulBMTBean> container = new StatefulContainer<StatefulBMTBean>("StatefulBMTBean", "Stateful Container", StatefulBMTBean.class);

Modified: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/MockEJBContext.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/MockEJBContext.java	2008-04-23 14:05:04 UTC (rev 72623)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/MockEJBContext.java	2008-04-23 14:25:05 UTC (rev 72624)
@@ -74,7 +74,7 @@
 
    public boolean getRollbackOnly() throws IllegalStateException
    {
-      return TxUtil.getRollbackOnly(getCurrentInvocation());
+      return TxUtil.getRollbackOnly();
    }
 
    public TimerService getTimerService() throws IllegalStateException
@@ -104,6 +104,6 @@
 
    public void setRollbackOnly() throws IllegalStateException
    {
-      throw new IllegalStateException("N/A");
+      TxUtil.setRollbackOnly();
    }
 }

Modified: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulBeanContext.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulBeanContext.java	2008-04-23 14:05:04 UTC (rev 72623)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulBeanContext.java	2008-04-23 14:25:05 UTC (rev 72624)
@@ -21,12 +21,25 @@
  */
 package org.jboss.ejb3.test.tx.common;
 
+import java.security.Identity;
+import java.security.Principal;
 import java.util.List;
+import java.util.Properties;
 import java.util.UUID;
 
+import javax.ejb.EJBHome;
+import javax.ejb.EJBLocalHome;
+import javax.ejb.EJBLocalObject;
+import javax.ejb.EJBObject;
+import javax.ejb.SessionContext;
+import javax.ejb.TimerService;
+import javax.transaction.UserTransaction;
+import javax.xml.rpc.handler.MessageContext;
+
 import org.jboss.aop.metadata.SimpleMetaData;
 import org.jboss.ejb3.cache.Identifiable;
 import org.jboss.ejb3.interceptors.container.DummyBeanContext;
+import org.jboss.ejb3.tx.TxUtil;
 
 /**
  * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
@@ -38,6 +51,95 @@
    private Object id = UUID.randomUUID();
    private SimpleMetaData metaData = new SimpleMetaData();
    
+   private SessionContext sessionContext = new SessionContext()
+   {
+      public <T> T getBusinessObject(Class<T> businessInterface) throws IllegalStateException
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public EJBLocalObject getEJBLocalObject() throws IllegalStateException
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public EJBObject getEJBObject() throws IllegalStateException
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public Class getInvokedBusinessInterface() throws IllegalStateException
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public MessageContext getMessageContext() throws IllegalStateException
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public Identity getCallerIdentity()
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public Principal getCallerPrincipal()
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public EJBHome getEJBHome()
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public EJBLocalHome getEJBLocalHome()
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public Properties getEnvironment()
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public boolean getRollbackOnly() throws IllegalStateException
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public TimerService getTimerService() throws IllegalStateException
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public UserTransaction getUserTransaction() throws IllegalStateException
+      {
+         return TxUtil.getUserTransaction(StatefulBeanContext.this);
+      }
+
+      public boolean isCallerInRole(Identity role)
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public boolean isCallerInRole(String roleName)
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public Object lookup(String name)
+      {
+         throw new RuntimeException("N/A");
+      }
+
+      public void setRollbackOnly() throws IllegalStateException
+      {
+         throw new RuntimeException("N/A");
+      }
+      
+   };
+   
    /**
     * @param instance
     * @param interceptors
@@ -56,4 +158,9 @@
    {
       return metaData;
    }
+   
+   protected SessionContext getSessionContext()
+   {
+      return sessionContext;
+   }
 }

Modified: projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java
===================================================================
--- projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java	2008-04-23 14:05:04 UTC (rev 72623)
+++ projects/ejb3/trunk/transactions/src/test/java/org/jboss/ejb3/test/tx/common/StatefulContainer.java	2008-04-23 14:25:05 UTC (rev 72624)
@@ -32,6 +32,7 @@
 
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
+import javax.ejb.SessionContext;
 
 import org.jboss.aop.Advisor;
 import org.jboss.aop.ClassAdvisor;
@@ -51,6 +52,7 @@
 import org.jboss.ejb3.interceptors.container.ContainerMethodInvocation;
 import org.jboss.ejb3.interceptors.container.DestructionInvocation;
 import org.jboss.ejb3.interceptors.lang.ClassHelper;
+import org.jboss.logging.Logger;
 
 /**
  * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
@@ -58,6 +60,8 @@
  */
 public class StatefulContainer<T> extends AbstractContainer<T, StatefulContainer<T>>
 {
+   private static final Logger log = Logger.getLogger(StatefulContainer.class);
+   
    private BeanContextFactory<T, StatefulContainer<T>> beanContextFactory = new BeanContextFactory<T, StatefulContainer<T>>()
    {
 
@@ -84,8 +88,20 @@
             Object initargs[] = null;
             T targetObject = getBeanClass().cast(advisor.invokeNew(initargs, idx));
             
-            BeanContext<T> component = new StatefulBeanContext<T>(targetObject, ejb3Interceptors);
+            StatefulBeanContext<T> component = new StatefulBeanContext<T>(targetObject, ejb3Interceptors);
             
+            // Do injection (sort of)
+            try
+            {
+               Method method = getBeanClass().getMethod("setSessionContext", SessionContext.class);
+               SessionContext sessionContext = component.getSessionContext();
+               method.invoke(targetObject, sessionContext);
+            }
+            catch(NoSuchMethodException e)
+            {
+               log.debug("no method found setSessionContext");
+            }
+            
             // Do lifecycle callbacks
             Interceptor interceptors[] = createLifecycleInterceptors(component, PostConstruct.class);
             

Modified: projects/ejb3/trunk/transactions/src/test/resources/instance/jboss-aop.xml
===================================================================
--- projects/ejb3/trunk/transactions/src/test/resources/instance/jboss-aop.xml	2008-04-23 14:05:04 UTC (rev 72623)
+++ projects/ejb3/trunk/transactions/src/test/resources/instance/jboss-aop.xml	2008-04-23 14:25:05 UTC (rev 72624)
@@ -16,6 +16,7 @@
    
    <domain name="Stateful Container">
       <bind pointcut="execution(public * *->*(..))">
+         <interceptor-ref name="org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor"/>
          <!--interceptor-ref name="org.jboss.aspects.tx.TxPropagationInterceptor"/-->
          <interceptor-ref name="org.jboss.ejb3.tx.CMTTxInterceptorFactory"/>
          <interceptor-ref name="org.jboss.ejb3.test.tx.common.StatefulInstanceInterceptor"/>




More information about the jboss-cvs-commits mailing list