Arun Gupta, Miles to go ...

Arun Gupta is a technology enthusiast, a passionate runner, and a community guy who works for Sun Microsystems.
« Previous day (Sep 6, 2007) | Main | Next day (Sep 8, 2007) »

http://blogs.sun.com/arungupta/date/20070907 Friday September 07, 2007

TOTD #7: Switch between JRuby and CRuby interpreter in NetBeans 6

The NetBeans 6 IDE comes pre-configured with JRuby interpreter. This TOTD explains how the JRuby interpreter can be swapped with a C-based Ruby interpreter and vice versa.

  1. Verify the JRuby interpreter
    1. Create a Rails Hello World using NetBeans 6 IDE.
    2. Expand "Views", "say", and open "hello.rhtml".
    3. Right-click in the "hello.rhtml" window and select "Run File" (Shift+F6 is the default shortcut). This starts the WEBrick container and displays "http://localhost:3000/say/hello". It shows the following in the output window:

      => Booting WEBrick...
      => Rails application started on http://0.0.0.0:3000
      => Ctrl-C to shutdown server; call with --help for options
      [2007-09-07 13:40:29] INFO WEBrick 1.3.1
      [2007-09-07 13:40:29] INFO ruby 1.8.5 (2007-09-06) [java]
      [2007-09-07 13:40:29] INFO WEBrick::HTTPServer#start: pid=6336176 port=3000
      127.0.0.1 - - [07/Sep/2007:13:40:33 PDT]  "GET /say/hello HTTP/1.1" 200 83
      - -> /say/hello
  2. Switch to C-based Ruby interpreter
    1. Download and install C-based Ruby interpreter (say RUBY_HOME).
    2. Install Rails gem by giving the following command in RUBY_HOME\bin directory:

      ruby -S gem install rails -y --no-rdoc
    3. In the NetBeans IDE "Tools" menu, select "Options", "Ruby", and change the value of "Ruby Interpreter" from



      to

    4. Stop the already running WEBrick server by clicking on the "x" in the bottom right corner of the IDE.

    5. Right-click in the "hello.rhtml" window and select "Run File" (Shift+F6 is the default shortcut). This starts the WEBrick container using the C-based Ruby interpreter and displays "http://localhost:3000/say/hello". It shows the following in the output window:

      => Booting WEBrick...
      [2007-09-07 14:13:54] INFO WEBrick 1.3.1
      => Rails application started on http://0.0.0.0:3000
      => Ctrl-C to shutdown server; call with --help for options
      [2007-09-07 14:13:54] INFO ruby 1.8.6 (2007-03-13) [i386-mswin32]
      [2007-09-07 14:13:54] INFO WEBrick::HTTPServer#start: pid=3900 port=3000
      127.0.0.1 - - [07/Sep/2007:14:13:56 Pacific Daylight Time] "GET /say/hello HTTP/1.1" 200 83
      - -> /say/hello
      127.0.0.1 - - [07/Sep/2007:14:14:20 Pacific Daylight Time] "GET /say/hello HTTP/1.1" 200 83
      - -> /say/hello

Of course, you can switch back to the JRuby interpreter using the same mechanism.

Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.

Technorati: totd rubyonrails jruby ruby netbeans

del.icio.us | furl | simpy | slashdot | technorati | digg |
|

JRuby on Rails, NetBeans 6 and GlassFish V2 - Simplified Steps

The NetBeans IDE has simplified the steps to deploy JRuby on Rails application on GlassFish. This blog explains the steps and is an update to screencast #web6.

  1. Download the install the latest NetBeans 6 Nightly. I downloaded the Ruby pack by clicking on the "Download" button in the Ruby column.
  2. Create a new Rails Application
    1. Right-click in the Project window and select "New Project...". Take all the defaults as shown below:

    2. Click on "Next" and enter the values as shown below:



      Notice, we have selected "Add rake targets to support app server deployment (.war)". This downloads the goldfish (nee rails integration) plugin and installs in your application. Choose the database that you'd like to work with using the combo box:



      and then click on "Finish" button. I'm choosing the default database (MySQL) in this case.
  3. Create Database
    1. Download and install MySQL Community Server 5.0 (lets say MYSQL_HOME).
    2. Start MySQL database server by giving the command 'mysqld -nt --user root' in MYSQL_HOME/bin directory on Windows or './bin/mysqld_safe' from MYSQL_HOME directory on Unix flavors.
    3. Create a database by giving the following commands:

      mysqladmin -u root create RailsApplication9_development
  4. Create Generators
    1. Generate a model - Right-select the project, select "Generate..." and enter the values as shown below:



      and click on "OK".
    2. Generate a controller - Right-select the project, select "Generate..." and enter the values as shown:



      and click on "OK".
  5. Configure Model and Controller
    1. Configure Model
      1. In the NetBeans IDE, expand "Database Configurations", "migrate" and open "001_create_greetings.rb". Change the "self.up" helper method such that it looks like:

        def self.up
          create_table :greetings do |t|
            t.column :data, :string
          end
        end
      2. Right-select the project, select 'Run Rake Target', 'db', 'migrate'. This generates the appropriate database tables and the following is shown in the output window:

        (in C:/Users/Arun Gupta/Documents/NetBeansProjects/RailsApplication9)
        == CreateGreetings: migrating =================================================
        -- create_table(:greetings)
        -> 0.1010s
        == CreateGreetings: migrated (0.1110s) ========================================
      3. Add data to the database tables using the following commands in a shell window:

        C:\Program Files\MySQL\MySQL Server 5.0\bin>mysql --user root
        Welcome to the MySQL monitor. Commands end with ; or \g.
        Your MySQL connection id is 5
        Server version: 5.0.45-community-nt MySQL Community Edition (GPL)

        Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

        mysql> use RailsApplication9_development;
        Database changed
        mysql> insert into greetings values (1, "Hello from database!");
        Query OK, 1 row affected (0.10 sec)

        mysql> exit;
        Bye

         
      4. In the NetBeans IDE, expand "Configuration" and open "database.yml". Change the database entry in production environment to point to "RailsApplication9_development" instead of "RailsApplication9_production".
    2. Configure Controller
      1. Expand "Controllers", open "say_controller.rb" and change "hello" helper method such that it looks like:

        def hello
          @hello_string = Greeting.find(1).data;
        end
      2. Expand "Views", "say", open "hello.rhtml" and add the following fragment at the bottom of the page:

        <%= @hello_string %>
  6. Create a WAR file
    1. In the NetBeans IDE, right-select the project, select 'Run Rake Target', 'war', 'standalone', 'create' as shown here:



      and the following is shown in the output window:

      (in C:/Users/Arun Gupta/Documents/NetBeansProjects/RailsApplication9)
      Assembling web application
        Adding Java library commons-pool-1.3
        Adding Java library activation-1.1
        Adding Java library jruby-complete-1.0
        Adding Java library bcprov-jdk14-124
        Adding Java library rails-integration-1.1.1
        Adding web application
        Adding Ruby gem rails version 1.2.3
        Adding Ruby gem rake version 0.7.3
        Adding Ruby gem activesupport version 1.4.2
        Adding Ruby gem activerecord version 1.15.3
        Adding Ruby gem actionpack version 1.13.3
        Adding Ruby gem actionmailer version 1.3.3
        Adding Ruby gem actionwebservice version 1.2.3
        Adding Ruby gem ActiveRecord-JDBC version 0.5
      Creating web archive
    2. This creates "RailsApplication9.war" in the project directory.
  7. Download, Install and Start GlassFish.
    1. Download GlassFish recent build.
    2. Install by giving the command:

      java -jar filename.jar

      This creates "glassfish" directory in your current directory.
    3. In GlassFish install directory, set up the server by giving the following command:

      lib\ant\bin\ant -f setup.xml
    4. Start the server by giving the following command

      bin\asadmin start-domain
  8. Copy the WAR file (RailsApplication9.war) in "domains/domain/autodeploy" directory.

The application is accessible at "http://localhost:8080/RailsApplication9/say/hello".

Here are the improvements from the last time:

  1. During the project creation, there was an option to select the database to be used by the application. This creates the "database.yml" using default values for that database.
  2. During the project creation, there was an option to add add rake targets for the WAR creation. This made the steps to explicitly install the repository and install the plugin redundant.
  3. ActiveRecord-JDBC 0.5 provides a simplified database configuration and is used by the "war:standalone:create" rake target. This allows the database to be accessed using the Ruby adapter instead of the JDBC adapter and also uses the original format of "database.yml".
     

Technorati: jruby ruby rubyonrails glassfish netbeans jrubyonglassfish mysql

del.icio.us | furl | simpy | slashdot | technorati | digg |
|
« Previous day (Sep 6, 2007) | Main | Next day (Sep 8, 2007) »

Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.