 |
TOTD #52: Getting Started with Merb using GlassFish Gem
Posted by arungupta on November 14, 2008 at 09:36 AM | Comments (3)
GlassFish
Gem 0.9.0 was recently
released.
It can run any Rack-compatible
framework such as Rails
and Merb.
Support for another Rack-based framework Sinatra will
be released in the near future. The gem is even extensible and
allows to plug any of your favorite Ruby framework using -apptype
switch (more on this in a future blog). This blog shows how to install
the gem and use it for running a Merb application.
Lets install the gem first:
~/tools/jruby-1.1.5 >gem install glassfish rack
JRuby limited openssl loaded. gem install jruby-openssl for full
support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Successfully installed glassfish-0.9.0-universal-java
Successfully installed rack-0.4.0
2 gems installed
Installing ri documentation for glassfish-0.9.0-universal-java...
Installing ri documentation for rack-0.4.0...
Installing RDoc documentation for glassfish-0.9.0-universal-java...
Installing RDoc documentation for rack-0.4.0...
|
The
Rack dependency will be fixed as part of the next gem update and for
now we have to install it explicitly. The
different options supported by the gem can be seen using "-h" switch as:
~/tools/jruby-1.1.5 >glassfish -h
Synopsis
--------
glassfish: GlassFish v3 server for rails, merb, sintra applications
Usage:
------
glassfish [OPTION] APPLICATION_PATH
-h,
--help:
show help
-c, --contextroot PATH: change the context root (default: '/')
-p, --port
PORT:
change server port (default: 3000)
-e, --environment ENV: change rails environment (default:
development)
-n --runtimes NUMBER: Number of JRuby runtimes to
crete initially
--runtimes-min NUMBER: Minimum JRuby runtimes to crete
--runtimes-max NUMBER: Maximum number of JRuby runtimes to
crete
APPLICATION_PATH (optional): Path to the application to be run (default:
current).
|
And complete rdocs are available here.
Lets create and run a Merb
application!
- Install Merb:
Merb
1.0 has some native
dependencies in DataMapper
and so cannot be installed as is with JRuby. However since Merb is
ORM-agnostic, ActiveRecord can be
installed as ORM layer. "gem install merb" uses DataMapper as the
default ORM so a Merb installation in JRuby (for now) is:
~/tools/jruby-1.1.5
>gem install
merb-core merb-more merb_activerecord
JRuby limited openssl loaded. gem install jruby-openssl for full
support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Successfully installed extlib-0.9.8
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.2
Successfully installed json_pure-1.1.3
Successfully installed rack-0.4.0
Successfully installed mime-types-1.15
Successfully installed thor-0.9.8
Successfully installed merb-core-1.0
Successfully installed ZenTest-3.11.0
Successfully installed RubyInline-3.8.1
Successfully installed sexp_processor-3.0.0
Successfully installed ParseTree-3.0.2
Successfully installed ruby2ruby-1.2.1
Successfully installed merb-action-args-1.0
Successfully installed merb-assets-1.0
Successfully installed merb-slices-1.0
Successfully installed merb-auth-core-1.0
Successfully installed merb-auth-more-1.0
Successfully installed merb-auth-slice-password-1.0
Successfully installed merb-auth-1.0
Successfully installed merb-cache-1.0
Successfully installed merb-exceptions-1.0
Successfully installed highline-1.5.0
Successfully installed diff-lcs-1.1.2
Successfully installed templater-0.4.0
Successfully installed merb-gen-1.0
Successfully installed haml-2.0.4
Successfully installed merb-haml-1.0
Successfully installed merb-helpers-1.0
Successfully installed mailfactory-1.4.0
Successfully installed merb-mailer-1.0
Successfully installed merb-param-protection-1.0
Successfully installed addressable-1.0.4
Successfully installed data_objects-0.9.6
Successfully installed dm-core-0.9.6
Successfully installed dm-migrations-0.9.6
Successfully installed merb_datamapper-1.0
Successfully installed merb-more-1.0
Successfully installed merb_activerecord-0.9.13
39 gems installed
Installing ri documentation for json_pure-1.1.3...
Installing ri documentation for rack-0.4.0...
Installing ri documentation for mime-types-1.15...
. . .
Installing RDoc documentation for dm-migrations-0.9.6...
Installing RDoc documentation for merb_datamapper-1.0...
Installing RDoc documentation for merb_activerecord-0.9.13... |
It would be nice if this can be further simplified to "gem install merb
--orm activerecord" or something similar.
- Create a Merb
application:
A
"jump start" Merb application created using "merb-gen app" uses ERB
templating and DataMapper for ORM. But as mentioned earlier DataMapper
does not work with JRuby (at least for now) so a Merb application needs
to be created using "merb-gen core". This command creates a Merb
application with Ruby-on-Rails like structure and allows to plugin
templating engine and ORM frameworks.
Lets create our first Merb application with no ORM and default
templating engine as:
~/samples/jruby/merb >merb-gen core hello
Generating with core generator:
[ADDED] gems
[ADDED] merb.thor
[ADDED] .gitignore
[ADDED]
public/.htaccess
[ADDED]
doc/rdoc/generators/merb_generator.rb
[ADDED]
doc/rdoc/generators/template/merb/api_grease.js
[ADDED]
doc/rdoc/generators/template/merb/index.html.erb
[ADDED]
doc/rdoc/generators/template/merb/merb.css
[ADDED]
doc/rdoc/generators/template/merb/merb.rb
[ADDED]
doc/rdoc/generators/template/merb/merb_doc_styles.css
[ADDED]
doc/rdoc/generators/template/merb/prototype.js
[ADDED]
public/favicon.ico
[ADDED]
public/merb.fcgi
[ADDED]
public/robots.txt
[ADDED]
public/images/merb.jpg
[ADDED] Rakefile
[ADDED]
app/controllers/application.rb
[ADDED]
app/controllers/exceptions.rb
[ADDED]
app/helpers/global_helpers.rb
[ADDED]
app/views/exceptions/not_acceptable.html.erb
[ADDED]
app/views/exceptions/not_found.html.erb
[ADDED]
autotest/discover.rb
[ADDED]
autotest/merb.rb
[ADDED]
autotest/merb_rspec.rb
[ADDED]
config/init.rb
[ADDED]
config/rack.rb
[ADDED]
config/router.rb
[ADDED]
config/environments/development.rb
[ADDED]
config/environments/production.rb
[ADDED]
config/environments/rake.rb
[ADDED]
config/environments/staging.rb
[ADDED]
config/environments/test.rb
[ADDED]
public/javascripts/application.js
[ADDED]
public/stylesheets/master.css
[ADDED] spec
[ADDED]
app/views/layout/application.html.erb |
ActiveRecord
can be used as pluggable ORM by using the command "merb-gen core --orm
activerecord hello". A future blog will cover creating a Merb scaffold
using ActiveRecord.
- Create a new
controller as:
~/samples/jruby/merb/hello >merb-gen controller Runners
Loading init file from
/Users/arungupta/samples/jruby/merb/jruby-1.1.5/samples/merb/hello/config/init.rb
Loading
/Users/arungupta/samples/jruby/merb/jruby-1.1.5/samples/merb/hello/config/environments/development.rb
Generating with controller generator:
Loading init file from
/Users/arungupta/samples/jruby/merb/jruby-1.1.5/samples/merb/hello/config/init.rb
Loading
/Users/arungupta/samples/jruby/merb/jruby-1.1.5/samples/merb/hello/config/environments/development.rb
[ADDED]
app/controllers/runners.rb
[ADDED]
app/views/runners/index.html.erb
[ADDED]
spec/requests/runners_spec.rb
[ADDED]
app/helpers/runners_helper.rb |
Notice the convention of controller name is a plural word and starting
with a capital letter.
- Edit application
- Edit "app/controllers/runners.rb" and change the "index"
action such
that it looks like:
def index
@message = "Miles to go ..."
render
end
|
- Edit "app/views/runners/index.html.erb" and add
"<br><%= @message %>" as the last line.
- Run
application:
Running a Merb application using the Gem is pretty straight
forward. If JRUBY_HOME/bin
is in PATH, just type the command "glassfish" in the application
directory and now you are running a Merb application using GlassFish.
The application is accessible at "http://localhost:3000" and the output
looks like:

