# modjy

Introduction

This howto shows you how to easily publish your PyAMF applications with Apache Tomcat and modjy. modjy is an implementation of a WSGI compliant gateway/server for Jython, built on Java/J2EE servlets. This allows you to run Jython WSGI applications inside a Java/J2EE servlet container, e.g. Apache Tomcat. modjy is part of the Jython project.

We’re going to assume you downloaded Apache Tomcat and are able to run it on `http://localhost:8080`, which is the default for Tomcat. Flash applications will be able to access your PyAMF remoting gateway on `http://localhost:8080/pyamf/`.

This was tested with Jython 2.5, Apache Tomcat 6.0.20, and Java 1.6 on Mac OS X 10.5.7.

## Deploying modjy

Start by simply taking a copy of the `modjy_webapp` directory in the `Demo` subdirectory of the Jython distribution, and drop it in the location where your container expects to find web applications.

The default installation of Apache Tomcat 6 has a subdirectory called `webapps`. If you’re running Tomcat 6, simply drop the `modjy_webapp` directory in there and rename it to `pyamf`:

    cd apache-tomcat-6.0.20
    cp -R /path/to/jython2.5.0/Demo/modjy_webapp webapps/pyamf

If you’re using a different J2EE container, or a non-default installation of Tomcat, you’ll need to read your container documentation to find out where web applications should live.

Now place the `jython.jar` file in the servlet container hierarchy so that it is available when the modjy servlet class is being loaded. Standard J2EE classloading behaviour when looking for support resources is to look first inside the `WEB-INF/lib` directory for a web application, so place the `jython.jar` file in there:

    cp -R /path/to/jython2.5.0/jython.jar webapps/pyamf/WEB-INF/lib

Modify the `webapps/pyamf/WEB-INF/web.xml` file so it contains:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app
         PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>

      <display-name>PyAMF/modjy demo application</display-name>
      <description>
         modjy PyAMF demo application
      </description>

      <servlet>
        <servlet-name>modjy</servlet-name>
        <servlet-class>com.xhaus.modjy.ModjyJServlet</servlet-class>
        <init-param>
          <param-name>python.home</param-name>
          <param-value>/path/to/jython2.5.0</param-value>
        </init-param>
        <init-param>
          <param-name>app_filename</param-name>
          <param-value>demo_app.py</param-value>
        </init-param>
        <init-param>
          <param-name>cache_callables</param-name>
          <param-value>1</param-value>
        </init-param>
        <init-param>
          <param-name>reload_on_mod</param-name>
          <param-value>1</param-value>
        </init-param>
        <init-param>
          <param-name>log_level</param-name>
          <param-value>debug</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet-mapping>
        <servlet-name>modjy</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>

    </web-app>

Make sure you replace `python.home` in the example `web.xml` above (see line 17) and point in to your local Jython installation.

Now start Tomcat and browse to http://localhost:8080/pyamf/ and you should see Jython is up and running:

    Modjy servlet running correctly: jython 2.5.0 (Release_2_5_0:6476, Jun 16 2009, 13:33:26)
    [Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_13

## Create your PyAMF application

First drop a copy of the `pyamf` source folder in `webapps/pyamf/WEB-INF/lib-python`:

    cp -R /path/to/pyamf/trunk/pyamf webapps/pyamf/WEB-INF/lib-python

Open `webapps/pyamf/demo_app.py` and replace it’s contents with:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12

    import logging

    def echo(data):
        return data

    def handler(environ, start_response):
        from pyamf.remoting.gateway.wsgi import WSGIGateway

        services = {'my_service.echo': echo}
        gw = WSGIGateway(services, logger=logging, debug=True)

        return gw(environ, start_response)

That’s it! Your Adobe Flash Player and AMF clients will now be able to access your PyAMF application through http://localhost:8080/pyamf.

## Client

You can test the application with this Python AMF client:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13

    import logging

    from pyamf.remoting.client import RemotingService
    from pyamf.remoting import RemotingError

    url = 'http://localhost:8080/pyamf'
    client = RemotingService(url, logger=logging, debug=True)
    service = client.getService('my_service')

    try:
        print service.echo('Hello World!')
    except RemotingError, e:
        print e