From: <ja...@ti...> - 2007-03-19 02:46:59
|
Hi John, I haven't had a chance to play with your code yet. Have you seen this version by Yan Weng?: http://yanweng.blogspot.com/2006/02/prototype-of-jython-scriptfactory-for.html James Tikalsky > I felt it was important to add jython support since the python > following is still strong. > > Also looks like the jython project is becoming more active with > ambitions to sync up to the latest version of c python. > > i still like python... call me old school. > > so anyway this is what I did: > > I created an extension to the spring-lang-2.0.xsd: > > <xsd:element name="jython" type="dynamicScriptType"> > <xsd:annotation> > <xsd:documentation><![CDATA[ > A Spring bean backed by a Jython class definition. > ]]></xsd:documentation> > </xsd:annotation> > </xsd:element> > > Then added a package org.springframework.scripting.jython with the > following sources added: JythonScriptFactory.java > > In "getScriptedObject" perform the following: > > public Object getScriptedObject(ScriptSource actualScriptSource, > Class[] actualInterfaces) > throws IOException, ScriptCompilationException { > try { > PythonInterpreter.initialize(System.getProperties(),null, new String[] > {}); > PythonInterpreter interpreter = new PythonInterpreter(); > interpreter.exec(actualScriptSource.getScriptAsString()); > PyStringMap locals = (PyStringMap) interpreter.getLocals(); > PyList list = locals.values(); > PyClass clazz = null; > for (int i=0; i < list.__len__(); i++) { > PyObject py = list.__getitem__(i); > if ((py instanceof PyClass) && !(py instanceof PyJavaClass)) { > if (clazz != null) throw new PyException(new PyString("multiple > python class declarations present")); > clazz = (PyClass) py; > } > } > PyInstance instance = (PyInstance) clazz.__call__(); > return instance.__tojava__(actualInterfaces[0]); > } > catch (PyException ex) { > throw new ScriptCompilationException("Could not compile Jython > script: " + actualScriptSource, ex); > } > } > > > This is just a POC. It would be nice if we could specify the class > name as part of the ScriptSource object so we don't have to guess > which in the locals namespace class we are interested in. > > Also in the org.springframework.scripting.config.LangNamespaceHandler > I add to the init method: > > if (ClassUtils.isPresent("org.python.core.PyObject")) { > registerBeanDefinitionParser("jython", new > ScriptBeanDefinitionParser(JythonScriptFactory.class)); > } > > So here is an excerpt from a beans file: > > <bean id="SPAM" class="jcoppola.atsi.application.SetRunnableTest"> > <property name="runnable" ref="runnable1"/> > </bean> > > <bean id="EGGS" class="jcoppola.atsi.application.SetRunnableTest"> > <property name="runnable" ref="runnable2"/> > </bean> > > <lang:jython id="runnable1" script-interfaces="java.lang.Runnable"> > <lang:inline-script> > from java.lang import Runnable > > class JythonRunnable(Runnable): > def __init__(self): > print "__init__: JythonRunnable1" > def run(self): > print "run: JythonRunnable1" > </lang:inline-script> > </lang:jython> > > <lang:jython id="runnable2" script-interfaces="java.lang.Runnable"> > <lang:inline-script> > from java.lang import Runnable > > class JythonRunnable(Runnable): > def __init__(self): > print "__init__: JythonRunnable2" > def run(self): > print "run: JythonRunnable2" > </lang:inline-script> > </lang:jython> > > I'll provide a patch if this gains traction. > > Cheers, > John Coppola > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share > your > opinions on IT & business topics through brief surveys-and earn cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > _______________________________________________ > Springframework-developer mailing list > Spr...@li... > https://lists.sourceforge.net/lists/listinfo/springframework-developer > |