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: *
22: *
23: * @author $Author: hani $
24: * @version $Revision: 1.3 $
25: */
26: public class BSFFunctionProvider implements FunctionProvider {
27: //~ Methods ////////////////////////////////////////////////////////////////
28:
29: public void execute(Map transientVars, Map args, PropertySet ps)
30: throws WorkflowException {
31: String language = (String) args
32: .get(AbstractWorkflow.BSF_LANGUAGE);
33: String source = (String) args.get(AbstractWorkflow.BSF_SOURCE);
34: int row = TextUtils.parseInt((String) args
35: .get(AbstractWorkflow.BSF_ROW));
36: int col = TextUtils.parseInt((String) args
37: .get(AbstractWorkflow.BSF_COL));
38: String script = (String) args.get(AbstractWorkflow.BSF_SCRIPT);
39:
40: WorkflowContext context = (WorkflowContext) transientVars
41: .get("context");
42: WorkflowEntry entry = (WorkflowEntry) transientVars
43: .get("entry");
44:
45: BSFManager mgr = new BSFManager();
46: ClassLoader loader = Thread.currentThread()
47: .getContextClassLoader();
48:
49: if (loader != null) {
50: mgr.setClassLoader(loader);
51: }
52:
53: mgr.registerBean("entry", entry);
54: mgr.registerBean("context", context);
55: mgr.registerBean("transientVars", transientVars);
56: mgr.registerBean("propertySet", ps);
57:
58: BSFEngine engine;
59:
60: try {
61: engine = mgr.loadScriptingEngine(language);
62: } catch (BSFException e) {
63: String message = "Could not load scripting engine for BSF language: "
64: + language;
65: throw new WorkflowException(message, e);
66: }
67:
68: try {
69: engine.eval(source, row, col, script);
70: } catch (BSFException e) {
71: String message = "BSF script/function could not run";
72: throw new WorkflowException(message, e);
73: }
74: }
75: }
|