Basic Jython webapp
- Create the webapp directory (you may use any name you want):
mkdir -p [dspace]/webapps-jython/WEB-INF/lib
Tip: The jython webapp is just another webapp like the individual DSpace webapps. So while you could put it next to the DSpace webapps into
[dspace]/webapps/jython/, it's preferable to choose a different location (e.g.
[dspace]/webapps-jython/) because the
[dspace]/webapps/directory is replaced every time you run "
ant update" (the old webapps directory will not be deleted, it will be renamed to "webapps-[timestamp]").
- Download the latest Jython installer jar (e.g.
http://www.jython.org/downloads.html(the jython.org website was last updated around 2015 [issue2658]; check Maven Central for latest jython version)
curl -O -J http://search.maven.org/remotecontent?filepath=org/python/jython-installer/2.7.1/jython-installer-2.7.1.jar
- either unzip the installer jar:
unzip -d [dspace]/lib/ jython-installer-2.7.1.jar jython.jar 'Lib/*'
unzip -d [dspace]/webapps-jython/WEB-INF/lib/ jython-installer-2.7.1.jar jython.jar 'Lib/*'
- or use it to install Jython:
java -jar jython-installer-2.7.1.jar --console
Note: Installation location doesn't matter, this is not necessary for DSpace. You can safely delete it after you retrieve
- either unzip the installer jar:
Associate .py files with Jython's PyServlet
Create a Hello World servlet:
Access to DSpace classes from Jython
- Copy DSpace jars to the jython webapp's lib directory:
cp -r [dspace]/lib/* [dspace]/webapps-jython/WEB-INF/lib/
Start up DSpace kernel on webapp startup and point it to your DSpace configuration:
Define the context in Tomcat's configuration. There are several ways how you can do that, so just use the same way you use for configuring DSpace contexts. The recommended one is to use a context fragment:
A few seconds after you save the file, Tomcat will notice it and load the "jython" context.
Adding Java libraries
- Copy the .jar to
- Reload the context
sudo touch /etc/tomcat8/Catalina/localhost/jython.xml
Tip: If you forgot which libraries you added (because it's hard to spot them among dozens of libraries which belong to DSpace), here's how you can filter out the DSpace libraries, which should leave you only with a list of libraries you added:
Adding Python libraries
Python libraries can either be added to
/dspace/webapps-jython/WEB-INF/lib/Lib/ or to context root (
Installing pure-python modules via pip: refer to https://github.com/openhab-scripters/openhab-helper-libraries/wiki/Using-PIP-in-Docker
Read the entire page including notes and warnings, it describes issues you will run into.
Creating nice action URLs
You may find a snippet like the one below to set up URL mapping for a Jython servlet. Unfortunately, it's not implemented in PyServlet, as it is more a demo than something to use in production (see this thread). For production deployment, Modjy is recommended.
Example of connection to the DSpace DB via ZxJDBC
An artificial example demonstrating a few techniques that are now possible thanks to the above:
- You can use zxJDBC, a pythonic (DB API 2.0) interface allowing the use of databases accessible via JDBC. The driver (postgresql-*.jar or ojdbc6.jar) used here is available in classpath because we copied it from /dspace/lib/.
- You can use Java libraries, demonstrated here by java.util.Properties used to read dspace.cfg.
- Here we read the database driver, connection string, user and password from dspace.cfg and then pass it to zxJDBC to create a connection.
- We could use DB API 2.0 methods like cursor.fetchall() to get query results. Here I chose to use the Python zip() function to return the query results in a custom format.
- You can use a context manager (Python "with" keyword) around a zxJDBC cursor to manage the scope of the DB transaction.
- You can't use the Python "with" keyword around the java.util.Properties as it is a Java class which doesn't implement a Python context manager.
- DB connection here is open in init() and closed in destroy() only to demonstrate the servlet's constructor and destructor. You should not keep a DB connection open for the whole time the servlet is loaded.
I'm not yet sure why, but some exceptions are neither logged to tomcat, nor to dspace.log, nor to the output HTML.
Example: As mentioned above, the python requests library fails to fetch certain pages with
error: java.util.zip.DataFormatException: invalid stored block lengths
Here's a workaround showing how to either log the error or print it in the browser.