KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > util > BSFEngineImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

55
56 package org.apache.bsf.util;
57
58 import java.rmi.RemoteException JavaDoc;
59 import java.util.*;
60 import java.io.*;
61 import java.beans.*;
62 import org.apache.bsf.*;
63
64 /**
65  * This is a base implementation of the BSFEngine interface which
66  * engine implementations may choose to extend to get the basic
67  * methods of the interface implemented.
68  * <p>
69  *
70  * @author Sanjiva Weerawarana
71  * @author Olivier Gruber (adding debugging support)
72  */

73
74 public abstract class BSFEngineImpl implements BSFEngine {
75
76     protected BSFManager mgr; // my manager
77
protected BSFDebugManagerImpl dbgmgr; // the global debug manager.
78
protected String JavaDoc lang; // my language string
79
protected Vector declaredBeans; // BSFDeclaredBeans
80
protected String JavaDoc classPath;
81     protected String JavaDoc tempDir;
82     protected ClassLoader JavaDoc classLoader;
83
84     /**
85      * By default, an engine does not support debugging.
86      * Subclasses will need to redefine this method in order
87      * to provide debuggers with their language-specific
88      * interface for debugging.
89      */

90     public Object JavaDoc getSpecificDebuggingInterface() {
91         return null;
92     }
93
94     /**
95      * Basic engines are not supporting breakpoints for the meaning
96      * and support is something that is language-specific.
97      */

98     public void disconnectedDebuggerNotify() {
99     }
100
101     public void placeBreakpointAtLine(int brkptid, String JavaDoc docname,
102                                       int lineno) throws BSFException {
103         throw new BSFException (BSFException.REASON_UNSUPPORTED_FEATURE,
104                                 "BSF:" + lang +
105                                 "engine does not yet support debugging.");
106     }
107
108     public void placeBreakpointAtOffset(int brkptid, String JavaDoc docname,
109                                         int offset) throws BSFException {
110         throw new BSFException (BSFException.REASON_UNSUPPORTED_FEATURE,
111                                 "BSF:" + lang +
112                                 "engine does not yet support debugging.");
113     }
114
115     public void removeBreakpoint(String JavaDoc docname, int brkptid)
116         throws BSFException {
117         throw new BSFException (BSFException.REASON_UNSUPPORTED_FEATURE,
118                                 "BSF:" + lang +
119                                 "engine does not yet support debugging.");
120     }
121
122     public void setEntryExit(String JavaDoc docname, boolean on)
123         throws BSFException {
124         throw new BSFException (BSFException.REASON_UNSUPPORTED_FEATURE,
125                                 "BSF:" + lang +
126                                 "engine does not yet support debugging.");
127     }
128     
129     /**
130      * Default impl of apply - calls eval ignorning parameters and returns
131      * the result.
132      */

133     public Object JavaDoc apply(String JavaDoc source, int lineNo, int columnNo,
134                         Object JavaDoc funcBody, Vector paramNames, Vector arguments)
135         throws BSFException {
136         return eval(source, lineNo, columnNo, funcBody);
137     }
138
139     /**
140      * Default impl of compileApply - calls compileExpr ignorning parameters.
141      */

142     public void compileApply(String JavaDoc source, int lineNo, int columnNo,
143                              Object JavaDoc funcBody, Vector paramNames,
144                              Vector arguments, CodeBuffer cb)
145         throws BSFException {
146         compileExpr(source, lineNo, columnNo, funcBody, cb);
147     }
148
149     /**
150      * Default impl of compileExpr - generates code that'll create a new
151      * manager, evaluate the expression, and return the value.
152      */

153     public void compileExpr(String JavaDoc source, int lineNo, int columnNo,
154                             Object JavaDoc expr, CodeBuffer cb) throws BSFException {
155         ObjInfo bsfInfo = cb.getSymbol("bsf");
156         
157         if (bsfInfo == null) {
158             bsfInfo = new ObjInfo(BSFManager.class, "bsf");
159             cb.addFieldDeclaration("org.apache.bsf.BSFManager bsf = " +
160                                    "new org.apache.bsf.BSFManager();");
161             cb.putSymbol("bsf", bsfInfo);
162         }
163
164         String JavaDoc evalString = bsfInfo.objName + ".eval(\"" + lang + "\", ";
165         evalString += "request.getRequestURI(), " + lineNo + ", " + columnNo;
166         evalString += "," + StringUtils.lineSeparator;
167         evalString += StringUtils.getSafeString(expr.toString()) + ")";
168
169         ObjInfo oldRet = cb.getFinalServiceMethodStatement();
170
171         if (oldRet != null && oldRet.isExecutable()) {
172             cb.addServiceMethodStatement(oldRet.objName + ";");
173         }
174
175         cb.setFinalServiceMethodStatement(new ObjInfo(Object JavaDoc.class,
176                                                       evalString));
177         
178         cb.addServiceMethodException("org.apache.bsf.BSFException");
179     }
180
181     /**
182      * Default impl of compileScript - generates code that'll create a new
183      * manager, and execute the script.
184      */

185     public void compileScript(String JavaDoc source, int lineNo, int columnNo,
186                               Object JavaDoc script, CodeBuffer cb)
187         throws BSFException {
188         ObjInfo bsfInfo = cb.getSymbol("bsf");
189         
190         if (bsfInfo == null) {
191             bsfInfo = new ObjInfo(BSFManager.class, "bsf");
192             cb.addFieldDeclaration("org.apache.bsf.BSFManager bsf = " +
193                                    "new org.apache.bsf.BSFManager();");
194             cb.putSymbol("bsf", bsfInfo);
195         }
196
197         String JavaDoc execString = bsfInfo.objName + ".exec(\"" + lang + "\", ";
198         execString += "request.getRequestURI(), " + lineNo + ", " + columnNo;
199         execString += "," + StringUtils.lineSeparator;
200         execString += StringUtils.getSafeString(script.toString()) + ")";
201
202         ObjInfo oldRet = cb.getFinalServiceMethodStatement();
203
204         if (oldRet != null && oldRet.isExecutable()) {
205             cb.addServiceMethodStatement(oldRet.objName + ";");
206         }
207
208         cb.setFinalServiceMethodStatement(new ObjInfo(void.class, execString));
209
210         cb.addServiceMethodException("org.apache.bsf.BSFException");
211     }
212
213     public void declareBean(BSFDeclaredBean bean) throws BSFException {
214         throw new BSFException(BSFException.REASON_UNSUPPORTED_FEATURE,
215                                "language " + lang +
216                                " does not support declareBean(...).");
217     }
218
219     /**
220      * Default impl of execute - calls eval and ignores the result.
221      */

222     public void exec(String JavaDoc source, int lineNo, int columnNo, Object JavaDoc script)
223         throws BSFException {
224         eval(source, lineNo, columnNo, script);
225     }
226
227     /**
228      * Get the debug manager in the constructor, not in initialize.
229      * First, this is ok since the debug manager is not BSFManager
230      * dependent. Second, the debug manager needs to be known
231      * sooner than "initialize" for the JavaScript engine.
232      */

233     public BSFEngineImpl() {
234         this.dbgmgr = (BSFDebugManagerImpl) BSFManager.getDebugManager();
235     }
236     
237     /**
238      * initialize the engine; called right after construction by
239      * the manager. Declared beans are simply kept in a vector and
240      * that's it. Subclasses must do whatever they want with it.
241      */

242     public void initialize(BSFManager mgr, String JavaDoc lang, Vector declaredBeans)
243         throws BSFException {
244         
245         this.mgr = mgr;
246         this.lang = lang;
247         this.declaredBeans = declaredBeans;
248
249         // initialize my properties from those of the manager. It'll send
250
// propagate change events to me
251
this.classPath = mgr.getClassPath();
252         this.tempDir = mgr.getTempDir();
253         this.classLoader = mgr.getClassLoader();
254         
255         // All BSFManager instances and engines are registering themselves
256
// to the debug manager. This enables the debug manager to make
257
// the connection between the existing engines and the registered
258
// debuggers.
259
// Also see terminate() for the notification of termination
260
// to the debug manager.
261
//
262
// NOTE: will happen only when BSF is in debug mode
263
// This will avoid the potentially severe bottleneck
264
// at the BSFDebugManager level in WebSphere or Tomcat...
265
if (dbgmgr != null) {
266             dbgmgr.registerEngine(mgr, this, lang);
267         }
268     }
269
270     /**
271      * Receive property change events from the manager and update my fields
272      * as needed.
273      *
274      * @param e PropertyChange event with the change data
275      */

276     public void propertyChange(PropertyChangeEvent e) {
277         String JavaDoc name = e.getPropertyName();
278         Object JavaDoc value = e.getNewValue();
279         
280         if (name.equals("classPath")) {
281             classPath = (String JavaDoc) value;
282         }
283         else if (name.equals("tempDir")) {
284             tempDir = (String JavaDoc) value;
285         }
286         else if (name.equals("classLoader")) {
287             classLoader = (ClassLoader JavaDoc) value;
288         }
289     }
290     
291     public void terminate() {
292         mgr = null;
293         declaredBeans = null;
294         classLoader = null;
295         
296         // See initialize method.
297
// Notify the debug manager so that it knows about
298
// the existing BSFManager and BSFEngine instances.
299
if (dbgmgr != null) {
300             dbgmgr.terminateEngineNotify(mgr, this, lang);
301         }
302     }
303     
304     public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
305         throw new BSFException(BSFException.REASON_UNSUPPORTED_FEATURE,
306                                "language " + lang +
307                                " does not support undeclareBean(...).");
308     }
309 }
310
Popular Tags