[jbpm-commits] JBoss JBPM SVN: r3635 - in jbpm3/trunk/modules: core/src/main/java/org/jbpm/command/impl and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Wed Jan 14 00:51:14 EST 2009


Author: alex.guizar at jboss.com
Date: 2009-01-14 00:51:14 -0500 (Wed, 14 Jan 2009)
New Revision: 3635

Modified:
   jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmConfiguration.java
   jbpm3/trunk/modules/core/src/main/java/org/jbpm/command/impl/CommandServiceImpl.java
   jbpm3/trunk/modules/enterprise/src/main/java/org/jbpm/ejb/impl/CommandServiceBean.java
   jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/jta/JtaDbPersistenceTest.java
Log:
adapt jta persistence test to exercise CMT as well as BMT
context not marked for rollback after exception in command service

Modified: jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmConfiguration.java
===================================================================
--- jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmConfiguration.java	2009-01-13 18:22:21 UTC (rev 3634)
+++ jbpm3/trunk/modules/core/src/main/java/org/jbpm/JbpmConfiguration.java	2009-01-14 05:51:14 UTC (rev 3635)
@@ -48,20 +48,19 @@
 
 /**
  * configuration of one jBPM instance.
- * 
  * <p>
- * During process execution, jBPM might need to use some services. A JbpmConfiguration contains the knowledge on how to
- * create those services.
+ * During process execution, jBPM might need to use some services. A JbpmConfiguration contains the
+ * knowledge on how to create those services.
  * </p>
- * 
  * <p>
- * A JbpmConfiguration is a thread safe object and serves as a factory for {@link org.jbpm.JbpmContext}s, which means
- * one JbpmConfiguration can be used to create {@link org.jbpm.JbpmContext}s for all threads. The single
- * JbpmConfiguration can be maintained in a static member or in the JNDI tree if that is available.
+ * A JbpmConfiguration is a thread safe object and serves as a factory for
+ * {@link org.jbpm.JbpmContext}s, which means one JbpmConfiguration can be used to create
+ * {@link org.jbpm.JbpmContext}s for all threads. The single JbpmConfiguration can be maintained in
+ * a static member or in the JNDI tree if that is available.
  * </p>
- * 
  * <p>
  * A JbpmConfiguration can be obtained in following ways:
+ * </p>
  * <ul>
  * <li>from a resource (by default <code>jbpm.cfg.xml</code> is used):
  * 
@@ -69,7 +68,9 @@
  * JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
  * </pre>
  * 
+ * <p>
  * or
+ * </p>
  * 
  * <pre>
  * String myXmlResource = &quot;...&quot;;
@@ -88,8 +89,8 @@
  * </pre>
  * 
  * </li>
- * <li>By specifying a custom implementation of an object factory. This can be used to specify a JbpmConfiguration in
- * other bean-style notations such as used by JBoss Microcontainer or Spring.
+ * <li>By specifying a custom implementation of an object factory. This can be used to specify a
+ * JbpmConfiguration in other bean-style notations such as used by JBoss Microcontainer or Spring.
  * 
  * <pre>
  * ObjectFactory of = new &lt;i&gt;MyCustomObjectFactory&lt;/i&gt;();
@@ -99,10 +100,9 @@
  * 
  * </li>
  * </ul>
- * </p>
- * 
  * <p>
- * JbpmConfigurations can be configured using a spring-like XML notation (in relax ng compact notation):
+ * JbpmConfigurations can be configured using a spring-like XML notation (in relax ng compact
+ * notation):
  * </p>
  * 
  * <pre>
@@ -234,20 +234,19 @@
  * </pre>
  * 
  * </p>
- * 
  * <p>
  * Other configuration properties
+ * </p>
  * <table>
  * <tr>
  * <td>jbpm.files.dir</td>
- * <td></td>
+ * <td/>
  * </tr>
  * <tr>
  * <td>jbpm.types</td>
- * <td></td>
+ * <td/>
  * </tr>
  * </table>
- * </p>
  */
 public class JbpmConfiguration implements Serializable
 {
@@ -281,46 +280,37 @@
       {
         resource = "jbpm.cfg.xml";
       }
-
-      instance = (JbpmConfiguration)instances.get(resource);
+      instance = (JbpmConfiguration) instances.get(resource);
       if (instance == null)
       {
-
         if (defaultObjectFactory != null)
         {
           log.debug("creating jbpm configuration from given default object factory '" + defaultObjectFactory + "'");
           instance = new JbpmConfiguration(defaultObjectFactory);
-
         }
         else
         {
-
           try
           {
             log.info("using jbpm configuration resource '" + resource + "'");
             InputStream jbpmCfgXmlStream = ClassLoaderUtil.getJbpmConfigurationStream(resource);
-
-            // if a custom resource is to be used, but is not found in the
-            // classpath
-            // log a warning (otherwise, users who want to load custom stuff
-            // will be confused if the resource is not found and not loaded,
-            // without
-            // any notice)
+            /*
+             * if a custom resource is to be used, but is not found in the classpath log a warning
+             * (otherwise, users who want to load custom stuff will be confused if the resource is
+             * not found and not loaded, without any notice)
+             */
             if (jbpmCfgXmlStream == null && !"jbpm.cfg.xml".equals(resource))
             {
               log.warn("jbpm configuration resource '" + resource + "' is not available");
             }
-
             ObjectFactory objectFactory = parseObjectFactory(jbpmCfgXmlStream);
             instance = createJbpmConfiguration(objectFactory);
-
           }
           catch (RuntimeException e)
           {
             throw new JbpmException("couldn't parse jbpm configuration from resource '" + resource + "'", e);
           }
         }
-
         instances.put(resource, instance);
       }
     }
