TOTD #52: Getting Started with Merb using GlassFish Gem

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!
  1. 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.
  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:

    ~/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.
  3. 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.
  4. Edit application
    1. Edit "app/controllers/runners.rb" and change the "index" action such that it looks like:

        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:



    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: totd glassfish v3 gem rubyonrails merb rack
Comments:

弓弩    http://www.168gongnu.cn
弩    http://www.168gongnu.cn
新奇特批发 http://www.jsxqt88.com
新奇特产品 http://www.xqt007.cn
成人玩具 http://www.jsxqt88.com/tupianl.htm
剪板机  http://www.jxwjs.cn
http://www.jsxqt88.com/indexi.htm
弓弩 http://www.jsxqt88.com/indexi.htm
乳化机 http://www.ntdyjx.cn
过滤器 http://www.dryent.com
卷板机 http://www.nthljc.cn
弓弩  http://www.gongnuw.cn
弩  http://www.gongnuw.cn
弓弩   http://www.gongnuw.com
弩   http://www.gongnuw.com
通风机 http://www.qdwdl.com.cn
三面翻  http://www.gr-door.com
混合机 http://www.85399888.cn
搅拌机 http://www.85399888.cn/jiaobanji-01.html
呼吸阀 http://www.ntdyjx.cn/product_29.html
阻火器 http://www.ntdyjx.cn/product_26.html
弓弩    http://www.168gongnu.cn
弩    http://www.168gongnu.cn
新奇特批发 http://www.jsxqt88.com
新奇特产品 http://www.xqt007.cn
成人玩具 http://www.jsxqt88.com/tupianl.htm
剪板机  http://www.jxwjs.cn
http://www.jsxqt88.com/indexi.htm
弓弩 http://www.jsxqt88.com/indexi.htm
乳化机 http://www.ntdyjx.cn
过滤器 http://www.dryent.com
卷板机 http://www.nthljc.cn
弓弩  http://www.gongnuw.cn
弩  http://www.gongnuw.cn
弓弩   http://www.gongnuw.com
弩   http://www.gongnuw.com
通风机 http://www.qdwdl.com.cn
三面翻  http://www.gr-door.com
混合机 http://www.85399888.cn
搅拌机 http://www.85399888.cn/jiaobanji-01.html
呼吸阀 http://www.ntdyjx.cn/product_29.html
阻火器 http://www.ntdyjx.cn/product_26.html

Posted by 弓弩 on November 15, 2008 at 08:09 PM PST #

[Trackback] GlassFish Gem 0.9.0 can be used to run Rails and Merb applications. Support another Rack-based framework Sinatra is coming up in the near future. This blog shows how to create a scaffold in Merb and run it using the...

Posted by Arun Gupta's Blog on November 17, 2008 at 10:04 PM PST #

[Trackback] GlassFish v3 Prelude is now available! Some of the cool features are: Modularity using OSGi Rapid deployment using retain session data across HTTP redeploys and deploy-on-save Embeddability Dynamic languages and frameworks Faster start up time Integra...

Posted by Arun Gupta's Blog on November 18, 2008 at 09:41 PM PST #

[Trackback] GlassFish Gem is a light-weight and robust deployment solution for Ruby-on-Rails and Merb applications. The gem can be easily installed as: gem install glassfish for any JRuby runtime. Support for other Rack-based frameworks such as Sinatra is coming ...

Posted by Arun Gupta's Blog on November 20, 2008 at 10:01 PM PST #

[Trackback] GlassFish v3 with Merb and Warbler explains how to use Warbler to deploy a Merb application on GlassFish v3. Here is a quote&nbsp;from the blog: this lovely server had a gem which you can use to fire up a...

Posted by Arun Gupta's Blog on December 03, 2008 at 01:15 AM PST #

[Trackback] GlassFish Gem can easily run both Rails and Merb application. Using JRuby, as opposed to MRI, allows you to use standard Java monitoring framework and tools. Java Management Extensions (JMX) is a standard management and monitoring solution for Java...

Posted by Arun Gupta's Blog on January 07, 2009 at 01:17 AM PST #

Thanks for information

Posted by sinema izle on March 13, 2009 at 06:33 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today