Thursday, August 18, 2011

Seam Framework Introduction

The traditional web application development in Java is usually handled using three tier architecture. The web layer is handled by one framework, the middle layer is handled by another framework and data access layer is handled by third framework. There are certain overlaps here like you can use Spring for web and middle layer and wither hibernate or JDBC or one of other ORM tools. or You can go for Struts as front layer, home grown middle layer and than one of ORM or JDBC tool as data access layer.
This kind of architecture has impedance when the flow moves from one framework to another. The context of three layers are separate so you have to move a lot of data back and forth. Also this has resulted in teams getting divided into similar roles. This division has resulted more because of lack of smooth flow in layers. In fact, if you look closely in these type of architectures, a lot of efforts goes into this and a lot of bug arises out of this. For example the famous LazyInitializationException in Hibernate is a classic example and it has given a lot of patterns based on the kind of frameworks employed in different layers.
Seam uses JSF and EJB3.0 and provides its own integration mechanism. What it means is that a EJB3.0 stateless bean can act as a JSF backing bean. Apart from EJB, Seam can make any POJO to act like a backing bean. In the case of POJO, Seam takes the responsibility of transactions, interception, threading and security. Also Seam provides Conversation and Business Process context in addition to Session and other context present in existing web applications. Seam uses Richfaces or Icefaces in the JSF layer which brings the AJAX feature automatically.
Seam derives its name from its ability to seam the front end and the back end layer together transparently.
To summarize,
  • If you have decided to go for JSF and EJB3.0 as your front end and back end layer than Seam will provide the glue for both the frameworks. Seam replaces JSF managed Bean container with a Seam container and also understands EJB. This results in a smooth transition from one framework another.
  • Seam can use POJO in place of EJB beans and even can use Spring beans.
  • Seam also has integration if you are working with other web frameworks like Wicket or Tapestry.

seam-gen is a utility to set up a CRUD application against a data model. This makes an eclipse project also and supports JBoss. Before we move forward do the following:
Download Seam from download section of http://www.seamframework.org. Download the lated GA version. Unzip it and put at a location.
Set up H2 Database  as we will use it to set the data model and than build a seam application. Also you need JBoss server 
We will use the data model having Student, Course and a Many to Many association between Student and Course. The SQL to insert the datamodel is:
CREATE TABLE STUDENT(ID INT PRIMARY KEY, NAME VARCHAR(255),HEIGHT NUMBER(10));
CREATE TABLE COURSE(ID INT PRIMARY KEY, NAME VARCHAR(255));
CREATE TABLE STUDENT_COURSE(STUDENT_ID INT,COURSE_ID INT, Foreign Key(STUDENT_ID) references STUDENT(ID), Foreign Key(COURSE_ID) references COURSE(ID))
Now go to seam download folder and run the command. In Linux it is ./seam and in windows it is seam.bat. You have to give a command also to the script. Running ./seam help will show all the command possible with descriptions.First let's run the setup command which basically sets the properties for the seam.
./seam setup
SEAM_HOME: /home/lalit/Desktop/Data/examples/training/seam/refrences/jboss-seam-2.1.1.GA
Using seam-gen sources from: /home/lalit/Desktop/Data/examples/training/seam/refrences/jboss-seam-2.1.1.GA/seam-gen
Buildfile: /home/lalit/Desktop/Data/examples/training/seam/refrences/jboss-seam-2.1.1.GA/seam-gen/build.xml
init:
setup:
     [echo] Welcome to seam-gen :-)
    [input] Enter your Java project workspace (the directory that contains your Seam projects) [C:/Projects] [C:/Projects]
/home/lalit/Desktop/Data/examples/training/seam/eclipseCode
    [input] Enter your JBoss AS home directory [C:/Program Files/jboss-4.2.3.GA] [C:/Program Files/jboss-4.2.3.GA]
/home/lalit/Desktop/ProgramFiles/jboss-5.0.0.GA
    [input] Enter the project name [myproject] [myproject]
