⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 executionserviceimpl.java

📁 一个java工作流引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.jbpm.service;

import java.util.*;
import org.apache.commons.logging.*;
import org.jbpm.*;
import org.jbpm.model.definition.*;
import org.jbpm.model.definition.impl.*;
import org.jbpm.model.execution.impl.*;
import org.jbpm.model.log.*;
import org.jbpm.model.log.impl.*;
import org.jbpm.persistence.*;

public class ExecutionServiceImpl extends ExecutionReadServiceImpl implements ExecutionService {
  
  protected boolean checkActors = false;
  
  public ExecutionServiceImpl(JbpmConfiguration jbpmConfiguration) {
		super(jbpmConfiguration);
    checkActors = jbpmConfiguration.getBoolean( "jbpm.check.actors" );
	}

  /**
   * creates a copy of the given execution service and reuses the given persistence session for all 
   * persistence interactions.  This scenario is used e.g. when starting a sub-process in 
   * a process-state. 
   */
  public ExecutionServiceImpl(ExecutionServiceImpl executionService, PersistenceSessionTx sessionTx) {
    super(executionService, sessionTx);
    checkActors = executionService.checkActors;
  }

  public InvocationLog startProcessInstance(String actorId, Long definitionId) throws ExecutionException {
    return startProcessInstance( actorId, definitionId, null );
  }

  public InvocationLog startProcessInstance(String actorId, Long definitionId, Map variables) throws ExecutionException {
    return startProcessInstance( actorId, definitionId, variables, null );
  }

  /**
   * allows to start process instances from within the execution that is calculating the next state of a process instance.   E.g. from within ProcessInvocationHandler's or ActionHandler's
   */
  public InvocationLog startProcessInstance(String actorId, Long definitionId, Map variables, String transitionName) throws ExecutionException {
    InvocationLogImpl invocationLog = null;
    
    // check the input parameters
    if ( definitionId == null ) {
      throw new NullPointerException( "definitionId is null in a call to ExecutionServiceImpl.startProcessInstance" );
    }
    
    PersistenceSessionTx sessionTx = null;
    try {
      log.info( "starting a process instance for '" + actorId + "' of definition '" + definitionId + "'..." );

      // get a persistence session with a transaction
      sessionTx = persistenceSessionFactory.createPersistenceSessionTx();
      
      // load the process definition from the database
      DefinitionImpl definition = (DefinitionImpl) sessionTx.load( DefinitionImpl.class, definitionId );

      // create a new process instance of the specified definition
      ProcessInstanceImpl processInstance = new ProcessInstanceImpl( definition );
      
      // get the root token
      TokenImpl rootToken = (TokenImpl) processInstance.getRoot();
      
      // create the invocation log
      invocationLog = new InvocationLogImpl( ServiceMethod.START_PROCESS_INSTANCE, actorId );
      rootToken.add( invocationLog );
      
      // TODO change id-generation in hibernate to 'assigned' and remove this save operation which only serves to assign an id to the token.
      sessionTx.save( processInstance );
      
      // create the execution context
      ExecutionContextImpl executionContext = new ExecutionContextImpl( actorId, rootToken, sessionTx, this );

      // if the start-node has an actor specified
      SwimlaneImpl startStateActor = (SwimlaneImpl) rootToken.getState().getSwimlane();
      if ( startStateActor != null ) {
        // set the actorId in the actor-variable
        executionContext.setVariable( startStateActor.getName(), actorId );
      }

      // store the variables 
      executionContext.setVariables( variables );
      
      // execute the actions that are specified on the process start event 
      definition.executeActions( EventType.PROCESS_START, executionContext );
      
      // start passing the token
      executionContext.obsoleteActor();
      TransitionImpl transition = ((StateImpl) definition.getStartState()).getLeavingTransition( transitionName );
      transition.acceptToken( executionContext );
      
      // save the process instance
      sessionTx.save( processInstance );
      
      // commit the transaction
      sessionTx.commitAndClose();
      sessionTx = null;

      if ( log.isDebugEnabled() ) log.debug( "started process instance successfully" );
      
    } catch ( SerializerException e ) {
      throw new DelegationException( e.getMessage(), e.getCause() );
    } finally {
      if ( sessionTx != null ) {
        sessionTx.rollbackAndClose();
      }
    }
    
    return invocationLog;
  }

  public InvocationLog endOfState(String actorId, Long tokenId) throws ExecutionException {
    return endOfState( actorId, tokenId, null );
  }

  public InvocationLog endOfState(String actorId, Long tokenId, Map variables) throws ExecutionException {
    return endOfState( actorId, tokenId, variables, null );
  }

