Articles
Developer: Open Source
Grails on Oracle for Java Developersby Tug Grall Learn how to quickly build a Grails application on Oracle Database XE, and then deploy it to Oracle Application Server 10g.Published May 2006 As you may have noticed, Oracle recently published an article by Richard Monson-Haefel about
Ruby on Rails on Oracle. This article introduces the Ruby on Rails framework and explains how to use it to access an Oracle database (
Oracle Database 10g Express Edition (XE), to be precise)
What is Groovy? What is Grails?Groovy is a dynamic language that leverages features from other languages such as Ruby, Jython, and Smalltalk. Groovy runs on a Java VM and makes available any existing Java objects (so all the APIs) to Groovy. Groovy is currently under standardization with the JSR-241; you can learn more about Groovy on the Groovy site and its project leader's (Guillaume Laforge) blog. Grails is to Groovy what Ruby on Rails is to Ruby. (Originally named "Groovy On Rails" , this name has been dropped in favor of "Grails" to avoid confusion/competition.) Like Ruby on Rails, Grails is designed to create CRUD ( Create Read Update Delete) Web applications. You can learn more about Grail on the Grails site and its project leader's (Graeme Rocher) blog. Next, let's dive into the sample application. Example: The Product CatalogStep 1: Set up the Oracle databaseIf you have not set up the schema and table from Richard's article you just need to create the following objects:
CREATE TABLE comics (
id NUMBER(10) NOT NULL,
title VARCHAR2(60),
issue NUMBER(4),
publisher VARCHAR2(60),
PRIMARY KEY (id)
);
CREATE SEQUENCE comics_seq;
Based on that article I have created this table in the ruby schema. Step 2: Install GrailsGrails installation is straightforward and explained in the Installation guide. Basically:
You're done! Step 3: Create the Web ApplicationNow that you have installed the product, the next step is to create the application itself.
> grails create-app
....
.....
create-app:
[input] Enter application name:
comics_catalog
.....
As you will see, Grails uses Ant intensively. The create-app command will ask you for an application name; enter for example comics_catalog.
> cd comics_catalog
> grails create-domain-class
....
create-domain-class:
[input] Enter domain class name:
comics
....
When the command asks you to enter the class name, enter "comics." Grails will not use the same naming convention that Ruby on Rails does, so you need to use the same name for the class and the table you want to map your object on. The persistence layer is made using GROM (Grails Object Relational Mapping), which leverages JBoss Hibernate.
./comics_catalog/grails-app/domain/Comics.groovy Note that by default Grails creates the class with two attributes: id and version. Keep them in place and add title, issue, and publisher.
class Comics {
@Property Long id
@Property Long version
// new properties for the Comics class
@Property String title
@Property Long issue
@Property String publisher
String toString() { "${this.class.name} : $id" }
}
Now you're all set and ready to run the magic command that will create the different screens and flow.
> grails generate-all
....
input-domain-class:
[input] Enter domain class name:
comics
....
This command creates the different Views and Controllers; you can take a look to the directories: ./comics_catalog/grails-app/controllers
Configure database access. Next you have to configure the application to use the Oracle database and schema. Grails uses a configuration file for data source: ./comics_catalog/grails-app/conf/ApplicationDataSource.groovy Edit this file to connect to your Oracle database.
class ApplicationDataSource {
@Property boolean pooled = true
@Property String dbCreate = "update" // one of 'create', 'create-drop','update'
@Property String url = "jdbc:oracle:thin:@localhost:1521:XE"
@Property String driverClassName = "oracle.jdbc.OracleDriver"
@Property String username = "ruby"
@Property String password = "ruby"
}
Nothing special here, just properties such as URL, DriverClassName, username, and password. The one that is interesting is dbCreate, which allows you to configure the behavior on the schema to create (or not) objects. In our sample the table exists, so you want to reuse the object—but first you want to confirm you have all the mandatory objects (columns too), so select update.
ORACLE_XE_HOME/app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14.jar to ./comics_catalog/lib/ Step 4: Run the ApplicationGrails provides a way to run the application in standalone mode (run-app). This command starts a Web container (based on Jetty) with the application deployed. > grails run-app Note: Jetty will start on port 8080; in order to start on a different port (like 9090) use: grails -Dserver.port=9090 run-app You can now access the application using the following URL: http://localhost:8080/comics_catalog/comics/ Your browser should show the list of comics from the Comics table. As you can see, creating an application is really easy. The next step is to deploy the application to your application server. Step 5: Deploy the ApplicationGrails provides a command to package the application as a WAR ready to be deployed, so in the root directory of your project you can run the following command: > grails war When you run this command you end up with a WAR with the name of your application located in the root of your project (in our case, comics_catalog.war).
You can also save this configuration in a deployment plan to facilitate later deployment. When the deployment is done you can access the application using the Oracle Application Server host and port, something like: http://localhost:8888/comics_catalog/comics/list You can now administer and monitor the application like any other J2EE application deployed to Oracle Application 10g.
ConclusionGrails and Ruby on Rails are really interesting frameworks that allow developers to quickly create Web applications that access a relational database, and especially Oracle Database. Grails is quite new (release 0.2), but the documentation is really nice and complete. I encourage all interested developers to use it and provide feedback to the development team. Tug Grall is a product manager on the Oracle Application Server Containers for J2EE team. |
||||||||||||||||||||||||||||||||||||||||||||||