Sunday Feb 03, 2008

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)
  • 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
% jruby $JRUBY_HOME/bin/rails moviestore
% cd moviestore
  • Edit database.yml in moviestore/config directory , so that the file has the following contents:
  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.
  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'
end do |config|
  • Start mysql and then create mysql databases as defined in database.yml
% ./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
  • 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):
  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.
  adapter: jdbc
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/moviestore_test
  username: root

  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

  • 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


Wednesday Sep 27, 2006

Running jMaki on Sun Java System Web Server 7.0

jMaki on Sun Java System Web Server 7.0

What is jMaki ?

jMaki allows you to quickly and easily add AJAX-enabled widgets to your web applications thereby enabling Java developers to use JavaScript in their Java based applications as either a JSP tag library or a JSF component, or Phobos JavaScript page. In addition, it also facilitates wrapping any AJAX-enabled widget, including those from any of the popular toolkits, such as Dojo, Google, Yahoo, Scriptaculous and more.

Enabling jMaki support in Sun Java System Web Server 7.0

You can deploy jMaki on Web Server 7.0 with a few simple steps outlined below:
  1. Install Web Server 7.0
  2. Start the Web Server 's admin server and the server instance
  3. Download the latest jmaki.war
  4. Deploy web application jmaki.war using Web Server's admin GUI or CLI
  5. Open the browser to http://localhost:8080/jmaki/
You are all set to use jMaki with Web Server 7.0. Now, you can navigate to use widgets from many of the popular toolkits.

Creating a HelloWorld jMaki widget

It is also easy to create your own jMaki application using the steps outlined in and deploy it on the Web Server 7.0.

In this example, I will create a simple "HelloWorld" jMaki widget. The "HelloWorld" jMaki widget is a input text field with AJAX behavior. Whatever you type will be redisplayed with "Hello, <what-you-type>".  This example is adapted from Excercise 4 of Sang Shin article.
1. To start, create a top level directory named jMakiHelloWorld
2. Under the jMakiHelloWorld directory, create a directory hierarchy resources/helloworld for template files component.htm, component.js and component.css
3. Add the code below to component.htm
 <input id="${uuid}_name" type="text" size="20" value="Enter your name"

<div id="${uuid}_hello" class="helloDiv"></div>
4. Create component.css file with the content below

.plain {
 color: black;
 font-weight: bold;
 font-family: Arial;
 background: white;

.over {
 color: white;
 font-weight: bold;
 font-family: Arial;
 background: blue;
 cursor: pointer;

.helloDiv {
    position: relative;
    width: 400px;
    height: 300px;
    overflow: auto;
5. Create component.js with the following content
function HelloWorld() {
    // The jMaki framework must have created widget JavaScript object.
    var uuid = widget.uuid;
    var service = widget.service;
    function getXHR(url) {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            return new ActiveXObject("Microsoft.XMLHTTP");
    // This function gets called when a user typed some characters
    // in the input text field whose id is set as ${uuid}_hello in
    // component.htm template file.
    this.submitData = function() {
        var target = document.getElementById(uuid + "_hello");
        var req = getXHR(service);
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                if (req.status == 200) {
                    target.innerHTML = req.responseText;
        };"POST", service, true);
        var name= document.getElementById(uuid + "_name");
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        req.send("name=" + name.value + "&uuid=" + uuid);

var hello = new HelloWorld();
jmaki.attributes.put(widget.uuid, hello);
6. Now, create a helloService.jsp with the code below
String name= request.getParameter("name");
out.println("Hello, " + name + "!");
7. Create an index.jsp with
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
The taglib directive below imports the jMaki library.
<%@ taglib prefix="a" uri="" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>

        <h1>This is using my own HelloWorld jMaki widget</h1>
        <a:ajax name="helloworld"   service="helloService.jsp"  />


8. Create WEB-INF/lib directory under directory jMakiHelloWorld and place the ajax-wrapper.jar under it. ajax-wrapper.jar is available as part of  jmaki.war under WEB-INF/lib.
9. Copy jmaki.js at the top level under jMakiHelloWorld. jmaki.js is available in  jmaki.war under resources directory.
At the end, the directory structure under jMakiHelloWorld looks as follows:

Now, you are all set, jar up the jMakiHelloWorld into jMakiHelloWorld.war and deploy to the Web Server 7.0 with uri /helloworld

Access the newly created widget using http://localhost:8080/helloworld

Tuesday May 16, 2006

What is Cool in Web Server 7 ?

Web Server 7 Blog

Sun Java System Web Server 7 Technology Preview released today has many neat features. I will give you an overview on what is new. You can also look for blogs from my team members on the individual topics.

Web Server 7 has an all new, easy to use administration GUI and a comprehensive, secure and scriptable CLI support. Web Server also has full regular expression support of configuration files. With all this, you now have excellent management and monitoring support. The product also features robust cluster management and HTTP session failover. In addition, it supports 64-bit making it ideal for large scale deployment.

You need J2EE 1.4 webtier technologies? Yes, we have that too. Web Server 7 has out of box support for J2EE 1.4 web tier technologies including Servlet 2.4, JSP 2.0, JSF 1.1 and JSTL 1.1. It also features web services support by integrating JWSDP 2.0. Content publishers can not only publish and manage content using Web Server's WebDAV support. Now, they can also manage and manipulate access control using DAV Access Control support in Web Server 7. Security is always a concern and Web Server 7 has addressed this by supporting new features including Elliptic Curve Cryptography (ECC), DoS attack awareness, cross-site scripting (XSS) detection and web services security (JSR-196). Its easy to develop and deploy on Web Server 7 as the product is also fully integrated with NetBean  5.0, Java Studio Enterprise 8.1 and Creator Developer Tools. It also supports third party technologies including PHP, Perl and Python. If you are already on an earlier Web Server product, Web Server's migration support can migrate you over to the Web Server 7, so that you can start using the new features.

Of course, all this is nice and great and leads you to wonder what is the price. What if I tell you its all FREE! Yes, free as in zero dollars. Web Server 7 is free for development and deployment. A license is required only for support and service. With out of box support for J2EE 1.4 web tier technologies, 64 bit support and parity with many Apache module features including mod_dav and mod_rewrite. Why not give it a try? There is also quite a bit of free support on the web through the software forums and developers resource. The team would love to hear your feedback.

So, download it and try it.  Lets us know what you think is cool and what we should improve on.




« April 2014