Skip to main content

WAR-based Packaging and Deployment of Rails on GlassFish - Goldspike, RailServlet, Warbler, Rack, ...

Posted by arungupta on May 15, 2008 at 6:32 AM PDT



WAR-based
packaging and dispatching of Rails application on Java Application
Servers is going through third iteration based what is used for
packaging and dispatching:

  • Goldspike + RailsServlet: The first iteration was
    WAR-packaging of Rails app as defined by href="http://wiki.jruby.org/wiki/Goldspike#Goldspike_Rake_Plugin">Goldspike href="http://wiki.jruby.org/wiki/Goldspike#Goldspike_Rake_Plugin">
    plugin (nee Rails-integration) and using style="font-style: italic;">RailsServlet (part
    of Goldspike) for dispatching.
  • Warbler + RailsServlet: The second iteration (slightly
    short lived) overcame the href="http://blog.nicksieger.com/articles/2007/09/04/warbler-a-little-birdie-to-introduce-your-rails-app-to-java">shortcomings
    of Goldspike (packaging with sane defaults, fast,
    light-weight, and flexible configuration) by using href="http://wiki.jruby.org/wiki/Warbler">Warbler
    for packaging. It decoupled packaging and dispatching by doing only
    packaging and allowing for pluggable dispatching mechanism.
    RailsServlet continues to be the default Servlet binding mechanism.
    This is the version currently supported by href="http://blogs.sun.com/arungupta/entry/jruby_1_1_1_rails">GlassFish
    v2 Update Center.
  • Warbler + Rack:  href="http://blog.nicksieger.com/">
    Nick released href="http://wiki.jruby.org/wiki/JRuby_Rack">JRuby-Rack
    (JRuby version of Rack,
    also see href="http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html">Introducing
    Rack and Docs)
    last week. And so the third iteration is using Warbler packaging and
    Rack-based dispatching. JRuby-Rack provides a more seamless connection
    between the Servlet environment and Rack.



The JRuby-Rack
wiki
says "JRuby-Rack
is a lightweight adapter for the Java servlet environment that allows
any Rack-based application to run unmodified in a Java servlet
container. JRuby-Rack supports Rails, Merb, as well as any
Rack-compatible Ruby web framework.
".



