KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > beanshell > BeanShellFunctionProvider


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.util.beanshell;
6
7 import bsh.EvalError;
8 import bsh.Interpreter;
9 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 JavaDoc;
20
21
22 /**
23  *
24  *
25  * @author $Author: hani $
26  * @version $Revision: 1.3 $
27  */

28 public class BeanShellFunctionProvider implements FunctionProvider {
29     //~ Static fields/initializers /////////////////////////////////////////////
30

31     private static final Log log = LogFactory.getLog(BeanShellFunctionProvider.class);
32
33     //~ Methods ////////////////////////////////////////////////////////////////
34

35     public void execute(Map JavaDoc transientVars, Map JavaDoc args, PropertySet ps) throws WorkflowException {
36         String JavaDoc script = (String JavaDoc) args.get(AbstractWorkflow.BSH_SCRIPT);
37         Interpreter i = null;
38         ClassLoader JavaDoc loader = null;
39         WorkflowContext context = (WorkflowContext) transientVars.get("context");
40         WorkflowEntry entry = (WorkflowEntry) transientVars.get("entry");
41         loader = Thread.currentThread().getContextClassLoader();
42
43         try {
44             i = new Interpreter();
45
46             if (loader != null) {
47                 i.setClassLoader(loader);
48             }
49
50             i.set("entry", entry);
51             i.set("context", context);
52             i.set("transientVars", transientVars);
53             i.set("propertySet", ps);
54         } catch (EvalError evalError) {
55             String JavaDoc message = "Could not set values for BSH script";
56             log.error(message, evalError);
57             throw new WorkflowException(message, evalError);
58         }
59
60         try {
61             i.eval(script);
62         } catch (TargetError targetError) {
63             if (targetError.getTarget() instanceof WorkflowException) {
64                 throw (WorkflowException) targetError.getTarget();
65             } else {
66                 String JavaDoc message = "Evaluation error while running BSH function script";
67                 throw new WorkflowException(message, targetError.getTarget());
68             }
69         } catch (EvalError evalError) {
70             String JavaDoc message = "Evaluation error while running BSH function script";
71             log.error(message, evalError);
72             throw new WorkflowException(message, evalError);
73         } finally {
74             if (loader != null) {
75                 i.setClassLoader(null);
76             }
77         }
78     }
79 }
80
Popular Tags