diff options
Diffstat (limited to '')
-rw-r--r-- | sandbox/ant/container.python/src/main/java/org/apache/tuscany/container/python/PythonScript.java | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/sandbox/ant/container.python/src/main/java/org/apache/tuscany/container/python/PythonScript.java b/sandbox/ant/container.python/src/main/java/org/apache/tuscany/container/python/PythonScript.java new file mode 100644 index 0000000000..1eab532582 --- /dev/null +++ b/sandbox/ant/container.python/src/main/java/org/apache/tuscany/container/python/PythonScript.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.container.python; + +import java.util.Map; + +import org.python.core.Py; +import org.python.core.PyObject; +import org.python.core.PySystemState; +import org.python.util.PythonInterpreter; + +/** + * JythonScript represents a compiled Jython script + */ +public class PythonScript { + + protected String moduleName; + + protected String className; + + protected String script; + + protected Class iface; + + protected ClassLoader classLoader; + + private PyObject pythonClass; + + /** + * Create a new RhinoInvoker. + * + * @param scriptName + * the name of the script. Can be anything, only used in messages to identify the script + * @param script + * the complete script + * @param context + * name-value pairs that are added in to the scope where the script is compiled. May be null. The value objects are made available to + * the script by using a variable with the name. + * @param classLoader + * the ClassLoader Rhino should use to locate any user Java classes used in the script + */ + public PythonScript(String moduleName, String className, String script, ClassLoader classLoader) { + this.moduleName = moduleName; + this.className = className; + this.script = script; + this.classLoader = classLoader; + + } + + private void initScript(String moduleName, String className, String script) { + PythonInterpreter interpreter = new PythonInterpreter(); + PySystemState sys = Py.getSystemState(); + PySystemState.add_package(iface.getPackage().getName(), null); + sys.setClassLoader(classLoader); + interpreter.exec(script); + + pythonClass = interpreter.get(className); + if (pythonClass == null) { + throw new RuntimeException("No callable (class or function) " + "named " + className + " in " + moduleName); + } + } + + /** + * Create a new invokeable instance of the script + * + * @param context + * objects to add to scope of the script instance + * @return a RhinoScriptInstance + */ + public Object createInstance(Map<String, Object> context) { + initScript(moduleName, className, script); + + PyObject instance = pythonClass.__call__(); + Object o = instance.__tojava__(iface); + if (o == Py.NoConversion) { + throw new RuntimeException("The value from " + className + " must extend " + iface.getName()); + } + return o; + } + + public String getModuleName() { + return moduleName; + } + + public String getClassName() { + return className; + } + + public String getScript() { + return script; + } + + public ClassLoader getClassLoader() { + return classLoader; + } + + public void setServiceInterface(Class iface) { + this.iface = iface; + } +} |