Upload
hchen1
View
2.225
Download
2
Embed Size (px)
DESCRIPTION
How WebWork and Spring function in gnizr. For developers who want to extend gnizr.
Citation preview
This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/
Gnizr Architecture
Dr. Harry Chen
CMSC 491S/691S
February 25, 2008
Agenda
Design patterns in gnizrWebWork and Spring configurationsHelloWorldDemo revisit
Design Patterns Found in Gnizr
Data Access Object (DAO)Inversion of Control (IoC)
a.k.a Dependency Injection
Singleton Interceptor
Gnizr Architecture
What’s DAO?
What’s DAO?
A design pattern that allows developers to abstract and encapsulate the access to the data source.
UserDB
(MySQL)User Registration
- add(user) - delete(user) - update(user)- lookup(user)
- User record- is OP okay?
How do you design your program to do these?
Why use DAO?
Hide SQL statements from the high-level program. Don’t want to write the same SQL multiple times in different class files
Allow high-level programs to access DB records without knowing how the DB schema is designed
Tight-coupling is a bad idea
Do you really want to write SQL every time you need to access the “customers” DB?
Fetching data from “ResultSet” is complicated – it’s column-driven. Not natural for OO programming
DAO Design Pattern
http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html
DAO Example
DAO Example
User RegistrationUser DB(MySQL)
User (class)
UserDAO
UserDao implements…
- addUser(User): int- deleteUser(User): boolean- updateUser(User): boolean- getUser(id): User
Gnizr DAO package
Let’s scan through gnizr DAO API Bookmark User Tag Link Etc.
http://dev.gnizr.com/javadocs/2.3.0/
Gnizr DAO Classes
Let’s walkthough some DAO class implementation in gnizr UserDao.java (interface)
UserDBDao.java (class impl) BookmarkDao.java (interface)
BookmarkDBDao.java (class impl)
[Switching to Eclipse…]
How Gnizr Business Logic is Created
When are these objects created?What’s the life span of these objects?How are JDBC connections initialized?
Inversion of Control (IoC)
IoC is a principle that encourage the decoupling of code. A.K.A. Dependency Injection.
Problem: The use of “MovieFinderImpl” is hardwired into “MovieLister”. Change this logic will require code rewrite and re-compile “MovieFinderImpl”
Read: http://martinfowler.com/articles/injection.html
Spring IoC
A framework that can be coupled with WebWork to provide Dependency Injection.
Spring IoC implements a flexible “Assembler”.
Developers can tell this “Assembler” how to perform “<create>” via XML configuration.
http://static.springframework.org/spring/docs/2.0.x/reference/beans.html
IoC in Gnizr
How to create “hocks” for Connecting DAO objects to Gnizr Core class
objects Connecting Gnizr Core class objects to
WebWork action objects Injecting application configuration at runtime
[Switching to Eclipse…]
Design Pattern: Singleton
A design pattern that restricts the instantiation of a class to one object.
Singleton is used when you want exactly one object to be created in the system.
Singletons in gnizr, for example, GnizrBasicDataSource (holds JDBC settings) All DAO objects (not Transport objects) Gnizr Core class objects
WebWork Actions are not Singletons
99% of times, you should not define WebWork actions as Singletons.
Because….
Actions are not Singleton
WebWork actions are not singletons because multiple action objects are needed to serve multiple user sessions.
user sessionaction
DAO
MySQL
Singleton is okay under what condition?
WebWork Actions in gnizr
Let’s review some Action class in gnizr[switching to Eclipse…]
WebWork Interceptors
Interceptors are classes that can be defined to pre-process request or post-process response. Validate parameter value (e.g., password length
must 8, no symbols are allowed) Map HTTP parameter values to Beans and call
“set” methods. Create HTTP session automatically …
Interceptor
http://java.sun.com/blueprints/corej2eepatterns/Patterns/InterceptingFilter.html
Interceptor
http://java.sun.com/blueprints/corej2eepatterns/Patterns/InterceptingFilter.html
Gnizr WebWork configuration
Let’s review Gnizr’s webwork configuration.
[switching to Eclipse]
WebWork Result Types
Result Types are classes that determine what happens after an Action executes and a Result is returned.
Which Result Type to Choose
The “name” of the result determines which Result Type to apply after an Action executes The “name” maybe “success”, “error” or any
string that you choose return in your “execute()” method, or “go()” if you extends the “AbstractAction” class.
Gnizr Result Type Examples
Let’s review some example of Action configuration in gnizr, and see how different Result Types play a role in the application File: gnizr-useradmin.xml File: gnizr-bookmark.xml File: gnizr-json.xml (searchproxy action)
[swithcing to Eclipse…]