The Source for Java Technology Collaboration
User: Password:



Vivek Pandey's Blog

Vivek Pandey Vivek Pandey is a Senior Staff Engineer at Sun Microsystems. He works on XML and Web Services technologies. He has represented Sun at WS-I and W3C WSDL Working Group. He is involved with various java.net projects, such as JAX-WS RI, Metro, WSIT, Hudson and Glassfish. Currently he is technical lead of scripting languages support in Glassfish.



Run Django applications on GlassFish v3 Preview

Posted by vivekp on June 05, 2009 at 09:41 AM | Permalink | Comments (0)

GlassFish v3 Preview is available and I am excited to announce support for Django applications. Django is a Python based framework, probably anyone hosting their applications on GAE might know Django is a Python based web framework. GlassFish-Scripting project provides implementation of Jython Container. Just like JRuby Container, Jython Container does the following:
  1. Provides GlassFish v3 connector and deployer as OSGi module. Which means that deployment of a Python application will trigger Jython Container code. 
  2. Wire up the HTTP request and response at very low level by implementing a GrizzlyAdapter, hence resulting in better runtime performance and scalability using grizzly scalable NIO framework.
  3. WSGI (Web Services Gateway Interface) is a Python standard to wire a Web Server to Python web frameworks such as Django or  TurboGears etc. Jython Container implements WSGI interface and so it would be pretty easy to add support for various Python web frameworks. Currently, we have Django and we will have others such as TuroboGears, Pylons etc.
  4. Currently Jython Container is available thru GlassFish v3 Update Tool. In the future it may appear with GlassFish v3 core distribution.

Here is how you can run a Jython application on GlassFish v3 Preview.

Install GlassFish v3 Preview

Follow the directions from here to install GlassFish v3 Preview.

Install Jython Container on GlassFish v3 Preview

$ cd /tools/glassfishv3
$ ./bin/updatetool

Below is the GlassFish v3 Update Tool image. Select GlassFish v3 Jython Container and click Install. This would install Jython Container OSGi module and Grizzly adapter jars inside glassfish/modules directory.

jython.png

Install Jython

To run your Django application, at the minimum you need Jython 2.5 release. Get Jython 2.5 RC3 from here. Once downloaded, run the installer by doing the following:

$ java -jar jython_installer-2.5rc3.jar

Follow the installer direction and after it is installed, add jython command to your path

$ export JYTHON_HOME=/path/to/jython-install
$ export PATH=$JYTHON_HOME/bin:$PATH
$ jython

should display jython interpreter shell

jython-cli.png

Install Django

Get Django 1.0.2 from here.

$ tar xzvf Django-1.0.2-final.tar.gz
$ cd Django-1.0.2-final
$ jython setup.py install

Follow the detailed instruction here.

Create a sample Django application

$ django-admin.py startproject mysite
$ cd mysite

django-ls.png

Start GlassFish v3 Preview

$ asadmin start-domain -v
$ asadmin create-jvm-options -Djython.home=/tools/jython

Above /tools/jython is where I installed jython. jython.home system property tells Jython container in GlassFish v3, where to find jython container.

Deploy the application

$ cd mysite
$ asadmin deploy .

Now access http://localhost:8080/mysite and it should show the first Django page:

django-app.png

Enable Django admin

Django framework comes with a built in administration utility and we will enable it in this sample. The admin application needs a database. So first we would need to setup the database.

Install PostgreSQL

Download and install PostgreSQL database server. Make sure it is running.

Once installed, make sure it is running. You would also need to download and install PostgreSQL JDBC driver inside jython installation.

$ cd /tools/jython
$ wget http://jdbc.postgresql.org/download/postgresql-8.3-604.jdbc4.jar

Above /tools/jython is where I have installed jython.

Install django-jython

You would need JDBC drivers for the database backends. So you would need to install django-jython on your Jython installation. django-jython is a Google Code project that has JDBC database backend for PostgreSQL only. There is SQLite3 experimantal version and no MySQL as of now.

Download the latest django-jython from here.

Then install it on Jython.

$ tar zxvf django-jython-1.0b1.tar.gz
$ cd django-jython-1.0b1
$ jython setup.py install

See further details on setting up backend here.

Create database

$ createdb mysite

Refer to details on how to go about creating database in PostgreSQL incase you encounter problem. We will use mysite database in our Django application.

Configure Database

Edit settings.py and change the database configuration. For example these are my entries:

DATABASE_ENGINE = 'doj.backends.zxjdbc.postgresql'
DATABASE_NAME = 'mysite'             # Or path to database file if using sqlite3.
DATABASE_USER = 'vivek'             # Not used with sqlite3.
DATABASE_PASSWORD = 'vivek'         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

Above, doj.backends.zxjdbc.postgresql is the name of the PostgreSQL JDBC driver, the database name is mysite and then the username and password. HOST and PORT values are empty meaning the default will be taken.

It is time to sync the DB. So first make the PostgreSQL JDBC driver available to jython classpath so that the following syncdb command works:

$ export CLASSPATH=/tools/jython/postgresql-8.3-604.jdbc4.jar:$CLASSPATH
$ jython manage.py syncdb