  /**
   * allows to signal end-of-states from within the execution that is calculating the next state of a process instance.  E.g. from within ActionHandler's
   */
  public InvocationLog endOfState(String actorId, Long tokenId, Map variables, String transitionName) throws ExecutionException {
    InvocationLogImpl invocationLog = null;
    
    // check the input parameters
    if ( tokenId == null ) {
      throw new NullPointerException( "tokenId is null in a call to ExecutionServiceImpl.endOfState" );
    }

    PersistenceSessionTx sessionTx = null;
    try {
      log.info( "actor '" + actorId + "' signals an end-of-state for token '" + tokenId + "'..." );

      // get a persistence session with a transaction
      sessionTx = persistenceSessionFactory.createPersistenceSessionTx();

      // get the token and lock the process instance
      TokenImpl token = loadToken( tokenId, sessionTx );
      
      // create the invocation log
      invocationLog = new InvocationLogImpl( ServiceMethod.END_OF_STATE, actorId, token );
      token.add( invocationLog );
      
      // check if the actor is authorized
      checkActor(actorId, token, ServiceMethod.END_OF_STATE);
      
      // create the execution context
      ExecutionContextImpl executionContext = new ExecutionContextImpl( actorId, token, sessionTx, this );

      // store the variables 
      executionContext.setVariables( variables );
      
      // execute the actions that are specified on end of state of the token-state
      StateImpl state = (StateImpl) token.getState();
      state.executeActions( EventType.STATE_LEAVE, executionContext );
      
      // start passing the token
      executionContext.obsoleteActor();
      TransitionImpl transition = state.getLeavingTransition( transitionName );
      transition.acceptToken( executionContext );

      // save all changes made in the process instance
      sessionTx.save( (ProcessInstanceImpl) token.getProcessInstance() );
      
      // commit the transaction
      sessionTx.commitAndClose();
      sessionTx = null;

      if ( log.isDebugEnabled() ) log.debug( "calculated next state successfully" );
      
    } catch ( SerializerException e ) {
      throw new DelegationException( e.getMessage(), e.getCause() );
    } finally {
      if ( sessionTx != null ) {
        sessionTx.rollbackAndClose();
      }
    }
    
    return invocationLog;
  }

  public InvocationLog undo(String actorId, Long processInstanceId, Date date) throws ExecutionException {
    InvocationLogImpl invocationLog = null;
    
    // check the input parameters
    if ( date == null ) {
      throw new NullPointerException( "date is null in a call to ExecutionServiceImpl.undo" );
    }
    if ( processInstanceId == null ) {
      throw new NullPointerException( "processInstanceId is null in a call to ExecutionServiceImpl.undo" );
    }
    
    PersistenceSessionTx sessionTx = null;
    try {
      log.info( "processing undo for actor '" + actorId + "' on process instance '" + processInstanceId + "' and date '" + date + "'..." );
      
      // get a persistence session with a transaction
      sessionTx = persistenceSessionFactory.createPersistenceSessionTx();

      // load the process instance from the database
      ProcessInstanceImpl processInstance = (ProcessInstanceImpl) sessionTx.load( ProcessInstanceImpl.class, processInstanceId );
      if ( date.before( processInstance.getStart() ) ) {
        throw new UnUndoableException( "process instance '" + processInstanceId + "' was not yet started at '" + date + "'" );
      }
      if ( date.after( new Date() ) ) {
        throw new UnUndoableException( "process instance '" + processInstanceId + "' can not be restored to a date in the future : '" + date + "'" );
      }
      
      // lock the process instance
      sessionTx.lock( processInstance );
      
      // create the undo invocation log
      TokenImpl root = (TokenImpl) processInstance.getRoot();
      invocationLog = new InvocationLogImpl( ServiceMethod.UNDO, actorId, root );
      root.add( invocationLog );

      // get the undoable logs
      List undoableLogs = sessionTx.findUndoableInvocationLogs( processInstanceId, date );
      // create an execution context 
      ExecutionContextImpl executionContext = new ExecutionContextImpl( actorId, sessionTx, this );
      // for all undoable logs
      Iterator iter = undoableLogs.iterator();
      while ( iter.hasNext() ) {
        InvocationLogImpl undoableLog = (InvocationLogImpl) iter.next();
        // undo the log
        undoableLog.undo( executionContext );
      }
      
      // save all changes made in the process instance
      sessionTx.save( processInstance );
      
      // commit the transaction
      sessionTx.commitAndClose();
      sessionTx = null;

    } catch ( SerializerException e ) {
      throw new DelegationException( e.getMessage(), e.getCause() );
    } finally {
      if ( sessionTx != null ) {
        sessionTx.rollbackAndClose();
      }
    }
    
    return invocationLog;
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -