01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.workflow.util.beanshell;
06:
07: import bsh.EvalError;
08: import bsh.Interpreter;
09: 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: *
25: * @author Hani
26: */
27: public class BeanShellCondition implements Condition {
28: //~ Static fields/initializers /////////////////////////////////////////////
29:
30: private static final Log log = LogFactory
31: .getLog(BeanShellCondition.class);
32:
33: //~ Methods ////////////////////////////////////////////////////////////////
34:
35: public boolean passesCondition(Map transientVars, Map args,
36: PropertySet ps) throws WorkflowException {
37: String script = (String) args.get(AbstractWorkflow.BSH_SCRIPT);
38:
39: WorkflowContext context = (WorkflowContext) transientVars
40: .get("context");
41: WorkflowEntry entry = (WorkflowEntry) transientVars
42: .get("entry");
43:
44: Interpreter i = new Interpreter();
45: ClassLoader loader = Thread.currentThread()
46: .getContextClassLoader();
47:
48: try {
49: if (loader != null) {
50: i.setClassLoader(loader);
51: }
52:
53: i.set("entry", entry);
54: i.set("context", context);
55: i.set("transientVars", transientVars);
56: i.set("propertySet", ps);
57: i.set("jn", transientVars.get("jn"));
58:
59: Object o = i.eval(script);
60:
61: if (o == null) {
62: return false;
63: } else {
64: return TextUtils.parseBoolean(o.toString());
65: }
66: } catch (TargetError targetError) {
67: if (targetError.getTarget() instanceof WorkflowException) {
68: throw (WorkflowException) targetError.getTarget();
69: } else {
70: String message = "Could not execute BeanShell script";
71: throw new WorkflowException(message, targetError
72: .getTarget());
73: }
74: } catch (EvalError e) {
75: String message = "Could not execute BeanShell script";
76: log.error(message, e);
77: throw new WorkflowException(message, e);
78: } finally {
79: if (loader != null) {
80: i.setClassLoader(null);
81: }
82: }
83: }
84: }
|