This means that,
other than Rails, conceptually Merb applications (which also use Rack
for deployment) can now also be deployed on href="http://glassfish.org">GlassFish. This blog
entry explains how to deploy a simple Rack-based Rails application.

  1. Install Rails and JRuby-Rack (as part of href="http://caldersphere.rubyforge.org/warbler/">Warbler)
    as:


    cellpadding="2" cellspacing="2">
    ~/testbed/jruby-1.1.1
    >bin/jruby -S gem
    install rails warbler --no-ri --no-rdoc


    JRuby limited openssl loaded. gem install jruby-openssl for full
    support.

    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL

    Updating metadata for 289 gems from http://gems.rubyforge.org/

    ..............................................................................................................................

    ..............................................................................................................................

    .....................................

    complete

    Successfully installed activesupport-2.0.2

    Successfully installed activerecord-2.0.2

    Successfully installed actionpack-2.0.2

    Successfully installed actionmailer-2.0.2

    Successfully installed activeresource-2.0.2

    Successfully installed rails-2.0.2

    Successfully installed warbler-0.9.9

    7 gems installed
  2. Create a template Rails app as:


    cellpadding="2" cellspacing="2">
    ~/testbed/jruby-1.1.1/samples/rails
    >../../bin/jruby
    -S rails hello -d mysql


          create 

          create 
    app/controllers

          create 
    app/helpers

          create 
    app/models

          create 
    app/views/layouts

          create 
    config/environments

          . . .

          create 
    doc/README_FOR_APP

          create 
    log/server.log

          create 
    log/production.log

          create 
    log/development.log

          create 
    log/test.log
  3. Disable database access from the application by
    uncommenting line 21 (remove "#" at the beginning) from
    "config/environment.rb" as:


    cellpadding="2" cellspacing="2">
      
    config.frameworks -= [ :active_record, :active_resource, :action_mailer
    ]
  4. Create a WAR file as:


    cellpadding="2" cellspacing="2">
    ~/testbed/jruby-1.1.1/samples/rails/hello
    >../../../bin/jruby
    -S warble


    JRuby limited openssl loaded. gem install jruby-openssl for full
    support.

    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL

    jar cf hello.war -C tmp/war .


  5. A new file "hello.war" is generated in the current directory.

  6. The generated WAR file can be easily deployed on GlassFish.
    1. Download and Install GlassFish v2 UR2 from href="https://glassfish.dev.java.net/downloads/v2ur2-b04.html">here.
    2. Start GlassFish Application Server as:


      cellpadding="2" cellspacing="2">
      ~/testbed/glassfish/v2ur2/glassfish
      >bin/asadmin
      start-domain --verbose


      Starting Domain domain1, please wait.

      May 13, 2008 11:23:44 AM
      com.sun.enterprise.admin.servermgmt.launch.ASLauncher buildCommand

      INFO:

      /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/bin/java

      . . .



      [#|2008-05-13T11:34:13.252-0700|INFO|sun-appserver9.1|javax.enterprise.system.container.web|_ThreadID=10;_ThreadName=main;4848;|WEB0712:
      Starting Sun-Java-System/Application-Server HTTP/1.1 on 4848|#]



      [#|2008-05-13T11:34:13.691-0700|INFO|sun-appserver9.1|javax.enterprise.system.core.selfmanagement|_ThreadID=10;_ThreadName=main;|SMGT0007:
      Self Management Rules service is enabled|#]



      [#|2008-05-13T11:34:13.718-0700|INFO|sun-appserver9.1|javax.enterprise.system.core|_ThreadID=10;_ThreadName=main;|Application
      server startup complete.|#]
    3. Deploy the WAR on GlassFish as:


      cellpadding="2" cellspacing="2">
      ~/testbed/jruby-1.1.1/samples/rails/hello
      >~/testbed/glassfish/v2ur2/glassfish/bin/asadmin
      deploy hello.war


      Command deploy executed successfully.



      The output in the GlassFish console looks like:


      cellpadding="2" cellspacing="2">
      [#|2008-05-13T11:34:23.330-0700|INFO|sun-appserver9.1|javax.enterprise.system.tools.admin|_ThreadID=14;_ThreadName=httpWorkerThread-4848-0;/private/tmp/s1astempdomain1server1547440193/hello.war;|ADM1006:Uploading
      the file to:[/private/tmp/s1astempdomain1server1547440193/hello.war]|#]



      [#|2008-05-13T11:34:26.019-0700|INFO|sun-appserver9.1|javax.enterprise.system.tools.deployment|_ThreadID=15;_ThreadName=Thread-30;|deployed
      with moduleid = hello|#]



      [#|2008-05-13T11:34:30.626-0700|INFO|sun-appserver9.1|javax.enterprise.system.container.web|_ThreadID=16;_ThreadName=httpWorkerThread-4848-1;|PWC1412:
      WebModule[/hello] ServletContext.log():Info: using runtime pool timeout
      of 30 seconds|#]



      [#|2008-05-13T11:34:30.626-0700|INFO|sun-appserver9.1|javax.enterprise.system.container.web|_ThreadID=16;_ThreadName=httpWorkerThread-4848-1;|PWC1412:
      WebModule[/hello] ServletContext.log():Warning: no initial runtimes
      specified.|#]



      [#|2008-05-13T11:34:30.627-0700|INFO|sun-appserver9.1|javax.enterprise.system.container.web|_ThreadID=16;_ThreadName=httpWorkerThread-4848-1;|PWC1412:
      WebModule[/hello] ServletContext.log():Warning: no max runtimes
      specified.|#]
    4. The default Rails page is now visible at
      "http://localhost:8080/hello" as shown below:



      src="http://blogs.sun.com/arungupta/resource/ror/warbler-jruby-rack-hello-default-page.png">
  7. Add some functionality to the application to show Servlet
    and Rack integration
    1. Add a Controller and View as


      cellpadding="2" cellspacing="2">
      ~/testbed/jruby-1.1.1/samples/rails/hello
      >../../../bin/jruby
      script/generate controller home index


      JRuby limited openssl loaded. gem install jruby-openssl for full
      support.

      http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL

            exists 
      app/controllers/

            exists 
      app/helpers/

            create 
      app/views/home

            exists 
      test/functional/

            create 
      app/controllers/home_controller.rb

            create 
      test/functional/home_controller_test.rb

            create 
      app/helpers/home_helper.rb

            create 
      app/views/home/index.html.erb
    2. Change the "index" helper method in
      "app/controllers/home_controller.rb" as:


      cellpadding="2" cellspacing="2">
      def
      index

             
      @greeting = "Hello from Rack!!"



             
      # access Servlet Context

             
      @server_info = $servlet_context.get_server_info

             
      # alternative way to get Servlet Context

             
      #@server_info2 = request.env['java.servlet_context'].get_server_info



             
      # access Servlet Request

             
      @method = request.env['java.servlet_request'].get_method

             
      @request_uri = request.env['java.servlet_request'].get_request_uri

             
      @protocol = request.env['java.servlet_request'].get_protocol

             
      @port = request.env['java.servlet_request'].get_server_port

      end
    3. Add the following fragment as the last line in
      "app/views/home/index.html.erb":


      cellpadding="2" cellspacing="2">
      <%=
      @greeting %><br><br>

      Hosted on "<%= @server_info %>" on port "<%= @port
      %>"<br>

      <%= @method %> <%= @request_uri %>
      <%= @protocol %>
  8. Re-create and deploy the WAR file
    1. Re-create the WAR file as explained in step 4.
    2. Re-deploy the WAR file as explained in step 5.3.
    3. Now "http://localhost:8080/hello/home/index" shows the
      output as:



      src="http://blogs.sun.com/arungupta/resource/ror/warbler-jruby-rack-hello-custom-page.png">

The magic fragment in "tmp/war/WEB-INF/web.xml" is:


cellspacing="2">
 
<filter>

   
<filter-name>RackFilter</filter-name>

   
<filter-class>org.jruby.rack.RackFilter</filter-class>

  </filter>

  <filter-mapping>

   
<filter-name>RackFilter</filter-name>

   
<url-pattern>/*</url-pattern>

  </filter-mapping>



  <listener>

   
<listener-class>org.jruby.rack.rails.RailsServletContextListener</listener-class>

  </listener>



And also "WEB-INF/lib/jruby-rack-0.9.jar" is bundled in the WAR.

Let us know if you try Rack-based deployment of Merb applications on
GlassFish.



Technorati: href="http://technorati.com/tag/rubyonrails">rubyonrails
jruby href="http://technorati.com/tag/ruby">ruby href="http://technorati.com/tag/rack">rack href="http://technorati.com/tag/merb">merb href="http://technorati.com/tag/glassfish">glassfish

Related Topics >>