Saturday, August 23, 2008

Getting Started Project-GraniteDS with Spring,Hibernate and Cairngorm,part 2

This is the second post in the series for getting started project Flex with spring/hibernate stack using GraniteDS as remoting server. So in my first post i created sample_graniteds database with a single table "Contact"
Now we'll create a server project using eclipse which has a GraniteDS nature and it will contain all the server related code as well as configuration files for graniteds such as service configuration and granite configuration.

Note:You can add the graniteds nature to a project by Right click on the project and add graniteds nature.

Before doing that i assume that you already have your eclipse setup with WTP plugin for running tomcat from eclipse, Flex builder plugin, and also a plugin for gas3 generator from GraniteDS,
Download granite-eclipse-builder-1.1.0RC4 and unzip it in your eclipse plugin directory and restart the eclipse. Also download the tomcat server, tomcat 6 would be fine. Follow the following steps

1. Define a Server in eclipse, select Windows->Preference then select server and installed runtimes, now add the tomcat 6 server as an installed runtime, as shown in snapshot below.

2. Now Create a new server project, File->New->Project then create a dynamic web project say GettingStarted_GDS as shown in the snapshots below.

i) Select Dynamic Web Project

ii) Define the Project Name and the Server

iii) Select this default configuration.

iv) define context root, content directory and click finish.

3. Now we have the project setup it's time to create a packages and required folders, as shown in the snapshot below.

name of the packages and folders are quite intuitive and since this is not a maven project we need the required libraries, ya it's absurd to play with jar files in the world dominated by maven. I have placed the complete project here, you can download the project and copy the jar files from WEB-INF/lib folder and to your lib folder.

4. Now need to write some code Contact.java as model, ContatDao.java as dao interface, ContactDaoImpl.java as implementation of dao and similarly ContactManager.java and ContactManagerImpl.java for service layer. Since these code contain very simple code for a CRUD operation i won't define them in detail here instead you can look at them directly by downloading the source. Similarly define the configuration files like
applicationContext.xml, daoContext.xml, managerContext.xml and hibernate.cfg.xml files. You can browse all these very simple configuration files here
Note: you might need to change the user/password for your database in the daoContext.xml file

5. Till now we have most of the server code and configuration ready now we'll define graniteds specific configuration files. Basically there are two configuration files that are needed for graniteds to work with spring/hibernate stack and the flex client. One is services-config.xml where you define your services that are to be exposed to the client via spring destinations that is invoked by a client using a RemoteObject. So a service defintion typically include destination which specifies the java class or service, channels to use to expose that service and the actual source that represents the destination. so we define our services-config.xml inside folder /webapp/WEB-INF/flex/ as services-config.xml

Other file is graniteds-config.xml, where we define the configuration to externalize our hibernate POJOs without implementing the Externalizable interface in our POJOs. The whole point of externalizing an object is to have a complete control over the format and contents of the stream for an object its parent and childs, as we are (de)serializing the object using AMF3 format over AMFChannel provided by flex messaging. But AMF3 serialization/deserialization does not provide any way to transfer private or protected fields (this is the case with BlazeDS as well as LCDS) so to serialize/deserialize the private and protected fields we use externalization mechanism in graniteDS so lets define grante-config.xml inside
/webapp/WEB-INF/granite folder as granite-config.xml

basically in this configuration file we are asking grantieds to externalize the beans(Contact.java) that is annotated with javax.persistence.Entity. Now we have the graniteds ready to externalize our java beans so we will use a AS3 bean generator tool to generate the corresponding actionscript class for the externalized java bean from the compiled java classes. GAS3 is an ant task based tool that relieves you from the painful task of writing corresponding actionscript classes for all your externalized java bean or say hibernate POJOs in our case. Now create a build.xml ant file in the root folder of your project as build.xml

So this build file defines the target generate.gas3 which generates the actionscript classes in output directory "as" and use all the compiled java classes from the folder build/classes/com/gettingstarted/gds/model. For each of the java class it generates two actionscript class so in our case it will generate

  • com.gettingstarted.gds.model.ContactBase.as: this bean mainly contains fields, getters, setters and IExternalizable methods (readExternal/writeExternal). This file is generated if it does not exist or if it is outdated.
  • com.gettingstarted.gds.model.Contact.as: this bean inherits from the "Base" one and is only generated if it does not exist.
We'll execute this ant file when we work on the client module. For now we'll finally define the web.xml for our server as web.xml
here we just define the AMF servlet and filter from graniteds. Now we are ready to run the server,To make it easy just checkout the project from the svn location here
or do svn checkout http://gettingstarted-gds.googlecode.com/svn/trunk/
If everything is setup properly you can run the server as Run->Run on Server and it should start the server without throwing any errors in the console.

So this is it, in the third post i will create a rich flex client using a Cairngorm MVC pattern. Do comment if anything needs to be updated or if anything is wrong.



7 comments:

maxter said...

thanks for explaining in detail, it was helpful to get started really.
thanks.

brisssou said...

what about part 3??

Chandra said...

Good one. But i faced some problem with spring jar in the project. it is not matching granite ds jar version. I replaced all sprinhg jars with spring.jar from graniteds site. Then it started working.
where is the 3 rd part??????

SIO said...

Please, the 3rd part ...

Puran said...

hope this helps
http://narup.blogspot.com/2008/12/getting-started-project-graniteds-with.html

waddle said...

excellent article, it was really helpfull.

I got one question. Is the HibernateExternalizer compatible with "true" hibernate framework, or is it only compatible with Hibernate Entity Manager (i.e. JPA implementation) ?

thx

Anonymous said...

thanks for the example but the database did not create

why??

thanks again