KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > bsf > BSFFunctionProvider


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

5 package com.opensymphony.workflow.util.bsf;
6
7 import com.ibm.bsf.BSFEngine;
8 import com.ibm.bsf.BSFException;
9 import com.ibm.bsf.BSFManager;
10
11 import com.opensymphony.module.propertyset.PropertySet;
12
13 import com.opensymphony.util.TextUtils;
14
15 import com.opensymphony.workflow.*;
16 import com.opensymphony.workflow.spi.WorkflowEntry;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import java.util.Map JavaDoc;
22
23
24 /**
25  *
26  *
27  * @author $Author: hani $
28  * @version $Revision: 1.2 $
29  */

30 public class BSFFunctionProvider implements FunctionProvider {
31     //~ Static fields/initializers /////////////////////////////////////////////
32

33     private static final Log log = LogFactory.getLog(BSFFunctionProvider.class);
34
35     //~ Methods ////////////////////////////////////////////////////////////////
36

37     public void execute(Map JavaDoc transientVars, Map JavaDoc args, PropertySet ps) throws WorkflowException {
38         String JavaDoc language = (String JavaDoc) args.get(AbstractWorkflow.BSF_LANGUAGE);
39         String JavaDoc source = (String JavaDoc) args.get(AbstractWorkflow.BSF_SOURCE);
40         int row = TextUtils.parseInt((String JavaDoc) args.get(AbstractWorkflow.BSF_ROW));
41         int col = TextUtils.parseInt((String JavaDoc) args.get(AbstractWorkflow.BSF_COL));
42         String JavaDoc script = (String JavaDoc) args.get(AbstractWorkflow.BSF_SCRIPT);
43
44         WorkflowContext context = (WorkflowContext) transientVars.get("context");
45         WorkflowEntry entry = (WorkflowEntry) transientVars.get("entry");
46
47         BSFManager mgr = new BSFManager();
48         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
49
50         if (loader != null) {
51             mgr.setClassLoader(loader);
52         }
53
54         mgr.registerBean("entry", entry);
55         mgr.registerBean("context", context);
56         mgr.registerBean("transientVars", transientVars);
57         mgr.registerBean("propertySet", ps);
58
59         BSFEngine engine = null;
60
61         try {
62             engine = mgr.loadScriptingEngine(language);
63         } catch (BSFException e) {
64             String JavaDoc message = "Could not load scripting engine for BSF language: " + language;
65             throw new WorkflowException(message, e);
66         }
67
68         try {
69             engine.eval(source, row, col, script);
70         } catch (BSFException e) {
71             String JavaDoc message = "BSF script/function could not run";
72             throw new WorkflowException(message, e);
73         }
74     }
75 }
76
Popular Tags