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.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: *
23: *
24: * @author Hani
25: */
26: public class BeanShellFunctionProvider implements FunctionProvider {
27: //~ Static fields/initializers /////////////////////////////////////////////
28:
29: private static final Log log = LogFactory
30: .getLog(BeanShellFunctionProvider.class);
31:
32: //~ Methods ////////////////////////////////////////////////////////////////
33:
34: public void execute(Map transientVars, Map args, PropertySet ps)
35: throws WorkflowException {
36: String script = (String) args.get(AbstractWorkflow.BSH_SCRIPT);
37: Interpreter i = null;
38: ClassLoader loader = null;
39: WorkflowContext context = (WorkflowContext) transientVars
40: .get("context");
41: WorkflowEntry entry = (WorkflowEntry) transientVars
42: .get("entry");
43: loader = Thread.currentThread().getContextClassLoader();
44:
45: try {
46: i = new Interpreter();
47:
48: if (loader != null) {
49: i.setClassLoader(loader);
50: }
51:
52: i.set("entry", entry);
53: i.set("context", context);
54: i.set("transientVars", transientVars);
55: i.set("propertySet", ps);
56: } catch (EvalError evalError) {
57: String message = "Could not set values for BSH script";
58: log.error(message, evalError);
59: throw new WorkflowException(message, evalError);
60: }
61:
62: try {
63: i.eval(script);
64: } catch (TargetError targetError) {
65: if (targetError.getTarget() instanceof WorkflowException) {
66: throw (WorkflowException) targetError.getTarget();
67: } else {
68: String message = "Evaluation error while running BSH function script";
69: throw new WorkflowException(message, targetError
70: .getTarget());
71: }
72: } catch (EvalError evalError) {
73: String message = "Evaluation error while running BSH function script";
74: log.error(message, evalError);
75: throw new WorkflowException(message, evalError);
76: } finally {
77: if (loader != null) {
78: i.setClassLoader(null);
79: }
80: }
81: }
82: }
|