Miles to go ...

Arun Gupta is a Technology Evangelist for Web Services and Web 2.0 Apps at Sun. He was the spec lead for APIs in the Java platform, committer in multiple Open Source projects, participated in standard bodies and contributed to Java EE and SE releases.
Main | Next page »

http://blogs.sun.com/arungupta/date/20080514 Wednesday May 14, 2008

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


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 Goldspike plugin (nee Rails-integration) and using RailsServlet (part of Goldspike) for dispatching.
  • Warbler + RailsServlet: The second iteration (slightly short lived) overcame the shortcomings of Goldspike (packaging with sane defaults, fast, light-weight, and flexible configuration) by using 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 GlassFish v2 Update Center.
  • Warbler + Rack:  Nick released JRuby-Rack (JRuby version of Rack, also see 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 GlassFish. This blog entry explains how to deploy a simple Rack-based Rails application.
  1. Install Rails and JRuby-Rack (as part of Warbler) as:

    ~/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:

    ~/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:

       config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
  4. Create a WAR file as:

    ~/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 here.
    2. Start GlassFish Application Server as:

      ~/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:

      ~/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:

      [#|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:

  7. Add some functionality to the application to show Servlet and Rack integration
    1. Add a Controller and View as

      ~/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:

      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":

      <%= @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:

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

  <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: rubyonrails jruby ruby rack merb glassfish

del.icio.us | furl | simpy | slashdot | technorati | digg

http://blogs.sun.com/arungupta/date/20080504 Sunday May 04, 2008

GlassFish v3 Gem 0.2.0 released


A newer version of GlassFish v3 Gem for Ruby on Rails is now available.

What's new ?

  • Codebase aligned with GlassFish v3 Technology Preview 2
  • Previous version (0.1.2) had some packaging issues which increased the size but now it's back to sweet 2.8 Mb.
Check if previously installed by using the following command:

~/testbed/jruby-1.1.1 >bin/jruby -S gem list glassfish

*** LOCAL GEMS ***

glassfish (0.1.2)

If already installed (as indicated by the list of gems) then uinstall it using the following command:

~/testbed/jruby-1.1.1 >bin/jruby -S gem uninstall glassfish
Successfully uninstalled glassfish-0.1.2-universal-java
Remove executables:
        glassfish_rails, asadmin, asadmin.bat

in addition to the gem? [Yn]  y
Removing glassfish_rails
Removing asadmin
Removing asadmin.bat

And then install it again as:

~/testbed/jruby-1.1.1 >bin/jruby -S gem install glassfish
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Successfully installed glassfish-0.2.0-universal-java
1 gem installed

Updating the gem is giving unpredictable results (mostly not updating) and will be investigated later.

Rails powered by the GlassFish Application Server explains why GlassFish is a better deployment option for Rails applications.

All the latest information about the gem can be found at GlassFish JRuby wiki or JRuby wiki.

Please use the gem and send us feedback on GlassFish forums, dev@glassfish or gem mailing list.

Technorati: rubyonrails jruby ruby glassfish v3 gem

del.icio.us | furl | simpy | slashdot | technorati | digg

http://blogs.sun.com/arungupta/date/20080503 Saturday May 03, 2008

JRuby 1.1.1, Rails 2.0.2, Warbler now in GlassFish v2 Update Center


Vivek updated JRuby module in GlassFish v2 UR2 Update Center. It now contains JRuby 1.1.1 + Rails 2.0.2 and Warbler (instead of Goldspike) for packaging the app.

Read all details on how to download, install and getting started here. The image below shows a snapshot of Update Center with the latest module selected:


After following all the steps (had to manually set executable perms on jruby-1.1.1/bin/jruby), your application is now hosted at "http://localhost:8080/HelloWorldRailsApp/say/hello" and shows the following output:



The generated "HelloWorldRailsApp.war" is approx 8.5 Mb and could quickly grow depending upon the gems installed and other factors. As an alternative, you can consider shared deployment as explained in each sample's GLASSFISH_README.txt.

Send us feedback at Forums or webtier@glassfish.

Technorati: rubyonrails jruby ruby glassfish updatecenter

del.icio.us | furl | simpy | slashdot | technorati | digg

http://blogs.sun.com/arungupta/date/20080421 Monday April 21, 2008

JRuby and GlassFish v2 - Another successful deployment @ WorldxChange Communication NZ


From proof-of-concept to production in 8 weeks, WorldxChange Communication NZ's online billing system is another succes story of JRuby and GlassFish v2. The portal is designed solely using NetBeans 6.1 IDE.

Here are some of the quotes from the completed questionnaire:

From my perspective, the main advantage was that I could deploy my JRuby project war file directly to Glassfish, allowing me to develop and test our online ViewBill portal using a production grade, scalable web server.

From a geek perspective, we love that Glassfish combined with JRuby and allowed us to integrate many different disparate systems to create a seamless interface for our customers to use.

started using the Glassfish v3 gem for final testing of new code releases and to check functionality prior to production deployment.

I do not believe that I could have developed this project any faster using different toolsets or technologies and have been massively impressed with the combination of Glassfish and JRuby.

Read more details here.

Rails powered by the GlassFish Application Server explains why to use GlassFish for powering your Rails applications.

You can find all all about JRuby and GlassFish efforts on the GlassFish wiki or JRuby wiki.

Technorati: glassfish netbeans rubyonrails jruby ruby stories

del.icio.us | furl | simpy | slashdot | technorati | digg

http://blogs.sun.com/arungupta/date/20080414 Monday April 14, 2008

Rails and Java EE integration - Native Rails on GlassFish v3


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 Rails app deployment on GlassFish v3.

GlassFish v3 is next version of GlassFish v2 and the focus is modularization, enablement of non-Java EE containers and modularity - download b09.

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:

    ~/testbed/jruby-1.1/samples/rails >../../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:

      <!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:

      <?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 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:

    JRUBY_HOME="/Users/arungupta/testbed/jruby-1.1"

  5. Deploy the Rails application as:

  6. ~/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:



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


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

    ~/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:

    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:

    <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:

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:
Technorati: rubyonrails netbeans glassfish v3 javaee5 servlets jruby ruby warbler

del.icio.us | furl | simpy | slashdot | technorati | digg

http://blogs.sun.com/arungupta/date/20080408 Tuesday April 08, 2008

JRuby 1.1 released - ready for production!

JRuby 1.1 is released and ready for production use today. You can unleash the potential in 3 simple steps - download, unzip & get started. Here is a quote from the announcement:

more and more reports of applications exceeding Ruby 1.8.6 performance; we are even beating Ruby 1.9 in some microbenchmarks

This is validated by 1 (Oct 2007), 2 (Nov 2007), 3 (Feb 2008), 4 (Feb 2008) and many other hidden/unknown reports. You can easily configure JRuby 1.1 as the platform of your choice in NetBeans 6.1 as described in TOTD #27.

Now, match this blazing performance of JRuby with GlassFish v3 gem (only 2.4 Mb) which provides a superior solution for developing and deploying Rails application. The main reasons for using GlassFish instead of WEBrick/Mongrel/LightTPD are:

  • Identical development and deployment environments
  • Handles multiple requests by maintaining JRuby runtimes and database connection pools
  • Ability to redploy an application without restarting container
  • In-built Clustering, Load Balancing, and High Availability support
  • Allows to leverage investment in a Java EE infrastructure and cohosting Ruby-on-Rails and Java EE applications
All the above mentioned reasons, and much more, are explained in detail in Rails powered by the GlassFish Application Server.

The gem installation is extremely simple as shown below:

~/testbed/jruby-1.1 >bin/jruby -S gem install glassfish
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Updating metadata for 93 gems from http://gems.rubyforge.org
.............................................................................................
complete
Successfully installed glassfish-0.1.2-universal-java
1 gem installed

After the gem is installed, get started by developing a simple scaffold application or Mephisto.

If you are attending JavaOne 2008, then LAB 8400 will show how to develop and deploy Rails applications using NetBeans and deploy them on GlassFish. LAB 4520 will teach you more about the modular and pluggable GlassFish v3 architecture.

GlassFish JRuby wiki or JRuby wiki gives you all the latest information about JRuby-on-Rails and GlassFish.

Technorati: jruby ruby rubyonrails netbeans glassfish v3 gem

del.icio.us | furl | simpy | slashdot | technorati | digg

http://blogs.sun.com/arungupta/date/20080407 Monday April 07, 2008

Rails and Java EE integration - Warbler instead of Goldspike


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 #5 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  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

del.icio.us | furl | simpy | slashdot | technorati | digg

http://blogs.sun.com/arungupta/date/20080404 Friday April 04, 2008

Rails and Java EE integration - Servlet co-bundled and invoked from Rails

One of the several advantages of deploying Rails applications on GlassFish is - co-hosting Rails and Java EE applications in the same container. The screencast #web9 shows how Rails applications can be easily deployed on GlassFish.

This blog takes the JRuby-on-Rails (jRoR) and Java EE integration to the next level. It show how a Java EE Servlet can be co-bundled within a jRoR application. And then it shows how such a servlet can be easily invoked from a Rails view. It uses WAR-based mode for deploying jRoR applications on GlassFish.

Here are the detailed steps:

  1. In an existing JRuby 1.1 RC3 installation (enabled with Rails), install the required gems:

    ~/testbed/jruby-1.1RC3 >bin/jruby -S gem install activerecord-jdbc-adapter   
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    Successfully installed activerecord-jdbc-adapter-0.8
    1 gem installed
    Installing ri documentation for activerecord-jdbc-adapter-0.8...
    Installing RDoc documentation for activerecord-jdbc-adapter-0.8...
  2. Create a Rails app as:

    ~/testbed/jruby-1.1RC3/samples/rails >../../bin/jruby -S rails railsee1
          create 
          create  app/controllers
          create  app/helpers
          create  app/models
          create  app/views/layouts
          create  config/environments
          create  config/initializers
          create  db
          create  doc
          create  lib
          create  lib/tasks
          create  log
          create  public/images
          create  public/javascripts
          create  public/stylesheets
          create  script/performance
          create  script/process
          create  test/fixtures
          create  test/functional
          create  test/integration
          create  test/mocks/development
          create  test/mocks/test
          create  test/unit
          create  vendor
          create  vendor/plugins
          create  tmp/sessions
          create  tmp/sockets
          create  tmp/cache
          create  tmp/pids
          create  Rakefile
          create  README
          create  app/controllers/application.rb
          create  app/helpers/application_helper.rb
          create  test/test_helper.rb
          create  config/database.yml
          create  config/routes.rb
          create  public/.htaccess
          create  config/initializers/inflections.rb
          create  config/initializers/mime_types.rb
          create  config/boot.rb
          create  config/environment.rb
          create  config/environments/production.rb
          create  config/environments/development.rb
          create  config/environments/test.rb
          create  script/about
          create  script/console
          create  script/destroy
          create  script/generate
          create  script/performance/benchmarker
          create  script/performance/profiler
          create  script/performance/request
          create  script/process/reaper
          create  script/process/spawner
          create  script/process/inspector
          create  script/runner
          create  script/server
          create  script/plugin
          create  public/dispatch.rb
          create  public/dispatch.cgi
          create  public/dispatch.fcgi
          create  public/404.html
          create  public/422.html
          create  public/500.html
          create  public/index.html
          create  public/favicon.ico
          create  public/robots.txt
          create  public/images/rails.png
          create  public/javascripts/prototype.js
          create  public/javascripts/effects.js
          create  public/javascripts/dragdrop.js
          create  public/javascripts/controls.js
          create  public/javascripts/application.js
          create  doc/README_FOR_APP
          create  log/server.log
          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" (remove "#" at  beginning of the line):

    config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
  3. Install Goldspike plugin in the application as:

    ~/testbed/jruby-1.1RC3/samples/rails/railsee1 >../../../bin/jruby script/plugin install svn://rubyforge.org/var/svn/jruby-extras/trunk/rails-integration/plugins/goldspike
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/test
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/test/war_config_test_config.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/test/test_java_library.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/test/test_maven_library.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/test/test_create_war.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/Rakefile
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/init.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/tasks
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/tasks/war.rake
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/lib
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/lib/util.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/lib/war_config.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/lib/run.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/lib/java_library.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/lib/packer.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/lib/create_war.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/install.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/generators
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/generators/goldspike
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/generators/goldspike/goldspike_generator.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/generators/goldspike/templates
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/generators/goldspike/templates/web.xml.erb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/generators/goldspike/templates/war.rb
    A    /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1/vendor/plugins/goldspike/README
    Exported revision 960.
          exists  config
          create  config/war.rb
          create  WEB-INF
          create  WEB-INF/web.xml.erb

    Optionally Warbler may be used for packaging and Goldspike for dispatching.
  4. Create a Java library with Servlet code
    1. Using NetBeans IDE, create a project of type "Java Class Library" and specify the name "HelloServlet".
    2. Right-click on the project, select "New" and "Servlet...". Enter the class name as "HelloServlet" and package as "server" as shown below:

    3. Right-click on the project, select Properties and in "Libraries", "Compile Tab Libraries" add "javaee.jar" from GlassFish v2 UR1 "lib" directory.
    4. Change the "try" block in "processRequest" method to following:

      String name = request.getParameter("name");
      if (name == null || name.equals(""))
          name = "Duke";
                 
      out.println("<h1>Servlet HelloServlet at " + request.getContextPath () + " says Hello " + name + "!</h1>");
    5. Right-click on the project and select "Clean and Build".
  5. Configure Rails app for Servlet
    1. In your Rails application directory, add the following fragments to "WEB-INF/web.xml.erb":

      <servlet>
                      <servlet-name>hello</servlet-name>
                      <servlet-class>server.HelloServlet</servlet-class>
      </servlet>

      <servlet-mapping>
                      <servlet-name>hello</servlet-name>
                      <url-pattern>/hello</url-pattern>
      </servlet-mapping>
    2. Create "WEB-INF/lib" directory and copy "HelloServlet.jar" from the "dist" directory of NetBeans project here.
  6. Create & Deploy the WAR
    1. Create a WAR file as:

      ~/testbed/jruby-1.1RC3/samples/rails/railsee1 >../../../bin/jruby -S rake war:standalone:create
      (in /Users/arungupta/testbed/jruby-1.1RC3/samples/rails/railsee1)
      info: Assembling web application
      info: Packing needed Java libraries ...
      info:   adding Java library jruby-complete-1.1RC3
      info:   adding Java library goldspike-1.6
      info:   adding Java library activation-1.1
      info:   adding Java library commons-pool-1.3
      info:   adding Java library bcprov-jdk14-124
      info: Packing needed Ruby gems ...
      info:   adding Ruby gem rails version 2.0.2
      info:   adding Ruby gem rake version 0.8.1
      info:   adding Ruby gem activesupport version 2.0.2
      info:   adding Ruby gem activerecord version 2.0.2
      info:   adding Ruby gem actionpack version 2.0.2
      info:   adding Ruby gem actionmailer version 2.0.2
      info:   adding Ruby gem activeresource version 2.0.2
      info:   adding Ruby gem activerecord-jdbc-adapter version 0.8
      info: Packing needed files ...
      info: Creating web archive
    2. Deploy the WAR to GlassFish v2 UR1 as:

      ~/testbed/jruby-1.1RC3/samples/rails/railsee1 >~/testbed/glassfish/v2ur1/glassfish/bin/asadmin deploy railsee1.war
      Command deploy executed successfully.
  7. The bundled Servlet is now accessible at "http://localhost:8080/railsee1/hello". The default browser output looks like:



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

  8. 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.1RC3/samples/rails/railsee1 >../../../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:

    class HomeController < ApplicationController

    include Java

      def index
            url = java.net.URL.new("http://localhost:8080/railsee1/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 & re-deploy the WAR as describd in bullet # 6 above. And now "http://localhost:8080/railsee1/home/index" shows the output as shown:

This shows how a Java EE 5 Servlet can be easily invoked from a Rails application deployed on GlassFish. JRuby-on-Rails and GlassFish allows you to leverage business knowledge that exists in Java EE applications very easily and still providing Rails agility.


Technorati: rubyonrails netbeans glassfish javaee5 servlets jruby ruby goldspike

del.icio.us | furl | simpy | slashdot | technorati | digg

http://blogs.sun.com/arungupta/date/20080403 Thursday April 03, 2008

Merb on JRuby 1.1 RC3


This blog provides how you can get started with Merb on JRuby 1.1 RC3.

Merb is another MVC framework (just like Rails) but with a pluggable ORM, JavaScript library and Template language. Rails has built-in support for these using ActiveRecord, Script.aculo.us and ERB templates. Making it pluggable keeps the core very lightweight and still providing support for a particular feature using plugins. Another big advantage of Merb is that unlike Rails it's thread-safe.

There are already third party supports for ActiveRecord, DataMapper and Sequel ORMs. I will hopefully be able to build support for Java Persistence API ORM in Merb. Until then, here is how you install and get started with Merb on JRuby 1.1 RC3.

Install Merb on JRuby as:

Macintosh-187:jruby-1.1RC3 arungupta$ bin/jruby -S gem install merb mongrel
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Updating metadata for 339 gems from http://gems.rubyforge.org
.........................................................................................................
.........................................................................................................
.........................................................................................................
........................
complete
Successfully installed abstract-1.0.0
Successfully installed erubis-2.5.0
Successfully installed json_pure-1.1.2
Successfully installed rack-0.3.0
Successfully installed hpricot-0.6-java
Successfully installed mime-types-1.15
Successfully installed merb-core-0.9.2
Successfully installed merb-action-args-0.9.2
Successfully installed merb-assets-0.9.2
Successfully installed activesupport-2.0.2
Successfully installed rubigen-1.2.4
Successfully installed merb-gen-0.9.2
Successfully installed merb-haml-0.9.2
Successfully installed merb-builder-0.9.2
Successfully installed mailfactory-1.2.3
Successfully installed merb-mailer-0.9.2
Successfully installed merb-parts-0.9.2
Successfully installed merb-cache-0.9.2
Successfully installed merb-more-0.9.2
Successfully installed merb-0.9.2
Successfully installed gem_plugin-0.2.3
Successfully installed mongrel-1.1.4-java
22 gems installed
Installing ri documentation for json_pure-1.1.2...
Installing ri documentation for rack-0.3.0...
Installing ri documentation for hpricot-0.6-java...
Installing ri documentation for mime-types-1.15...
Installing ri documentation for merb-core-0.9.2...
Installing ri documentation for merb-action-args-0.9.2...
Installing ri documentation for merb-assets-0.9.2...
Installing ri documentation for activesupport-2.0.2...
Installing ri documentation for rubigen-1.2.4...
Installing ri documentation for merb-gen-0.9.2...
Installing ri documentation for merb-haml-0.9.2...
Installing ri documentation for merb-builder-0.9.2...
Installing ri documentation for mailfactory-1.2.3...
Installing ri documentation for merb-mailer-0.9.2...
Installing ri documentation for merb-parts-0.9.2...
Installing ri documentation for merb-cache-0.9.2...
Installing ri documentation for gem_plugin-0.2.3...
Installing ri documentation for mongrel-1.1.4-java...
Installing RDoc documentation for json_pure-1.1.2...
Installing RDoc documentation for rack-0.3.0...
Installing RDoc documentation for hpricot-0.6-java...
Installing RDoc documentation for mime-types-1.15...
Installing RDoc documentation for merb-core-0.9.2...
Installing RDoc documentation for merb-action-args-0.9.2...
Installing RDoc documentation for merb-assets-0.9.2...
Installing RDoc documentation for activesupport-2.0.2...
Installing RDoc documentation for rubigen-1.2.4...
Installing RDoc documentation for merb-gen-0.9.2...
Installing RDoc documentation for merb-haml-0.9.2...
Installing RDoc documentation for merb-builder-0.9.2...
Installing RDoc documentation for mailfactory-1.2.3...
Installing RDoc documentation for merb-mailer-0.9.2...
Installing RDoc documentation for merb-parts-0.9.2...
Installing RDoc documentation for merb-cache-0.9.2...
Installing RDoc documentation for gem_plugin-0.2.3...
Installing RDoc documentation for mongrel-1.1.4-java...
This is so much simpler than couple of weeks ago where all the dependencies had to be explicitly installed. Now create a sample application as:

Macintosh-187:jruby-1.1RC3 arungupta$ cd samples/
Macintosh-187:samples arungupta$ mkdir merb
Macintosh-187:samples arungupta$ cd merb/
Macintosh-187:merb arungupta$ ../../bin/jruby -S merb-gen app hello
RubiGen::Scripts::Generate
      create  app
      create  autotest
      create  config
      create  public
      create  spec
      create  app/controllers
      create  app/helpers
      create  app/views
      create  app/views/exceptions
      create  app/views/layout
      create  config/environments
      create  public/images
      create  public/stylesheets
      create  autotest/discover.rb
      create  autotest/merb.rb
      create  autotest/merb_rspec.rb
      create  config/rack.rb
      create  config/router.rb
      create  config/init.rb
      create  public/merb.fcgi
      create  spec/spec.opts
      create  spec/spec_helper.rb
      create  app/controllers/application.rb
      create  app/controllers/exceptions.rb
      create  app/helpers/global_helpers.rb
      create  app/views/exceptions/internal_server_error.html.erb
      create  app/views/exceptions/not_acceptable.html.erb
      create  app/views/exceptions/not_found.html.erb
      create  app/views/layout/application.html.erb
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/rake.rb
      create  config/environments/test.rb
      create  public/images/merb.jpg
      create  public/stylesheets/master.css
      create  /Rakefile

The top-level directory looks like:

Macintosh-187:hello arungupta$ ls -la
total 8
drwxr-xr-x  8 arungupta  arungupta   272 Apr  1 22:20 .
drwxr-xr-x  3 arungupta  arungupta   102 Apr  1 22:20 ..
-rw-r--r--  1 arungupta  arungupta  3334 Apr  1 22:20 Rakefile
drwxr-xr-x  5 arungupta  arungupta   170 Apr  1 22:20 app
drwxr-xr-x  5 arungupta  arungupta   170 Apr  1 22:20 autotest
drwxr-xr-x  6 arungupta  arungupta   204 Apr  1 22:20 config
drwxr-xr-x  5 arungupta  arungupta   170 Apr  1 22:20 public
drwxr-xr-x  4 arungupta  arungupta   136 Apr  1 22:20 spec

And finally start the app as:

Macintosh-187:hello arungupta$ ../../../bin/jruby -S merb
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
 ~ Loaded DEVELOPMENT Environment...
 ~ Compiling routes...
 ~ Using 'share-nothing' cookie sessions (4kb limit per client)
 ~ Using Mongrel adapter

And the default app is hosted at "http://localhost:4000/" and shown in the browser as:

There is a wealth of information available to get you going after this. Here are couple of things to try:
  • If possible, use WARbler to package Merb app and deploy on GlassFish.
  • Try Java Persistence API as the pluggable ORM.

Technorati: rubyonrails merb jruby ruby jpa glassfish

del.icio.us | furl | simpy | slashdot | technorati | digg

http://blogs.sun.com/arungupta/date/20080327 Thursday March 27, 2008

Slides & Demos for Rails/GlassFish/jMaki session at TSS JS


As reported earlier, I presented on "Rails powered by GlassFish and jMaki" yesterday at The Server Side Java Symposium - Las Vegas. The slides are available here. The demos shown in the talk are available at:

Would you like to know why use GlassFish for Rails deployment ? Rails powered by the GlassFish Application Server provides all the details. All the latest information about JRuby and GlassFish effort can be found at GlassFish JRuby wiki or JRuby wiki.

As announced earlier, Ruby Developer Center is a new page launched earlier today and provides all the resources to get started with Ruby, JRuby, Ruby-on-Rails @ Sun.

Check out some pictures from the event:

</