001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2002 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
027: * must not be used to endorse or promote products derived from
028: * this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many individuals
050: * on behalf of the Apache Software Foundation and was originally created by
051: * Sanjiva Weerawarana and others at International Business Machines
052: * Corporation. For more information on the Apache Software Foundation,
053: * please see <http://www.apache.org/>.
054: */
055:
056: package org.apache.bsf.engines.jython;
057:
058: import java.util.Vector;
059:
060: import org.python.util.*;
061: import org.python.core.*;
062:
063: import org.apache.bsf.*;
064: import org.apache.bsf.util.BSFEngineImpl;
065: import org.apache.bsf.util.BSFFunctions;
066:
067: /**
068: * This is the interface to Jython (http://www.jython.org/) from BSF.
069: * It's derived from the JPython 1.x engine
070: *
071: * @author Sanjiva Weerawarana
072: * @author Finn Bock <bckfnn@worldonline.dk>
073: * @author Chuck Murcko
074: */
075:
076: public class JythonEngine extends BSFEngineImpl {
077: PythonInterpreter interp;
078:
079: /**
080: * call the named method of the given object.
081: */
082: public Object call(Object object, String method, Object[] args)
083: throws BSFException {
084:
085: PyObject[] pyargs = Py.EmptyObjects;
086: if (args != null) {
087: pyargs = new PyObject[args.length];
088: for (int i = 0; i < pyargs.length; i++)
089: pyargs[i] = Py.java2py(args[i]);
090: }
091:
092: if (object != null) {
093: PyObject o = Py.java2py(object);
094: return unwrap(o.invoke(method, pyargs));
095: }
096: PyObject m = interp.get(method);
097: if (m == null)
098: m = interp.eval(method);
099: if (m != null) {
100: return unwrap(m.__call__(pyargs));
101: }
102: return null;
103: }
104:
105: /**
106: * Declare a bean
107: */
108: public void declareBean(BSFDeclaredBean bean) throws BSFException {
109: interp.set(bean.name, bean.bean);
110: }
111:
112: /**
113: * Evaluate an expression.
114: */
115: public Object eval(String source, int lineNo, int columnNo,
116: Object script) throws BSFException {
117: try {
118: Object result = interp.eval(script.toString());
119: if (result != null && result instanceof PyJavaInstance)
120: result = ((PyJavaInstance) result)
121: .__tojava__(Object.class);
122: return result;
123: } catch (PyException e) {
124: throw new BSFException(BSFException.REASON_EXECUTION_ERROR,
125: "exception from Jython: " + e, e);
126: }
127: }
128:
129: /**
130: * Execute a script.
131: */
132: public void exec(String source, int lineNo, int columnNo,
133: Object script) throws BSFException {
134: try {
135: interp.exec(script.toString());
136: } catch (PyException e) {
137: throw new BSFException(BSFException.REASON_EXECUTION_ERROR,
138: "exception from Jython: " + e, e);
139: }
140: }
141:
142: /**
143: * Initialize the engine.
144: */
145: public void initialize(BSFManager mgr, String lang,
146: Vector declaredBeans) throws BSFException {
147: super .initialize(mgr, lang, declaredBeans);
148:
149: // create an interpreter
150: interp = new PythonInterpreter();
151:
152: // register the mgr with object name "bsf"
153: interp.set("bsf", new BSFFunctions(mgr, this ));
154:
155: int size = declaredBeans.size();
156: for (int i = 0; i < size; i++) {
157: declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
158: }
159: }
160:
161: /**
162: * Undeclare a previously declared bean.
163: */
164: public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
165: interp.set(bean.name, null);
166: }
167:
168: public Object unwrap(PyObject result) {
169: if (result != null) {
170: Object ret = result.__tojava__(Object.class);
171: if (ret != Py.NoConversion)
172: return ret;
173: }
174: return result;
175: }
176: }
|