diff --git a/jython/build.xml b/jython/build.xml new file mode 100644 index 0000000..e21d272 --- /dev/null +++ b/jython/build.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/jython/jython-engine/LICENSE.TXT b/jython/jython-engine/LICENSE.TXT new file mode 100644 index 0000000..9502008 --- /dev/null +++ b/jython/jython-engine/LICENSE.TXT @@ -0,0 +1,32 @@ +Copyright (c) 2006, Sun Microsystems, Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + - Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + - Neither the name of the Sun Microsystems, Inc. nor the names of + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND +CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +DAMAGE. diff --git a/jython/jython-engine/README.TXT b/jython/jython-engine/README.TXT new file mode 100644 index 0000000..7d8bc42 --- /dev/null +++ b/jython/jython-engine/README.TXT @@ -0,0 +1,3 @@ +This is JSR-223 script engine for Jython - Java implementation of Python. +Jython is available for download at http://www.jython.org/. We have built +and tested with Jython version 2.2.1. diff --git a/jython/jython-engine/THIRDPARTYLICENSES.TXT b/jython/jython-engine/THIRDPARTYLICENSES.TXT new file mode 100644 index 0000000..224c2bd --- /dev/null +++ b/jython/jython-engine/THIRDPARTYLICENSES.TXT @@ -0,0 +1,144 @@ +HISTORY OF THE SOFTWARE +======================= + +JPython was created in late 1997 by Jim Hugunin. Jim was also the +primary developer while he was at CNRI. In February 1999 Barry Warsaw +took over as primary developer and released JPython version 1.1. +In October 2000 Barry helped move the software to SourceForge +where it was renamed to Jython. Jython 2.0 is developed by a group +of volunteers. + + +The standard library is covered by the BeOpen / CNRI license. See the +Lib/LICENSE file for details. + +The oro regular expresion matcher is covered by the apache license. +See the org/apache/LICENSE file for details. + +The zxJDBC package was written by Brian Zimmer and originally licensed +under the GNU Public License. The package is now covered by the Jython +Software License. + +Jython changes Software License. +================================ + +Copyright (c) 2000, Jython Developers +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + + - Neither the name of the Jython Developers nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + + + +JPython Software License. +========================= + +______________________________________________________________________ + +IMPORTANT: PLEASE READ THE FOLLOWING AGREEMENT CAREFULLY. + +BY CLICKING ON THE "ACCEPT" BUTTON WHERE INDICATED, OR BY INSTALLING, +COPYING OR OTHERWISE USING THE SOFTWARE, YOU ARE DEEMED TO HAVE AGREED TO +THE TERMS AND CONDITIONS OF THIS AGREEMENT. + +______________________________________________________________________ + +JPython version 1.1.x + + 1. This LICENSE AGREEMENT is between the Corporation for National Research + Initiatives, having an office at 1895 Preston White Drive, Reston, VA + 20191 ("CNRI"), and the Individual or Organization ("Licensee") + accessing and using JPython version 1.1.x in source or binary form and + its associated documentation as provided herein ("Software"). + + 2. Subject to the terms and conditions of this License Agreement, CNRI + hereby grants Licensee a non-exclusive, non-transferable, royalty-free, + world-wide license to reproduce, analyze, test, perform and/or display + publicly, prepare derivative works, distribute, and otherwise use the + Software alone or in any derivative version, provided, however, that + CNRI's License Agreement and CNRI's notice of copyright, i.e., + "Copyright ¿1996-1999 Corporation for National Research Initiatives; + All Rights Reserved" are both retained in the Software, alone or in any + derivative version prepared by Licensee. + + Alternatively, in lieu of CNRI's License Agreement, Licensee may + substitute the following text (omitting the quotes), provided, however, + that such text is displayed prominently in the Software alone or in any + derivative version prepared by Licensee: "JPython (Version 1.1.x) is + made available subject to the terms and conditions in CNRI's License + Agreement. This Agreement may be located on the Internet using the + following unique, persistent identifier (known as a handle): + 1895.22/1006. The License may also be obtained from a proxy server on + the Web using the following URL: http://hdl.handle.net/1895.22/1006." + + 3. In the event Licensee prepares a derivative work that is based on or + incorporates the Software or any part thereof, and wants to make the + derivative work available to the public as provided herein, then + Licensee hereby agrees to indicate in any such work, in a prominently + visible way, the nature of the modifications made to CNRI's Software. + + 4. Licensee may not use CNRI trademarks or trade name, including JPython + or CNRI, in a trademark sense to endorse or promote products or + services of Licensee, or any third party. Licensee may use the mark + JPython in connection with Licensee's derivative versions that are + based on or incorporate the Software, but only in the form + "JPython-based ___________________," or equivalent. + + 5. CNRI is making the Software available to Licensee on an "AS IS" basis. + CNRI MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY + OF EXAMPLE, BUT NOT LIMITATION, CNRI MAKES NO AND DISCLAIMS ANY + REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY + PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE + ANY THIRD PARTY RIGHTS. + + 6. CNRI SHALL NOT BE LIABLE TO LICENSEE OR OTHER USERS OF THE SOFTWARE FOR + ANY INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF + USING, MODIFYING OR DISTRIBUTING THE SOFTWARE, OR ANY DERIVATIVE + THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. SOME STATES DO NOT + ALLOW THE LIMITATION OR EXCLUSION OF LIABILITY SO THE ABOVE DISCLAIMER + MAY NOT APPLY TO LICENSEE. + + 7. This License Agreement may be terminated by CNRI (i) immediately upon + written notice from CNRI of any material breach by the Licensee, if the + nature of the breach is such that it cannot be promptly remedied; or + (ii) sixty (60) days following notice from CNRI to Licensee of a + material remediable breach, if Licensee has not remedied such breach + within that sixty-day period. + + 8. This License Agreement shall be governed by and interpreted in all + respects by the law of the State of Virginia, excluding conflict of law + provisions. Nothing in this Agreement shall be deemed to create any + relationship of agency, partnership, or joint venture between CNRI and + Licensee. + + 9. By clicking on the "ACCEPT" button where indicated, or by installing, + copying or otherwise using the Software, Licensee agrees to be bound by + the terms and conditions of this License Agreement. + + [ACCEPT BUTTON] + diff --git a/jython/jython-engine/bin/jython.bat b/jython/jython-engine/bin/jython.bat new file mode 100644 index 0000000..c6cd2f1 --- /dev/null +++ b/jython/jython-engine/bin/jython.bat @@ -0,0 +1,2 @@ +jrunscript -Dpython.path=../lib/jython.jar -cp ../build/jython-engine.jar;../lib/jython.jar -l jython %* + diff --git a/jython/jython-engine/bin/jython.sh b/jython/jython-engine/bin/jython.sh new file mode 100644 index 0000000..d707b21 --- /dev/null +++ b/jython/jython-engine/bin/jython.sh @@ -0,0 +1,2 @@ +jrunscript -Dpython.path=../lib/jython.jar -cp ../build/jython-engine.jar:../lib/jython.jar -l jython $* + diff --git a/jython/jython-engine/lib/jython.jar b/jython/jython-engine/lib/jython.jar new file mode 100644 index 0000000..13be842 Binary files /dev/null and b/jython/jython-engine/lib/jython.jar differ diff --git a/jython/jython-engine/make/build.xml b/jython/jython-engine/make/build.xml new file mode 100644 index 0000000..1251825 --- /dev/null +++ b/jython/jython-engine/make/build.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/jython/jython-engine/src/META-INF/services/javax.script.ScriptEngineFactory b/jython/jython-engine/src/META-INF/services/javax.script.ScriptEngineFactory new file mode 100644 index 0000000..d30bc7c --- /dev/null +++ b/jython/jython-engine/src/META-INF/services/javax.script.ScriptEngineFactory @@ -0,0 +1,2 @@ +com.sun.script.jython.JythonScriptEngineFactory + diff --git a/jython/jython-engine/src/com/sun/script/jython/JythonScope.java b/jython/jython-engine/src/com/sun/script/jython/JythonScope.java new file mode 100644 index 0000000..6724bd0 --- /dev/null +++ b/jython/jython-engine/src/com/sun/script/jython/JythonScope.java @@ -0,0 +1,123 @@ +/* + * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: Redistributions of source code + * must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. Neither the name of the Sun Microsystems nor the names of + * is contributors may be used to endorse or promote products derived from this software + * without specific prior written permission. + + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * JythonScope.java + * @author A. Sundararajan + */ + +package com.sun.script.jython; + +import javax.script.*; +import java.util.*; +import org.python.core.*; + +final class JythonScope extends PyObject { + private ScriptContext ctx; + + JythonScope(ScriptEngine engine, ScriptContext ctx) { + this.ctx = ctx; + + // global module's name is expected to be 'main' + __setitem__("__name__", new PyString("main")); + + // JSR-223 requirement: context is exposed as variable + __setitem__("context", Py.java2py(ctx)); + // expose current engine as another top-level variable + __setitem__("engine", Py.java2py(engine)); + } + + public synchronized PyObject invoke(String name) { + if (name.equals("keys")) { + // special case for "keys" so that dir() will + // work for the global "module" + PyList keys = new PyList(); + synchronized (ctx) { + List scopes = ctx.getScopes(); + for (int scope : scopes) { + Bindings b = ctx.getBindings(scope); + if (b != null) { + for (String key : b.keySet()) { + keys.append(new PyString(key)); + } + } + } + } + return keys; + } else { + return super.invoke(name); + } + } + + public PyObject __findattr__(String key) { + return __finditem__(key); + } + + public synchronized PyObject __finditem__(String key) { + synchronized (ctx) { + int scope = ctx.getAttributesScope(key); + if (scope == -1) { + return null; + } else { + Object value = ctx.getAttribute(key, scope); + return JythonScriptEngine.java2py(value); + } + } + } + + public void __setattr__(String key, PyObject value) { + __setitem__(key, value); + } + + public synchronized void __setitem__(String key, PyObject value) { + synchronized (ctx) { + int scope = ctx.getAttributesScope(key); + if (scope == -1) { + scope = ScriptContext.ENGINE_SCOPE; + } + Object obj = value; + if (!(obj instanceof PyClass)) { + obj = JythonScriptEngine.py2java(value); + } + ctx.setAttribute(key, obj, scope); + } + } + + public void __delattr__(String key) { + __delitem__(key); + } + + public synchronized void __delitem__(String key) { + synchronized (ctx) { + int scope = ctx.getAttributesScope(key); + if (scope != -1) { + ctx.removeAttribute(key, scope); + } + } + } + + public String toString() { + return ""; + } +} diff --git a/jython/jython-engine/src/com/sun/script/jython/JythonScriptEngine.java b/jython/jython-engine/src/com/sun/script/jython/JythonScriptEngine.java new file mode 100644 index 0000000..3a113e1 --- /dev/null +++ b/jython/jython-engine/src/com/sun/script/jython/JythonScriptEngine.java @@ -0,0 +1,309 @@ +/* + * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: Redistributions of source code + * must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. Neither the name of the Sun Microsystems nor the names of + * is contributors may be used to endorse or promote products derived from this software + * without specific prior written permission. + + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * JythonScriptEngine.java + * @author A. Sundararajan + */ + +package com.sun.script.jython; + +import javax.script.*; +import java.lang.reflect.*; +import java.io.*; +import org.python.core.*; + + +public class JythonScriptEngine extends AbstractScriptEngine + implements Compilable, Invocable { + + // my factory, may be null + private ScriptEngineFactory factory; + // my scope -- associated with the default context + private PyObject myScope; + + public static final String JYTHON_COMPILE_MODE = "com.sun.script.jython.comp.mode"; + + private static ThreadLocal systemState; + static { + PySystemState.initialize(); + systemState = new ThreadLocal(); + } + + public JythonScriptEngine() { + myScope = newScope(context); + } + + // my implementation for CompiledScript + private class JythonCompiledScript extends CompiledScript { + // my compiled code + private PyCode code; + + JythonCompiledScript (PyCode code) { + this.code = code; + } + + public ScriptEngine getEngine() { + return JythonScriptEngine.this; + } + + public Object eval(ScriptContext ctx) throws ScriptException { + return evalCode(code, ctx); + } + } + + // Compilable methods + public CompiledScript compile(String script) + throws ScriptException { + PyCode code = compileScript(script, context); + return new JythonCompiledScript(code); + } + + public CompiledScript compile (Reader reader) + throws ScriptException { + return compile(readFully(reader)); + } + + // Invocable methods + public Object invokeFunction(String name, Object... args) + throws ScriptException, NoSuchMethodException { + return invokeImpl(null, name, args); + } + + public Object invokeMethod(Object obj, String name, Object... args) + throws ScriptException, NoSuchMethodException { + if (obj == null) { + throw new IllegalArgumentException("script object is null"); + } + return invokeImpl(obj, name, args); + } + + private Object invokeImpl(Object obj, String name, Object... args) + throws ScriptException, NoSuchMethodException { + if (name == null) { + throw new NullPointerException("method name is null"); + } + setSystemState(); + + PyObject thiz; + if (obj instanceof PyObject) { + thiz = (PyObject) obj; + } else if (obj == null) { + thiz = myScope; + } else { + thiz = java2py(obj); + } + + PyObject func = thiz.__findattr__(name); + if (func == null || !func.isCallable()) { + if (thiz == myScope) { + // lookup in built-in functions. This way + // user can call invoke built-in functions. + PyObject builtins = systemState.get().builtins; + func = builtins.__finditem__(name); + } + } + + if (func == null || !func.isCallable()) { + throw new NoSuchMethodException(name); + } + PyObject res = func.__call__(wrapArguments(args)); + return py2java(res); + } + + + public T getInterface(Object obj, Class clazz) { + if (obj == null) { + throw new IllegalArgumentException("script object is null"); + } + return makeInterface(obj, clazz); + } + + public T getInterface(Class clazz) { + return makeInterface(null, clazz); + } + + private T makeInterface(Object obj, Class clazz) { + if (clazz == null || !clazz.isInterface()) { + throw new IllegalArgumentException("interface Class expected"); + } + final Object thiz = obj; + return (T) Proxy.newProxyInstance( + clazz.getClassLoader(), + new Class[] { clazz }, + new InvocationHandler() { + public Object invoke(Object proxy, Method m, Object[] args) + throws Throwable { + Object res = invokeImpl( + thiz, m.getName(), args); + return py2java(java2py(res), m.getReturnType()); + } + }); + } + + + // ScriptEngine methods + public Object eval(String str, ScriptContext ctx) + throws ScriptException { + PyCode code = compileScript(str, ctx); + return evalCode(code, ctx); + } + + public Object eval(Reader reader, ScriptContext ctx) + throws ScriptException { + return eval(readFully(reader), ctx); + } + + public ScriptEngineFactory getFactory() { + synchronized (this) { + if (factory == null) { + factory = new JythonScriptEngineFactory(); + } + } + return factory; + } + + public Bindings createBindings() { + return new SimpleBindings(); + } + + public void setContext(ScriptContext ctx) { + super.setContext(ctx); + // update myScope to keep it in-sync + myScope = newScope(context); + } + + // package-private methods + void setFactory(ScriptEngineFactory factory) { + this.factory = factory; + } + + static PyObject java2py(Object javaObj) { + return Py.java2py(javaObj); + } + + static Object py2java(PyObject pyObj, Class type) { + return (pyObj == null)? null : pyObj.__tojava__(type); + } + + static Object py2java(PyObject pyObj) { + return py2java(pyObj, Object.class); + } + + static PyObject[] wrapArguments(Object[] args) { + if (args == null) { + return new PyObject[0]; + } + + PyObject[] res = new PyObject[args.length]; + for (int i = 0; i < args.length; i++) { + res[i] = java2py(args[i]); + } + return res; + } + + // internals only below this point + private PyObject getJythonScope(ScriptContext ctx) { + if (ctx == context) { + return myScope; + } else { + return newScope(ctx); + } + } + + private PyObject newScope(ScriptContext ctx) { + return new JythonScope(this, ctx); + } + + private void setSystemState() { + /* + * From my reading of Jython source, it appears that + * PySystemState is set on per-thread basis. So, I + * maintain it in a thread local and set it. Besides, + * this also helps in setting correct class loader + * -- which is thread context class loader. + */ + if (systemState.get() == null) { + // we entering into this thread for the first time. + + PySystemState newState = new PySystemState(); + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + newState.setClassLoader(cl); + systemState.set(newState); + Py.setSystemState(newState); + } + } + + private PyCode compileScript(String script, ScriptContext ctx) + throws ScriptException { + try { + setSystemState(); + String fileName = (String) ctx.getAttribute(ScriptEngine.FILENAME); + if (fileName == null) { + fileName = ""; + } + + /* + * Jython parser seems to have 3 input modes (called compile "kind") + * These are "single", "eval" and "exec". I don't clearly understand + * the difference. But, with "eval" and "exec" certain features are + * not working. For eg. with "eval" assignments are not working. + * I've used "exec". But, that is customizable by special attribute. + */ + String mode = (String) ctx.getAttribute(JYTHON_COMPILE_MODE); + if (mode == null) { + mode = "exec"; + } + return __builtin__.compile(script, fileName, mode); + } catch (Exception exp) { + throw new ScriptException(exp); + } + } + + private Object evalCode(PyCode code, ScriptContext ctx) + throws ScriptException { + try { + setSystemState(); + PyObject scope = getJythonScope(ctx); + PyObject res = Py.runCode(code, scope, scope); + return res.__tojava__(Object.class); + } catch (Exception exp) { + throw new ScriptException(exp); + } + } + + private String readFully(Reader reader) throws ScriptException { + char[] arr = new char[8*1024]; // 8K at a time + StringBuilder buf = new StringBuilder(); + int numChars; + try { + while ((numChars = reader.read(arr, 0, arr.length)) > 0) { + buf.append(arr, 0, numChars); + } + } catch (IOException exp) { + throw new ScriptException(exp); + } + return buf.toString(); + } +} diff --git a/jython/jython-engine/src/com/sun/script/jython/JythonScriptEngineFactory.java b/jython/jython-engine/src/com/sun/script/jython/JythonScriptEngineFactory.java new file mode 100644 index 0000000..4923d13 --- /dev/null +++ b/jython/jython-engine/src/com/sun/script/jython/JythonScriptEngineFactory.java @@ -0,0 +1,152 @@ +/* + * Copyright 2006 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: Redistributions of source code + * must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. Neither the name of the Sun Microsystems nor the names of + * is contributors may be used to endorse or promote products derived from this software + * without specific prior written permission. + + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * JythonScriptEngineFactory.java + * @author A. Sundararajan + */ + +package com.sun.script.jython; + +import javax.script.*; +import java.util.*; + +public class JythonScriptEngineFactory implements ScriptEngineFactory { + public String getEngineName() { + return "jython"; + } + + public String getEngineVersion() { + return "2.2.1"; + } + + public List getExtensions() { + return extensions; + } + + public String getLanguageName() { + return "python"; + } + + public String getLanguageVersion() { + return "2.2.1"; + } + + public String getMethodCallSyntax(String obj, String m, String... args) { + StringBuilder buf = new StringBuilder(); + buf.append(obj); + buf.append("."); + buf.append(m); + buf.append("("); + if (args.length != 0) { + int i = 0; + for (; i < args.length - 1; i++) { + buf.append(args[i] + ", "); + } + buf.append(args[i]); + } + buf.append(")"); + return buf.toString(); + } + + public List getMimeTypes() { + return mimeTypes; + } + + public List getNames() { + return names; + } + + public String getOutputStatement(String toDisplay) { + StringBuilder buf = new StringBuilder(); + int len = toDisplay.length(); + buf.append("print(\""); + for (int i = 0; i < len; i++) { + char ch = toDisplay.charAt(i); + switch (ch) { + case '"': + buf.append("\\\""); + break; + case '\\': + buf.append("\\\\"); + break; + default: + buf.append(ch); + break; + } + } + buf.append("\")"); + return buf.toString(); + } + + public String getParameter(String key) { + if (key.equals(ScriptEngine.ENGINE)) { + return getEngineName(); + } else if (key.equals(ScriptEngine.ENGINE_VERSION)) { + return getEngineVersion(); + } else if (key.equals(ScriptEngine.NAME)) { + return getEngineName(); + } else if (key.equals(ScriptEngine.LANGUAGE)) { + return getLanguageName(); + } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) { + return getLanguageVersion(); + } else if (key.equals("THREADING")) { + return "MULTITHREADED"; + } else { + return null; + } + } + + public String getProgram(String... statements) { + StringBuilder buf = new StringBuilder(); + for (int i = 0; i < statements.length; i++) { + buf.append("\t"); + buf.append(statements[i]); + buf.append("\n"); + } + return buf.toString(); + } + + public ScriptEngine getScriptEngine() { + JythonScriptEngine engine = new JythonScriptEngine(); + engine.setFactory(this); + return engine; + } + + private static List names; + private static List extensions; + private static List mimeTypes; + static { + names = new ArrayList(2); + names.add("jython"); + names.add("python"); + names = Collections.unmodifiableList(names); + extensions = new ArrayList(2); + extensions.add("jy"); + extensions.add("py"); + extensions = Collections.unmodifiableList(extensions); + mimeTypes = new ArrayList(0); + mimeTypes = Collections.unmodifiableList(mimeTypes); + } +} diff --git a/jython/mkjar.sh b/jython/mkjar.sh deleted file mode 100755 index 0b8fb56..0000000 --- a/jython/mkjar.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/sh - -script_dir=$(cd "$(dirname "$0")"; pwd) -jython_url='http://downloads.sourceforge.net/project/jython/jython/2.2.1/jython_installer-2.2.1.jar?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fjython%2Ffiles%2Fjython%2F2.2.1%2F&ts=1338203268&use_mirror=iweb' -tmp_dir=$script_dir/tmp -output_dir=$script_dir/jar -editorconfig_py_dir=$script_dir/.. - -mkdir -p "$tmp_dir" - -cd "$tmp_dir" - -# Download Jython if it is not downloaded yet -if [ ! -f jython_installer.jar ]; then - wget $jython_url -O jython_installer.jar - - if [ $? != 0 ]; then - exit 1 - fi -fi - -# install the standalone package, and move it to current dir -java -jar jython_installer.jar -s -v -d ./install -t standalone - -if [ $? != 0 ]; then - exit 1 -fi - -mv install/jython.jar jython_editorconfig.jar - -# package EditorConfig python files -rm ./Lib -ln -s "$editorconfig_py_dir" ./Lib - -zip -r jython_editorconfig.jar Lib/README.rst Lib/editorconfig/*.py - -# Copy jython_editorconfig.jar to dest dir and create the editorconfig shell -# script -mkdir -p "$output_dir" -cd "$output_dir" -cp $tmp_dir/jython_editorconfig.jar .