Miles to go …

October 6, 2009

TOTD #110: JRuby on Rails application using Oracle on GlassFish

Filed under: General — arungupta @ 11:00 pm

GlassFish v3 is the Reference Implementation for Java EE 6. Following the "extensibility" principle of Java EE 6, it also allows Ruby-on-Rails, Groovy and Grails and Python/Django applications to be seamlessly deployed as well, without any additional packaging. This blog has published multiple entries on deploying a Rails application on GlassFish as given below:

  • TOTD #105: Monitor Rails application using JavaScript
  • TOTD #104: Redmine, Typo, Substruct on GlassFish v3
  • TOTD #84: Apache + mod_proxy_balancer to load balance Rails applications on GlassFish
  • TOTD #81: nginx to load balance Rails applications on GlassFish Gem
  • TOTD #73: Deploying Rails application as WAR on GlassFish v2.1
  • TOTD #72: Deploying Rails application on GlassFish v3
  • TOTD #70: Deploying Rails application on GlassFish Gem

All the existing applications have used JavaDB, SQLite3, or MySQL as the database so far. In the process of getting ready for the upcoming Oracle Open World 2009, this Tip Of The Day will show how to use an Oracle database with a JRuby-on-Rails application deployed on GlassFish v3.

Lets get started!

  1. Install Oracle database as explained in TOTD #106.
  2. Configure JRuby/Rails in GlassFish v3 using one of the mechanisms explained in TOTD #104. Alternatively you can also install the GlassFish gem as:
    >./bin/jruby -S gem install glassfish
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    
    Successfully installed rack-1.0.0
    Successfully installed glassfish-0.9.5-universal-java
    2 gems installed
    Installing ri documentation for rack-1.0.0...
    Installing ri documentation for glassfish-0.9.5-universal-java...
    Installing RDoc documentation for rack-1.0.0...
    Installing RDoc documentation for glassfish-0.9.5-universal-java...
    

    This blog will use GlassFish Gem for running the application described below.

  3. Create a new database user and grant rights using SQL*Plus as shown:
    Macintosh-187:~ oracle$ sqlplus "/ as sysdba"
    SQL*Plus: Release 10.2.0.4.0 - Production on Thu Oct 1 12:32:33 2009
    Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.
    Connected to:
    Oracle Database 10g Release 10.2.0.4.0 - Production
    SQL> CREATE USER glassfish IDENTIFIED BY glassfish DEFAULT tablespace users TEMPORARY tablespace temp;
    User created.
    SQL> GRANT CONNECT TO glassfish IDENTIFIED BY glassfish;
    Grant succeeded.
    SQL> GRANT UNLIMITED TABLESPACE TO glassfish;
    Grant succeeded.
    SQL> GRANT CREATE TABLE TO glassfish;
    Grant succeeded.
    SQL> GRANT CREATE SEQUENCE TO glassfish;
    Grant succeeded.
    SQL> exit
    Disconnected from Oracle Database 10g Release 10.2.0.4.0 - Production
    

    The user name and password are chosen as "glassfish" for simplicity. This is not a recommended setting for production usage though.

  4. Copy Oracle JDBC drivers (odjc6.jar) in JRUBY_HOME/lib directory.
  5. Create a simple Rails application
    1. Make sure the following gems are pre-installed:
      rails (2.3.4)
      activerecord-jdbc-adapter (0.9.2)
      glassfish (0.9.5)
      

      If not, then install them as:

      jruby -S gem install rails activercord-jdbc-adapter glassfish
      
    2. Create a simple Rails application as:
      jruby -S rails bookstore -d oracle
      

    3. Using the normal "jdbc" adapter will give the following error later:
      ActionView::TemplateError (book_url failed to generate from {:controller=>"books", :action=>"show", :id=>#<Book id: #<BigDecimal:3feef1eb,'10000.0',1(8)>, title: "Ultramarathon Man", author: "Dean Karnazes", created_at: "2009-10-06 00:03:14", updated_at: "2009-10-06 00:03:14">}, expected: {:controller=>"books", :action=>"show"}, diff: {:id=>#<Book id: #<BigDecimal:459bdb65,'10000.0',1(8)>, title: "Ultramarathon Man", author: "Dean Karnazes", created_at: "2009-10-06 00:03:14", updated_at: "2009-10-06 00:03:14">}) on line #13 of app/views/books/index.html.erb:
      

      As evident, the "id" column is returned as BigDecimal where as it should be integer. Fortunately the fix is simple, install the "oracle_enhanced_adapter" (docs) as:

      bookstore >~/tools/jruby/bin/jruby -S gem install activerecord-oracle_enhanced-adapter
      JRuby limited openssl loaded. gem install jruby-openssl for full support.
      
      http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
      
      Successfully installed activerecord-oracle_enhanced-adapter-1.2.2
      1 gem installed
      Installing ri documentation for activerecord-oracle_enhanced-adapter-1.2.2...
      Installing RDoc documentation for activerecord-oracle_enhanced-adapter-1.2.2...
      

      Using this "enhanced adapter" is highly recommended for connecting with Oracle databases from Rails applications.

    4. Edit "config/database.yml" and change the "development" section to:
      development:
      adapter: oracle_enhanced
      host: localhost
      database: orcl
      username: glassfish
      password: glassfish
      

      Notice, the username and password values are the same as chosen in the SQL statements above.

    5. Generate a scaffold as:
      bookstore >~/tools/jruby/bin/jruby script/generate scaffold book title:string author:string
      JRuby limited openssl loaded. gem install jruby-openssl for full support.
      
      http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
      
      exists app/models/
      exists app/controllers/
      exists app/helpers/
      create app/views/books
      exists app/views/layouts/
      exists test/functional/
      exists test/unit/
      create test/unit/helpers/
      exists public/stylesheets/
      create app/views/books/index.html.erb
      create app/views/books/show.html.erb
      create app/views/books/new.html.erb
      create app/views/books/edit.html.erb
      create app/views/layouts/books.html.erb
      create public/stylesheets/scaffold.css
      create app/controllers/books_controller.rb
      create test/functional/books_controller_test.rb
      create app/helpers/books_helper.rb
      create test/unit/helpers/books_helper_test.rb
      route map.resources :books
      dependency model
      exists app/models/
      exists test/unit/
      exists test/fixtures/
      create app/models/book.rb
      create test/unit/book_test.rb
      create test/fixtures/books.yml
      create db/migrate
      create db/migrate/20091005233152_create_books.rb
      
      
    6. Prepare your application for JDBC as:
      bookstore >~/tools/jruby/bin/jruby script/generate jdbc
      JRuby limited openssl loaded. gem install jruby-openssl for full support.
      
      http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
      
      exists config/initializers
      create config/initializers/jdbc.rb
      exists lib/tasks
      create lib/tasks/jdbc.rake
      

    7. Migrate the database as:
      ~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby -S rake db:migrate
      (in /Users/arungupta/samples/v3/rails/oracle/bookstore)
      == CreateBooks: migrating ====================================================
      -- create_table(:books)
      -> 0.0740s
      -> 0 rows
      == CreateBooks: migrated (0.0750s) ===========================================
      

  6. Lets run the application as:
    ~/samples/v3/rails/oracle/bookstore >~/tools/jruby/bin/jruby -S glassfish -l
    Starting GlassFish server at: 129.145.133.197:3000 in development environment...
    Writing log messages to: /Users/arungupta/samples/v3/rails/oracle/bookstore/log/development.log.
    Press Ctrl+C to stop.
    Oct 6, 2009 9:45:51 AM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
    INFO: Listening on port 3000
    . . .
    

    he application is now accessible at "http://localhost:3000/books" and looks like:

    Click on "New Book" and enter the values as shown:

    Click on "Create" to see the output as:

    Click on "Back" to see the main page as:

    After adding another book, this page looks like:

    And another book …

So we created a brand new JRuby/Rails application and ran it using GlassFish and Oracle backend. A subsequent blog entry will show how to create a similar application using an existing database.

A complete archive of all the TOTDs is available here. The complete list of Rails blog entries are available here.

This and other similar applications will be demonstrated at the upcoming Oracle Open World.

Technorati: totd oracle database glassfish v3 jruby rails oow

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

Pictures from Rock’n'Roll San Jose Half Marathon 2009

Filed under: Running — arungupta @ 12:26 pm

Here are some pictures (courtesy ASI Photo) from my recently concluded Rock-n-Roll San Jose Half Marathon:

Technorati: running marathon rnrsj rocknroll sanjose results

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 4, 2009

Rock-n-Roll Half Marathon 2009 Completed – 6:57 pace, personal best!

Filed under: Running — arungupta @ 11:21 pm

I ran Rock ‘n’ Roll San Jose 1/2 marathon this morning – oh what a joy!

The race was very well organized, ample parking space in the San Jose Downtown, very well marked course, 14 lively local rock bands playing through out the course, gun time at each mile marker, short queues at porta potties, nice big sized medal, and last but not the least cheerful crowd at each turn was very pumping.

And all that contributed to run my personal best so far, the results and time splits are:

These are my best timings for 5K, 10K, 10 mile and 1/2 marathon so far. Its exciting to shave off 34 secs from the pace from little over 2 months ago in San Francisco 1/2 Marathon. Read previous marathon results.

The winner was Meb Keflezighi who completed in 1:01:00 at 4:40 pace, congratulations! I’m happy with my results :)

I followed a 1:30 pacer for the first 10 miles but then constant coughing slowed me down little bit. But, I still enjoyed running the wide streets of downtown!

Check out couple of pictures after the finish line:

Some friends and family members ran along with me which made the marathon a special event as well. As you can see, kids also ran in their own spirit. Completing 400 miles of training from Jul-Sep 2009 was not possible without family’s support, many thanks to them!

Enjoy a small video clip of the start line and at 4.5 mile:

Some improvement suggestions …

  • The pacers showed up barely 10 minutes before the start of marathon. And their timing flags did not pop out until right before the race. I’ve seen pacers at other marathons ready typically 15-20 minutes before the start with their flags wide above the runners.
  • The water station volunteers were very cheerful and enthusiastic but were way too many. They were literally jamming the running route at some places. If a runner wants water, they’ll come get it so please keep them back and the running route empty.
  • Generally I don’t hydrate during 1/2 marathons at all. But felt the need to drink some Cytomax around 8 miler and so grabbed a glass. It had more ice (biiig cubes) than Cytomax which certainly made drinking a bit of challenge, especially if you are trying to keep the pace as well. The glass should contain liquid and only that to make it easy for the runners.

Over all, I thoroughly loved and will most likely run it again next year, hopefully with a better timing :)

