Plug-in any Ruby/Rack based framework to GlassFish
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 glassfishNow, lets write a trivial app and save it in file: myapp.rb
class MyApp<br> def self.Run<br> "Hello World"<br> end<br>end<br>Write a rack-up script and save it in myframework.ru
<br>require 'rubygems'<br>gem 'rack'<br>require 'rack/handler/grizzly'<br>require 'myapp'<br><br>app = proc do |env|<br> resp = MyApp.Run<br> [<br> 200, # Status code<br> { # Response headers<br> 'Content-Type' => 'text/html',<br> 'Content-Length' => resp.length,<br> },<br> [resp] # Response body<br> ]<br>end<br>
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.
<br>% glassfish<br>Starting GlassFish server at: 192.168.1.103:3000 in development environment...<br>Writing log messages to: /Users/vivek/dev/foo/log/development.log.<br>Press Ctrl+C to stop.<br></code><br>
Now access, http://localhost:3000
in your browser to access your app.
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.
- Login or register to post comments
- Printer-friendly version
- vivekp's blog
- 2899 reads






Comments
by jamesbritt - 2009-07-19 12:15
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)by johnlloydjones - 2009-08-01 09:16
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.)by johnlloydjones - 2009-08-01 08:50
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?by johnlloydjones - 2009-08-01 08:36
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?