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

📄 compositecommand.java

📁 workflow first jbpm
💻 JAVA
字号:
package org.jbpm.command;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jbpm.JbpmContext;
import org.jbpm.JbpmException;

public class CompositeCommand implements Command {

  private static final long serialVersionUID = 1L;
  
  List commands = null;
  
  public CompositeCommand(List commands) {
    this.commands = commands;
  }

  public Object execute(JbpmContext jbpmContext) throws Exception {
    List results = null;
    if (commands!=null) {
      Object lastResult = null;
      results = new ArrayList(commands.size());
      Iterator iter = commands.iterator();
      while(iter.hasNext()) {
        Command command = (Command)iter.next();
        if (lastResult!=null) {
          tryToInject(lastResult, command);
        }
        lastResult = command.execute(jbpmContext);
        results.add(lastResult);
      }
    }
    return results;
  }

  protected void tryToInject(Object lastResult, Command command) {
    Field field = findField(lastResult.getClass());
    if (field!=null) {
      field.setAccessible(true);
      try {
        field.set(command, lastResult);
      } catch (Exception e) {
        throw new JbpmException("couldn't propagate composite command context", e);
      }
    }
  }

  protected Field findField(Class clazz) {
    Field field = null;
    int i=0;
    Field[] fields = clazz.getDeclaredFields();
    while ( (i<fields.length)
            && (field==null) 
          ) {
      Field candidate = fields[i];
      if ( (candidate.getType().isAssignableFrom(clazz))
           && (candidate.getName().startsWith("previous"))
         ) {
        field = candidate;
      }
      i++;
    }
    return field;
  }
}

⌨️ 快捷键说明

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