Deploying JRuby on Rails WAR on Sun Java System Web Server 7 Update 2

Deploying JRuby on Rails on Web Server 7 Update 2

Deploying JRuby on Rails WAR on Sun Java System Web Server 7 Update 2




Sun Java System Web Server 7 Update 2 (henceforth referred to as WS7U2)  is an industry leader in performance and scalability. It is now easy to deploy JRuby on Rails apps as WAR files on WS7U2.

What is JRuby ?

JRuby is an 100% pure-Java implementation of the Ruby programming language. Ruby, the full-featured object-oriented dynamic (scripting) language, with strong support for functional programming and metaprogramming allows for flexibility and ease of development. JRuby, a JVM-based interpreter for Ruby, combines the ease of the Ruby language with execution in the powerful JVM, including full integration to and from Java libraries.

What is Rails?

Rails speeds and simplifies database backed Web application development. With JRuby, Rails will gain access to the functionality, power, and industry acceptance of Java libraries and the JVM. .

Why JRuby on Rails?

Ruby on Rails  with its ease of development and Java platform with its JVM, libraries and Web Servers are a natural fit to develop enterprise quality applications in an easy and timely manner. This article details  the advantages of JRuby on Rails.

Why Sun Java System Web Server Update 2?

  • Allows JRuby on Rails to co-exist with servlet/jsp based web-apps
  • Deploying to WS7U2 would automatically give all the web server advantages including scalability, performance and security
  • Availability of Monitoring, Clustering and Administration capability to JRuby on Rails apps.

Creating JRuby on Rails WAR file

I used a Sparc, Solaris10 box for my deployment. WS7U2 supports several platforms. You may choose a platform suitable to your application development.
  • Download JRuby-1.1RC1, set JRUBY_HOME and add JRUBY_HOME/bin to your path.
%  cd  /space
% tar xvf /tmp/jruby-bin-1.1RC1.tar
% setenv JRUBY_HOME /space/jruby-1.1RC1
% set path=($JRUBY_HOME/bin $path)
% cd $JRUBY_HOME
  • Install rails. If behind a firewall, set HTTP_PROXY (format http://${http-proxy-host}:${http-proxy-port}/)
%  $JRUBY_HOME/bin/gem install rails -y --no-ri --no-rdoc
  • Download mysql and unpack it in say $MYSQL_HOME
%  cd /space
%  tar xvf /tmp/mysql-5.0.51a-solaris10-sparc.tar
%  cd /space
% tar xvf /tmp/mysql-connector-java-5.1.5.tar
% cp /space/mysql-connector-java-5.1.5/mysql-connector-java-5.1.5-bin.jar $JRUBY_HOME/lib
  • Create an app and cd to the directory
% cd $JRUBY_HOME
% jruby $JRUBY_HOME/bin/rails moviestore
% cd moviestore
  • Edit database.yml in moviestore/config directory , so that the file has the following contents:
development:
  adapter: jdbc
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/moviestore_development
  username: root


# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
  adapter: jdbc
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/moviestore_test
  username: root
  • Edit environment.rb in moviestore/config  to add the following lines(in green). Note the location of the lines should be in between the 2 lines in red.
require File.join(File.dirname(__FILE__), 'boot')

if RUBY_PLATFORM =~ /java/
   require 'rubygems'
   RAILS_CONNECTION_ADAPTERS = %w(jdbc)
end

Rails::Initializer.run do |config|
  • Start mysql and then create mysql databases as defined in database.yml
% cd $MYSQL_HOME
% ./configure
% $MYSQL_HOME/bin/mysqladmin -u root create moviestore_development
%  $MYSQL_HOME/bin/mysqladmin -u root create moviestore_test
  • cd to $JRUBY_HOME/moviestore and Install activerecord-jdbc gem
% cd $JRUBY_HOME/moviestore
% jruby -S gem install activerecord-jdbcmysql-adapter
  • Create the databases for the application with the rake command
% jruby $JRUBY_HOME/bin/rake db:create:all
  • Use the script/generate command to create the scaffolding for the application
%  jruby script/generate scaffold Movie title:string description:text
  • Create the database table using the generated migration file
% jruby $JRUBY_HOME/bin/rake  db:migrate
(in /space/jruby-1.1RC1/moviestore)
== 1 CreateMovies: migrating ==================================================
-- create_table(:movies)
   -> 0.0530s
   -> 0 rows
== 1 CreateMovies: migrated (0.0550s) =========================================
  • Install  goldspike (rails-integration plugin)
%  jruby script/plugin install http://jruby-extras.rubyforge.org/svn/trunk/rails-integration/plugins/goldspike
  • In order for goldspike plugin to bundle the mysql connector in the WAR, you need to add the following line(shown in green)  to the current list of java libraries being included in vendor/plugins/goldspike/lib/war_config.rb
      @java_libraries = {}
      # default java libraries
      add_library(maven_library('org.jruby', 'jruby-complete', '1.0.3'))
      add_library(maven_library('org.jruby.extras', 'goldspike', '1.4'))
      add_library(maven_library('javax.activation', 'activation', '1.1'))
      add_library(maven_library('commons-pool', 'commons-pool', '1.3'))
      add_library(maven_library('bouncycastle', 'bcprov-jdk14', '124'))
      add_library(maven_library ('mysql', 'mysql-connector-java', '5.0.5'))
  • Now, we are all set to create the WAR with goldspike. I ran into 2 problems that needed to be corrected. Otherwise, the created WAR does not work correctly. First, goldspike defaults to production environment. Therefore, you need to create a production database that should point to your development database in your config/database.yml (shown in bold). Add the following lines (shown in green):
development:
  adapter: jdbc
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/moviestore_development
  username: root


# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
  adapter: jdbc
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/moviestore_test
  username: root

production:
  adapter: jdbc
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/moviestore_development
  username: root

          Run the migration script again for the production
% jruby $JRUBY_HOME/bin/rake  db:migrate RAIL_ENV=production
  • Second, Rails 2.0 has by default cross-site scripting forging prevention. As outlined, in this document you can either use other forms of store or disable this default behavior. For this example, I will use database for sessions. Edit config/environment.rb and uncomment the following line(shown in green)
  # Use the database for sessions instead of the cookie-based default,
  # which shouldn't be used to store highly confidential information
  # (create the session table with 'rake db:sessions:create')
   config.action_controller.session_store = :active_record_store


          change the following line in your app/controllers/application.rb
  
# See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery  :secret => 'f2de6acb89c9437235bfd4f4b9fef1a3
  • Now, create the WAR file moviestore.war
% jruby $JRUBY_HOME/bin/rake war:standalone:create
  • You are now ready deploy moviestore.war to WS7U2.

Deploying WAR file on Sun Java System Web Server 7 Update 2

You can now deploy the WAR generated in the previous section on WS7U2.
% mkdir /space/ws7install
% cd /space/ws7install
% tar xvf /tmp/sjsws-7_0u2-solaris-sparc.tar.gz
% ./setup
% setenv WS /space/webserver7
  • You can use GUI or CLI to deploy. I will use the command line deployment
%  $WS/admin-server/bin/startserv
% $WS/bin/wadm add-webapp
--vs test --config test --user admin --host foobar --port 8989 --password-file /tmp/admin.passwd --uri /moviestore $JRUBY_HOME/moviestore/moviestore.war

          When I installed WS7U2, I gave the password as adminadmin and that is what is in my admin.passwd file

wadm_password=adminadmin
  • Now, deploy the config and start the instance
%   $WS/bin/wadm deploy-config --user admin --password-file /tmp/admin.passwd test
  • You can now access the JRuby on Rails app using http://localhost:8080/moviestore/movies 
Jruby on Rails Application

References

http://edgibbs.com/2007/05/25/deploying-rails-to-tomcat-as-a-war-with-jruby

http://fairleads.blogspot.com/2007/12/rails-20-and-scaffolding-step-by-step.html



Comments:

Hi all,

I am working for a software integrator company. My projects includes working on Java and Ruby on Rails and Ajax. I think Web Services is really cool. We also recently have to now work on REST and they are talking about mashups and Struts. Can anyone tell me if there are some good training or conferences so that me and my team members can get to speed with these technologies. Learning from books is not my cup of tea, even not when I was doing engineering ;)

All the help that group members can provide in this regard is much appreciated.

Thanks,
Vaibhavi

Posted by Vaibhavi on January 23, 2009 at 03:09 PM PST #

Hi Vaibhavi,

There are several online resources available that you just google for. If any of your team like to read then quality books from wiley and oreilly cover such technologies in detail.

I also highly recommend you could attend the upcoming Great Indian Developer Summit (developersummit dot com) that is covering Java, Agile, REST, JAX-RS, mashups, .NET, Rich Web, JPA, SOA, rich user experiences, Spring, Groovy and more. They have most of the creators of these technologies as speakers. My team is attending this summit 22-25 apr at IISc campus where we are attending the web conference on April 23 and java on April 24. We have been able to get very good discounts. Maybe all those who are interested from your group can sign up together and get a good bargain from them. what say? I also attended last year's conference and had a really cool time.

In Hyderabad there is Sun Tech Days with some sun speakers.

Thanks,
Anaz

Posted by Anaz Amina on January 23, 2009 at 04:04 PM PST #

How about using warbler to package a rails app as a war. I found this to be a simple and way to create the war. And last time I checked goldspike was lagging behind on releases while warbler was current (supports jruby 1.1.6 I believe)

Posted by sud on January 28, 2009 at 10:59 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

sabada

Search

Categories
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