@@ -380,7 +370,7 @@
     // now we make the bean jbpm.configuration always availble
     if (objectFactory instanceof ObjectFactoryImpl)
     {
-      ObjectFactoryImpl objectFactoryImpl = (ObjectFactoryImpl)objectFactory;
+      ObjectFactoryImpl objectFactoryImpl = (ObjectFactoryImpl) objectFactory;
       ObjectInfo jbpmConfigurationInfo = new ValueInfo("jbpmConfiguration", jbpmConfiguration);
       objectFactoryImpl.addObjectInfo(jbpmConfigurationInfo);
 
@@ -400,7 +390,7 @@
       return true;
     }
     Object object = objectFactory.createObject("jbpm.hide.stale.object.exceptions");
-    return object instanceof Boolean ? ((Boolean)object).booleanValue() : true;
+    return object instanceof Boolean ? ((Boolean) object).booleanValue() : true;
   }
 
   public static JbpmConfiguration parseInputStream(InputStream inputStream)
@@ -429,7 +419,7 @@
 
   public JbpmContext createJbpmContext(String name)
   {
-    JbpmContext jbpmContext = (JbpmContext)objectFactory.createObject(name);
+    JbpmContext jbpmContext = (JbpmContext) objectFactory.createObject(name);
     jbpmContext.jbpmConfiguration = this;
     jbpmContextCreated(jbpmContext);
     return jbpmContext;
@@ -460,7 +450,7 @@
     ProcessClassLoaderFactory factory = null;
     if (Configs.hasObject("jbpm.processClassLoader"))
     {
-      factory = (ProcessClassLoaderFactory)Configs.getObject("jbpm.processClassLoader");
+      factory = (ProcessClassLoaderFactory) Configs.getObject("jbpm.processClassLoader");
     }
     else
     {
@@ -509,22 +499,22 @@
 
     public static String getString(String name)
     {
-      return (String)getObject(name);
+      return (String) getObject(name);
     }
 
     public static long getLong(String name)
     {
-      return ((Long)getObject(name)).longValue();
+      return ((Long) getObject(name)).longValue();
     }
 
     public static int getInt(String name)
     {
-      return ((Integer)getObject(name)).intValue();
+      return ((Integer) getObject(name)).intValue();
     }
 
     public static boolean getBoolean(String name)
     {
-      return ((Boolean)getObject(name)).booleanValue();
+      return ((Boolean) getObject(name)).booleanValue();
     }
   }
 
@@ -539,7 +529,8 @@
     try
     {
       Services services = jbpmContext.getServices();
-      DbPersistenceServiceFactory persistenceServiceFactory = (DbPersistenceServiceFactory)services.getServiceFactory(Services.SERVICENAME_PERSISTENCE);
+      DbPersistenceServiceFactory persistenceServiceFactory = (DbPersistenceServiceFactory) services
+          .getServiceFactory(Services.SERVICENAME_PERSISTENCE);
       persistenceServiceFactory.cleanSchema();
     }
     finally
@@ -559,7 +550,8 @@
     try
     {
       Services services = jbpmContext.getServices();
-      DbPersistenceServiceFactory persistenceServiceFactory = (DbPersistenceServiceFactory)services.getServiceFactory(Services.SERVICENAME_PERSISTENCE);
+      DbPersistenceServiceFactory persistenceServiceFactory = (DbPersistenceServiceFactory) services
+          .getServiceFactory(Services.SERVICENAME_PERSISTENCE);
       persistenceServiceFactory.createSchema();
     }
     finally
@@ -580,7 +572,8 @@
     try
     {
       Services services = jbpmContext.getServices();
-      DbPersistenceServiceFactory persistenceServiceFactory = (DbPersistenceServiceFactory)services.getServiceFactory(Services.SERVICENAME_PERSISTENCE);
+      DbPersistenceServiceFactory persistenceServiceFactory = (DbPersistenceServiceFactory) services
+          .getServiceFactory(Services.SERVICENAME_PERSISTENCE);
       persistenceServiceFactory.dropSchema();
     }
     finally
@@ -624,7 +617,7 @@
         Iterator iter = serviceFactories.values().iterator();
         while (iter.hasNext())
         {
-          ServiceFactory serviceFactory = (ServiceFactory)iter.next();
+          ServiceFactory serviceFactory = (ServiceFactory) iter.next();
           serviceFactory.close();
         }
       }
