Skip to main content

TOTD #52: Getting Started with Merb using GlassFish Gem

Posted by arungupta on November 14, 2008 at 9:36 AM PST

GlassFish
Gem 0.9.0 was href="http://weblogs.java.net/blog/vivekp/archive/2008/11/new_and_improve_1.html">recently
released.
It can run any Rack-compatible
framework such as Rails
and Merb.
Support for another Rack-based framework  href="http://sinatra.rubyforge.org/">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.


style="width: 441px; height: 298px;" alt=""
src="http://blogs.sun.com/arungupta/resource/images/ruby-frameworks-glassfish.png">



Lets install the gem first:


style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">
~/tools/jruby-1.1.5 > style="font-weight: bold;">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:


style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">
~/tools/jruby-1.1.5 > style="font-weight: bold;">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 href="http://glassfishgem.rubyforge.org/">here.



Lets create and run a Merb
application!

  1. Install Merb:
    Merb
    1.0 has some href="http://groups.google.com/group/merb/browse_thread/thread/8a3f0433a1fa1fed">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:


    cellpadding="2" cellspacing="2">
    ~/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.

  2. 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:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/samples/jruby/merb > style="font-weight: bold;">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.

  3. Create a new
    controller
    as:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/samples/jruby/merb/hello > style="font-weight: bold;">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.

  4. Edit application
    1. Edit "app/controllers/runners.rb" and change the "index"
      action such
      that it looks like:


      cellpadding="2" cellspacing="2">
       
      def index

          @message = "Miles to go ..."

          render

        end
    2. Edit "app/views/runners/index.html.erb" and add
      "<br><%= @message %>" as the last line.
  5. 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:



    href="http://blogs.sun.com/arungupta/resource/images/gem090-merb-fresh-app.png"> style="border: 0px solid ; width: 700px; height: 620px;" alt=""
    src="http://blogs.sun.com/arungupta/resource/images/gem090-merb-fresh-app.png">



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



    href="http://blogs.sun.com/arungupta/resource/images/gem090-merb-runners-controller.png"> style="border: 0px solid ; width: 700px; height: 173px;" alt=""
    src="http://blogs.sun.com/arungupta/resource/images/gem090-merb-runners-controller.png">

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 href="http://blog.headius.com/2008/10/ffi-for-ruby-now-available.html">Foreign
Function Interface
can be used to port your native gems to Ruby.



Lets make it more real by running href="http://code.google.com/p/substruct/">Substruct
- an open source E-Commerce project. Install it as explained href="http://blogs.sun.com/arungupta/entry/substruct_on_glassfish_v3_ruby">here,
type "glassfish" in the application directory and your application is
now accessible at "http://localhost:3000" as shown below:



href="http://blogs.sun.com/arungupta/resource/ror/gem090-substruct-main-screen.png"> style="border: 0px solid ; width: 700px; height: 427px;" alt=""
src="http://blogs.sun.com/arungupta/resource/ror/gem090-substruct-main-screen.png">



Really simple, easy and powerful!

href="http://developers.sun.com/appserver/reference/techart/rails_gf/">

Rails
powered by GlassFish explains the benefits of running Rails
application on GlassFish. And now href="http://blogs.sun.com/arungupta/entry/glassfish_v3_prelude_now_available">GlassFish
v3 Prelude allows you to even buy production support
for your Rails applications! href="http://blogs.sun.com/arungupta/entry/screencast_26_develop_run_debug">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  href="http://www.opensourcerails.com/">www.opensourcerails.com
?



Technorati: href="http://technorati.com/tag/glassfish">glassfish
v3 href="http://technorati.com/tag/gem">gem href="http://technorati.com/tag/rubyonrails">rubyonrails
merb href="http://technorati.com/tag/rack">rack

Related Topics >>

Comments

Good. I'm looking forward the update.

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).

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.