Skip to main content

Plug-in any Ruby/Rack based framework to GlassFish

Posted by vivekp on April 3, 2009 at 10:33 PM PDT


GlassFish
gem
as well as href="http://download.java.net/glassfish/v3/promoted/latest-web.zip">GlassFish
v3 supports href="http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html">Rack.
Rack provides an interface to plugin a Ruby web framework with a web sever.
Similar to Python href="http://www.wsgi.org/wsgi/What_is_WSGI">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 href="http://dist.codehaus.org/jruby/1.2.0/">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 src="http://weblogs.java.net/blog/vivekp/archive/images/rackapp.png"
height="208" width="551">

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 href="http://blogs.sun.com/Jacobkessler/entry/the_rack">blog
where he explains how Sintra application is plugged in to GlassFish.

Related Topics >>

Comments

I've been tryig to get this working, but it fails right away with this: SEVERE: uninitialized constant Rack::VERSION Any idea why? jruby 1.4.0dev (ruby 1.8.6p287) (2009-07-13 6586) (Java HotSpot(TM) Client VM 1.6.0_07) [i386-java] glassfish (0.9.5), rack (1.0.0)

OK, success. I added this line to the .ru file: require 'jruby/rack/grizzly_helper' (Oh and I still need to define Rack::VERSION as above.)

OK, maybe not so simple. I added this to the top of my test.rb file: module Rack VERSION = '1.0' end Now I get this: SEVERE: uninitialized constant JRuby::Rack from :1 com.sun.grizzly.jruby.rack.RackInitializationException: uninitialized constant JRuby::Rack from :1 at com.sun.grizzly.jruby.rack.DefaultRackApplicationFactory.createApplication(DefaultRackApplicationFactory.ja va:169) at com.sun.grizzly.jruby.rack.DefaultRackApplicationFactory.newApplication(DefaultRackApplicationFactory.java: 69) Something is missing?

I also get the uninitialized constant Rack::VERSION error. As far as I can see, this is because the jruby-rack.jar is not being found. Where should it be placed for the glassfish gem to use it?