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

📄 executioncontextimpl.java

📁 一个java工作流引擎
💻 JAVA
字号:
package org.jbpm.model.execution.impl;

import java.util.*;
import org.jbpm.*;
import org.jbpm.delegation.*;
import org.jbpm.util.log.*;
import org.jbpm.model.definition.*;
import org.jbpm.model.definition.impl.*;
import org.jbpm.model.execution.*;
import org.jbpm.model.log.*;
import org.jbpm.model.log.impl.*;
import org.jbpm.persistence.*;
import org.jbpm.service.*;

public class ExecutionContextImpl implements ExecutionContext, AssignmentContext, ForkContext, JoinContext, ProcessInvocationContext {
  
  private String actorId = null;
  private String previousActorId = null;
  private TokenImpl token = null;
  private Map transientVariables = null;
  private ProcessInstanceImpl processInstance = null;
  private DefinitionImpl definition = null;
  private NodeImpl node = null;
  private InvocationLogImpl event = null;
  private PersistenceSessionTx sessionTx = null;
  private ExecutionServiceImpl executionService = null;
  
  public ExecutionContextImpl( String actorId, PersistenceSessionTx sessionTx, ExecutionServiceImpl executionService ) {
    this.actorId = actorId;
    this.sessionTx = sessionTx;
    this.executionService = new ExecutionServiceImpl( executionService, sessionTx );
    this.transientVariables = new HashMap();
  }
  
  public ExecutionContextImpl( String actorId, TokenImpl token, PersistenceSessionTx sessionTx, ExecutionServiceImpl executionService ) {
    this.actorId = actorId;
    this.sessionTx = sessionTx;
    this.executionService = new ExecutionServiceImpl( executionService, sessionTx );
    this.transientVariables = new HashMap();
    setToken( token );
  }
  
  public ExecutionContextImpl(ExecutionContextImpl subExecutionContext, TokenImpl superProcessToken ) {
    this.actorId = subExecutionContext.actorId;
    this.transientVariables = subExecutionContext.transientVariables;
    this.token = superProcessToken;
    this.processInstance = (ProcessInstanceImpl) token.getProcessInstance();
    this.definition = (DefinitionImpl) processInstance.getDefinition();
    this.event = subExecutionContext.event;
    this.sessionTx = subExecutionContext.sessionTx;
    this.executionService = subExecutionContext.executionService;
    this.transientVariables = new HashMap();
  }
  
  public void setToken( TokenImpl token ) {
    this.token = token;
    this.processInstance = (ProcessInstanceImpl) token.getProcessInstance();
    this.definition = (DefinitionImpl) processInstance.getDefinition();
    this.node = (NodeImpl) token.getState();
  }

  public void obsoleteActor() {
    previousActorId = actorId;
    this.actorId = null; 
    this.node = null;
  }

  public String getActorId() { return actorId; }
  public String getPreviousActorId() { return previousActorId; }
  public Token getToken() { return token; }
	public Definition getDefinition() { return definition; }
	public ProcessInstance getProcessInstance() { return processInstance; }
  public Node getNode() { return node; }
  
  public PersistenceSessionTx getSessionTx() { return sessionTx; }
  public ExecutionService getExecutionService() { return executionService; }
  
  /**
   * @return the variable-object or null if it doesn't exist.
   */
	public Object getVariable( String name ) {
    Object value = null;
    
    if ( token != null ) {
      value = token.getVariable( name );
    }
      
		return value;
	}

  public void setVariables(Map variables) {
    if (variables!=null) {
      Iterator iter = variables.entrySet().iterator();
      while (iter.hasNext()) {
        Map.Entry entry = (Map.Entry) iter.next();
        setVariable( (String) entry.getKey(), entry.getValue() );
      }
    }
  }
    
  public void setVariable(String name, Object value) {

    // if no major problem to set the variable
    if ( ( name != null ) 
         && ( token != null ) ) {
      
      // try to set it on the token
      // if the token could not set the variable
      // (this means : no variable instances with that name existed in the 
      //  scope of the token and the definition could not create a variable
      //  instance on the root-token because it could not determine the type
      //  of the variable)
      if ( ! token.setVariable( name, value ) ) {
        transientVariables.put( name, value );
      }
    }
  }
  
  public void clearTransientVariables() {
    this.transientVariables = new HashMap(); 
  }

  public Token createToken(Transition transition, String tokenName) throws ExecutionException {
    return createToken( transition, token, tokenName );
  }
    
  public Token createToken(Transition transition, TokenImpl parent, String childTokenName) throws ExecutionException {
    
    // get the selected transition.
    if ( transition == null ) {
      throw new IllegalTransitionException( "transition is null" );
    }
    
    // execute the actions
    node.executeActions( EventType.FORK_EVERY_LEAVE, this );

    // create the token.
    TokenImpl child = new TokenImpl( parent, childTokenName ); 
    token.add( child );

    activateToken(child, (TransitionImpl) transition);
    
    return child;
  }

  private void activateToken(TokenImpl tokenToActivate, TransitionImpl transition) throws ExecutionException {
		// remember the original token and fork-node.
    TokenImpl originalToken = token;
    NodeImpl originalNode = node;

    try {
      // set this context to the child token.
      token = tokenToActivate;
      // pass the child-token over the selected transition.
      transition.acceptToken( this );

    } finally {
      // restore this context to the original state.
      token = originalToken;
      node = originalNode;
    }
	}

  public Type findTypeByJavaType(String javaType) {
    return definition.findTypeByJavaType( javaType );
  }

  public Type findTypeByVariableName(String variableName) {
    return definition.findTypeByVariableName( variableName );
  }
    
  public Type findTypeByObject(Object value) {
    return definition.findTypeByObject( value );
  }

  public void createLocalVariable(Token token, String variableName, Type type, Object value) {
    TokenImpl tokenImpl = (TokenImpl) token;
    if ( getVariable( variableName ) != null ) {
      throw new IllegalArgumentException( "cannot create local variable : variable '" + variableName + "' already exists in token '" + token.getName() + "'" );
    }
    if ( type == null ) {
      throw new IllegalArgumentException( "cannot create local variable '" + variableName + "' : type is null" );
    }
    VariableInstanceImpl variableInstance = tokenImpl.add( new VariableInstanceImpl( variableName, tokenImpl, (TypeImpl)type ) );
		variableInstance.setValue( value );
  }

  public Map getOtherActiveBrothersAndSisters() {
    Map otherActiveBrothersAndSisters = new HashMap();
    TokenImpl parent = (TokenImpl) token.getParent();
    Iterator iter = parent.getChildren().values().iterator();
		while (iter.hasNext()) {
			TokenImpl brotherOrSister = (TokenImpl) iter.next();
			if ( ( brotherOrSister != token )
           && ( brotherOrSister.getState() != null ) ) {
        otherActiveBrothersAndSisters.put( brotherOrSister.getName(), brotherOrSister );
      }
		}
    return otherActiveBrothersAndSisters;
  }

  public Token getToken(String path) {
    TokenImpl startToken = null;
    if ( path.startsWith( "//" ) ) {
      startToken = (TokenImpl) processInstance.getRoot();
      path = path.substring( 2 );
    } else {
      startToken = token;
    }
    return startToken.getToken( path );
  }

  public void reactivateToken(Token token) throws ExecutionException {
    // TODO see EventType.JOIN_LEAVE why this is commented out
    // node.executeActions( EventType.JOIN_LEAVE, this );

    // take the leaving transition
    TransitionImpl transition = (TransitionImpl) node.getLeavingTransitions().iterator().next();
    activateToken( (TokenImpl) token, transition );
  }

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

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

	public InvocationLog startSubProcessInstance(String actorId, Long definitionId, Map variables, String transitionName) throws ExecutionException {
    return executionService.startProcessInstance( actorId, definitionId, variables, transitionName);
	}

  public void log(String msg) {
    throw new UnsupportedOperationException( "logging not yet supported on the execution context" );
  }

  public void log(Level level, String msg) {
    throw new UnsupportedOperationException( "logging not yet supported on the execution context" );
  }

  public void cancelEvent(String reference) {
    throw new UnsupportedOperationException( "scheduling time based events is not yet supported on the execution context" );
  }

  public void setNode( NodeImpl node ) {
    this.node = node; 
  }

  public SchedulerService getSchedulerService() {
    return new SchedulerServiceImpl( sessionTx );
  }
}

⌨️ 快捷键说明

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