01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.workflow.util.bsf;
06:
07: import com.opensymphony.module.propertyset.PropertySet;
08:
09: import com.opensymphony.util.TextUtils;
10:
11: import com.opensymphony.workflow.*;
12: import com.opensymphony.workflow.spi.WorkflowEntry;
13:
14: import org.apache.bsf.BSFEngine;
15: import org.apache.bsf.BSFException;
16: import org.apache.bsf.BSFManager;
17:
18: import java.util.Map;
19:
20: /**
21: * @author Hani
22: */
23: public class BSFCondition implements Condition {
24: //~ Methods ////////////////////////////////////////////////////////////////
25:
26: public boolean passesCondition(Map transientVars, Map args,
27: PropertySet ps) throws WorkflowException {
28: String language = (String) args
29: .get(AbstractWorkflow.BSF_LANGUAGE);
30: String source = (String) args.get(AbstractWorkflow.BSF_SOURCE);
31: int row = TextUtils.parseInt((String) args
32: .get(AbstractWorkflow.BSF_ROW));
33: int col = TextUtils.parseInt((String) args
34: .get(AbstractWorkflow.BSF_COL));
35: String script = (String) args.get(AbstractWorkflow.BSF_SCRIPT);
36:
37: WorkflowContext context = (WorkflowContext) transientVars
38: .get("context");
39: WorkflowEntry entry = (WorkflowEntry) transientVars
40: .get("entry");
41:
42: BSFManager mgr = new BSFManager();
43: ClassLoader loader = Thread.currentThread()
44: .getContextClassLoader();
45:
46: if (loader != null) {
47: mgr.setClassLoader(loader);
48: }
49:
50: mgr.registerBean("entry", entry);
51: mgr.registerBean("context", context);
52: mgr.registerBean("transientVars", transientVars);
53: mgr.registerBean("propertySet", ps);
54: mgr.registerBean("jn", transientVars.get("jn"));
55:
56: try {
57: BSFEngine engine = mgr.loadScriptingEngine(language);
58: Object o = engine.eval(source, row, col, script);
59:
60: if (o == null) {
61: return false;
62: } else {
63: return TextUtils.parseBoolean(o.toString());
64: }
65: } catch (BSFException e) {
66: String message = "Could not execute BSF script";
67: throw new WorkflowException(message, e);
68: }
69: }
70: }
|