gnizr architecture (for developers)

27
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

Upload: hchen1

Post on 13-Jan-2015

2.225 views

Category:

Technology


2 download

DESCRIPTION

How WebWork and Spring function in gnizr. For developers who want to extend gnizr.

TRANSCRIPT

Page 1: Gnizr Architecture (for developers)

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

Page 2: Gnizr Architecture (for developers)

Agenda

Design patterns in gnizrWebWork and Spring configurationsHelloWorldDemo revisit

Page 3: Gnizr Architecture (for developers)

Design Patterns Found in Gnizr

Data Access Object (DAO)Inversion of Control (IoC)

a.k.a Dependency Injection

Singleton Interceptor

Page 4: Gnizr Architecture (for developers)

Gnizr Architecture

What’s DAO?

Page 5: Gnizr Architecture (for developers)

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?

Page 6: Gnizr Architecture (for developers)

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

Page 7: Gnizr Architecture (for developers)

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

Page 8: Gnizr Architecture (for developers)

DAO Design Pattern

http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html

Page 9: Gnizr Architecture (for developers)

DAO Example

Page 10: Gnizr Architecture (for developers)

DAO Example

User RegistrationUser DB(MySQL)

User (class)

UserDAO

UserDao implements…

- addUser(User): int- deleteUser(User): boolean- updateUser(User): boolean- getUser(id): User

Page 11: Gnizr Architecture (for developers)

Gnizr DAO package

Let’s scan through gnizr DAO API Bookmark User Tag Link Etc.

http://dev.gnizr.com/javadocs/2.3.0/

Page 12: Gnizr Architecture (for developers)

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…]

Page 13: Gnizr Architecture (for developers)

How Gnizr Business Logic is Created

When are these objects created?What’s the life span of these objects?How are JDBC connections initialized?

Page 14: Gnizr Architecture (for developers)

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

Page 15: Gnizr Architecture (for developers)

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

Page 16: Gnizr Architecture (for developers)

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…]

Page 17: Gnizr Architecture (for developers)

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

Page 18: Gnizr Architecture (for developers)

WebWork Actions are not Singletons

99% of times, you should not define WebWork actions as Singletons.

Because….

Page 19: Gnizr Architecture (for developers)

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?

Page 20: Gnizr Architecture (for developers)

WebWork Actions in gnizr

Let’s review some Action class in gnizr[switching to Eclipse…]

Page 21: Gnizr Architecture (for developers)

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 …

Page 22: Gnizr Architecture (for developers)

Interceptor

http://java.sun.com/blueprints/corej2eepatterns/Patterns/InterceptingFilter.html

Page 23: Gnizr Architecture (for developers)

Interceptor

http://java.sun.com/blueprints/corej2eepatterns/Patterns/InterceptingFilter.html

Page 24: Gnizr Architecture (for developers)

Gnizr WebWork configuration

Let’s review Gnizr’s webwork configuration.

[switching to Eclipse]

Page 25: Gnizr Architecture (for developers)

WebWork Result Types

Result Types are classes that determine what happens after an Action executes and a Result is returned.

Page 26: Gnizr Architecture (for developers)

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.

Page 27: Gnizr Architecture (for developers)

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…]