Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: jython 2.7.1, installing python libraries using pip

...

  1. Create the webapp directory (you may use any name you want):
    mkdir [dspace]/webapps/jython/
    Tip: The location [dspace]/webapps/jython/ is used just to illustrate that the jython webapp is just another webapp like other DSpace webapps. It's possible to choose a different location - in fact, it's preferable 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]").
  2. Download the latest Jython installer jar (e.g. jython-installer-2.7.0.jar) from 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
  3. Get jython.jar and the Lib directory.;
    1. either unzip the installer jar:
      unzip -d [dspace]/lib/ jython-installer-2.7.0.jar jython.jar 'Lib/*'
      unzip -d [dspace]/webapps/jython/WEB-INF/lib/ jython-installer-2.7.0.jar jython.jar 'Lib/*'
    2. or use it to install Jython: 
      java -jar jython-installer-2.7.0.jar --console
      Note: Installation location doesn't matter, this is not necessary for DSpace. You can safely delete it after you retrieve jython.jar and Lib
  4. Associate .py files with Jython's PyServlet

    Code Block
    languagexml
    title\[dspace\]/webapps/jython/WEB-INF/web.xml
    <web-app>
        <servlet>
            <servlet-name>PyServlet</servlet-name>
            <servlet-class>org.python.util.PyServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>PyServlet</servlet-name>
            <url-pattern>*.py</url-pattern>
        </servlet-mapping>
    </web-app>

     

  5. Create a Hello World servlet:

    Code Block
    languagepy
    title\[dspace\]/webapps/jython/hello.py
    # -*- coding: utf-8 -*-
    from javax.servlet.http import HttpServlet
    
    
    class hello(HttpServlet):
        def doGet(self, request, response):
            self.doPost(request, response)
    
        def doPost(self, request, response):
            toClient = response.getWriter()
            toClient.println('Hello World!')

    Access to DSpace classes from Jython

  6. Copy DSpace jars to the jython webapp's lib directory:
    cp -r [dspace]/lib/* [dspace]/webapps/jython/WEB-INF/lib/
  7. Start up DSpace kernel on webapp startup and point it to your DSpace configuration:

    Code Block
    languagexml
    title\[dspace\]/webapps/jython/WEB-INF/web.xml
    <web-app>
        ...
        <!-- DSpace Configuration Information -->
        <context-param>
            <param-name>dspace-config</param-name>
            <param-value>/dspace/config/dspace.cfg</param-value>
        </context-param>
        <!-- new ConfigurationService initialization for dspace.dir -->
        <context-param>
            <description>The location of the main DSpace configuration file</description>
            <param-name>dspace.dir</param-name>
            <param-value>/dspace</param-value>
        </context-param>
        <listener>
            <listener-class>org.dspace.app.util.DSpaceContextListener</listener-class>
        </listener>
        <listener>
            <listener-class>org.dspace.servicemanager.servlet.DSpaceKernelServletContextListener</listener-class>
        </listener>
    </web-app>


  8. 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:

    Code Block
    languagexml
    titlesudo vim /etc/tomcat7/Catalina/localhost/jython.xml
    <Context docBase="/dspace/webapps/jython" reloadable="true" cachingAllowed="false" />

    A few seconds after you save the file, Tomcat will notice it and load the "jython" context.

...

Python libraries can either be added to /dspace/webapps/jython/WEB-INF/lib/Lib/ or to context root (/dspace/webapps/jython/).

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.

Code Block
languagebash
# install pip (do not upgrade pip after installing it):
JYTHON_HOME=[dspace]/webapps/jython/WEB-INF/lib/ java -jar [dspace]/webapps/jython/WEB-INF/lib/jython.jar -m ensurepip
# install a PyPI package (e.g. requests):
JYTHON_HOME=[dspace]/webapps/jython/WEB-INF/lib/ java -jar [dspace]/webapps/jython/WEB-INF/lib/jython.jar -m pip install requests

Creating nice action URLs

...

See also

...