UPDATED: More detailed results (compared to other runners) are now available:

Fully searchable results and leader board is also available:

Read more detailed story listing the records made etc. here.

Technorati: running marathon rnrsj rocknroll sanjose results

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

Silicon Valley Code Code Camp 2009 Trip Report

Filed under: General — arungupta @ 5:18 pm

The Foothill college was sprawling with developers, architects, engineers, et al from all over the San Francisco Bay Area to attend the Silicon Valley Code Camp 2009. This was my third speaking engagement (2008, 2007) and the code camp has certainly matured over the last years. The attendance is steadily growing and the quality of sessions is become more mature as well.

I presented on Java EE 6, GlassFish, and Eclipse Tooling for GlassFish/Java EE 6 and the slides are available below:

and

Both the talks were demo intensive and showed Deploy-on-Save, Preserving session state across deployments, Java EE 6 wizards in NetBeans (1, 2, 3, 4, 5) and Eclipse (1, 2, 3), Metro JAX-WS Web services development in Eclipse, GlassFish v3 Monitoring, GlassFish v3 REST interface to management and monitoring and many others. All the demos are available as screencasts and/or blog entries and the complete set of links are listed in the presentations.

Here are some pictures from the event:

And the complete album at:

That’s it folks, see ya next year!

Technorati: siliconvalleycodecamp svcc glassfish javaee eclipse

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 2, 2009