seamFirstProject
     [echo] Accepted project name as: seamFirstProject
    [input] Do you want to use ICEfaces instead of RichFaces [n] (y, [n])
n
    [input] skipping input as property icefaces.home.new has already been set.
    [input] Select a RichFaces skin [classic] (blueSky, [classic], deepMarine, DEFAULT, emeraldTown, japanCherry, ruby, wine)
    [input] Is this project deployed as an EAR (with EJB components) or a WAR (with no EJB support) [ear] ([ear], war)
ear
    [input] Enter the Java package name for your session beans [com.mydomain.seamFirstProject] [com.mydomain.seamFirstProject]
com.oyeJava.seamFirstProject
    [input] Enter the Java package name for your entity beans [com.oyeJava.seamFirstProject] [com.oyeJava.seamFirstProject]
    [input] Enter the Java package name for your test cases [com.oyeJava.seamFirstProject.test] [com.oyeJava.seamFirstProject.test]
    [input] What kind of database are you using? [hsql] ([hsql], mysql, oracle, postgres, mssql, db2, sybase, enterprisedb, h2)
h2
    [input] Enter the Hibernate dialect for your database [org.hibernate.dialect.H2Dialect] [org.hibernate.dialect.H2Dialect]
    [input] Enter the filesystem path to the JDBC driver jar [/home/lalit/Desktop/Data/examples/training/seam/refrences/jboss-seam-2.1.1.GA/lib/hsqldb.jar] [/home/lalit/Desktop/Data/examples/training/seam/refrences/jboss-seam-2.1.1.GA/lib/hsqldb.jar]
/home/lalit/Desktop/ProgramFiles/h2/bin/h2-1.1.111.jar
    [input] Enter JDBC driver class for your database [org.h2.Driver] [org.h2.Driver]
org.h2.Driver
    [input] Enter the JDBC URL for your database [jdbc:h2:.] [jdbc:h2:.]
jdbc:h2:tcp://localhost/~/test
    [input] Enter database username [sa] [sa]
    [input] Enter database password [] []
    [input] Enter the database schema name (it is OK to leave this blank) [] []
    [input] Enter the database catalog name (it is OK to leave this blank) [] []
    [input] Are you working with tables that already exist in the database? [n] (y, [n])
y
    [input] Do you want to drop and recreate the database tables and data in import.sql each time you deploy? [n] (y, [n])
[propertyfile] Creating new property file: /home/lalit/Desktop/Data/examples/training/seam/refrences/jboss-seam-2.1.1.GA/seam-gen/build.properties
     [echo] Installing JDBC driver jar to JBoss AS
     [copy] Copying 1 file to /home/lalit/Desktop/ProgramFiles/jboss-5.0.0.GA/server/default/lib
     [echo] Type './seam create-project' to create the new project
BUILD SUCCESSFUL
Total time: 4 minutes 28 seconds
Now run the command 
./seam create-project. 
This will create a project structure with all dependencies set in.
After this run ./seam generate which will reverse engineer the data model and create a CRUD application. to deploy it on Jboss call ./seam deploy. If you go to JBoss deploy directory and navigate to server/default/deploy/ you will see the ear seamFirstProject.ear. Now run the server by navigating to JBoss deploy directory  and to bin folder. Issue the command ./run.sh. The server should start with no exceptions.
Hit your application in browser at http://localhost.:8080/seamFirstProject . Please change the host name and othe setting as per your local settings.It will open the home page of your application with menus on the top. This will have all the CRUD functionalities.
You can remove your application from JBoss server by issuing command ./seam undeploy.
There is another way of deploying application. In place of ./seam deploy, you issue command ./seam explode. This does not bundles the application in an archive but copies the whole directory structure in Jboss. In this way, you can hot deployment of classes and other artifacts. This is a preferable mode in development as it saves time in terms of deploy and undeploy  cycle. Seam-gen tools comes as an eclipse plugin also for eclipse IDE which can be downloaded from JBoss site.
The automated CRUD application may not be what you perceive your application to look like. But it's a good start and you can always use the generated artifact later on.

No comments:

Post a Comment