KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > SimpleMethodBsfEngine


1 /*
2  * $Id: SimpleMethodBsfEngine.java 5823 2005-09-25 23:11:57Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.minilang.method.MethodContext;
33
34 import com.ibm.bsf.BSFDeclaredBean;
35 import com.ibm.bsf.BSFException;
36 import com.ibm.bsf.BSFManager;
37 import com.ibm.bsf.util.BSFEngineImpl;
38
39 /**
40  * <P>This is the OFBiz MiniLang SimpleMethod adapter for IBM's Bean Scripting Famework.
41  * It is an implementation of the BSFEngine class, allowing BSF aware
42  * applications to use SimpleMethod as a scripting language.
43  *
44  * <P>There should only be ONE simple-method in the XML file and it will be run as an event.
45  *
46  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
47  */

48 public class SimpleMethodBsfEngine extends BSFEngineImpl {
49     
50     public static final String JavaDoc module = SimpleMethodBsfEngine.class.getName();
51     
52     protected Map JavaDoc context = new HashMap JavaDoc();
53     
54     public void initialize(BSFManager mgr, String JavaDoc lang, Vector JavaDoc declaredBeans) throws BSFException {
55         super.initialize(mgr, lang, declaredBeans);
56         
57         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
58         
59         // declare the bsf manager for callbacks, etc.
60
context.put("bsf", mgr);
61         
62         for(int i=0; i<declaredBeans.size(); i++) {
63             BSFDeclaredBean bean = (BSFDeclaredBean)declaredBeans.get(i);
64             declareBean(bean);
65         }
66     }
67     
68     public void setDebug(boolean debug) {
69         //interpreter.DEBUG=debug;
70
}
71     
72     /**
73      * Invoke method name on the specified scripted object.
74      * The object may be null to indicate the global namespace of the
75      * interpreter.
76      * @param object may be null for the global namespace.
77      */

78     public Object JavaDoc call(Object JavaDoc object, String JavaDoc name, Object JavaDoc[] args) throws BSFException {
79         throw new BSFException("The call method is not yet supported for SimpleMethods");
80     }
81     
82     
83     /**
84      * This is an implementation of the apply() method.
85      * It exectutes the funcBody text in an "anonymous" method call with
86      * arguments.
87      */

88     public Object JavaDoc apply(String JavaDoc source, int lineNo, int columnNo, Object JavaDoc funcBody, Vector JavaDoc namesVec, Vector JavaDoc argsVec) throws BSFException {
89         //if (namesVec.size() != argsVec.size()) throw new BSFException("number of params/names mismatch");
90
//if (!(funcBody instanceof String)) throw new BSFException("apply: function body must be a string");
91

92         throw new BSFException("The apply method is not yet supported for simple-methods");
93     }
94     
95     public Object JavaDoc eval(String JavaDoc source, int lineNo, int columnNo, Object JavaDoc expr) throws BSFException {
96         if (!(expr instanceof String JavaDoc)) throw new BSFException("simple-method expression must be a string");
97
98         //right now only supports one method per file, so get all methods and just run the first...
99
Map JavaDoc simpleMethods = null;
100         try {
101             simpleMethods = SimpleMethod.getDirectSimpleMethods(source, (String JavaDoc) expr, "<bsf source>");
102         } catch (MiniLangException e) {
103             throw new BSFException("Error loading/parsing simple-method XML source: " + e.getMessage());
104         }
105         Set JavaDoc smNames = simpleMethods.keySet();
106         if (smNames.size() == 0) throw new BSFException("Did not find any simple-methods in the file");
107
108         String JavaDoc methodName = (String JavaDoc) smNames.iterator().next();
109         if (smNames.size() > 1) Debug.logWarning("Found more than one simple-method in the file, running the [" + methodName + "] method, you should remove all but one method from this file", module);
110
111         SimpleMethod simpleMethod = (SimpleMethod) simpleMethods.get(methodName);
112         MethodContext methodContext = new MethodContext(context, null, MethodContext.EVENT);
113         return simpleMethod.exec(methodContext);
114         //methodContext.getResults();
115
}
116     
117     
118     public void exec(String JavaDoc source, int lineNo, int columnNo, Object JavaDoc script) throws BSFException {
119         eval(source, lineNo, columnNo, script);
120     }
121     
122     
123 /*
124         public void compileApply (String source, int lineNo, int columnNo,
125                 Object funcBody, Vector paramNames, Vector arguments, CodeBuffer cb)
126                 throws BSFException;
127  
128         public void compileExpr (String source, int lineNo, int columnNo,
129                 Object expr, CodeBuffer cb) throws BSFException;
130  
131         public void compileScript (String source, int lineNo, int columnNo,
132                 Object script, CodeBuffer cb) throws BSFException;
133  */

134     
135     public void declareBean(BSFDeclaredBean bean) throws BSFException {
136         context.put(bean.name, bean.bean);
137     }
138     
139     public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
140         context.remove(bean.name);
141     }
142     
143     public void terminate() { }
144     
145     private String JavaDoc sourceInfo(String JavaDoc source, int lineNo, int columnNo) {
146         return "SimpleMethod: " + source + " at line: " + lineNo +" column: " + columnNo;
147     }
148 }
149
Popular Tags