TOTD #109: How to convert a JSF managed bean to JSR 299 bean (Web Beans) ?

Filed under: General — arungupta @ 1:40 pm

This entry is a follow up to TOTD #95 and shows how to use the recent integrations of JSR 299 in GlassFish v3 to convert a JSF managed bean to a JSR 299 bean (aka Web Beans). The TOTD #95 describes a simple Java EE 6 web application that uses Java Server Faces 2.0 components for displaying the results of a database query conducted by EJB 3.1 and JPA 2.0 classes.

The EJB class, which also acts as the JSF managed bean, looks like:

@javax.ejb.Stateless
@ManagedBean
public class StateList {
@PersistenceUnit
EntityManagerFactory emf;
public List getStates() {
return    emf.createEntityManager().createNamedQuery(”States.findAll”).getResultList();
}
}

Three changes are required to convert this class into a JSR 299 compliant bean (Web Bean) as listed below:

  1. Add an empty "beans.xml" to the WEB-INF directory.
  2. Replace "@ManagedBean" with "@javax.inject.Named annotation". "@javax.inject" annotations are defined by JSR 330.
  3. Resource injection does not work with JPA classes, yet, so populate EntityManager explicitly as explained below:
    1. Replace EntityManagerFactory resource injection:

      @PersistenceUnit
      EntityManagerFactory emf;
      

      with:

      EntityManager emf = Persistence.createEntityManagerFactory("HelloEclipseLinkPU");
      
    2. Add the required entity classes explicitly to "persistence.xml". If the persistence unit is injected then the container automatically scans the web application root for any entity classes.
      1. Expand "Configuration Files" and edit "persistence.xml".
      2. Uncheck "Include All Entity Classes in …" check box.
      3. Click on "Add Class…", select "state.States", and click on "OK".

