Oracle GlassFish Server 3.0.1 Scripting Framework Guide

Chapter 3 Jython on Django

This chapter provides an overview of Jython and Django and how to get started using them on Oracle GlassFishTM Server.

The following topics are addressed here:

Overview

Jython is a JavaTM implementation of the Python language. Jython is integrated with the Java platform and generates the code that runs on Java. Jython implements almost all modules of Python, except those written in C. Jython programs can import and use Java classes effortlessly. Jython provides the following advantages:

To learn more about Jython, see the Jython Project pages.

Django is a web framework for Python and implementations of Python such as Jython. Django allows quick and easy creation of high-performance web applications. Django provides the following advantages:

To learn more about Django, see the Django Project pages.

You have the advantages of both Jython and Django when you build web applications using Jython on Django for the GlassFish Server.

Installing Jython and Django

Jython and Django installation on GlassFish Server comprises several substeps:

Jython can be installed in either of the following ways:

The following sections explain these tasks in more detail.

ProcedureTo Install Jython as Standalone

You can download Jython and install it as a standalone product. If Jython is installed as standalone, you need to perform additional configuration steps so GlassFish Server can use the Jython installation.

  1. Download Jython from the following location:

    http://downloads.sourceforge.net/project/jython/jython/2.5.1/jython_installer-2.5.1.jar

  2. Run the installer as follows:


    java -jar jython_installer-2.5.1.jar
    
  3. Set the following environmental variables:

    Set the JYTHON_HOME variable to the Jython install location:


    export JYTHON_HOME=jython-install-location
    

    Add the JYTHON_HOME/bin directory to the path:


    export PATH=$JYTHON_HOME/bin:$PATH

    You should now be able to invoke Jython from command line as follows:


    jython
    
  4. Configure GlassFish Server to use the Jython installation with the following command:


    asadmin deploy --property jython.home=jython-install-location
    

ProcedureTo Install Jython from Update Tool

The GlassFish Server Update Tool provides a Jython package. This package installs a Jython instance that enables creation of Jython applications for GlassFish Server. If you installed Jython from Update Tool, there is no need for further configuration of the jython.home property.

  1. Start Update Tool with the following command:


    as-install/bin/updatetool
    
  2. Choose the following option from Available Add-ons and click Install:


    Jython Runtime IPS package for GlassFish v3

    Update center automatically completes the installation of the container and configures it for use with GlassFish Server.

  3. Set the following environmental variables:

    Set the JYTHON_HOME variable to the Jython install location:


    export JYTHON_HOME=as-install/glassfish/jython

    Add the JYTHON_HOME/bin directory to the path:


    export PATH=$JYTHON_HOME/bin:$PATH

    You should now be able to invoke Jython from command line as follows:


    jython
    

ProcedureTo Install Django

  1. Download Django from the following location:

    http://media.djangoproject.com/releases/1.1.1/Django-1.1.1.tar.gz.

  2. Extract the tar file:


    gunzip Django-1.1.1.tar.gz
    tar -xvf Django-1.1.1.tar
    
  3. Change to the extracted directory:


    cd Django-1.1.1
    
  4. Install Django with the following command:


    jython setup.py install
    

ProcedureTo Install the Jython Container for Oracle GlassFish Server

The GlassFish Server Update Tool provides a Jython container package. The following procedure explains how to install the Jython Container module and Grizzly adapter JAR files in the as-install/glassfish/modules directory, and enable deployment of Jython/Django applications on GlassFish Server.

  1. Start Update Tool with the following command:


    as-install/bin/updatetool
    
  2. Choose the following option from Available Add-ons and click Install:


    GlassFish V3 Jython Container

    Update Tool automatically completes the installation of the container and configures it for use with GlassFish Server.

  3. Start the GlassFish Server:


    as-install/bin/asadmin start-domain -v
    
  4. Test the configuration:

    1. Change to the Django samples directory:


      cd Django-1.1.1/samples
      
    2. Deploy the example applications on the server:


      as-install/bin/asadmin deploy .
      
    3. Access the deployed example applications from a web browser:


      http://localhost:8080/examples

ProcedureTo Install Jython Support Libraries for Django

The django-jython project created database backends and management commands for Django and Jython application development. With this task you can install the django-jython packages and enable database support for Django.

  1. Download the django-jython packages from the following location:

    http://django-jython.googlecode.com/files/django-jython-1.0.0.tar.gz

  2. Extract the tar file:


    gunzip django-jython-1.0.0.tar.gz
    tar -xvf django-jython-1.0.0.tar
    
  3. Change to the extracted directory:


    cd django-jython-1.0.0
    
  4. Install the package:


    jython setup.py install
    

Creating and Deploying a Simple Jython Application using Django

After completing the software installation procedures described in Installing Jython and Django, you are ready to create Jython applications using Django. This section explains how to create a simple application. GlassFish Server users can deploy Django applications with the directory deployment method.

ProcedureTo Create a Simple Django Project

Django comes with a built-in administration utility. You can enable it to make the process of creating projects easier.

  1. Use the following command to enable the Django administration utility:


    alias django–admin-jy="jython jython-install/bin/django-admin.py"
    
  2. Change to Django install directory:


    cd django-install
    
  3. Use the following command to create a project:


    django-admin-jy startproject myproject
    

ProcedureTo Deploy a Django Application From the Command Line

To deploy a Django application from the command line using the asadmin command, do the following:

  1. Make sure JYTHON_HOME and PATH environmental variables are set, as described in Installing Jython and Django.

  2. Change to the directory containing the project. For example:


    cd /tools/jython/projects
    
  3. Use the following command to deploy the application:


    as-install/bin/asadmin deploy myproject/
    

asadmin CLI for Jython

The asadmin deploy command enables you to set several deployment-specific properties for Jython applications. Table 3–1 lists these properties.

Table 3–1 Jython Deployment Properties

Property 

Default Value 

Possible Value 

Description 

jython.home

None 

Path to a directory 

Path to a Jython installation. Required. 

jython.mediaRoot

None 

Path to a directory 

Optional parameter containing the path to the location, for server to serve the static files. 

jython.frameworkRoot

None 

Path to a directory 

Optional parameter containing the path of the framework being used. Currently supports Django. 

jython.applicationType

None 

String representing application type such as Django 

Optional parameter to specify the framework, including non-Django applications. 

Use the following command syntax to set these properties:


asadmin deploy --property property=value

For example, you can set the jython.frameworkRoot property to the Django directory as follows:


asadmin deploy --property jython.frameworkRoot=django-install

These values are persistent in the domain.xml file.

Using the Django Administration Utility

Installing Jython and Django explained Jython on Django installation and configuration for Oracle GlassFish Server. To use the Django administration utility for creating applications based on a database, you need a database and the JDBC drivers for Jython to connect to that database. The following steps briefly describe the tasks involved:

  1. Install a database such as PostgreSQL.

  2. Create a database instance.

  3. Install the django-jython packages for the database connectors, if not already installed.

  4. Edit the settings.py file and configure the database and administration utility.

  5. Add the database drivers to the class path to allow Jython to access the database.

  6. Synchronize the database with the following command:


    jython manage.py syncdb
    
  7. Edit the urls.py file and uncomment the lines pertaining to the administration utility.

  8. Make the stylesheets available to the Jython container:


    asadmin deploy --property jython.mediaRoot=jython-install/Lib/site-packages/django/contrib/admin/
    

You can find more information on how to install and use databases with Django administration in the following tutorial:

http://weblogs.java.net/blog/vivekp/archive/2009/06/run_django_appl_1.html

Further Information

The following links contains more details on the information provided in this chapter: