Tuesday Mar 10, 2009

TOTD # 73: JRuby and GlassFish Integration Test #4: JRuby 1.2.0 RC2 + Rails 2.2.x + GlassFish v2 + Warbler


TOTD #70, #71, #72 shows the first three integration tests that I typically run to ensure that the latest JRuby and GlassFish versions work nicely with each other.  #70 showed how to create a trivial Rails application and run it using GlassFish Gem#71 showed how the same application can be deployed on GlassFish v3 Prelude#72 showed how to deploy the same application on GlassFish v3.

The next test in the series is to ensure WAR-based deployment of a Rails application continue to work on GlassFish v2. It also shows that JNDI database connection pooling also work as expected. The latest publicly available build is GlassFish v2.1.

Lets begin integration test #4.
  1. Install Warbler gem ...

    ~/tools/jruby-1.2.0RC2/samples/rails/runner >../../../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
    Successfully installed warbler-0.9.12
    1 gem installed
    Installing ri documentation for warbler-0.9.12...
    Installing RDoc documentation for warbler-0.9.12...
  2. Edit "config/database.yml" and change the production database adapater from:

    production:
      adapter: mysql
      encoding: utf8
      database: runner_production
      pool: 5
      username: root
      password:
      socket: /tmp/mysql.sock

    to

    production:
      adapter: jdbcmysql
      encoding: utf8
      database: runner_production
      pool: 5
      username: duke
      password: glassfish
      socket: /tmp/mysql.sock
      jndi: jdbc/runner_production
      driver: com.mysql.jdbc.Driver

    The changes are highlighted in bold.

    Notice "jndi" key/value pair is specified along with "username" and "password". The JNDI reference is created for the GlassFish domain later. The reference is not resolved when this adapter is used with the JRuby CLI and so it falls back to username/password. However this JNDI reference is correctly resolved during runtime when the application is deployed as a WAR file in GlassFish.
  3. Create and migrate the production database as:

    ~/tools/jruby-1.2.0RC1/samples/rails/runner >../../../bin/jruby -S rake db:create RAILS_ENV=production
    (in /Users/arungupta/tools/jruby-1.2.0RC1/samples/rails/runner)
    ~/tools/jruby-1.2.0RC1/samples/rails/runner >../../../bin/jruby -S rake db:migrate RAILS_ENV=production
    (in /Users/arungupta/tools/jruby-1.2.0RC1/samples/rails/runner)
    ==  CreateRunners: migrating ==================================================
    -- create_table(:runners)
       -> 0.1150s
       -> 0 rows
    ==  CreateRunners: migrated (0.1170s) =========================================

    Note, how "RAILS_ENV=production" is specified at the command-line to ensure the production environment.
  4. Copy MySQL Connector/J jar in GLASSFISH_HOME/lib as:

    ~/tools/glassfish/v2.1/glassfish/ >cp ~/tools/mysql-connector-java-5.1.6/mysql-connector-java-5.1.6-bin.jar ./lib

    This is required for connection to the MySQL database.
  5. Fire up GlassFish v2.1 as:

    ~/tools/glassfish/v2.1/glassfish/bin >./asadmin start-domain
    Starting Domain domain1, please wait.
    Default Log location is /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/logs/server.log.
    Redirecting output to /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/logs/server.log
    Domain domain1 started.
    Domain [domain1] is running [Sun GlassFish Enterprise Server v2.1 (9.1.1) (build b60e-fcs)] with its configuration and logs at: [/Users/arungupta/tools/glassfish/v2.1/glassfish/domains].
    Admin Console is available at [http://localhost:4848].
    Use the same port [4848] for "asadmin" commands.
    User web applications are available at these URLs:
    [http://localhost:8080 https://localhost:8181 ].
    Following web-contexts are available:
    [/web1  /__wstx-services runner ].
    Standard JMX Clients (like JConsole) can connect to JMXServiceURL:
    [service:jmx:rmi:///jndi/rmi://Macintosh-187.local:8686/jmxrmi] for domain management purposes.
    Domain listens on at least following ports for connections:
    [8080 8181 4848 3700 3820 3920 8686 ].
    Domain supports application server clusters and other standalone instances.

    The logs are created in "domains/domain1/logs/server.log". Optionally, you can specify "--verbose" on the command-line to dump the log on the console itself.
  6. Create JDBC connection pool as:

    ~/tools/glassfish/v2.1/glassfish/bin >./asadmin create-jdbc-connection-pool --datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource --restype javax.sql.DataSource --property "User=duke:Password=glassfish:URL=jdbc\\:mysql\\://localhost/runner_production" jdbc/runner_pool
    Command create-jdbc-connection-pool executed successfully.

  7. Create JDBC resource as:

    ~/tools/glassfish/v2.1/glassfish/bin >./asadmin create-jdbc-resource --connectionpoolid jdbc/runner_pool jdbc/runner_production
    Command create-jdbc-resource executed successfully.
  8. Create Warbler config file as:

    ~/tools/jruby-1.2.0RC2/samples/rails/runner >../../../bin/jruby -S warble config
    cp /Users/arungupta/tools/jruby-1.2.0RC2/lib/ruby/gems/1.8/gems/warbler-0.9.12/generators/warble/templates/warble.rb config/warble.rb
  9. Edit "config/warble.rb" to bundle the required gems by adding the following fragment:

     # Include all gems which are used by the web application
      require "#{RAILS_ROOT}/config/environment"
      BUILD_GEMS = %w(warbler rake rcov)
      for gem in Gem.loaded_specs.values
        next if BUILD_GEMS.include?(gem.name)
        config.gems[gem.name] = gem.version.version
      end

    as specified here. And then explicitly specify the runtime gem dependency by adding the following line:

    config.gems += ["activerecord-jdbc-adapter"]

    right after the previous code fragment. The "activerecord-jdbc-adapter" dependency needs to be explicitly included because this is required only at the runtime and so not resolved correctly by the previous code fragment.
  10. And create the WAR file as:

    ~/tools/jruby-1.2.0RC2/samples/rails/runner >../../../bin/jruby -S warble
    mkdir -p tmp/war/WEB-INF/gems/specifications
    cp /Users/arungupta/tools/jruby-1.2.0RC2/lib/ruby/gems/1.8/specifications/rails-2.2.2.gemspec tmp/war/WEB-INF/gems/specifications/rails-2.2.2.gemspec
    mkdir -p tmp/war/WEB-INF/gems/gems
    . . .
    cp public/javascripts/prototype.js tmp/war/javascripts/prototype.js
    cp public/stylesheets/scaffold.css tmp/war/stylesheets/scaffold.css
    mkdir -p tmp/war/WEB-INF
  11. Deploy the WAR file ...

    ~/tools/jruby-1.2.0RC2/samples/rails/runner >~/tools/glassfish/v2.1/glassfish/bin/asadmin deploy runner.war
    Command deploy executed successfully.

    After adding few entries the page at "http://localhost:8080/runner/runners" looks like:


So we are able to deploy a trivial Rails application as WAR file on GlassFish v2.1 and also leverage the JDBC connection pooling, that passes Test# 4.

Later blogs will show the remainder of tests. The current set of tests are available using the tags rubyonrails+glassfish+integrationtest.

Please leave suggestions on other TOTD (Tip Of The Day) that you'd like to see. A complete archive of all tips is available here.

Technorati: totd rubyonrails glassfish v2 jruby warbler connectionpooling jdbc jndi integrationtest

Wednesday Dec 03, 2008

LOTD #15: Deploying Merb application on GlassFish v3 using Warbler


GlassFish v3 with Merb and Warbler explains how to use Warbler to deploy a Merb application on GlassFish v3. Here is a quote from the blog:

this lovely server had a gem which you can use to fire up a Glassfish server. Easy as pie and within no time at all we were developing our Merb app on Glassfish.

The blog requires some workaround for Warbler and Nick is planning to fix them in the next rev. And other workaround is required because of Webrat's recently introduced dependency on Nokogiri which does not run on JRuby yet.

In addition, TOTD #52 explains how to deploy a Merb application using GlassFish gem and TOTD #53 explains how to create a scaffold in Merb.

Submit your Merb/GlassFish bugs here, talk to us using GlassFish Forum, and get the latest information on JRuby wiki.

All previous entries in this series are archived at LOTD.

Technorati: lotd glassfish v3 gem warbler merb

Tuesday Nov 11, 2008

LOTD #13: Warbling with RMagick on GlassFish




GlassFish with RMagick explains the Warbler configuration required for using RMagick with GlassFish.

Did you know Rails application can be natively deployed on GlassFish v3 Prelude ? And you can even buy production support!

All previous entries in this series are archived at LOTD.

Technorati: lotd rubyonrails rmagick glassfish

Sunday Apr 13, 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

Sunday Apr 06, 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 #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  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
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today