See how Spring Python works with Jython

Java Spring Python Python Jython

I recently completed a patch that replaces the amara library with python’s default elementTree library. As much as I like amara’s API, its C-extension basis was unacceptable. I just merged the patch into the trunk, and verified it works with Jython 2.5.1.FINAL. When Spring Python’s 1.1.0.M2 release comes out, we will be Jython-compatible.

With this change, there were only a couple of tweaks to the rest of Spring Python.

What does this mean? How about looking at some code to see how we can start with a Java app, and migrate things over to Python. To kick this off, let’s start with an uber-contrived example Java app: a wiki engine. The wiki engine calls into a data access layer in order to look up some statistics on the site.

public interface DataAccess {  
  
        public void statistics();  
  
}  
public class MySqlDataAccess implements DataAccess {  
  
        public void statistics() {  
                System.out.println("MySQL: You are calling for some statistics.");  
        }  
  
}  
public class WikiService {  
  
        private DataAccess dataAccess;  
  
        public DataAccess getDataAccess() {  
                return this.dataAccess;  
        }  
  
        public void setDataAccess(DataAccess dataAccess) {  
                this.dataAccess = dataAccess;  
        }  
  
        public void calculateWikiStats() {  
                System.out.println("You are passing through a pure Java WikiService...");  
                dataAccess.statistics();  
        }  
  
}

Let’s assume this is a Spring app. That means we need an XML application context. Let’s create one and call it javaBeans.xml.

  
xml version="1.0" encoding="UTF-8"?  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  
          
  
          
   
   
  
  

We can start up our pure java app along with its context by writing jython script javabeans.py. It uses Spring Python’s SpringJavaConfig parser.

if \_\_name\_\_ == "\_\_main\_\_":  
    from springpython.context import ApplicationContext  
    from springpython.config import SpringJavaConfig  
    ctx = ApplicationContext(SpringJavaConfig("javaBeans.xml"))  
    service = ctx.get\_object("wikiService")  
    service.calculateWikiStats()

We just need to compile the java code, and then run our script using jython.

gregturn@startrek:~$ javac *.java  
gregturn@startrek:~$ jython javabeans.py  
You are passing through a pure Java WikiService...  
MySQL: You are calling for some statistics.  
gregturn@startrek:~$

Okay, we’re off to a good start. We have tapped a running java system. For our next step, let’s write a pure python version of the data access code and put it into Py.py.

import DataAccess  
  
class MySqlDataAccess(DataAccess):  
    def statistics(self):  
        print "PyMySQL: You are calling for some statistics."  
  
class StubDataAccess(DataAccess):  
    def statistics(self):  
        print "PyStub: You are calling for some statistics."

Now, to really crank things up a bit, let’s migrate from the Spring Java XML configuration over to Spring Python’s pure Python container.

from springpython.config import PythonConfig, Object  
from Py import MySqlDataAccess  
import WikiService  
  
class WikiProductionAppConfig(PythonConfig):  
    def \_\_init\_\_(self):  
        PythonConfig.\_\_init\_\_(self)  
  
    @Object  
    def data\_access(self):  
        return MySqlDataAccess()  
  
    @Object  
    def wiki\_service(self):  
        results = WikiService()  
        results.dataAccess = self.data\_access()  
        return results  
  
if \_\_name\_\_ == "\_\_main\_\_":  
    from springpython.context import ApplicationContext  
    ctx = ApplicationContext(WikiProductionAppConfig())  
    service = ctx.get\_object("wiki\_service")  
    service.calculateWikiStats()

The top half of our mixed.py script contains an IoC container, except there is no XML whatsoever. Instead, its raw python. You can see how we are also using our python version of MySqlDataAccess.

gregturn@startrek:~$ jython mixed.py  
You are passing through a pure Java WikiService...  
PyMySQL: You are calling for some statistics.  
gregturn@startrek:~$

This shows that we are still using the pure java WikiService class, and it calls into our pure python MySqlDataAccess.

Another useful feature that Spring Python provides is easy access to Pyro, the Python Remote Objects library. This library is for making RPC calls from Python-to-Python. Spring Python makes it easy to link clients and services together with Pyro.

First, let’s take our wiki service, and export it with a server script.

  
from springpython.config import PythonConfig, Object  
from springpython.remoting.pyro import *  
  
from Py import MySqlDataAccess  
import WikiService  
  
class WikiProductionAppConfig(PythonConfig):  
    def \_\_init\_\_(self):  
        PythonConfig.\_\_init\_\_(self)  
  
    @Object  
    def data\_access(self):  
        return MySqlDataAccess()  
  
    @Object  
    def wiki\_service(self):  
        results = WikiService()  
        results.dataAccess = self.data\_access()  
        return results  
  
    @Object  
    def exported\_wiki\_service(self):  
        return PyroServiceExporter(service=self.wiki\_service(), service\_name="wiki", service\_port=9000)  
  
if \_\_name\_\_ == "\_\_main\_\_":  
    from springpython.context import ApplicationContext  
    print "Starting up exported WikiService..."  
    ctx = ApplicationContext(WikiProductionAppConfig())  

We made some slight tweaks to the IoC container by adding exported_wiki_service. It targets wiki_service, and links it with a name and port to expose it as a Pyro service. Spring Python by default will instantiate every object, so in this case, it creates the PyroProxyExporter. Because this isn’t the caller, we don’t need to fetch anything from the context.

Next, let’s code a client to call our service.

  
from springpython.config import PythonConfig, Object  
from springpython.remoting.pyro import *  
  
class WikiProductionAppConfig(PythonConfig):  
    def \_\_init\_\_(self):  
        PythonConfig.\_\_init\_\_(self)  
  
    @Object  
    def wiki\_service(self):  
        results = PyroProxyFactory()  
        results.service\_url="PYROLOC://localhost:9000/wiki"  
        return results  
  
if \_\_name\_\_ == "\_\_main\_\_":  
    from springpython.context import ApplicationContext  
    ctx = ApplicationContext(WikiProductionAppConfig())  
    service = ctx.get\_object("wiki\_service")  
    service.calculateWikiStats()  

Now, let’s start up the server.

  
gregturn@startrek:~$ jython mixed\_with\_pyro\_server.py &  
[1] 4953  
Starting up exported WikiService...  

The script is backgrounded, because it is running a Pyro daemon advertising our service. Now, let’s launch the client script.

  
gregturn@startrek:~$ jython mixed\_with\_pyro\_client.py  
You are passing through a pure Java WikiService...  
PyMySQL: You are calling for some statistics.  
gregturn@startrek:~$  

As you can see, our client makes remote to the exported WikiService, which happens to be a pure java service. The java service then calls our python version of MySqlDataAccess to do its statistics call. This goes to show how easy Spring Python makes it mix and match Java and Python.