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 java.util.Map;
17:
18: /**
19: * A register that executes a beanshell script when invoked.
20: * @author Hani
21: */
22: public class BeanShellRegister implements Register {
23: //~ Methods ////////////////////////////////////////////////////////////////
24:
25: public Object registerVariable(WorkflowContext context,
26: WorkflowEntry entry, Map args, PropertySet ps)
27: throws WorkflowException {
28: String script = (String) args.get(AbstractWorkflow.BSH_SCRIPT);
29:
30: Interpreter i = new Interpreter();
31: ClassLoader loader = Thread.currentThread()
32: .getContextClassLoader();
33:
34: try {
35: if (loader != null) {
36: i.setClassLoader(loader);
37: }
38:
39: i.set("entry", entry);
40: i.set("context", context);
41: i.set("propertySet", ps);
42:
43: return i.eval(script);
44: } catch (TargetError targetError) {
45: if (targetError.getTarget() instanceof WorkflowException) {
46: throw (WorkflowException) targetError.getTarget();
47: } else {
48: String message = "Could not get object registered in to variable map";
49: throw new WorkflowException(message, targetError
50: .getTarget());
51: }
52: } catch (EvalError e) {
53: String message = "Could not get object registered in to variable map";
54: throw new WorkflowException(message, e);
55: } finally {
56: if (loader != null) {
57: i.setClassLoader(null);
58: }
59: }
60: }
61: }
|