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.util.TextUtils; 14 15 import com.opensymphony.workflow.*; 16 import com.opensymphony.workflow.spi.WorkflowEntry; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 21 import java.util.Map ; 22 23 24 30 public class BeanShellCondition implements Condition { 31 33 private static final Log log = LogFactory.getLog(BeanShellCondition.class); 34 35 37 public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException { 38 String script = (String ) args.get(AbstractWorkflow.BSH_SCRIPT); 39 40 WorkflowContext context = (WorkflowContext) transientVars.get("context"); 41 WorkflowEntry entry = (WorkflowEntry) transientVars.get("entry"); 42 43 Interpreter i = new Interpreter(); 44 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 45 46 try { 47 if (loader != null) { 48 i.setClassLoader(loader); 49 } 50 51 i.set("entry", entry); 52 i.set("context", context); 53 i.set("transientVars", transientVars); 54 i.set("propertySet", ps); 55 i.set("jn", transientVars.get("jn")); 56 57 Object o = i.eval(script); 58 59 if (o == null) { 60 return false; 61 } else { 62 return TextUtils.parseBoolean(o.toString()); 63 } 64 } catch (TargetError targetError) { 65 if (targetError.getTarget() instanceof WorkflowException) { 66 throw (WorkflowException) targetError.getTarget(); 67 } else { 68 String message = "Could not execute BeanShell script"; 69 throw new WorkflowException(message, targetError.getTarget()); 70 } 71 } catch (EvalError e) { 72 String message = "Could not execute BeanShell script"; 73 log.error(message, e); 74 throw new WorkflowException(message, e); 75 } finally { 76 if (loader != null) { 77 i.setClassLoader(null); 78 } 79 } 80 } 81 } 82 | Popular Tags |