JRuby on Glassfish - User Guide

The goal of the document is to capture all the common information relevant to JRuby and GlassFish.

Setup

Refer to the Getting Starting Guide for detailed information of setting up JRuby and GlassFish. In case you want to use the JRuby bits checked out from the trunk use the following commands :
<pre>
svn checkout svn://svn.codehaus.org/jruby/trunk/jruby
cd jruby
ant jruby jruby-complete test
</pre>

Set the environment variable JRUBY_HOME to point to the "jruby" directory that has been checked out earlier. Also ensure that you have added the $JRUBY_HOME/bin to the PATH environment variable.
Once the jruby code has been successfully installed then install the gems that are required to get going :
<pre>
To install Rails 2.x :
gem install rails --no-rdoc --no-ri --include-dependencies<br>
In case you would like to work with Rails 1.2.6 then ensure that you have provided the -v option :
gem install rails -v=1.2.6 --no-rdoc --no-ri --include-dependencies
</pre>

Database Connection Pooling

A sample on how to use GlassFish database connection pooling with a JRuby on Rails application. In Rails the database configuration would be specified in the config/database.yml file. Lets use an example to further explain the steps.
<pre>
production:
adapter: mysql
database: jndi_rails_production
username: mysqluser
password: mysqlpwd
host: localhost
</pre>
The Rails application in the "production" mode is connecting to a MySQL database which is present on the same machine. To be able to make use of GlassFish database connection pooling we need to do the following 3 steps :

  1. Define a connection pool pointing to the MySQL database
  2. Define a jndi name associated to the connection pool
  3. Change the application database.yml to use the connection pool

This can be achieved by issuing these commands :
<pre>
asadmin create-jdbc-connection-pool
--datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
--restype javax.sql.DataSource
--property User=mysqluser:Password=mysqlpwd:URL=jdbc\:mysql\://localhost/jndi_rails_production
jdbc/jndi_rails_pool<br>
asadmin create-jdbc-resource --connectionpoolid jdbc/jndi_rails_pool jdbc/jndi_rails<br>
</pre>
Edit the application database.yml to reflect :
<pre>
production:
adapter: jdbc
jndi: jdbc/jndi_rails
driver: com.mysql.jdbc.Driver
</pre>

Using JPA with Rails on GlassFish

Rails by default uses ActiveRecords for Object Relational Mapping purposes or ORM. JPA is another ORM technique in JavaEE. Model class and database migration in Rails is what Entity class and persistence provider does for you in Java. Essentially in Java you just provide the Entity class and on the Rails side, you would provide the Model corresponding to the Entity class to do CRUD operations and that is pretty much it. The JPA provider does the rest. Here is how you can use JPA(an Entity bean) with your Rails application on GlassFish using MySQL database.

Setup GlassFish v2

  1. Install NetBeans 6.1 Beata with Web and Java EE profile.
  2. Follow these directions to install and setup JRuby on GlassFish v2.
  3. Setup JRuby jars as shared libraries on Glassfish

<pre>
cd $GLASSFISH_HOME/jruby
$ant -f install.xml setup-shared-env
</pre>

  1. Copy MySQL driver from your NetBeans installation:

<pre>
cp "C:\Program Files\NetBeans 6.1 Beta\ide9\modules\ext\mysql-connector-java-5.1.5-bin.jar" c:/tools/glassfish/lib
</pre>
or from here to $GLASSFISH_HOME/lib.
5. Start MySQL server and create test database if not already there
<pre>
mysqladmin -u root create test;
</pre>

Create Java Entity Beans with MySQL

First we create an entity class and a persistence unit.

  1. Create a new Java Application named Post. There's no need for a Main Class.
  2. Create a new Persistence Unit. Select jdbc:mysql://localhost:3306/test root on Default Schema as the database connection and the persistence unit name as PostPU, then click Finish. This will generate src/META-INF/persistence.xml
  3. Create a new Entity Class named Posts. Set the package name to entity.
  4. Add fields and accessors for title and body. Your completed class should look as follows:

<pre>
package entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
*

  • @author vivek
    */
    @Entity
    public class Posts implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String title;
    private String body;

public void setId(Long id)

Unknown macro: { this.id = id; }

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId()

Unknown macro: { return id; }

@Override
public int hashCode()

Unknown macro: { int hash = 0; hash += (id != null ? id.hashCode() }

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Posts))

Unknown macro: { return false; }

Posts other = (Posts) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))

return true;
}

@Override
public String toString()

Unknown macro: { return "entity.Post[id=" + id + "|id="id"]"; }

public String getTitle()

Unknown macro: { return title; }

public void setTitle(String title)

Unknown macro: { this.title = title; }

public String getBody()

Unknown macro: { return body; }

public void setBody(String body)

Unknown macro: { this.body = body; }

}
</pre>

  1. Create Post.jar by clicking Build->Clean and Build Main Project or Shift+F11. This will create dist/Post.jar inside your project.
  2. Copy Post.jar to $GLASSFISH_HOME/lib directory. $GLASSFISH_HOME is where you installed GlassFish.

<pre>
cp dist/Post.jar c:/tools/glassfish/lib
</pre>

//TODO

Note <br>
To get GlassFish to connect to MySQL database you would have to download the MySQL JConnector and copy it to the GlassFish lib directory.