NOTE: postgresql-8.3-604.jdbc4.jar was downloaded and installed inside jython directory installation earlier.

Enable admin app

Edit settings.py and add 'django.contrib.admin' under INSTALLED_APPS.

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
)


Edit urls.py and uncomment all the lines that contains admin. Make sure you do not have space at the beginning on a line.

urls.png

Now you would need to redeploy the application so that you can access the admin application. But before we do that, we need to make the stylesheets available to the Jython container. 


$ asadmin deploy --property jython.mediaRoot=/tools/jython/Lib/site-packages/django/contrib/admin/ --force=true .

There is a bug in Jython container and this would not work so as a work around copy the admin media files to the GlassFish v3 domain1 docroot.

$ cp -r /tools/jython/Lib/site-packages/django/contrib/admin/media   /tools/glassfishv3/glassfish/domains/domain1/docroot/

Note: If you deploy your application at context root '/' then the above work around to make admin media files will not be needed.

Now access : http://localhost:8080/mysite/admin, it should show:

admin1.png

After you login,  the admin console would let you manage users/groups.

admin2.png

To summarise
  • Install GlassFish v3
  • Install Jython
  • Install Django
  • To use admin application or any DB backed application
    • Install django-jython
    • Install PostgreSQL
    • Make PostgreSQL JDBC driver available to jython installation
    • Enable admin application by editing settings.py and urls.py
    • Make admin media files available to GlassFish v3 Jython container by using jython.mediaRoot deploy time property or simply putting the admin static content inside GlassFish v3 docroot
The above sample creation is completely based on the Django tutorial.

A note about the Ruby application support, GlassFish v3 Preview release improves on the Ruby application support. Now the Ruby application deployment support is Rack based. Just like WSGI, Rack is a Ruby standard to wire a web server to a Ruby web framework. The Ruby web frameworks supported out of the box are: Rails, Merb and Sinatra. 

Try it out by running on GlassFish v3 Preview and send us feedback at GlassFish mailing list.


Slides for Dynamic Language on GlassFish technical session

Posted by vivekp on June 04, 2009 at 04:26 PM | Permalink | Comments (0)

The slides for my JavaOne technical session: Dynamic Languages Powered by Glassfish are available here.

GlassFish gem 0.9.5 available now

Posted by vivekp on June 04, 2009 at 04:16 PM | Permalink | Comments (0)

I guess was too busy with JavaOne to announce the release of GlassFish gem 0.9.5. This release is based on GlassFish v3 Kernel from GlassFish v3 Preview.

This release also has few critical fixes related to logging and an improvement in logging where you would be able to log the message on cosole using -l option without a log file name.

See the changes in this release and follow the documentation for installation and getting started. As always send your feedback to the forum and report any issues here.

Plug-in any Ruby/Rack based framework to GlassFish

Posted by vivekp on April 03, 2009 at 10:33 PM | Permalink | Comments (0)

GlassFish gem as well as GlassFish v3 supports Rack. Rack provides an interface to plugin a Ruby web framework with a web sever. Similar to Python WSGI. This means that any ruby based framework that can talk Rack can be simply deployed on GlassFish.

Here is how you can do it on GlassFish gem:

Insall JRuby

Install JRuby from here. Setup JRUBY_HOME to the install location and add JRUBY_HOME/bin to your PATH.

Install GlassFish gem

$ gem install glassfish

Now, lets write a trivial app and save it in file: myapp.rb
class MyApp
def self.Run
"Hello World"
end
end
Write a rack-up script and save it in myframework.ru

require 'rubygems'
gem 'rack'
require 'rack/handler/grizzly'
require 'myapp'

app = proc do |env|
resp = MyApp.Run
[
200, # Status code
{ # Response headers
'Content-Type' => 'text/html',
'Content-Length' => resp.length,
},
[resp] # Response body
]
end

That is pretty much your sample Ruby application and the Rack-up script to run on GlassFish. Above require 'rack/handler/grizzly' makes Grizzly handler available to Rack.

To run it, simply run glassfish in side the directory where myapp.rb and myframework.ru are placed.


% glassfish
Starting GlassFish server at: 192.168.1.103:3000 in development environment...
Writing log messages to: /Users/vivek/dev/foo/log/development.log.
Press Ctrl+C to stop.

Now access, http://localhost:3000 in your browser to access your app.

rackapp.png

If you are running GlassFish v3 server and want to deploy this app, you would simply use the same asadmin command from inside the application directory:

$ asadmin deploy .
GlassFish gem as well as GlassFish v3 server can auto-detect Rails, Merb and Sinatra applications. You can use the above mentioned technique to plugin other frameworks. For reference also see Jacob's blog where he explains how Sintra application is plugged in to GlassFish.


New features in GlassFish 0.9.3 gem

Posted by vivekp on March 16, 2009 at 11:09 AM | Permalink | Comments (0)

I am happy to announce much awaited release of GlassFish gem ver 0.9.3. This release has many critical improvements and bug fixes.

Here are the highlight of new features:
gem-usage.png

Daemon mode

