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

📄 tokenimpl.java

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

import java.util.*;

import org.jbpm.*;
import org.jbpm.model.definition.*;
import org.jbpm.model.definition.impl.*;
import org.jbpm.model.execution.*;
import org.jbpm.model.log.impl.*;

public class TokenImpl implements Token {

  private Long id = null;
  private String name = null;
  private ProcessInstanceImpl processInstance = null;
  private TokenImpl parent = null; // the root-token has null as a parent.
  private Map children = null;
  private StateImpl state = null;
  private String actorId = null;
  private Map variableInstances = null;
  private Date start = null;
  private Date end = null;
  private JoinImpl reactivationJoin = null;
  private ProcessInstanceImpl subProcessInstance = null;
  private AssignmentLogImpl lastAssignmentLog = null;
  private SortedSet invocationLogs = null;
  private transient InvocationLogImpl currentInvocationLog = null;

  public TokenImpl() {}
  
  public TokenImpl(ProcessInstanceImpl processInstance) {
    this.name = "root";
    this.processInstance = processInstance;
    this.start = new Date();
    this.state = (StateImpl) processInstance.getDefinition().getStartState();
  }

	public TokenImpl(TokenImpl parent, String name) {
    this.processInstance = (ProcessInstanceImpl) parent.getProcessInstance();
    this.parent = parent;
    this.start = new Date();
    this.name = name;
    parent.add( this );
  }

  public Long getId() { return this.id; }
  public void setId(Long id) { this.id = id; }
  
	public String getName() { return this.name; }
	public void setName(String name) { this.name = name; }
  
  public ProcessInstance getProcessInstance() { return this.processInstance; }
  public void setProcessInstance(ProcessInstanceImpl processInstance) { this.processInstance = processInstance; }
  
  public Token getParent() { return this.parent; }
	public void setParent(TokenImpl parent) { this.parent = parent; }
  
	public Map getChildren() { return this.children; }
	public void setChildren(Map children) { this.children = children; }
  public TokenImpl add( TokenImpl child ) {
    if ( child != null ) {
      if ( children == null ) children = new HashMap();
      if ( ! children.containsKey( child.name ) ) {
        children.put( child.name, child );
        child.setParent( this );
      }
    }
    return child;
  }

  public TokenImpl getChild(String tokenName) {
    return (TokenImpl) children.get( tokenName );
  }

	public State getState() { return this.state; }
	public void setState(StateImpl state) { this.state = state; }
  
	public String getActorId() { return this.actorId; }
	public void setActorId(String actorId) { this.actorId = actorId; }

	public Map getVariableInstances() { return this.variableInstances; }
	public void setVariableInstances(Map variableInstances) { this.variableInstances = variableInstances; }
  public VariableInstanceImpl add( VariableInstanceImpl variableInstance ) {
    if ( variableInstance != null ) {
      if ( variableInstances == null ) variableInstances = new HashMap();
      if ( ! variableInstances.containsKey( variableInstance.getName() ) ) {
        variableInstances.put( variableInstance.getName(), variableInstance );
        variableInstance.setToken( this );
      }
    }
    return variableInstance;
  }
  public void remove(VariableInstanceImpl variableInstance) {
    if ( variableInstance != null ) {
      if ( variableInstances != null ) {
        variableInstances.remove( variableInstance );
      }
    }
  }
  public VariableInstanceImpl getVariableInstance(String name) {
    return (VariableInstanceImpl) variableInstances.get( name );
  }
  
  
  public Date getStart() { return this.start; }
  public void setStart(Date start) { this.start = start; }
  
  public Date getEnd() { return this.end; }
  public void setEnd(Date end) { this.end = end; }

  public JoinImpl getReactivationJoin() { return this.reactivationJoin; }
  public void setReactivationJoin(JoinImpl reactivationJoin) { this.reactivationJoin = reactivationJoin; }
  
  public ProcessInstance getSubProcessInstance() { return this.subProcessInstance; }
  public void setSubProcessInstance(ProcessInstanceImpl subProcessInstance) { 
    this.subProcessInstance = subProcessInstance;
    if ( ( subProcessInstance != null )
         && ( subProcessInstance.getSuperProcessToken() != this ) ) {
      subProcessInstance.setSuperProcessToken( this );
    }
  }
  
  public AssignmentLogImpl getLastAssignmentLog() { return lastAssignmentLog; }
  public void setLastAssignmentLog(AssignmentLogImpl lastAssignmentLog) { this.lastAssignmentLog = lastAssignmentLog; }
  
  public SortedSet getInvocationLogs() { return this.invocationLogs; }
  public void setInvocationLogs(SortedSet invocationLogs) { this.invocationLogs = invocationLogs; }
  public InvocationLogImpl add( InvocationLogImpl invocationLog ) {
    if ( invocationLogs == null ) {
      invocationLogs = new TreeSet();
    }
    currentInvocationLog = invocationLog;
    invocationLog.setToken( this );
    invocationLogs.add( invocationLog );
    return invocationLog;
  }
  public ExecutionLogImpl add( ExecutionLogImpl executionLog ) {
    if ( currentInvocationLog != null ) {
      currentInvocationLog.addExecutionLog( executionLog );
    }
    return executionLog;
  }
  
  public boolean isRoot() {
    return ( parent == null ); 
  }
  
  public boolean hasEnded() {
    return (this.end != null);
  }

  public void end( ExecutionContextImpl executionContext, boolean checkProcessInstance ) throws ExecutionException {
    
    // if this token is in a process-state...
    if ( ( state != null )
         && ( state instanceof ProcessState ) ) {
      // end the sub process instance
      executionContext.setToken( (TokenImpl) subProcessInstance.getRoot() );
      subProcessInstance.end( executionContext, false );
    }

    // set the state of this token to ended...
    actorId = null;
    state = null;
    end = new Date();
    add( new EndOfTokenLogImpl( this ) );
    
    // update the execution context to this token 
    executionContext.setToken( this );
    
    // terminate all the children of this token
    if ( children != null ) {
      Iterator iter = children.values().iterator();
      while (iter.hasNext()) {
        TokenImpl child = (TokenImpl) iter.next();
        if ( ! child.hasEnded() ) {
          executionContext.setToken( child );
          child.end( executionContext, false );
        }
      }
    }
    
    // if this is a root-token and the process instance is still alive
    if ( checkProcessInstance
         && isRoot()
         && ( ! processInstance.hasEnded() ) ) {
      // kill it
      processInstance.end( executionContext, true );
    }
  }

  /**
   * collects all variable values for this token and the parent tokens recursively.
   */
  public Map getVariables() {
    Map variables = null;
    
    // if this is the root token
    if ( isRoot() ) {
      // start with an empty map of variable values
      variables = new HashMap();

    } else { // this is not the root token
      // start with the variables of the parent
      variables = parent.getVariables();
    }

    // now add this token's variable values
    variables.putAll( getLocalVariables() );

    return variables;
  }
  
  /**
   * collects all variable values for this token only.
   */
  public Map getLocalVariables() {
    Map localVariables = new HashMap();

    // add all the variable-instance-values from this token
    Iterator iter = variableInstances.values().iterator();
    while (iter.hasNext()) {
      VariableInstanceImpl variableInstance = (VariableInstanceImpl) iter.next();
      localVariables.put( variableInstance.getName(), variableInstance.getValue() );
    }
    
    return localVariables;
  }
  
  public VariableInstanceImpl findVariableInstance( String name ) {
    VariableInstanceImpl variableInstance = null;
    
    // if this token has variable instances
    if ( variableInstances != null ) {
      // try to get the variable instance from this token
      variableInstance = (VariableInstanceImpl) variableInstances.get( name );
    }

    // if no variable instance was found in this token
    if ( ( variableInstance == null )
         // and if this is not the root
         && ( ! isRoot() ) ) {
      // ask the parent if it knows the variable instance
      variableInstance = parent.findVariableInstance( name );
    }
    
    return variableInstance;
  }
  
  public Object getVariable( String name ) {
    Object value = null;

    if ( name != null ) {

      // try to get the variable instance 
      VariableInstanceImpl variableInstance = findVariableInstance( name );
      
      // if the variable instance was found
      if ( variableInstance != null ) {

        // take its value
        value = variableInstance.getValue();
      }
    }
      
    return value;
  }

  /**
   * A variable instance will be searched for recursively up until the root flow.
   * If the variable instance is found, the value of taht instance is updated, otherwise
   * a new variable instance is created in the root-token.  If the type of the variable
   * instance can be determined, the variable instance is created and the value is set.
   * Otherwise nothing is done and this method returns false.
   * @return true if the variable was set, false otherwise.
   */
  public boolean setVariable( String name, Object newValue ) {
    boolean isSet = false;
    
    if ( name != null ) {
      
      // try to get the variable instance 
      VariableInstanceImpl variableInstance = findVariableInstance( name );
      
      // if the variable instance was found
      if ( variableInstance != null ) {
        
        TypeImpl type = (TypeImpl) variableInstance.getType();
        if ( ( variableInstance.getDefaultType() != null )
             || ( ! type.isTransient() ) ) {
          updateVariableInstance(variableInstance, newValue);
          isSet = true;
        }
        
      // else means the there was no variable instance found 
      } else {

        // we gonna try to create a new variable instance on the root token
        
        // get the root token
        TokenImpl root = (TokenImpl) processInstance.getRoot();
        DefinitionImpl definition = (DefinitionImpl)processInstance.getDefinition();
        variableInstance = definition.createVariableInstance( root, name, newValue );

        // if the definition can determine the proper type for the variable instance
        if ( variableInstance != null ) {

          // the variable is created and the value is set
          updateVariableInstance(variableInstance, newValue);
          isSet = true;
        }
      }
    }
    
    return isSet;
  }

  private void updateVariableInstance(VariableInstanceImpl variableInstance, Object newValue) {
		String oldSerializedValue = variableInstance.getSerializedValue(); 
		variableInstance.setValue( newValue );
	}

	public Token getToken(String relativePath) {
    TokenImpl token = this;
    StringTokenizer tokenizer = new StringTokenizer( relativePath.substring(13), "/" );
    while ( tokenizer.hasMoreTokens() ) {
      String tokenName = tokenizer.nextToken();
      String previousTokenName = token.getName();
      if ( tokenName.equals( ".." ) ) {
        token = (TokenImpl) token.getParent();
        if ( token == null ) {
          throw new IllegalArgumentException( "could not take the parent token from '" + previousTokenName +"'.  exception occurred while calculating token from relative path '" + relativePath  + "'" );
        }
      } else {
        token = (TokenImpl) token.getChild( tokenName );
        if ( token == null ) {
          throw new IllegalArgumentException( "token '" + tokenName +"' is not a child-token of '" + previousTokenName + "'.  exception occurred while calculating token from relative path '" + relativePath  + "'" );
        }
      }
    }
    return token;
  }

}
 

⌨️ 快捷键说明

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