3import java.io.IOException;
5import org.python.core.PyObject;
6import org.python.util.PythonInterpreter;
8import org.springframework.scripting.ScriptCompilationException;
9import org.springframework.scripting.ScriptSource;
11import script.GenericScriptFactory;
15 * Factory for Jython scripts.
19public class JythonScriptFactory extends GenericScriptFactory {
21 private PythonInterpreter pyinterp;
23 public JythonScriptFactory(String scriptSourceLocator, String beanName, Class returnType, PythonInterpreter pyinterp) {
24 super(scriptSourceLocator, beanName, returnType);
25 setPythonInterpreter(pyinterp);
28 public void setPythonInterpreter(PythonInterpreter pyinterp) {
29 this.pyinterp = pyinterp;
32 public Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfaces) throws IOException, ScriptCompilationException {
34 synchronized (getScriptClassMonitor()) {
35 if (getScriptResult() == null || scriptSource.isModified()) {
36 String s = scriptSource.getScriptAsString();
39 PyObject pyi = pyinterp.get(getBeanName());
41 if (getScriptInterfaces() != null && getScriptInterfaces().length > 0) {
42 // default to using the first interface
43 setScriptResult(pyi.__tojava__(getScriptInterfaces()[0]));
51 return getScriptResult();
54 throw new ScriptCompilationException("Could not exec jython script: " + scriptSource, e);