And then run jruby --command bundle install to get the gems ready.
Note: The tux gem works as a console to interact with my Sinatra application. I would also love to have the shotgun gem, but unfortuantely it’s unavaible for JRuby.
The JDBC driver (sqljdbc4.jar) for SQL Server is included in the source code, but if you prefer, you can also get it from Microsoft’s web site. We’ll just save it to our application root.
SQL Server table
Our application will use one table called “users”.
require'rubygems'require"sinatra"require"sinatra/activerecord"require'sinatra/form_helpers'require'active_record'require'active_record/connection_adapters/jdbc_adapter'require'sqljdbc4.jar'config_dev={:adapter=>"jdbc",:driver=>"com.microsoft.sqlserver.jdbc.SQLServerDriver",:url=>"jdbc:sqlserver://SERVER:1433;databaseName=DB_NAME",:username=>"USER",:password=>"PASS"}# JBoss connectoin pool. Need to set up datasource in JBossconfig_jboss={:adapter=>"jdbc",:driver=>"com.microsoft.sqlserver.jdbc.SQLServerDriver",:jndi=>"DATASOURCE_NAME"}# On JBoss $servlet_context is definedconfig=defined?($servlet_context)?config_jboss:config_devActiveRecord::Base.establish_connection(config)classUser<ActiveRecord::Basevalidates:consent,acceptance:true,presence:truevalidates:firstname,presence:truevalidates:lastname,presence:truevalidates:email,presence:true,format:{with:/\A[^@]+@([^@\.]+\.)+[^@\.]+\z/}endhelpersdodefh(text)Rack::Utils.escape_html(text)endendget"/users"do@users=User.take(10)erb:"users/index"endget"/users/:id"do@user=User.find(params[:id])erb:"users/show"endput"/users/:id"do@user=User.find(params[:id])if@user.update_attributes(params[:user])redirect'/users'elseerb:"users/show"endend
Our layouts and templates sit in the views folder with the following content.
Let’s get started by installing Warbler as a gem jruby --command gem install warbler. Next, we need to configure Warbler:
Create a new folder called config under our application root
Run jruby --command warble config, which generates a configuration template called warble.rb under the config folder
Edit config/warble.rb and update these lines:
config/warble.rb
12345678
# Application directories to be included in the webapp.config.dirs=%w(views)# Additional files/directories to include, above those in config.dirsconfig.includes=FileList['app.rb','sqljdbc4.jar']# Uncomment this if you don't want to package rails gem.config.gems-=["rails"]
Now we’re ready to package our app: jruby --command warble
A war file will be created as myapp.war.
Deploy the WAR file
I tried the JBoss 6 Admin console first, but got an Out of Memory error. I worked around it by copying the WAR file to the deploy folder (in my case
/usr/jboss6/server/default/deploy/) and restarting JBoss. And voila, our application is live on JBoss at http://jboss_address/myapp .