001: /*
002: * Copyright 2004,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.bsf.util;
018:
019: import java.beans.PropertyChangeEvent;
020: import java.util.Vector;
021:
022: import org.apache.bsf.BSFDeclaredBean;
023: import org.apache.bsf.BSFEngine;
024: import org.apache.bsf.BSFException;
025: import org.apache.bsf.BSFManager;
026:
027: /**
028: * This is a base implementation of the BSFEngine interface which
029: * engine implementations may choose to extend to get the basic
030: * methods of the interface implemented.
031: * <p>
032: *
033: * @author Sanjiva Weerawarana
034: * @author Olivier Gruber (added original debugging support)
035: */
036:
037: public abstract class BSFEngineImpl implements BSFEngine {
038:
039: protected BSFManager mgr; // my manager
040: protected String lang; // my language string
041: protected Vector declaredBeans; // BSFDeclaredBeans
042: protected String classPath;
043: protected String tempDir;
044: protected ClassLoader classLoader;
045:
046: /**
047: * Default impl of apply - calls eval ignoring parameters and returns
048: * the result.
049: */
050: public Object apply(String source, int lineNo, int columnNo,
051: Object funcBody, Vector paramNames, Vector arguments)
052: throws BSFException {
053: return eval(source, lineNo, columnNo, funcBody);
054: }
055:
056: /**
057: * Default impl of compileApply - calls compileExpr ignoring parameters.
058: */
059: public void compileApply(String source, int lineNo, int columnNo,
060: Object funcBody, Vector paramNames, Vector arguments,
061: CodeBuffer cb) throws BSFException {
062: compileExpr(source, lineNo, columnNo, funcBody, cb);
063: }
064:
065: /**
066: * Default impl of compileExpr - generates code that'll create a new
067: * manager, evaluate the expression, and return the value.
068: */
069: public void compileExpr(String source, int lineNo, int columnNo,
070: Object expr, CodeBuffer cb) throws BSFException {
071: ObjInfo bsfInfo = cb.getSymbol("bsf");
072:
073: if (bsfInfo == null) {
074: bsfInfo = new ObjInfo(BSFManager.class, "bsf");
075: cb.addFieldDeclaration("org.apache.bsf.BSFManager bsf = "
076: + "new org.apache.bsf.BSFManager();");
077: cb.putSymbol("bsf", bsfInfo);
078: }
079:
080: String evalString = bsfInfo.objName + ".eval(\"" + lang
081: + "\", ";
082: evalString += "request.getRequestURI(), " + lineNo + ", "
083: + columnNo;
084: evalString += "," + StringUtils.lineSeparator;
085: evalString += StringUtils.getSafeString(expr.toString()) + ")";
086:
087: ObjInfo oldRet = cb.getFinalServiceMethodStatement();
088:
089: if (oldRet != null && oldRet.isExecutable()) {
090: cb.addServiceMethodStatement(oldRet.objName + ";");
091: }
092:
093: cb.setFinalServiceMethodStatement(new ObjInfo(Object.class,
094: evalString));
095:
096: cb.addServiceMethodException("org.apache.bsf.BSFException");
097: }
098:
099: /**
100: * Default impl of compileScript - generates code that'll create a new
101: * manager, and execute the script.
102: */
103: public void compileScript(String source, int lineNo, int columnNo,
104: Object script, CodeBuffer cb) throws BSFException {
105: ObjInfo bsfInfo = cb.getSymbol("bsf");
106:
107: if (bsfInfo == null) {
108: bsfInfo = new ObjInfo(BSFManager.class, "bsf");
109: cb.addFieldDeclaration("org.apache.bsf.BSFManager bsf = "
110: + "new org.apache.bsf.BSFManager();");
111: cb.putSymbol("bsf", bsfInfo);
112: }
113:
114: String execString = bsfInfo.objName + ".exec(\"" + lang
115: + "\", ";
116: execString += "request.getRequestURI(), " + lineNo + ", "
117: + columnNo;
118: execString += "," + StringUtils.lineSeparator;
119: execString += StringUtils.getSafeString(script.toString())
120: + ")";
121:
122: ObjInfo oldRet = cb.getFinalServiceMethodStatement();
123:
124: if (oldRet != null && oldRet.isExecutable()) {
125: cb.addServiceMethodStatement(oldRet.objName + ";");
126: }
127:
128: cb.setFinalServiceMethodStatement(new ObjInfo(void.class,
129: execString));
130:
131: cb.addServiceMethodException("org.apache.bsf.BSFException");
132: }
133:
134: public void declareBean(BSFDeclaredBean bean) throws BSFException {
135: throw new BSFException(BSFException.REASON_UNSUPPORTED_FEATURE,
136: "language " + lang
137: + " does not support declareBean(...).");
138: }
139:
140: /**
141: * Default impl of execute - calls eval and ignores the result.
142: */
143: public void exec(String source, int lineNo, int columnNo,
144: Object script) throws BSFException {
145: eval(source, lineNo, columnNo, script);
146: }
147:
148: /**
149: * Default impl of interactive execution - calls eval and ignores the result.
150: */
151: public void iexec(String source, int lineNo, int columnNo,
152: Object script) throws BSFException {
153: eval(source, lineNo, columnNo, script);
154: }
155:
156: /**
157: * initialize the engine; called right after construction by
158: * the manager. Declared beans are simply kept in a vector and
159: * that's it. Subclasses must do whatever they want with it.
160: */
161: public void initialize(BSFManager mgr, String lang,
162: Vector declaredBeans) throws BSFException {
163:
164: this .mgr = mgr;
165: this .lang = lang;
166: this .declaredBeans = declaredBeans;
167:
168: // initialize my properties from those of the manager. It'll send
169: // propagate change events to me
170: this .classPath = mgr.getClassPath();
171: this .tempDir = mgr.getTempDir();
172: this .classLoader = mgr.getClassLoader();
173: }
174:
175: /**
176: * Receive property change events from the manager and update my fields
177: * as needed.
178: *
179: * @param e PropertyChange event with the change data
180: */
181: public void propertyChange(PropertyChangeEvent e) {
182: String name = e.getPropertyName();
183: Object value = e.getNewValue();
184:
185: if (name.equals("classPath")) {
186: classPath = (String) value;
187: } else if (name.equals("tempDir")) {
188: tempDir = (String) value;
189: } else if (name.equals("classLoader")) {
190: classLoader = (ClassLoader) value;
191: }
192: }
193:
194: public void terminate() {
195: mgr = null;
196: declaredBeans = null;
197: classLoader = null;
198: }
199:
200: public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
201: throw new BSFException(BSFException.REASON_UNSUPPORTED_FEATURE,
202: "language " + lang
203: + " does not support undeclareBean(...).");
204: }
205: }
|