Jython

From PyWPS
Jump to: navigation, search

Contents

PyWPS and Java

Since 3.2x version, PyWPS is able to run in Java environment, using Python interpreter in Java (Jython]). With Jython it is possible to access Java classes from Python code, as well as acces Python classes from Java code. So, it is no big problem to setup PyWPS, so it runs as Java servlet under Apache Tomcat server and so, being able to access all the mighty Java tools for geodata processing.

Good Jython tutorial can be found in the following link and they should be used as reference material to work with Jython and PyWPS:

Jython Webapp Tutorial by Sean McGrath
Distributing Jython Scripts

Nevertheless PyWPS has the following constrains when working with Jython:

  • Current version of Jython (2.5.1) does NOT support os.fork() calls, which is how, PyWPS is able to perform request assynchronously (if you are requesting status=true). So currently, it is NOT possible to use assynchronous calls on PyWPS Execute request, if running within Jython. There are techniques, which do enable overcome this issue, but currently, they are not implemented in PyWPS yet.
  • We are using Tomcate 6.x. If you test PyWPS on 7.x, please let us know.
  • You should work with distribution of Java from Sun (now Oracle).Other JVMs are not tested (and usually, their usage is problematic). You can test, which interpreter you are using by running:
$ java -version

 java version "1.6.0_20"
 Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
 Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode)

Before running PyWPS as Java servlet, several steps have to be performed:

  1. Install Apache Tomcat server
  2. Configure Apache Tomcat
  3. Prepare the Jython-lib package
  4. Write the PyWPS-Servlet wrapper (similar to CGI wrapper locate in the webserver folder of the SVN)
  5. Run the server

Configure Tomcat

Create wps directory within the webapps directory of Tomcat.

$ cd apache-tomcat-6.0.29/wps
$ mkdir wps

Create also the configuration directory within new `wps` directory and location for used Java lib

$ mkdir wps/WEB-INF
$ mkdir wps/lib

Tomcat's configuration is defined in file web.xml, located in the WEB-INF directory. There is an example configuration file, that can copied from the PyWPS source :

$ cp $PYWPS_SOURCE/webservices/tomcat/web.xml wps/WEB-INF/

Then the PyWPS module should be copied to wps directory

$ cp -r $PYWPS_SOURCE/pywps wps/

You should now have directories like wps/pywps/default.py, wps/pywps/Exceptions.py, wps/pywps/Parser.py

Jython-lib package

Download and install Jython. Once, you download it, you have to run the install script:

$ java -jar jython_installer-2.5.1.jar

Once it is installed, you have to create Java archive, with all the necessary Python modules and copy it to wps directory of Tomcat server:

$ cd $JYTHON_HOME
$ cp jython.jar jythonlib.jar
$ zip -r jythonlib.jar Lib
$ cp jythonlib.jar $CATALINA_HOME/webapps/wps/lib/

Prepare the PyWPS-Servlet wrapper

The next stay is to prepare the PyWPS-Servlet wrapper file, this file is used by TomCat to run PyWPS.

The file PywpsServlet.py located in the source tree should be copied to the folder webapps/wps/

$cp $PYWPS_SOURCE/webservices/tomcat/PywpsServlet.py $CATALINA_HOME/webapps/wps

Now the PywpsServlet.py should be configure to indicate processes path location by setting the variable PYWPS_PROCESSES as follows:

import os
os.environ["PYWPS_PROCESSES"] = "/path/to/processes"

The PYWPS_PROCESSES is located in the doWPS() method and it is enough to change the path there. In the end we will have a PywpsServlet.py more or less like this:

 from java.io import *
 from javax.servlet.http import HttpServlet

 import pywps
 from pywps.Exceptions import *
 import traceback
 import os

 class PywpsServlet(HttpServlet):

 def doGet(self,request,response):

 inputQuery = request.getQueryString()
 if not inputQuery:
 e = NoApplicableCode("Missing request value")
 pywps.response.response(e,response)
 self.doPywps(request, response, inputQuery, pywps.METHOD_GET)

 def doPost(self,request,response):

 inputQuery = request.getQueryString()
 self.doPywps(request, response, inputQuery, pywps.METHOD_POST)

 def doPywps(self,request, response, inputQuery,method):

**os.environ["PYWPS_PROCESSES"] = "/usr/local/src/pywps/trunk/tests/processes/"**
 # create the WPS object
 try:
 wps = pywps.Pywps(method)
 if wps.parseRequest(inputQuery):
 pywps.debug(wps.inputs)
 wpsresponse = wps.performRequest()
 if wpsresponse:
 pywps.response.response(wps.response, response, wps.parser.isSoap)
 except WPSException,e:
 pywps.response.response(e, response)

Run the server

After all configurations the tomcat can be started as follows:

% $CATALINA_HOME/bin/startup.sh

or

% /etc/init.d/tomcat6 start

Test the PyWPS, to to url http://localhost:8080/wps/PywpsServlet?service=wps&request=getcapabilities

And let us execute something as well: http://localhost:8080/wps/PywpsServlet.py?service=wps&request=execute&version=1.0.0&identifier=dummyprocess

Conclusion

This chapter describes the basics, how to setup Apache Tomcat server together with PyWPS. Advanced users will probably skip the configuration part and go directly to PyWPS part. It shows, how easy it is to connect two worlds: Python and Java. It makes it easy to access Java-based analytical tools being exposed to the outside world via OGC WPS.

--Wikiadmin 16:13, 10 January 2011 (UTC)