Skip to main content

Rails and Java EE integration - Native Rails on GlassFish v3

Posted by arungupta on April 14, 2008 at 8:18 AM PDT



The last part of this tri-series blog ( href="http://blogs.sun.com/arungupta/entry/rails_and_java_ee_integration">Part
1, href="http://blogs.sun.com/arungupta/entry/rails_and_java_ee_integration1">Part
2) will show how a Rails application can be deployed on
GlassFish - without the need of href="http://wiki.jruby.org/wiki/Goldspike">Goldspike,
Warbler
or any other gem or plugin. Yes, that's a native Rails app deployment
on GlassFish v3.



href="http://wiki.glassfish.java.net/Wiki.jsp?page=PlanForGlassFishV3">GlassFish
v3 is next version of GlassFish v2 and the focus is
modularization, enablement of non-Java EE containers and modularity - href="http://download.java.net/javaee5/v3/releases/preview/glassfish-v3-preview2-b09.zip">download
b09.



href="http://developers.sun.com/appserver/reference/techart/rails_gf/">Rails
powered by GlassFish provides all the details on why
GlassFish provides an industry-grade and functionally-rich Application
Server.



Now detailed steps:

  1. Using JRuby
    1.1
     (installed with Rails), create a Rails app
    "railsee3" as:


    cellpadding="2" cellspacing="2">
    ~/testbed/jruby-1.1/samples/rails > style="font-weight: bold;">../../bin/jruby -S rails
    railsee3

          create 

          create 
    app/controllers

          create 
    app/helpers

          create 
    app/models

          . . .

          create 
    log/production.log

          create 
    log/development.log

          create 
    log/test.log
  2. Add Servlet descriptors
    1. Create a new directory "WEB-INF", and a new file
      "web.xml" in that directory using
      the following contents:


      cellpadding="2" cellspacing="2">
      <!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>
    2. Create a new file "sun-web.xml" in "WEB-INF" using the
      following contents:


      cellpadding="2" cellspacing="2">
      <?xml
      version="1.0" encoding="UTF-8"?>

      <!DOCTYPE
      sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server
      9.0 Servlet 2.5//EN" "http://www.sun.com/software

      /appserver/dtds/sun-web-app_2_5-0.dtd">

      <sun-web-app error-url="">

       
      <context-root>/servlet</context-root>

        <class-loader delegate="true"/>

      </sun-web-app>
    3. Create a new directory "WEB-INF/lib".
  3. Create and Copy Servlet
    1. Create a Java library with Servlet code as explained in href="http://blogs.sun.com/arungupta/entry/rails_and_java_ee_integration">bullet
      #5 here.
    2. Copy "HelloServlet.jar" from "dist" directory of NetBeans
      project to "WEB-INF/lib" directory.
  4. Configure JRuby-on-Rails in GlassFish - Edit
    "config/asenv.conf" in GlassFish directory and specify JRUBY_HOME as
    the last line:


    cellpadding="2" cellspacing="2">
    JRUBY_HOME="/Users/arungupta/testbed/jruby-1.1"


  5. Deploy the Rails application as:

  6. cellpadding="2" cellspacing="2">
    ~/testbed/jruby-1.1/samples/rails
    >~/testbed/glassfish/v3/p2b9/glassfish/bin/asadmin
    deploy --force=true railsee3


    railsee3 deployed successfully

    Command deploy executed successfully.
  7. The bundled Servlet is now accessible at
    "http://localhost:8080/servlet/hello". The default browser output
    looks like:



    src="http://blogs.sun.com/arungupta/resource/ror/railsee3-v3-helloworld-servlet-default-output.png">



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



    src="http://blogs.sun.com/arungupta/resource/ror/railsee3-v3-helloworld-servlet-custom-output.png">

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



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


    cellpadding="2" cellspacing="2">
    style="font-weight: bold;">~/testbed/jruby-1.1/samples/rails/railsee3
    >../../../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 generated controller in
    "app/controllers/home_controller.rb" to:


    cellpadding="2" cellspacing="2">
    class
    HomeController < ApplicationController



    include Java



      def index

           
    url = java.net.URL.new("http://localhost:8080/servlet/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:


    cellpadding="2" cellspacing="2">
    <h1>Home#index</h1>

    <p>Find me in
    app/views/home/index.html.erb</p>



    <%= @servlet_output %>
  4. Re-deploy the Rails application as shown in bullet # 5
    above and "http://localhost:8080/railsee3/home/index" shows the output
    as shown:



    src="http://blogs.sun.com/arungupta/resource/ror/railsee3-v3-default-output.png">

So this blog explained how a Rails application can be deployed on
GlassFish v3 without the need of any gems like Warbler or plugin like
Goldspike - total native deployment!



In summary, the tri-part blog showed the deployment models for a Rails
application on GlassFish. Each model showed how a Java EE 5 Servlet can
be co-bundled with Rails application and invoked from Rails view:

  • Part 1- href="http://blogs.sun.com/arungupta/entry/rails_and_java_ee_integration">WAR-based
    deployment on GlassFish v2 UR1 using Goldspike
  • Part 2 - href="http://blogs.sun.com/arungupta/entry/rails_and_java_ee_integration1">WAR-based
    deployment on GlassFish v2 UR1 using Warbler
  • Part 3 - href="http://blogs.sun.com/arungupta/entry/rails_and_java_ee_integration2">Native
    deployment on GlassFish v3 without any additional gems or plugins

Technorati: href="http://technorati.com/tags/rubyonrails">rubyonrails
netbeans
glassfish
v3 href="http://technorati.com/tags/javaee5">javaee5
servlets
jruby
ruby href="http://technorati.com/tags/warbler">warbler

Related Topics >>