KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > engines > jython > JythonEngine


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.engines.jython;
57
58 import java.util.Vector JavaDoc;
59
60 import org.python.util.*;
61 import org.python.core.*;
62
63 import org.apache.bsf.*;
64 import org.apache.bsf.util.BSFEngineImpl;
65 import org.apache.bsf.util.BSFFunctions;
66
67 /**
68  * This is the interface to Jython (http://www.jython.org/) from BSF.
69  * It's derived from the JPython 1.x engine
70  *
71  * @author Sanjiva Weerawarana
72  * @author Finn Bock <bckfnn@worldonline.dk>
73  * @author Chuck Murcko
74  */

75
76 public class JythonEngine extends BSFEngineImpl {
77   PythonInterpreter interp;
78
79   /**
80    * call the named method of the given object.
81    */

82   public Object JavaDoc call (Object JavaDoc object, String JavaDoc method, Object JavaDoc[] args)
83                                                         throws BSFException {
84     
85     PyObject[] pyargs = Py.EmptyObjects;
86     if (args != null) {
87       pyargs = new PyObject[args.length];
88       for (int i = 0; i < pyargs.length; i++)
89         pyargs[i] = Py.java2py(args[i]);
90     }
91
92     if (object != null) {
93       PyObject o = Py.java2py(object);
94       return unwrap(o.invoke(method, pyargs));
95     }
96     PyObject m = interp.get(method);
97     if (m == null)
98         m = interp.eval(method);
99     if (m != null) {
100     return unwrap(m.__call__(pyargs));
101     }
102     return null;
103   }
104   /**
105    * Declare a bean
106    */

107   public void declareBean (BSFDeclaredBean bean) throws BSFException {
108     interp.set (bean.name, bean.bean);
109   }
110   /**
111    * Evaluate an expression.
112    */

113   public Object JavaDoc eval (String JavaDoc source, int lineNo, int columnNo,
114               Object JavaDoc script) throws BSFException {
115     try {
116       Object JavaDoc result = interp.eval (script.toString ());
117       if (result != null && result instanceof PyJavaInstance)
118         result = ((PyJavaInstance)result).__tojava__(Object JavaDoc.class);
119       return result;
120     } catch (PyException e) {
121       e.printStackTrace ();
122       throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
123                   "exception from Jython: " + e, e);
124     }
125   }
126   /**
127    * Execute a script.
128    */

129   public void exec (String JavaDoc source, int lineNo, int columnNo,
130             Object JavaDoc script) throws BSFException {
131     try {
132       interp.exec (script.toString ());
133     } catch (PyException e) {
134       e.printStackTrace ();
135       throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
136                   "exception from Jython: " + e, e);
137     }
138   }
139   /**
140    * Initialize the engine.
141    */

142   public void initialize (BSFManager mgr, String JavaDoc lang,
143                           Vector JavaDoc declaredBeans) throws BSFException {
144     super.initialize (mgr, lang, declaredBeans);
145
146     // create an interpreter
147
interp = new PythonInterpreter ();
148
149     // register the mgr with object name "bsf"
150
interp.set ("bsf", new BSFFunctions (mgr, this));
151
152     int size = declaredBeans.size ();
153     for (int i = 0; i < size; i++) {
154       declareBean ((BSFDeclaredBean) declaredBeans.elementAt (i));
155     }
156   }
157
158   /**
159    * Undeclare a previously declared bean.
160    */

161   public void undeclareBean (BSFDeclaredBean bean) throws BSFException {
162     interp.set (bean.name, null);
163   }
164   public Object JavaDoc unwrap(PyObject result) {
165     if (result != null) {
166        Object JavaDoc ret = result.__tojava__(Object JavaDoc.class);
167        if (ret != Py.NoConversion)
168           return ret;
169     }
170     return result;
171   }
172 }
173
Popular Tags