GlassFish gem is started by JRuby, which starts a JVM in which GlassFish runs. In daemon mode, GlassFish forks itself with the optimal JVM settings as a daemon process and writes out the process id in to a file. The default filename is tmp/pids/glassfish-<PID>.pid. To stop GlassFish gem server, you would send it SIGINT. This process id file is automatically deleted when server is stopped using SIGINT. This will enable kill and bounce of GlassFish gem server.

You can override the name and location of the PID file using -P or --pid option. At any time if you want to clean up zombie  PID files, simply run the following rake task inside your app directory:

[~/dev/depot]$ jruby rake tmp:pids:clear

The daemon mode is made possible by using Akuma, which is another very useful library from Kohsuke. I am very interested in experimenting with the Network Server feature and see how it can help us create GlassFish gem cluster.

daemon.png

During daemonization, during fork, daemon logs are written in log/glassfish-daemon.log file. Here, the time stamp, name of the PID file and other informations are recorded. This file is appended with the each new daemon instance. Rest of the server log goes in to usual logging file.

Daemon mode will enable Capistrano style deployment for GlassFish gem. Jacob is going to start working on the Capistrano recipe for GlassFish gem as well.

GlassFish rake task

A new rake task, gfrake is introduced in this release. It basically would let you create a template configuration file (see below about what it is) and clean up glassfish generated temporary files. gfrake.png

Configuration

At times, specially in production you would like to define the configuration information in a static file instead of wrapping CLI options in another script. Now you can do so by creating a glassfish.yml file in the config directory. To create it run gfrake config inside your application directory. By default, GlassFish gem looks for glassfish.yml inside the config directory. You can override it by passing your own using --config option.

glassfish.yml contain not only CLI but other options as well. For example, you can define the JVM options with which GlassFish should start in daemon mode. This would be useful to tune the JVM to get  optimal performance for your given hardware resources.
config.png
Please note that this is the first version of configuration file and in latter releases will include more options. Although we will be very careful about not breaking backward compatibility but if it happens, such decisions will be taken judiciously and communicated.

sudo not needed any more

This is really not a feature rather a bug fix. GlassFish gem is based on the GlassFish v3 kernel. A fundamental aspect of GlassFish v3 server is that it runs an instance to serve a domain and the domain by default is located inside the GlassFish gem install location. This is a real problem for those who install GlassFish gem in a root privilege location. When trying to run glassfish with user privilege would fail as glassfish would detect that it can not write in the root privilege location. This has been reported by many users.

The real fix is in GlassFish v3 where such error reporting are deffered and since there is no write in case of gem, such deffered checks will not cause failures for gem.

In the mean time, GlassFish gem has a temporary solution, on startup it creates a temporary domain inside tmp/.glasfish of your application directory.

Improved logging

Unlike previous versions of gem logging is not displayed on the console. On startup, GlassFish gem displays basic information and the logging goes to log/development.log or whatever your environment is. For example, if you are running the app in production, logs will go to log/production.log.

You can override where logging should go using -l or --log option.

[~/dev/depot]$ glassfish -l mylog.log

You can easily control the log level using --log-level option. The various log levels are:
log-level.png

Improved error reporting

There have been many improvements in the error detection, reporting. For example, instead of reporting port is busy after server starts, the check will happen early on, various validation improvements etc. This is a continuing work and with every release we will improve on anything that can make it easier to diagnose and help fix the problems.

Support for Sinatra and framework auto-detection

Beside Rails and Merb Sinatra is supported as well. GlassFish supports any Rack based framework. To run your framework of choice simply place the rackup script (.ru) inside your application directory or set JVM property -Djruby.applicationType=/path/to/myframework.ru and GlassFish will plugin your script automatially.

Save the code below in a .rb file and simply run glassfish in that directory and access your RESTful service at http://localhost:3000/.


require 'rubygems'
require 'sinatra'
get '/' do
"Hello world"
end

Server busy handling

In prior releases, when GlassFish gem is busy loading the application, a new request will give 404 HTTP error. This causes problems when you front-end Glassfish gem with some LoadBalancer, as it would treat 404 as client error and would not know what to do with it. This is fixed. Now, when GlassFish gem is busy loading application and a new request arrives, it will give a 503 (Server Unavailable) and a 'Loading application..." page and the load balancer will know not to route to that server while the 503 is in effect.

Check out README and start using GlassFish gem and send your feedback to the forum and report any issues at the issue tracker.
 



June 2009
Sun Mon Tue Wed Thu Fri Sat
  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        


Search this blog:
  

Categories
Community: Glassfish
Community: Java Enterprise
Community: Java Web Services and XML
Extreme Programming
J2EE
JavaOne
Web Applications
Web Services and XML
Archives

June 2009
April 2009
March 2009
January 2009
December 2008
November 2008
October 2008
June 2008
May 2008
March 2008
January 2008
December 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006
October 2006
September 2006
August 2006
May 2006
January 2006

Recent Entries

Run Django applications on GlassFish v3 Preview

Slides for Dynamic Language on GlassFish technical session

GlassFish gem 0.9.5 available now



Powered by
Movable Type 3.01D


 Feed java.net RSS Feeds