summaryrefslogtreecommitdiffstats
path: root/sandbox/ant/container.python/src/main/java/org/apache/tuscany/container/python/PythonScript.java
blob: 1eab532582a66ca4a5cacf3ae83ad4ef796301f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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;
    }
}