@@ -641,14 +634,14 @@
     Stack stack = getJbpmConfigurationStack();
     if (!stack.isEmpty())
     {
-      currentJbpmConfiguration = (JbpmConfiguration)stack.peek();
+      currentJbpmConfiguration = (JbpmConfiguration) stack.peek();
     }
     return currentJbpmConfiguration;
   }
 
   static synchronized Stack getJbpmConfigurationStack()
   {
-    Stack stack = (Stack)jbpmConfigurationsStacks.get();
+    Stack stack = (Stack) jbpmConfigurationsStacks.get();
     if (stack == null)
     {
       stack = new Stack();
@@ -673,14 +666,14 @@
     Stack stack = getJbpmContextStack();
     if (!stack.isEmpty())
     {
-      currentJbpmContext = (JbpmContext)stack.peek();
+      currentJbpmContext = (JbpmContext) stack.peek();
     }
     return currentJbpmContext;
   }
 
   Stack getJbpmContextStack()
   {
-    Stack stack = (Stack)jbpmContextStacks.get();
+    Stack stack = (Stack) jbpmContextStacks.get();
     if (stack == null)
     {
       stack = new Stack();
@@ -699,12 +692,12 @@
     Stack stack = getJbpmContextStack();
     if (stack.isEmpty())
     {
-      throw new JbpmException("closed JbpmContext more then once... check your try-finally's around JbpmContexts blocks");
+      throw new JbpmException("closed JbpmContext more than once... check your try-finally's around JbpmContexts blocks");
     }
-    JbpmContext popped = (JbpmContext)stack.pop();
+    JbpmContext popped = (JbpmContext) stack.pop();
     if (jbpmContext != popped)
     {
-      throw new JbpmException("closed JbpmContext in different order then they were created... check your try-finally's around JbpmContexts blocks");
+      throw new JbpmException("closed JbpmContext in some order that differs from creation... check your try-finally's around JbpmContexts blocks");
     }
   }
 
@@ -731,11 +724,12 @@
     {
       try
       {
-        jobExecutor = (JobExecutor)this.objectFactory.createObject("jbpm.job.executor");
+        jobExecutor = (JobExecutor) this.objectFactory.createObject("jbpm.job.executor");
       }
       catch (ClassCastException e)
       {
-        throw new JbpmException("jbpm configuration object under key 'jbpm.job.executor' is not a " + JobExecutor.class.getName(), e);
+        throw new JbpmException("jbpm configuration object under key 'jbpm.job.executor' is not a "
+            + JobExecutor.class.getName(), e);
       }
     }
     return jobExecutor;

Modified: jbpm3/trunk/modules/core/src/main/java/org/jbpm/command/impl/CommandServiceImpl.java
===================================================================
--- jbpm3/trunk/modules/core/src/main/java/org/jbpm/command/impl/CommandServiceImpl.java	2009-01-13 18:22:21 UTC (rev 3634)
+++ jbpm3/trunk/modules/core/src/main/java/org/jbpm/command/impl/CommandServiceImpl.java	2009-01-14 05:51:14 UTC (rev 3635)
@@ -35,17 +35,20 @@
  * Provide services for accessing the jBPM engine. Access is currently provided through a set of
  * {@link org.jbpm.command.Command} derived operations.
  * 
- * @author Jim Rigsbee, Tom Baeyens
+ * @author Jim Rigsbee
+ * @author Tom Baeyens
  */
 public class CommandServiceImpl implements CommandService, Serializable
 {
+
+  protected final JbpmConfiguration jbpmConfiguration;
+
   private static final long serialVersionUID = 1L;
 
-  protected JbpmConfiguration jbpmConfiguration = null;
-
   /**
-   * Establish an instance of the command service with a particular jBPM configuration which provides connectivity to
-   * the jBPM engine and its related services including the persistence manager.
+   * Establish an instance of the command service with a particular jBPM configuration which
+   * provides connectivity to the jBPM engine and its related services including the persistence
+   * manager.
    * 
    * @param jbpmConfiguration jBPM Configuration
    */
@@ -55,29 +58,34 @@
   }
 
   /**
-   * Executes command based on its current context. Each command contains the appropriate context information such as
-   * token, process instance, etc. to insure that the operation is carried out on the proper graph object.
+   * Executes command based on its current context. Each command contains the appropriate context
+   * information such as token, process instance, etc. to insure that the operation is carried out
+   * on the proper graph object.
    * 
    * @param command jBPM engine command to execute
    */
   public Object execute(Command command)
   {
-    Object result = null;
     JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
     try
     {
       log.debug("executing " + command);
-      result = command.execute(jbpmContext);
+      return command.execute(jbpmContext);
     }
+    catch (RuntimeException e)
+    {
+      jbpmContext.setRollbackOnly();
+      throw e;
+    }
     catch (Exception e)
     {
-      throw new JbpmException("couldn't execute " + command, e);
+      jbpmContext.setRollbackOnly();
+      throw new JbpmException("failed to execute " + command, e);
     }
     finally
     {
       jbpmContext.close();
     }
-    return result;
   }
 
   private static final Log log = LogFactory.getLog(CommandServiceImpl.class);

Modified: jbpm3/trunk/modules/enterprise/src/main/java/org/jbpm/ejb/impl/CommandServiceBean.java
===================================================================
--- jbpm3/trunk/modules/enterprise/src/main/java/org/jbpm/ejb/impl/CommandServiceBean.java	2009-01-13 18:22:21 UTC (rev 3634)
+++ jbpm3/trunk/modules/enterprise/src/main/java/org/jbpm/ejb/impl/CommandServiceBean.java	2009-01-14 05:51:14 UTC (rev 3635)
@@ -37,18 +37,15 @@
 import org.jbpm.command.CommandService;
 import org.jbpm.msg.jms.JmsMessageServiceFactory;
 import org.jbpm.persistence.jta.JtaDbPersistenceServiceFactory;
-import org.jbpm.tx.TxService;
 
 /**
- * Stateless session bean that executes {@linkplain Command commands} by
- * calling their {@link Command#execute(JbpmContext) execute} method on
- * a separate {@link JbpmContext jBPM context}.
- * 
- * <h3>Environment</h3>
- * 
- * <p>The environment entries and resources available for customization are
- * summarized in the table below.</p>
- * 
+ * Stateless session bean that executes {@linkplain Command commands} by calling their
+ * {@link Command#execute(JbpmContext) execute} method on a separate {@link JbpmContext jBPM
+ * context}. <h3>Environment</h3>
+ * <p>
+ * The environment entries and resources available for customization are summarized in the table
+ * below.
+ * </p>
  * <table border="1">
  * <tr>
  * <th>Name</th>
@@ -58,44 +55,36 @@
  * <tr>
  * <td><code>JbpmCfgResource</code></td>
  * <td>Environment Entry</td>
- * <td>The classpath resource from which to read the {@linkplain 
- * JbpmConfiguration jBPM configuration}. Optional, defaults to <code>
- * jbpm.cfg.xml</code>.
- * </td>
+ * <td>The classpath resource from which to read the {@linkplain JbpmConfiguration jBPM
+ * configuration}. Optional, defaults to <code>jbpm.cfg.xml</code>.</td>
  * </tr>
  * <tr>
  * <td><code>ejb/TimerEntityBean</code></td>
  * <td>EJB Reference</td>
- * <td>Link to the local {@linkplain TimerEntityBean entity bean} that
- * implements the scheduler service. Required for processes that contain
- * timers.
- * </td>
+ * <td>Link to the local {@linkplain TimerEntityBean entity bean} that implements the scheduler
+ * service. Required for processes that contain timers.</td>
  * </tr>
  * <tr>
  * <td><code>jdbc/JbpmDataSource</code></td>
  * <td>Resource Manager Reference</td>
  * <td>Logical name of the data source that provides JDBC connections to the
- * {@linkplain JtaDbPersistenceServiceFactory persistence service}. Must match
- * the <code>hibernate.connection.datasource</code> property in the Hibernate 
- * configuration file.
- * </td>
+ * {@linkplain JtaDbPersistenceServiceFactory persistence service}. Must match the
+ * <code>hibernate.connection.datasource</code> property in the Hibernate configuration file.</td>
  * </tr>
  * <tr>
  * <td><code>jms/JbpmConnectionFactory</code></td>
  * <td>Resource Manager Reference</td>
- * <td>Logical name of the factory that provides JMS connections to the 
- * {@linkplain JmsMessageServiceFactory message service}. Required for 
- * processes that contain asynchronous continuations.
- * </td>
+ * <td>Logical name of the factory that provides JMS connections to the
+ * {@linkplain JmsMessageServiceFactory message service}. Required for processes that contain
+ * asynchronous continuations.</td>
  * </tr>
  * <tr>
  * <td><code>jms/JobQueue</code></td>
  * <td>Message Destination Reference</td>
- * <td>The message service sends job messages to the queue referenced here.
- * To ensure this is the same queue from which the {@linkplain JobListenerBean
- * job listener bean} receives messages, the <code>message-destination-link
- * </code> points to a common logical destination, <code>JobQueue</code>.
- * </td>
+ * <td>The message service sends job messages to the queue referenced here. To ensure this is the
+ * same queue from which the {@linkplain JobListenerBean job listener bean} receives messages, the
+ * <code>message-destination-link
+ * </code> points to a common logical destination, <code>JobQueue</code>.</td>
  * </tr>
  * </table>
  * 
@@ -103,75 +92,87 @@
  * @author Tom Baeyens
  * @author Alejandro Guizar
  */
-public class CommandServiceBean implements SessionBean, CommandService {
+public class CommandServiceBean implements SessionBean, CommandService
+{
 
+  private SessionContext sessionContext;
+  private JbpmConfiguration jbpmConfiguration;
+
   private static final long serialVersionUID = 1L;
+  private static final Log log = LogFactory.getLog(CommandServiceBean.class);
 
-  JbpmConfiguration jbpmConfiguration = null;
-  SessionContext sessionContext = null;
-
-  /**
-   * creates a command service that will be used to execute the commands that 
-   * are passed in the execute method.  The command service will be build by 
-   * creating a jbpm configuration.  In case the environment key JbpmCfgResource
-   * is specified for this bean, that value will be used to resolve the jbpm 
-   * configuration file as a resource.  If that key is not configured, the default 
-   * jbpm configuration file will be used (jbpm.cfg.xml).
-   */
-  public void ejbCreate() throws CreateException {
-    String jbpmCfgResource = null; 
-    try {
-      log.debug("getting jbpm configuration resource from the environment properties");
-      Context initial = new InitialContext();
-      jbpmCfgResource = (String) initial.lookup("java:comp/env/JbpmCfgResource");
-    } catch (NamingException e) {
-      log.debug("couldn't find configuration property JbpmCfgResource through JNDI");
-    }
-
-    if (log.isDebugEnabled()) {
-      if (jbpmCfgResource==null) {
-        log.debug("getting default jbpm configuration resource (jbpm.cfg.xml)");
-      } else {
-        log.debug("getting jbpm configuration from resource "+jbpmCfgResource);
-      }
-    }
-
-    jbpmConfiguration = JbpmConfiguration.getInstance(jbpmCfgResource);
-  }
-
-  public Object execute(Command command) {
-    Object result = null;
+  public Object execute(Command command)
+  {
     JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
-    try {
+    try
+    {
       log.debug("executing " + command);
-      result = command.execute(jbpmContext);
-      // check whether command requested a rollback
-      TxService txService = jbpmContext.getServices().getTxService();
-      if (txService.isRollbackOnly()) {
-        sessionContext.setRollbackOnly();
-      }
-    } catch (RuntimeException e) {
+      return command.execute(jbpmContext);
+    }
+    catch (RuntimeException e)
+    {
+      sessionContext.setRollbackOnly();
       throw e;
-    } catch (Exception e) {
+    }
+    catch (Exception e)
+    {
+      sessionContext.setRollbackOnly();
       throw new JbpmException("failed to execute " + command, e);
-    } finally {
+    }
+    finally
+    {
       jbpmContext.close();
     }
-    return result;
   }
 
-  public void setSessionContext(SessionContext sessionContext) {
+  public void setSessionContext(SessionContext sessionContext)
+  {
     this.sessionContext = sessionContext;
   }
 
-  public void ejbRemove() {
+  /**
+   * Creates a command service that will execute the commands passed to the execute method. The
+   * command service will be build by creating a jBPM configuration.
+   */
+  public void ejbCreate() throws CreateException
+  {
+    try
+    {
+      String resource = lookup("java:comp/env/JbpmCfgResource");
+      jbpmConfiguration = resource != null ? JbpmConfiguration.getInstance(resource)
+          : JbpmConfiguration.getInstance();
+    }
+    catch (NamingException e)
+    {
+      log.debug("could not retrieve configuration resource from environment, using default", e);
+      jbpmConfiguration = JbpmConfiguration.getInstance();
+    }
+  }
+
+  private static <T> T lookup(String name) throws NamingException
+  {
+    Context initial = new InitialContext();
+    try
+    {
+      return (T) initial.lookup(name);
+    }
+    finally
+    {
+      initial.close();
+    }
+  }
+
+  public void ejbRemove()
+  {
     sessionContext = null;
     jbpmConfiguration = null;
   }
 
-  public void ejbActivate() {}
+  public void ejbActivate()
+  {
+  }
 
-  public void ejbPassivate() {}
-
-  private static final Log log = LogFactory.getLog(CommandServiceBean.class);
+  public void ejbPassivate()
+  {
+  }
 }

Modified: jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/jta/JtaDbPersistenceTest.java
===================================================================
--- jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/jta/JtaDbPersistenceTest.java	2009-01-13 18:22:21 UTC (rev 3634)
+++ jbpm3/trunk/modules/enterprise/src/test/java/org/jbpm/enterprise/jta/JtaDbPersistenceTest.java	2009-01-14 05:51:14 UTC (rev 3635)
@@ -24,61 +24,81 @@
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
-import javax.transaction.UserTransaction;
 
 import junit.framework.Test;
 
 import org.apache.cactus.ServletTestCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hibernate.HibernateException;
+import org.jboss.bpm.api.test.IntegrationTestSetup;
 import org.jbpm.JbpmConfiguration;
 import org.jbpm.JbpmContext;
-import org.jboss.bpm.api.test.IntegrationTestSetup;
 import org.jbpm.command.Command;
+import org.jbpm.command.CommandService;
 import org.jbpm.command.DeleteProcessDefinitionCommand;
 import org.jbpm.command.DeployProcessCommand;
 import org.jbpm.command.GetProcessInstanceCommand;
 import org.jbpm.command.StartProcessInstanceCommand;
+import org.jbpm.command.impl.CommandServiceImpl;
+import org.jbpm.ejb.LocalCommandServiceHome;
 import org.jbpm.graph.def.ProcessDefinition;
 import org.jbpm.graph.exe.ProcessInstance;
-import org.jbpm.persistence.jta.JtaDbPersistenceService;
 
 public class JtaDbPersistenceTest extends ServletTestCase {
 
-  private CommandExecutor commandExecutor;
+  private CommandService commandService;
+  private static LocalCommandServiceHome commandServiceHome;
 
+  private static final Log log = LogFactory.getLog(JtaDbPersistenceTest.class);
+
   public static Test suite() throws Exception {
     return new IntegrationTestSetup(JtaDbPersistenceTest.class, "enterprise-test.war");
   }
 
-  public void testExternalTx() throws Exception {
-    commandExecutor = new CommandExecutor(getUserTransaction());
+  protected void setUp() throws Exception {
+    log.info("### " + getName() + " started ###");
+  }
+
+  protected void tearDown() throws Exception {
+    log.info("### " + getName() + " done ###");
+  }
+
+  public void testCMTSuccess() throws Exception {
+    commandService = getCommandServiceHome().create();
     playTx(false);
   }
 
-  public void testExternalTxRollback() throws Exception {
-    commandExecutor = new CommandExecutor(getUserTransaction());
+  public void testCMTFailure() throws Exception {
+    commandService = getCommandServiceHome().create();
     playTx(true);
   }
 
-  public void testInternalTx() throws Exception {
-    commandExecutor = new CommandExecutor();
+  public void testBMTSuccess() throws Exception {
+    commandService = new CommandServiceImpl(JbpmConfiguration.getInstance());
     playTx(false);
   }
 
-  public void testInternalTxRollback() throws Exception {
-    commandExecutor = new CommandExecutor();
+  public void testBMTFailure() throws Exception {
+    commandService = new CommandServiceImpl(JbpmConfiguration.getInstance());
     playTx(true);
   }
 
-  private void playTx(boolean rollback) throws Exception {
+  private void playTx(boolean fail) throws Exception {
     long definitionId = deployProcess();
     long instanceId = launchProcess(definitionId);
-    signal(instanceId, rollback);
-    assertEquals(!rollback, hasEnded(instanceId));
+    try {
+      signal(instanceId, fail);
+    }
+    catch (RuntimeException e) {
+      log.debug("signal failed on process instance #" + instanceId, e);
+    }
+    assertEquals(!fail, hasEnded(instanceId));
     deleteProcess(definitionId);
   }
 
   private long deployProcess() throws Exception {
-    ProcessDefinition definition = (ProcessDefinition) commandExecutor
+    ProcessDefinition definition = (ProcessDefinition) commandService
         .execute(new DeployProcessCommand("<process-definition name='tx'>"
             + "  <start-state name='start'>"
             + "    <transition to='midway' />"
@@ -92,97 +112,44 @@
   }
 
   private long launchProcess(long definitionId) throws Exception {
-    ProcessInstance instance = (ProcessInstance) commandExecutor
+    ProcessInstance instance = (ProcessInstance) commandService
         .execute(new StartProcessInstanceCommand().processDefinitionId(definitionId));
     return instance.getId();
   }
 
-  private void signal(final long instanceId, boolean rollback) throws Exception {
-    commandExecutor.execute(new Command() {
+  private void signal(final long instanceId, final boolean fail) throws Exception {
+    commandService.execute(new Command() {
       private static final long serialVersionUID = 1L;
 
       public Object execute(JbpmContext jbpmContext) throws Exception {
         jbpmContext.loadProcessInstance(instanceId).signal();
+        if (fail)
+          throw new HibernateException("simulated failure");
         return null;
       }
-    }, rollback);
+    });
   }
 
   private boolean hasEnded(long instanceId) throws Exception {
-    ProcessInstance instance = (ProcessInstance) commandExecutor
+    ProcessInstance instance = (ProcessInstance) commandService
         .execute(new GetProcessInstanceCommand(instanceId));
     return instance.hasEnded();
   }
 
   private void deleteProcess(long definitionId) throws Exception {
-    commandExecutor.execute(new DeleteProcessDefinitionCommand(definitionId));
+    commandService.execute(new DeleteProcessDefinitionCommand(definitionId));
   }
 
-  static class CommandExecutor {
-
-    private JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
-    private UserTransaction tx;
-
-    public CommandExecutor() {
-    }
-
-    public CommandExecutor(UserTransaction userTransaction) {
-      tx = userTransaction;
-    }
-
-    public Object execute(Command command) throws Exception {
-      return execute(command, false);
-    }
-
-    public Object execute(Command command, boolean rollback) throws Exception {
-      if (tx != null)
-        tx.begin();
+  private static LocalCommandServiceHome getCommandServiceHome() throws NamingException {
+    if (commandServiceHome == null) {
+      Context initial = new InitialContext();
       try {
-        JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
-        Object result;
-        try {
-          assertEquals(tx == null, isTxCreatedByService(jbpmContext));
-          result = command.execute(jbpmContext);
-          if (rollback && tx == null)
-            jbpmContext.setRollbackOnly();
-        }
-        catch (RuntimeException e) {
-          if (tx == null)
-            jbpmContext.setRollbackOnly();
-          throw e;
-        }
-        finally {
-          jbpmContext.close();
-        }
-        if (tx != null) {
-          if (rollback)
-            tx.rollback();
-          else
-            tx.commit();
-        }
-        return result;
+        return (LocalCommandServiceHome) initial.lookup("java:comp/env/ejb/CommandServiceBean");
       }
-      catch (Exception e) {
-        if (tx != null)
-          tx.rollback();
-        throw e;
+      finally {
+        initial.close();
       }
     }
+    return commandServiceHome;
   }
-
-  private static boolean isTxCreatedByService(JbpmContext jbpmContext) {
-    JtaDbPersistenceService persistenceService = (JtaDbPersistenceService) jbpmContext
-        .getServices().getPersistenceService();
-    return persistenceService.isJtaTxCreated();
-  }
-
-  private static UserTransaction getUserTransaction() throws NamingException {
-    Context initial = new InitialContext();
-    try {
-      return (UserTransaction) initial.lookup("java:comp/UserTransaction");
-    }
-    finally {
-      initial.close();
-    }
-  }
 }




More information about the jbpm-commits mailing list