The controller is accessible at "http://localhost:3000/runners" and the
output is:

Hit Ctrl+C to stop the application.
A Merb app generated using MRI can also be run using
GlassFish, provided it does not have any native dependencies. On my
MacBook, I had to update gems (gem
update --system) and install
XCode.
This same gem can be used to run Rails application, and guess what?
That is pretty straight forward too. Just type "glassfish" in the
application
directory and now you are running a Rails application on GlassFish.
These applications can very well be created using MRI but they must be
using pure-Ruby gems/plugins. Alternatively Foreign
Function Interface
can be used to port your native gems to Ruby.
Lets make it more real by running Substruct
- an open source E-Commerce project. Install it as explained here,
type "glassfish" in the application directory and your application is
now accessible at "http://localhost:3000" as shown below:

Really simple, easy and powerful!
Rails
powered by GlassFish explains the benefits of running Rails
application on GlassFish. And now GlassFish
v3 Prelude allows you to even buy production support
for your Rails applications! Screencast
#26 how to develop/run/debug your Rails application using
NetBeans and GlassFish.
Open Source Rails
has a gallery of open source Rails projects, have you tried any of them
?
Is there any Merb equivalent of www.opensourcerails.com
?
Technorati: glassfish
v3 gem rubyonrails
merb rack
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Hi Arun! Thanks for the tutorial.
I tried it with merb-1.1.0 and jruby-1.1.5 and it worked. The only problem I can not specify environment variable. It looks like -e option have no effect.
Posted by: kolach on November 25, 2008 at 02:23 PM
-
Thanks kolach, this is a known issue as described at: https://glassfish.dev.java.net/issues/show_bug.cgi?id=6767. It has been fixed and will be available during next gem refresh (coming soon).
Posted by: arungupta on November 25, 2008 at 02:40 PM
-
Good. I'm looking forward the update.
Posted by: kolach on November 26, 2008 at 05:17 AM
|