That’s it, re-deploy your application and now you are using the Web Beans integration in GlassFish v3 instead of JSF managed bean. The output is available at "http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" as shown:

This is the exact same output as shown in TOTD #95.

Now, one-by-one, JPA, EJB, Transactions and other components will start working. Read Roger’s blog for another example of Web Beans in GlassFish.

A complete archive of all the tips is available here.

Technorati: totd glassfish v3 mysql javaee6 javaserverfaces webbeans jsr299 netbeans

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 1, 2009

TOTD #108: Java EE 6 web application (JSF 2.0 + JPA 2.0 + EJB 3.1) using Oracle, NetBeans, and GlassFish

Filed under: General — arungupta @ 7:00 pm

TOTD #106 explained how to install Oracle database 10g R2 on Mac OS X. TOTD #107 explained how to connect this Oracle database using NetBeans. This Tip Of The Day will explain how to use the sample HR database (that comes with Oracle database server) to write a simple Java EE 6 application.

This application will use Java Server Faces 2.0 for displaying the results, Enterprise Java Beans 3.1 + Java Persistence API 2.0 for middle tier, and Oracle database server + GlassFish v3 as the backend. The latest promoted build (65 of this writing) will not work because of the issue #9885 so this blog will use build 63 instead.

Several improvements have been made over NetBeans 6.8 M1 build and this blog is using the nightly build of 9/27. The environment used in this blog is:

  • NetBeans 9/27 nightly
  • GlassFish v3 build 63
  • Oracle database server 10.2.0.4.0 R2 on Mac OS X
  • Oracle JDBC Driver type 4 (ojdbc6.jar)

