Miles to go …

April 7, 2008

Rails and Java EE integration – Warbler instead of Goldspike

Filed under: web2.0 — arungupta @ 12:02 am

Part 1 of this tri-series blog explained how a Java EE Servlet and Rails application can be packaged together as WAR file using Goldspike plugin and deployed on GlassFish v2 UR1. There are few issues with Goldspike as explained here. A better and recommended option is to use Warbler for packaging which provides a minimal, flexible and Ruby-like way to create WAR. Warbler really cleans up the packagingof WAR, for example excluding .svn directories, tests and migrations – really nice. For now, it uses RailsServlet for dispatching but even that is pluggable.

This blog, Part 2, will explain how to generate a WAR file using Warbler. Rails powered by GlassFish provides all the details on why GlassFish provides an industry-grade and functionally-rich Application Server.

Now detailed steps:

  1. In an existing JRuby 1.1 installation (enabled with Rails), install Warbler gem:

    ~/testbed/jruby-1.1 >bin/jruby -S gem install warbler
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    Updating metadata for 35 gems from http://gems.rubyforge.org
    ……………………………..
    complete
    Successfully installed warbler-0.9.4
    1 gem installed
    Installing ri documentation for warbler-0.9.4…
    Installing RDoc documentation for warbler-0.9.4…
  2. Create a Rails app “railsee2″ as:
    ~/testbed/jruby-1.1/samples/rails >../../bin/jruby -S rails railsee2
          create 
          create  app/controllers
          create  app/helpers
          create  app/models
          . . .
          create  log/production.log
          create  log/development.log
          create  log/test.log

    In order to keep it simple, this application will not be using any database so uncomment the following line from “config/environment.rb”(by removing “#” from beginning of the line):

    config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
  3. Create a new file “web.xml” in “config” directory and use the following contents:
    <!DOCTYPE web-app PUBLIC
      “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
      “http://java.sun.com/dtd/web-app_2_3.dtd”>
    <web-app>
            <servlet>
                    <servlet-name>HelloServlet</servlet-name>
                    <servlet-class>server.HelloServlet</servlet-class>
            </servlet>
            <servlet-mapping>
                    <servlet-name>HelloServlet</servlet-name>
                    <url-pattern>/hello</url-pattern>
            </servlet-mapping>
    </web-app>

    This “web.xml” is used by Warbler when packaging the WAR file.

  4. Create and Copy Servlet
    1. Create a Java library with Servlet code as explained in bullet #4 here.
    2. Copy “HelloServlet.jar” from “dist” directory of NetBeans project to “lib” directory of Rails application.
  5. Create and deploy the WAR
    1. Create a WAR file as:

      ~/testbed/jruby-1.1/samples/rails/railsee2 >~/testbed/jruby-1.1/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 railsee2.war -C tmp/war .
    2. Deploy the WAR to GlassFish v2 UR1 as:
      ~/testbed/jruby-1.1/samples/rails/railsee2 >~/testbed/glassfish/v2ur1/glassfish/bin/asadmin deploy railsee2.war
      Command deploy executed successfully.
  6. The bundled Servlet is now accessible at “http://localhost:8080/railsee2/hello”. The default browser output looks like:

    And passing a parameter to the URL as “http://localhost:8080/railsee2/hello?name=Arun” shows the output as:

With this, your Java EE Servlet is now bundled with your Rails application deployed on GlassFish v2 UR1.

Now, lets add Controller and View to Rails application and invoke this servlet from there to show complete integration with Rails.

  1. Create a new Controller and View as

    ~/testbed/jruby-1.1/samples/rails/railsee2 >../../../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&nbsp
    ; 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 generated controller in “app/controllers/home_controller.rb” to:
    class HomeController < ApplicationController

    include Java

      def index
            url = java.net.URL.new(”http://localhost:8080/railsee2/hello”);
            conn = url.open_connection;
            reader = java.io.BufferedReader.new(java.io.InputStreamReader.new(conn.get_input_stream));
            @servlet_output = “”;
            input_line = reader.read_line;
            while input_line != nil
                    @servlet_output << input_line;
                    input_line = reader.read_line;
            end
            reader.close;
      end
    end

  3. Change the generated view in “app/views/home/index.rhtml.erb” to:
    <h1>Home#index</h1>
    <p>Find me in app/views/home/index.html.erb</p>

    <%= @servlet_output %>

  4. Re-create the WAR as describd in bullet # 5 above. And now “http://localhost:8080/railsee2/home/index” shows the output as shown:

This blog explained how a Java EE 5 Servlet and Rails application can be packaged Warbler and deployed on GlassFish. Warbler docs provide detail about usage, configuration, web.xml and other details.

The next blog in this tri-series blog will explain how such an application can be deployed on GlassFish v3. And the cool thing there is that you don’t need Goldspike, Warbler or any other additional plugins – total native deployment, stay tuned!

Technorati: rubyonrails netbeans glassfish v3 javaee5 servlets jruby ruby warbler

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. WAR-based Packaging and Deployment of Rails on GlassFish – Goldspike, RailServlet, Warbler, Rack, …
  2. Rails and Java EE integration – Native Rails on GlassFish v3
  3. Rails and Java EE integration – Servlet co-bundled and invoked from Rails
  4. TOTD #73: JRuby and GlassFish Integration Test #4: JRuby 1.2.0 RC2 + Rails 2.2.x + GlassFish v2 + Warbler
  5. LOTD #15: Deploying Merb application on GlassFish v3 using Warbler

9 Comments »

  1. Nice blog

    Comment by Guddu — April 8, 2008 @ 11:24 pm

  2. [Trackback] The last part of this tri-series blog (Part 1, Part 2) will show how a Rails application can be deployed on GlassFish – without the need of Goldspike, Warbler or any other gem or plugin. Yes, that’s a native…

    Comment by Arun Gupta's Blog — April 14, 2008 @ 8:20 am

  3. In step #4 above, I think you mean bullet #4 of the other blog.

    Cheers,
    Bobby

    Comment by Bobby Bissett — August 21, 2008 @ 9:11 am

  4. [Trackback] Last day of Rails Conf Europe 2008 (Day 1 &amp; Day 2), and it’s finally over! David Black’s opening session talked about Ruby and Rails Symposium: Versions, Implementations, and the Future. Here is a brief summary of MRI Ruby…

    Comment by Arun Gupta's Blog — September 5, 2008 @ 10:19 pm

  5. cool thing there is that you don’t need Goldspike, Warbler right dude…

    Comment by Girl Pics — March 8, 2009 @ 2:29 pm

  6. Great article, beacause i was generating the WAR file with Goldspike, but Goldspike is deprecated, so Wabler really help me a lot. I will visit your blog constantly because now i’m on a jruby/blazeDs/glassfish project for my degree in Computer Science (University).
    Greetings.

    Comment by Alexis Tejeda — June 25, 2009 @ 1:16 am

  7. Alexis, please let us know once your project is complete. Do you plan to publish a blog entry describing the details ?

    Comment by Arun Gupta — July 2, 2009 @ 10:22 am

  8. Hi Arun, in few words, i’m integrating BlazeDs (live cycle data from Adobe) with Jruby (with rails), but i’m experimenting some troubles with a servlet mapping of BlazeDs, if you have time to help me with this big-tiny issue please contact me in my email alexis.tejeda [At] gmail Dot com, thanks in advance.

    Comment by Alexis Tejeda — July 10, 2009 @ 12:15 pm

  9. Alexis, I’ve not played with BlazeDS so don’t how much I can help but sent you an email and we can discuss there.

    Comment by Arun Gupta — July 10, 2009 @ 12:26 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress