changelog shortlog tags changeset files revisions annotate raw

src/script/jython/JythonScriptFactory.java

changeset 8: 846b10b4db8a
parent:af0edff8750f
author: jason@briggs.net.nz
date: Thu Nov 01 21:59:09 2007 +1300 (2 years ago)
permissions: -rw-r--r--
description: add readme.txt
1package script.jython;
2
3import java.io.IOException;
4
5import org.python.core.PyObject;
6import org.python.util.PythonInterpreter;
7
8import org.springframework.scripting.ScriptCompilationException;
9import org.springframework.scripting.ScriptSource;
10
11import script.GenericScriptFactory;
12
13
14/**
15 * Factory for Jython scripts.
16 *
17 * @author jason
18 */
19public class JythonScriptFactory extends GenericScriptFactory {
20
21 private PythonInterpreter pyinterp;
22
23 public JythonScriptFactory(String scriptSourceLocator, String beanName, Class returnType, PythonInterpreter pyinterp) {
24 super(scriptSourceLocator, beanName, returnType);
25 setPythonInterpreter(pyinterp);
26 }
27
28 public void setPythonInterpreter(PythonInterpreter pyinterp) {
29 this.pyinterp = pyinterp;
30 }
31
32 public Object getScriptedObject(ScriptSource scriptSource, Class[] actualInterfaces) throws IOException, ScriptCompilationException {
33 try {
34 synchronized (getScriptClassMonitor()) {
35 if (getScriptResult() == null || scriptSource.isModified()) {
36 String s = scriptSource.getScriptAsString();
37 pyinterp.exec(s);
38
39 PyObject pyi = pyinterp.get(getBeanName());
40
41 if (getScriptInterfaces() != null && getScriptInterfaces().length > 0) {
42 // default to using the first interface
43 setScriptResult(pyi.__tojava__(getScriptInterfaces()[0]));
44 }
45 else {
46 setScriptResult(pyi);
47 }
48 }
49 }
50
51 return getScriptResult();
52 }
53 catch (Exception e) {
54 throw new ScriptCompilationException("Could not exec jython script: " + scriptSource, e);
55 }
56 }
57}