This chapter provides an overview of Jython and Django and how to get started using them on Sun GlassFish Enterprise Server.
The following topics are addressed here:
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:
Provides the advantages of easy and powerful Python syntax
Allows the import of Java classes and extending those classes
Provides the ability to compile programs to Java bytecode
To learn more about Jython, see the Jython project site.
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:
Provides an automatic administrative interface to web applications
Provides an extensible and powerful templating system
Allows building of data models that can access the databases quickly
To learn more about Django, see the Django project site.
You have the advantages of both Jython and Django when you build web applications using Jython on Django for the Enterprise Server.
To develop Jython on Django applications for Enterprise Server, you must do the following :
Install Jython
Install Django
Install the Jython container for Enterprise Server
Jython can be installed in one of the following ways:
Install Jython as a standalone product
Install Jython from GlassFish Update Center
The following sections explain these tasks in more detail.
You can download Jython and install it as a standalone product. If Jython is installed as standalone, you need to inform the Jython install location to the Enterprise Server.
Download Jython from the following location:
http://downloads.sourceforge.net/project/jython/jython/2.5.1/jython_installer-2.5.1.jar
Run the installer as follows:
java -jar jython_installer-2.5.1.jar
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
Inform and configure the Enterprise Server with the location of Jython installation with the following command:
asadmin deploy --property jython.home=/jython-install-location
The GlassFish Update Center provides a Jython package. The package installs a Jython instance that enables creation of Jython applications for the Enterprise Server. If you installed Jython from Update Center, there is no need for further configuration of jython.home property.
Start the Update Tool with the following command:
as-install/bin/updatetool |
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 Enterprise Server.
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
Download Django from the following location:
http://media.djangoproject.com/releases/1.1.1/Django-1.1.1.tar.gz.
Extract the tar file:
gunzip Django-1.1.1.tar.gz
tar -xvf Django-1.1.1.tar
Change to the extracted directory:
cd Django-1.1.1
Install Django with the following command:
jython setup.py install
The GlassFish Update Center provides the Jython container package. With this task you can install Jython Container module and Grizzly adapter JAR files in the as-install/glassfish/modules directory, and enable deployment of Jython/Django applications on the Enterprise Server.
Make sure that the asadmin command is available from the PATH variable. Alternately you can use the as-install/bin/asadmin command.
Start the Update Center with the following command:
as-install/bin/updatetool |
Choose the following option from Available Add-ons and click Install:
GlassFish V3 Jython Container |
Update center automatically completes the installation of the container and configures it for use with Enterprise Server.
Start the Enterprise Server:
asadmin start-domain -v
Test the configuration with the following process:
The django-jython project created database back-ends 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.
Download the django-jython packages from the following location:
http://django-jython.googlecode.com/files/django-jython-1.0.0.tar.gz
Extract the tar file:
gunzip django-jython-1.0.0.tar.gz
tar -xvf django-jython-1.0.0.tar
Change to the extracted directory:
cd django-jython-1.0.0
Install the package:
jython setup.py install
After completing the software installations, you are ready to create Jython applications using Django. This section explains how to create a simple application. GlassFish users can deploy Django applications with the directory deployment method.
Django comes with a built-in administration utility. You can enable it to make the process of creating projects easier.
Use the following command to enable the Django administration utility:
alias django–admin-jy="jython jython-install-location/bin/django-admin.py"
Change to Django install directory:
cd django-install-location
Use the following command to create a project:
django-admin-jy startproject <myproject>
To deploy a Django application from the command line using the asadmin command, do the following:
Make sure JYTHON_HOME and PATH environmental variables are set.
Change to the directory containing the project. For example:
cd /tools/jython/projects
Use the following command to deploy the application:
asadmin deploy myproject/
The asadmin deploy command allows you to set deployment specific properties for the Jython applications. The following table lists these properties.
Table 3–1 Jython 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 Django directory as follows:
asadmin deploy --property jython.frameworkRoot=/django-install-location
These values are persistent in the domain.xml file.
The previous sections discussed Jython on Django installation and configuration for Sun GlassFish Enterprise Server. To utilize 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:
Install a database such as PostgreSQL.
Create a database instance.
Install django–jython packages for the database connectors, if not already installed.
Edit the settings.py file and configure the database.
Edit the settings.py file and configure the administration utility.
Add the database drivers to the class path to allow Jython to access the database.
Sync the database with the following command:
jython manage.py syncdb
Edit the urls.py file and uncomment the lines pertaining to the administration utility.
Make the stylesheets available to the Jython container:
asadmin deploy --property jython.mediaRoot=/jython-install-location/Lib/site-packages/django/contrib/admin/
You can obtain more information on how to install and use databases with Django administration from the following tutorial:
http://weblogs.java.net/blog/vivekp/archive/2009/06/run_django_appl_1.html
The following links contains more details on the information provided in this chapter: