Coverage Report - org.mule.providers.bpm.jbpm.Jbpm
 
Classes in this File Line Coverage Branch Coverage Complexity
Jbpm
0%
0/111
0%
0/8
2.043
 
 1  
 /*
 2  
  * $Id: Jbpm.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.providers.bpm.jbpm;
 12  
 
 13  
 import org.mule.providers.bpm.BPMS;
 14  
 import org.mule.providers.bpm.MessageService;
 15  
 import org.mule.util.IOUtils;
 16  
 import org.mule.util.NumberUtils;
 17  
 
 18  
 import java.io.FileNotFoundException;
 19  
 import java.io.IOException;
 20  
 import java.io.InputStream;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 import org.jbpm.JbpmConfiguration;
 27  
 import org.jbpm.JbpmContext;
 28  
 import org.jbpm.graph.def.ProcessDefinition;
 29  
 import org.jbpm.graph.exe.ProcessInstance;
 30  
 import org.jbpm.taskmgmt.exe.TaskInstance;
 31  
 
 32  
 /**
 33  
  * jBPM's implementation of Mule's generic BPMS interface.
 34  
  * This class should be set as the "bpms" property of the BPM Connector:
 35  
  *
 36  
  *   <connector name="jBpmConnector" className="org.mule.providers.bpm.ProcessConnector">
 37  
  *       <properties>
 38  
  *           <spring-property name="bpms">
 39  
  *              <ref local="jbpm" />
 40  
  *           </spring-property>
 41  
  *       </properties>
 42  
  *   </connector>
 43  
  *
 44  
  *   <bean id="jbpm" class="org.mule.providers.bpm.jbpm.Jbpm" destroy-method="destroy">
 45  
  *       <spring-property name="jbpmConfiguration">
 46  
  *           <ref local="jbpmConfig" />
 47  
  *       </spring-property>
 48  
  *   </bean>
 49  
  */
 50  
 public class Jbpm implements BPMS
 51  
 {
 52  0
     protected static transient Log logger = LogFactory.getLog(Jbpm.class);
 53  
 
 54  0
     protected JbpmConfiguration jbpmConfiguration = null;
 55  
 
 56  
     /**
 57  
      * Indicates whether jBPM has been instantiated by the connector (false) or was passed 
 58  
      * in from somewhere else (true).
 59  
      */
 60  
     protected boolean containerManaged;
 61  
     
 62  
     // ///////////////////////////////////////////////////////////////////////////
 63  
     // Lifecycle methods
 64  
     // ///////////////////////////////////////////////////////////////////////////
 65  
 
 66  
     /**
 67  
      * Creates the Mule wrapper for jBPM using a default configuration. 
 68  
      */
 69  
     public Jbpm()
 70  
     {
 71  0
         this(JbpmConfiguration.getInstance());
 72  0
         containerManaged = false;
 73  0
     }
 74  
 
 75  
     /**
 76  
      * Creates the Mule BPM wrapper based on an already-initialized jBPM instance
 77  
      * 
 78  
      * @param jbpmConfiguration - the already-initialized jBPM instance
 79  
      */
 80  
     public Jbpm(JbpmConfiguration jbpmConfiguration)
 81  0
     {
 82  0
         setJbpmConfiguration(jbpmConfiguration);
 83  0
         containerManaged = true;
 84  0
     }
 85  
 
 86  
     public void destroy()
 87  
     {
 88  0
         if (containerManaged == false && jbpmConfiguration != null)
 89  
         {
 90  0
             jbpmConfiguration.close();
 91  0
             jbpmConfiguration = null;
 92  
         }
 93  0
     }
 94  
 
 95  
     public void setMessageService(MessageService msgService)
 96  
     {
 97  0
         JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
 98  
         try
 99  
         {
 100  0
             ((MuleMessageService)jbpmContext.getServices().getMessageService()).setMessageService(msgService);
 101  
         }
 102  
         finally
 103  
         {
 104  0
             jbpmContext.close();
 105  0
         }
 106  0
     }
 107  
     
 108  
     // ///////////////////////////////////////////////////////////////////////////
 109  
     // Process manipulation
 110  
     // ///////////////////////////////////////////////////////////////////////////
 111  
 
 112  
     /**
 113  
      * Start a new process.
 114  
      * 
 115  
      * @return the newly-created ProcessInstance
 116  
      */
 117  
     public synchronized Object startProcess(Object processType) throws Exception
 118  
     {
 119  0
         return startProcess(processType, /* transition */null, /* processVariables */null);
 120  
     }
 121  
 
 122  
     /**
 123  
      * Start a new process.
 124  
      * 
 125  
      * @return the newly-created ProcessInstance
 126  
      */
 127  
     public synchronized Object startProcess(Object processType, Object transition, Map processVariables) throws Exception
 128  
     {
 129  0
         ProcessInstance processInstance = null;
 130  
 
 131  0
         JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
 132  
         try
 133  
         {
 134  0
             ProcessDefinition processDefinition = jbpmContext.getGraphSession().findLatestProcessDefinition(
 135  
                 (String)processType);
 136  0
             if (processDefinition == null)
 137  0
                 throw new IllegalArgumentException("No process definition found for process " + processType);
 138  
 
 139  0
             processInstance = new ProcessInstance(processDefinition);
 140  
 
 141  
             // Set any process variables.
 142  0
             if (processVariables != null && !processVariables.isEmpty())
 143  
             {
 144  0
                 processInstance.getContextInstance().addVariables(processVariables);
 145  
             }
 146  0
             processInstance.getContextInstance().addVariables(processVariables);
 147  
 
 148  
             // Leave the start state.
 149  0
             processInstance.signal();
 150  
 
 151  0
             jbpmContext.save(processInstance);
 152  
 
 153  0
             return processInstance;
 154  
         }
 155  0
         catch (Exception e)
 156  
         {
 157  0
             jbpmContext.setRollbackOnly();
 158  0
             throw e;
 159  
         }
 160  
         finally
 161  
         {
 162  0
             jbpmContext.close();
 163  
         }
 164  
     }
 165  
 
 166  
     /**
 167  
      * Advance a process instance one step.
 168  
      * 
 169  
      * @return the updated ProcessInstance
 170  
      */
 171  
     public synchronized Object advanceProcess(Object processId) throws Exception
 172  
     {
 173  0
         return advanceProcess(processId, /* transition */null, /* processVariables */null);
 174  
     }
 175  
 
 176  
     /**
 177  
      * Advance a process instance one step.
 178  
      * 
 179  
      * @return the updated ProcessInstance
 180  
      */
 181  
     public synchronized Object advanceProcess(Object processId, Object transition, Map processVariables)
 182  
         throws Exception
 183  
     {
 184  0
         ProcessInstance processInstance = null;
 185  
 
 186  0
         JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
 187  
         try
 188  
         {
 189  
             // Look up the process instance from the database.
 190  0
             processInstance = jbpmContext.getGraphSession()
 191  
                 .loadProcessInstance(NumberUtils.toLong(processId));
 192  
 
 193  0
             if (processInstance.hasEnded())
 194  
             {
 195  0
                 throw new IllegalStateException(
 196  
                     "Process cannot be advanced because it has already terminated, processId = " + processId);
 197  
             }
 198  
 
 199  
             // Set any process variables.
 200  
             // Note: addVariables() will replace the old value of a variable if it
 201  
             // already exists.
 202  0
             if (processVariables != null && !processVariables.isEmpty())
 203  
             {
 204  0
                 processInstance.getContextInstance().addVariables(processVariables);
 205  
             }
 206  
 
 207  
             // Advance the workflow.
 208  0
             if (transition != null)
 209  
             {
 210  0
                 processInstance.signal((String)transition);
 211  
             }
 212  
             else
 213  
             {
 214  0
                 processInstance.signal();
 215  
             }
 216  
 
 217  
             // Save the process state back to the database.
 218  0
             jbpmContext.save(processInstance);
 219  
 
 220  0
             return processInstance;
 221  
         }
 222  0
         catch (Exception e)
 223  
         {
 224  0
             jbpmContext.setRollbackOnly();
 225  0
             throw e;
 226  
         }
 227  
         finally
 228  
         {
 229  0
             jbpmContext.close();
 230  
         }
 231  
     }
 232  
 
 233  
     /**
 234  
      * Update the variables for a process instance.
 235  
      * 
 236  
      * @return the updated ProcessInstance
 237  
      */
 238  
     public synchronized Object updateProcess(Object processId, Map processVariables) throws Exception
 239  
     {
 240  0
         ProcessInstance processInstance = null;
 241  
 
 242  0
         JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
 243  
         try
 244  
         {
 245  
             // Look up the process instance from the database.
 246  0
             processInstance = jbpmContext.getGraphSession()
 247  
                 .loadProcessInstance(NumberUtils.toLong(processId));
 248  
 
 249  
             // Set any process variables.
 250  
             // Note: addVariables() will replace the old value of a variable if it
 251  
             // already exists.
 252  0
             if (processVariables != null && !processVariables.isEmpty())
 253  
             {
 254  0
                 processInstance.getContextInstance().addVariables(processVariables);
 255  
             }
 256  
 
 257  
             // Save the process state back to the database.
 258  0
             jbpmContext.save(processInstance);
 259  
 
 260  0
             return processInstance;
 261  
         }
 262  0
         catch (Exception e)
 263  
         {
 264  0
             jbpmContext.setRollbackOnly();
 265  0
             throw e;
 266  
         }
 267  
         finally
 268  
         {
 269  0
             jbpmContext.close();
 270  
         }
 271  
     }
 272  
 
 273  
     /**
 274  
      * Delete a process instance.
 275  
      */
 276  
     public synchronized void abortProcess(Object processId) throws Exception
 277  
     {
 278  0
         JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
 279  
         try
 280  
         {
 281  0
             jbpmContext.getGraphSession().deleteProcessInstance(NumberUtils.toLong(processId));
 282  
         }
 283  0
         catch (Exception e)
 284  
         {
 285  0
             jbpmContext.setRollbackOnly();
 286  0
             throw e;
 287  
         }
 288  
         finally
 289  
         {
 290  0
             jbpmContext.close();
 291  0
         }
 292  0
     }
 293  
 
 294  
     // ///////////////////////////////////////////////////////////////////////////
 295  
     // Process status / lookup
 296  
     // ///////////////////////////////////////////////////////////////////////////
 297  
 
 298  
     public boolean isProcess(Object obj) throws Exception
 299  
     {
 300  0
         return (obj instanceof ProcessInstance);
 301  
     }
 302  
 
 303  
     public Object getId(Object process) throws Exception
 304  
     {
 305  0
         return new Long(((ProcessInstance)process).getId());
 306  
     }
 307  
 
 308  
     // By default the process is lazily-initialized so we need to open a new session to the
 309  
     // database before calling process.getRootToken().getNode().getName()
 310  
     public Object getState(Object process) throws Exception
 311  
     {
 312  0
         ProcessInstance processInstance = (ProcessInstance) process;
 313  
 
 314  0
         JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
 315  
         try
 316  
         {
 317  
             // Look up the process instance from the database.
 318  0
             processInstance = jbpmContext.getGraphSession()
 319  
                 .loadProcessInstance(processInstance.getId());
 320  0
             return processInstance.getRootToken().getNode().getName();
 321  
         }
 322  
         finally
 323  
         {
 324  0
             jbpmContext.close();
 325  
         }
 326  
     }
 327  
 
 328  
     public boolean hasEnded(Object process) throws Exception
 329  
     {
 330  0
         return ((ProcessInstance)process).hasEnded();
 331  
     }
 332  
 
 333  
     /**
 334  
      * Look up an already-running process instance.
 335  
      * 
 336  
      * @return the ProcessInstance
 337  
      */
 338  
     public Object lookupProcess(Object processId) throws Exception
 339  
     {
 340  0
         ProcessInstance processInstance = null;
 341  
 
 342  0
         JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
 343  
         try
 344  
         {
 345  
             // Look up the process instance from the database.
 346  0
             processInstance = jbpmContext.getGraphSession()
 347  
                 .loadProcessInstance(NumberUtils.toLong(processId));
 348  0
             return processInstance;
 349  
         }
 350  
         finally
 351  
         {
 352  0
             jbpmContext.close();
 353  
         }
 354  
     }
 355  
 
 356  
     // ///////////////////////////////////////////////////////////////////////////
 357  
     // Miscellaneous
 358  
     // ///////////////////////////////////////////////////////////////////////////
 359  
 
 360  
     /**
 361  
      * Deploy a new process definition.
 362  
      */
 363  
     public void deployProcess(String processDefinitionFile) throws FileNotFoundException, IOException
 364  
     {
 365  0
         deployProcessFromStream(IOUtils.getResourceAsStream(processDefinitionFile, getClass()));
 366  0
     }
 367  
 
 368  
     public void deployProcessFromStream(InputStream processDefinition)
 369  
         throws FileNotFoundException, IOException
 370  
     {
 371  0
         deployProcess(ProcessDefinition.parseXmlInputStream(processDefinition));
 372  0
     }
 373  
 
 374  
     private void deployProcess(ProcessDefinition processDefinition)
 375  
     {
 376  0
         JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
 377  
         try
 378  
         {
 379  0
             jbpmContext.deployProcessDefinition(processDefinition);
 380  
         }
 381  
         finally
 382  
         {
 383  0
             jbpmContext.close();
 384  0
         }
 385  0
     }
 386  
 
 387  
     public List/* <TaskInstance> */loadTasks(ProcessInstance process)
 388  
     {
 389  0
         List/* <TaskInstance> */taskInstances = null;
 390  
 
 391  0
         JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
 392  
         try
 393  
         {
 394  0
             taskInstances = jbpmContext.getTaskMgmtSession().findTaskInstancesByToken(
 395  
                 process.getRootToken().getId());
 396  0
             return taskInstances;
 397  
         }
 398  
         finally
 399  
         {
 400  0
             jbpmContext.close();
 401  
         }
 402  
     }
 403  
 
 404  
     public synchronized void completeTask(TaskInstance task)
 405  
     {
 406  0
         completeTask(task, /* transition */null);
 407  0
     }
 408  
 
 409  
     public synchronized void completeTask(TaskInstance task, String transition)
 410  
     {
 411  0
         JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
 412  
         try
 413  
         {
 414  0
             task = jbpmContext.getTaskMgmtSession().loadTaskInstance(task.getId());
 415  0
             if (transition != null)
 416  
             {
 417  0
                 task.end(transition);
 418  
             }
 419  
             else
 420  
             {
 421  0
                 task.end();
 422  
             }
 423  
         }
 424  
         finally
 425  
         {
 426  0
             jbpmContext.close();
 427  0
         }
 428  0
     }
 429  
 
 430  
     // ///////////////////////////////////////////////////////////////////////////
 431  
     // Getters and setters
 432  
     // ///////////////////////////////////////////////////////////////////////////
 433  
 
 434  
     public JbpmConfiguration getJbpmConfiguration()
 435  
     {
 436  0
         return jbpmConfiguration;
 437  
     }
 438  
 
 439  
     public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration)
 440  
     {
 441  0
         this.jbpmConfiguration = jbpmConfiguration;
 442  0
     }
 443  
 }