001: /*
002: * $Id: SimpleMethodBsfEngine.java,v 1.1 2003/08/17 06:06:13 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028: import java.util.Set;
029: import java.util.Vector;
030:
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.minilang.method.MethodContext;
033:
034: import com.ibm.bsf.BSFDeclaredBean;
035: import com.ibm.bsf.BSFException;
036: import com.ibm.bsf.BSFManager;
037: import com.ibm.bsf.util.BSFEngineImpl;
038:
039: /**
040: * <P>This is the OFBiz MiniLang SimpleMethod adapter for IBM's Bean Scripting Famework.
041: * It is an implementation of the BSFEngine class, allowing BSF aware
042: * applications to use SimpleMethod as a scripting language.
043: *
044: * <P>There should only be ONE simple-method in the XML file and it will be run as an event.
045: *
046: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
047: */
048: public class SimpleMethodBsfEngine extends BSFEngineImpl {
049:
050: public static final String module = SimpleMethodBsfEngine.class
051: .getName();
052:
053: protected Map context = new HashMap();
054:
055: public void initialize(BSFManager mgr, String lang,
056: Vector declaredBeans) throws BSFException {
057: super .initialize(mgr, lang, declaredBeans);
058:
059: ClassLoader classLoader = Thread.currentThread()
060: .getContextClassLoader();
061:
062: // declare the bsf manager for callbacks, etc.
063: context.put("bsf", mgr);
064:
065: for (int i = 0; i < declaredBeans.size(); i++) {
066: BSFDeclaredBean bean = (BSFDeclaredBean) declaredBeans
067: .get(i);
068: declareBean(bean);
069: }
070: }
071:
072: public void setDebug(boolean debug) {
073: //interpreter.DEBUG=debug;
074: }
075:
076: /**
077: * Invoke method name on the specified scripted object.
078: * The object may be null to indicate the global namespace of the
079: * interpreter.
080: * @param object may be null for the global namespace.
081: */
082: public Object call(Object object, String name, Object[] args)
083: throws BSFException {
084: throw new BSFException(
085: "The call method is not yet supported for SimpleMethods");
086: }
087:
088: /**
089: * This is an implementation of the apply() method.
090: * It exectutes the funcBody text in an "anonymous" method call with
091: * arguments.
092: */
093: public Object apply(String source, int lineNo, int columnNo,
094: Object funcBody, Vector namesVec, Vector argsVec)
095: throws BSFException {
096: //if (namesVec.size() != argsVec.size()) throw new BSFException("number of params/names mismatch");
097: //if (!(funcBody instanceof String)) throw new BSFException("apply: function body must be a string");
098:
099: throw new BSFException(
100: "The apply method is not yet supported for simple-methods");
101: }
102:
103: public Object eval(String source, int lineNo, int columnNo,
104: Object expr) throws BSFException {
105: if (!(expr instanceof String))
106: throw new BSFException(
107: "simple-method expression must be a string");
108:
109: //right now only supports one method per file, so get all methods and just run the first...
110: Map simpleMethods = null;
111: try {
112: simpleMethods = SimpleMethod.getDirectSimpleMethods(source,
113: (String) expr);
114: } catch (MiniLangException e) {
115: throw new BSFException(
116: "Error loading/parsing simple-method XML source: "
117: + e.getMessage());
118: }
119: Set smNames = simpleMethods.keySet();
120: if (smNames.size() == 0)
121: throw new BSFException(
122: "Did not find any simple-methods in the file");
123:
124: String methodName = (String) smNames.iterator().next();
125: if (smNames.size() > 1)
126: Debug
127: .logWarning(
128: "Found more than one simple-method in the file, running the ["
129: + methodName
130: + "] method, you should remove all but one method from this file",
131: module);
132:
133: SimpleMethod simpleMethod = (SimpleMethod) simpleMethods
134: .get(methodName);
135: MethodContext methodContext = new MethodContext(context, null,
136: MethodContext.EVENT);
137: return simpleMethod.exec(methodContext);
138: //methodContext.getResults();
139: }
140:
141: public void exec(String source, int lineNo, int columnNo,
142: Object script) throws BSFException {
143: eval(source, lineNo, columnNo, script);
144: }
145:
146: /*
147: public void compileApply (String source, int lineNo, int columnNo,
148: Object funcBody, Vector paramNames, Vector arguments, CodeBuffer cb)
149: throws BSFException;
150:
151: public void compileExpr (String source, int lineNo, int columnNo,
152: Object expr, CodeBuffer cb) throws BSFException;
153:
154: public void compileScript (String source, int lineNo, int columnNo,
155: Object script, CodeBuffer cb) throws BSFException;
156: */
157:
158: public void declareBean(BSFDeclaredBean bean) throws BSFException {
159: context.put(bean.name, bean.bean);
160: }
161:
162: public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
163: context.remove(bean.name);
164: }
165:
166: public void terminate() {
167: }
168:
169: private String sourceInfo(String source, int lineNo, int columnNo) {
170: return "SimpleMethod: " + source + " at line: " + lineNo
171: + " column: " + columnNo;
172: }
173: }
|