1 5 package com.opensymphony.workflow.util.beanshell; 6 7 import bsh.EvalError; 8 import bsh.Interpreter; 9 import bsh.TargetError; 10 11 import com.opensymphony.module.propertyset.PropertySet; 12 13 import com.opensymphony.workflow.*; 14 import com.opensymphony.workflow.spi.WorkflowEntry; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 import java.util.Map ; 20 21 22 28 public class BeanShellFunctionProvider implements FunctionProvider { 29 31 private static final Log log = LogFactory.getLog(BeanShellFunctionProvider.class); 32 33 35 public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException { 36 String script = (String ) args.get(AbstractWorkflow.BSH_SCRIPT); 37 Interpreter i = null; 38 ClassLoader loader = null; 39 WorkflowContext context = (WorkflowContext) transientVars.get("context"); 40 WorkflowEntry entry = (WorkflowEntry) transientVars.get("entry"); 41 loader = Thread.currentThread().getContextClassLoader(); 42 43 try { 44 i = new Interpreter(); 45 46 if (loader != null) { 47 i.setClassLoader(loader); 48 } 49 50 i.set("entry", entry); 51 i.set("context", context); 52 i.set("transientVars", transientVars); 53 i.set("propertySet", ps); 54 } catch (EvalError evalError) { 55 String message = "Could not set values for BSH script"; 56 log.error(message, evalError); 57 throw new WorkflowException(message, evalError); 58 } 59 60 try { 61 i.eval(script); 62 } catch (TargetError targetError) { 63 if (targetError.getTarget() instanceof WorkflowException) { 64 throw (WorkflowException) targetError.getTarget(); 65 } else { 66 String message = "Evaluation error while running BSH function script"; 67 throw new WorkflowException(message, targetError.getTarget()); 68 } 69 } catch (EvalError evalError) { 70 String message = "Evaluation error while running BSH function script"; 71 log.error(message, evalError); 72 throw new WorkflowException(message, evalError); 73 } finally { 74 if (loader != null) { 75 i.setClassLoader(null); 76 } 77 } 78 } 79 } 80 | Popular Tags |