/* * * $author: $ * * $Date: $ * * $Archive: $ * * $Log: $ */ package de.fs24.services.wfms.actionhandler; import java.util.Iterator; import java.util.Map; import org.apache.log4j.Logger; import org.jbpm.ConfigurationException; import org.jbpm.delegation.Configurable; import org.jbpm.delegation.DecisionHandler; import org.jbpm.delegation.ExecutionContext; import org.jbpm.model.execution.Token; import bsh.EvalError; import bsh.Interpreter; /** * Integrates beanshell scripts into a DecisionHandler. */ public class BeanShellDecisionHandler implements DecisionHandler, Configurable { private static final Logger logger = Logger.getLogger(BeanShellDecisionHandler.class); String beanShellScript = null; public void configure(String config) throws ConfigurationException { this.beanShellScript = config; } /** * */ public BeanShellDecisionHandler() { super(); } /** * Integrates the use of beanshell into an {@link org.jbpm.delegation.DecisionHandler}. All * workflow variables in the variables Map that use a java.lang.String-Object as a * key are passed to the beanshell script and can be used there.
* The beanshell script must define a variable named transition that is set to the * transition that should be taken. The method decide returns this variable's value. * * @param executionContext jbpm ExecutionContext */ public String decide(ExecutionContext executionContext) { Interpreter i = new Interpreter(); Token currentToken = executionContext.getToken(); // Uebertrage Inhalte der Workflow-Variablen (nur solche mit Key-Typ String!) Map variables = executionContext.getExecutionService().getVariables(currentToken.getId()); for (Iterator iter = variables.entrySet().iterator(); iter.hasNext();) { Map.Entry mapEntry = (Map.Entry) iter.next(); if (mapEntry.getKey() instanceof String) { logger.debug("var: " + mapEntry.getKey() + "=" + mapEntry.getValue()); try { i.set((String)mapEntry.getKey(), mapEntry.getValue()); } catch (EvalError e) { logger.error("Fehler beim Setzen von BeanShell var " + mapEntry); throw new RuntimeException("Fehler beim Setzen von BeanShell var " + mapEntry); } } } try { i.eval(this.beanShellScript); String transition = (String)i.get("transition"); return transition; } catch (EvalError e) { logger.error("Fehler beim Ausfuehren des BeanShell-Scriptes " + this.beanShellScript); throw new RuntimeException("Fehler beim Ausfuehren des BeanShell-Scriptes " + this.beanShellScript); } } }