Lets get started!

  1. Configure GlassFish v3 with JDBC connection
    1. Download and unzip build 63.
    2. Download ojdbc6.jar and copy to "glassfishv3/glassfish/domains/domain1/lib/ext" directory.
    3. Start the Application Server as:
      ./bin/asadmin start-domain --verbose &
      
    4. Create a JDBC connection pool as:
      ./bin/asadmin create-jdbc-connection-pool --datasourceclassname oracle.jdbc.pool.OracleDataSource --restype javax.sql.DataSource --property "User=hr:Password=hr:URL=jdbc\:oracle\:thin\:@localhost\:1521\:orcl" jdbc/hr
      

      and verify the connection pool as:

      ./bin/asadmin ping-connection-pool jdbc/hr
      
    5. Create a JDBC resource as:
      ./bin/asadmin create-jdbc-resource --connectionpoolid jdbc/hr jdbc/hr
      
  2. Configure GlassFish v3 build 63 in NetBeans
    1. In NetBeans IDE "Services" panel, right-click on "Servers" and click on "Add Server…". Choose "GlassFish v3" and provide a name as shown below:

    2. Click on "Next >" and specify the unzipped GlassFish location as:

      and click on "Finish".

  3. Create the Java EE 6 application
    1. In "Projects" pane, right-click and select "New Project…".
    2. Choose "Java Web" and "Web Application" and click on "Next". Choose the project name as "HelloOracle":

      and click on "Next >".

    3. Select the recently added GlassFish v3 server and choose "Java EE 6 Web" profile:

      and click on "Next >". Notice "Java EE 6 Web" profile is chosen as the Java EE version.

    4. Select "JavaServer Faces" on the frameworks page:

      and click on "Finish". Notice the JSF libraries bundled with the App Server are used.

  4. Create the Java Persistence Unit
    1. Right-click on the project, select "New", "Entity Classes from Database…":

    2. From the Data Source, select "jdbc/hr" as shown:

      This is the same JDBC resource created earlier. Select "EMPLOYEES" from the Available Table, click on "Add >" to see the output as:

      The related tables are automatically included. Click on "Next >".

    3. Click on "Create Persistence Unit …" and take all the defaults and click on "Create".
    4. Specify the package name as "model":

      and click on "Finish". This generates a JPA-compliant POJO class that provide access to tables in the underlying Oracle database. The class name corresponding to each table is shown in the wizard.

  5. Create Enterprise Java Beans
    1. Right-click on the project and select "New Class…".
    2. Specify the class name as "EmployeesBean" and package as "controller", click on "Finish".
    3. Annotate the class to make it an Enterprise Java Bean and a JSF Managed Bean as:
      @javax.ejb.Stateless
      @javax.faces.bean.ManagedBean
      

      Notice, the EJB is bundled in the WAR file and no special type of modules are required. Java EE 6 provides simplified packaging of EJB which makes it really ease to use.

      Also this application is currently using JSF managed bean but will use JSR 299 (aka Web Beans) in a future blog.

    4. Inject the Persistence Unit by adding the following variable:
      @PersistenceUnit
      EntityManagerFactory emf;
      
    5. Add a new method to retrieve the list of all employees as:
      public List getEmployees() {
      return em.createNamedQuery("Employees.findAll").getResultList();
      }
      

      "Employees.findAll" is a default NamedQuery generated by NetBeans and makes it easy to query the database. Several other queries are generated for each mapped JPA class, such as "Employees.findByEmployeeId" and "Employees.findByFirstName". Custom queries can also be created and specified on the POJO class.

      The completed class looks like:

      @Stateless
      @ManagedBean
      public class EmployeesBean {
      public List getEmployees() {
      return emf.createEntityManager().createNamedQuery("Employees.findAll").getResultList();
      }
      }
      
  6. Use EJB in the generated JSF page
    1. JSF 2 uses Facelets as the templating mechanism and NetBeans generate a simple "index.xhtml" file to start with. Expand "Web Pages" and open "index.xhtml".
    2. Replace the body template with:
      <h1>First Java EE 6 app using Oracle database</>
      <h:dataTable var="emp" value="#{employeesBean.employees}" border="1">
      <h:column><h:outputText value="#{emp.lastName}"/>, <h:outputText value="#{emp.firstName}"/></h:column>
      <h:column><h:outputText value="#{emp.email}"/></h:column>
      <h:column><h:outputText value="#{emp.hireDate}"/></h:column>
      </h:dataTable>
      

      It uses JSF value expressions to bind the Enterprise Java Bean and dumps the HTML formatted name, email, and hire date of each employee in the database.

  7. Run the project: Right-click on the project and select "Run" to see the output at "http://localhost:8080/HelloOracle/" as:

So we can easily create a Java EE 6 application using NetBeans, Oracle, and GlassFish.

A complete archive of all the TOTDs is available here.

This and other similar applications will be demonstrated at the upcoming Oracle Open World.

Technorati: totd oracle database glassfish v3 javaee javaserverfaces ejb jpa netbeans oow

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

Running Summary Q3 2009 – 400 miles, 80% of running days

Filed under: Running — arungupta @ 6:34 am

Here is the summary of my running logs from Q3 2009 (Jul – Sep):

Even though 3 days of less running than Q2, but the overall mileage was increased. Barely hit that 400 miles mark in Q3 and it did took some extra efforts to reach there ;-) But now tapering for the upcoming Rock-n-Roll 1/2 Marathon this weekend.

5 miles was the most favorite distance being run 17 times and 19.7 miles and 17.4 miles were run only once.

Would you like to generate similar charts for your running logs as well ?

This can be easily achieved using a Rails application or a Wicket application on GlassFish.

Technorati: rails wicket glassfish running rnrsj

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
« Newer Posts

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress