The use of design patterns in J2EE applications is an exciting new field, adding to the existing wealth of software design patterns. However these patterns do net exist in isolation, and inevitably they need to be assembled to form targer and more com- plex frameworks. Selecting patterns and turning them into real-world solutions is never an easy task. Furthermore applying patterns to address business and technical requirements poses enormous challenges.
This book is a guide to creating scalable and secure J2EE applications using pat- terns; including sound object-oriented design principles and reaFworld practices. The aim is to offer designers and developers access to the best techniques for designing and building J2EE solutions.
This book is not intended to be a catalog of J2EE patterns; rather the focus is on solving problems with patterns and devising implementation and deployment strate- gies. Each chapter is oriented around using patterns to achieve a specific purpose or more generally contribute to a goal.
Who is this book for?
This book is for developers who are familiar with the J2EE platform, and want to learn how to better utilize this technology. Developers and architects alike will be interested in how to exploit the power of patterns in their J2EE applications. What you will learn from this book
How to apply patterns to construct a robust and manageable web tier
How to apply patterns to construct a reusable persistence framework
How to apply patterns to improve your applications' performance and scalability
How to apply patterns to manage your applications' security
How to apply patterns to enable enterprise integration
Introduction
Who is this Book For?
What's Covered in this Book?
What You Need to Use this Book
Conventions
Customer Support
How to Download the Sample Code for the Book
Errata
E-Mail Support
p2p.wrox.com
Why this System Offers the Best Support
Chapter 1: Design Patterns Applied to J2EE
The Evolution of Patterns
Patterns in Software Engineering
What are Design Patterns?
Identifying Patterns
Representing Design Patterns
How Do Design Patterns Solve Design Problems?
Selecting a Design Pattern
Using a Design Pattern
Refactoring
Antipattems
J2EE and Design Patterns
Why Use Patterns in J2EE?
J2EE-Specific Patterns vs. Patterns in a J2EE Context
The Most Important J2EE Patterns
Sun Java Center J2EE Patterns Catalog
TheServerSide.com Patterns Catalog
Relations Between J2EE Patterns
Other Important Design Patterns
Problem Domains for J2EE Patterns
Relationships Between Patterns
Summary
Chapter 2: Patterns Applied to the Web Tier
Presentation Patterns
Case Study: A Hotel Bookings Management System
The Use Case Model
The Hotel Administrator Use Cases
Identifying the Patterns
Processing Requests
The Front Controller Design Pattern
Session Management
The Intercepting Filter Design Pattern
View Management
The Composite View Design Pattern
The Composite View Implementation
The View Helper Implementation
Validation
Assembling the Patterns into a Framework
The Service-to-Worker Design Pattern
The Dispatcher View Design Pattern
Comparing the Service-to-Worker and Dispatcher View Patterns
The Service*to-Worker Implementation
The Hotel Application Admin Site
Summary
Chapter 3: Patterns Applied to a Persistence Framework
A Starting Model
What is a Persistence Framework?
The Data Access Object Pattern
Persistence Framework Exceptions
Implementing the Data Access Object Pattern
The TitleDAO Session Bean
The TitleDAOBean Class
DAOs and Relationships
The Value Object Pattern
The TitleVO Value Object
The Service Locator Pattern
The Persistence Framework in Action
Persistence Framework Strategies
Primary Key Generation Strategies
Concurrency Strategies
Transaction Management Strategies
Performance Strategies
Migration Strategies
Summary
Chapter 4: Patterns Applied to Improve Performance and Scalability
What Causes Performance Problems?
Remote Method Invocation
Latency of the Network Call
Common Design Problems
What Causes Scalability Problems?
Tightly-Coupled Systems
Synchronous vs. Asynchronous Behavior
Indirection
The City Break Booking Application
Implementing the Entities
Identifying Patterns to Improve Performance
The Service Locater Pattern
Implementing Service Locater in the City Break Application
The Value Object Pattern
Local Interfaces
Composite Value Objects and the Value Object Assembler
Implementing Value Objects in the City Break Application
The Session Fa(;ade Pattern
Implementing Session Fa(;ade in the City Break Application
Identifying Patterns to Improve Scalability
The Message Fac;ade Pattern
Implementing Message Facade in the City Break Application
The Business Delegate Pattern
Implementing Business Delegates in the City Break Application
The Complete City Break Architecture
The Customer Processes
The Quote Process
The Booking Process
Summary
Chapter 5: Patterns Applied to Manage Security
What are Security Patterns?
Benefits of Using Security Patterns
When to Use Security Patterns
Secure Programming
The Web Banking Case Study
High-level Overview
Assumptions
Business Requirements
Technical Requirements
Security Patterns
The Single Access Point Pattern
The Check Point Pattern
The Role Pattern
Risk Assessment and Management
Authoritative Source of Data
Wrox Web Banking Use Cases
Actors
Use Cases
View Public Pages
Main Flow of Events
Login Use Case
Main Flow of Events
Alternative Flow of Events
Logout Use Case
Main Flow of Events
Alternative Flow of Events
View Account Balances Use Case
Main Flow of Events
Alternative Flow of Events
View Account Activity Use Case
Main Flow of Events
Alternative Flow of Events
Transfer Funds Use Case
Main Flow of Events
Alternative Flow of Events
Implementing the Case Study
Designing the Database
Application Architecture
Application Security
Roles Used in the System
Request URIs
Web Deployment Descriptor
EJB Deployment Descriptor
Application Classes
Summary
Chapter 6: Patterns Applied to Enable Enterprise Integration
What is Enterprise Application Integration?
Integration Architecture
Dat~-Level Integration
Application interface Integration
Business Method Integration
Presentation Integration
Inter-EAI
EAI with J2EE
Integration Patterns for J2EE
Integration Broker Pattern
Context
Problem
Forces
Solution
Consequences
Integration Wrapper Pattern
Context
Problem
Forces
Solution
Consequences
Integration Mediator Pattern
Context
Problem
Forces
Solution
Consequences
Virtual Component Pattern
Context
Problem
Forces
Solution
Consequences
Data Mapping Pattern
Context
Problem
Forces
Solution
Consequences
Process Automator Pattern
Context
Problem
Forces
Solution
Consequences
Relationships Between Integration Patterns
Simple Integration Scenario
Design with Pattern Identification
Authorization (Accessing the Customer Information)
Check Account Balance
Implementing the Integration Patterns
Implementing the Integration Wrapper Pattern
Define the Interface
Connect with Existing Application
Implement the Server Process
Implement the Client to Test the Component Wrapper
Implementing the Integration Mediator Pattern
Define the Interface and Connect with Existing Application
Develop the Client
implementing the Data Mapping Pattern
Define the Interface
Develop implementation Class for CMP
Client Application for the Entity Bean
Implement the Multi-Step Data Mapping Using BMP
Implementing the Virtual Component Pattern
Implementing the Process Automator Pattern
Client
Running the Example
Using Stateful Session Beans for Process Automators
Using Message-Driven Beans for Process Automators
Using Integration Patterns for B2B
Summary
Chapter 7: Patterns Applied to Enable Reusability, Maintainability, and
Extensibility
Why Write Reusable Software?
Why Write Maintainable Software?
Why Write Extensible Software?
A Component-Based Case Study
The Interface
The Fa(~ade Design Pattern
Implementing Fa(~ade in the J2EE Context
The Abstract Factory Design Pattern
Implementing Abstract Factory in the J2EE Context
The Decorator Design Pattern
Implementing Decorator in the J2EE Context
The Builder Design Pattern
Implementing Builder in the J2EE Context
The Template Method Design Pattern
Implementing Template Method in the J2EE Context
Assembling the Patterns into a Framework
Summary
Index
Welcome toJ2EE Design Patterns Applied. As the title suggests, this book will use theJ2EE design patterns and apply them to build quality applications. Over the years, patterns have proved to be an effective way to gather, formalize, and disseminate experience about recurring solutions to common problems in some contexts. TheJ2EE platform design patterns add to the existing wealth of software design patterns. However, these patterns do not exist in isolation, and inevitably they need to be assembled to form larger and more complex frameworks.
Selecting patterns and turning them into real-world solutions is never an easy task. Furthermore, applying patterns in general, orJ2EE patterns in particular, to address business and technical requirements poses enormous challenges. This book is a guide to creating scalable and secureJ2EE applications usingJ2EE patterns, including good object-oriented design principles and real-world practices.
In this book we'll not only describe the common problems that we face in enterprise development withJ2EE and provide sound solutions for them, but also show how to apply these solutions to real world-projects.
Who is this Book For?
This book is for developers who are reasonably familiar with the Java 2 Platform, Enterprise Edition, and want to learn how to leverage this technology into better practice. They can be:
ProficientJ2EE Developers who are interested in moving with the technology and pushing theirJ2EE knowledge to its limits
J2EE Architects who are tasked with setting system specifications, orchestrating Java 2 Enterprise solutions, and integrating them with backend systems like databases and legacy systems
What's Covered in this Book?
This book will cover the various patterns that are used while designing enterprise applications. We'll cover these patterns in individual chapters and apply them to various areas of design and development. We'll discuss each of these patterns with the help of a case study and apply them. The chapters in this book are organized as follows:
Chapter 1: Design Patterns Applied to J2EE This chapter provides an overview of patterns and focuses more specifically on design patterns andJ2EE. Some guidelines as to how to use patterns in an effective way are also discussed.
Chapter 2: Patterns Applied to the Web Tier This chapter describes issues that surround designing and managing the top layer of a web application. We discuss the technical requirements expected of the client layer and how the .J2EE presentation design patterns can address these.
Chapter 3: Patterns Applied to a Persistence Framework This chapter focuses exclusively on developing the data tier using a combination of common data persistence design patterns andJ2EE technologies. Specifically, this chapter covers the topics such as what persistent frameworks are and discusses extensively the core patterns that should be considered while building a persistence framework.
Chapter 4: Patterns Applied to Improve Performance and Scalability In this chapter we look at a number of patterns that lead to a better performing and more scalable design.
Chapter 5: Patterns Applied to Manage Security In this chapter, we introduce security patterns and their benefits. Security patterns provide techniques for addressing security issues asJ2EE and other object-oriented patterns provide proven techniques for solving known programming problems.
Chapter 6: Patterns Applied to Enable Enterprise Integration In this chapter, we discuss the most important integration patterns and show how to apply them in theJ2EE standards.
Chapter 7: Patterns Applied to Enable Reusability, Maintainability, and Extensibility This chapter discusses the application of design patterns for the specific purpose of reusing software and developing more maintainable and extensible software. We shall then focus on a group of patterns that demonstrate these aspects in a generic framework, which is composed of a series of components in the context of aJ2EE framework.
What You Need to Use this Book
The code in this book was tested with the Java 2 Platform, Enterprise Edition SDK 1.3 Reference Implementation, which is available for download from http://java.sun.com/j2ee/download.html. Several chapters also require access to a database. For these chapters, we used MySQL, an open source database server, which is available free from http://www.mysql.com/downloads/index.html.
Conventions
To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.
For instance:
These boxes hold important, not-to-be-forgotten information, which is directly relevant to the surrounding text.
While the background style is used for asides to the current discussion. As for styles in the text:
When we introduce them, we highlight important words
We show keyboard strokes like this: Ctrl-K
We show filenames and code within the text like so: persistance, properties
Text on user interfaces and URLs are shown as: Menu
We present code in two different ways:
In our code examples, the code foreground style shows new, important, pertinent code.
While code background shows code that's less important in the present context, or has been seen before.
Customer Support
We always value hearing from our readers, and we want to know what you think about this book: what you liked, what you didn't like, and what you think we can do better next time. You can send us your comments, either by returning the reply card in the back of the book, or by e-mail to feedback@wrox.com. Please be sure to mention the book title in your message.
How to Download the Sample Code for the Book
When you visit the Wrox site, http://www.wrox.com/simply locate the title through our Search facility or by using one of the title lists. Click on Download in the Code column, or on Download Code on the book's detail page.
The files that are available for download from our site have been archived using WinZip. When you have saved the attachments to a folder on your hard-drive, you need to extract the files using a de-compression program such as WinZip or PKUnzip. When you extract the files, the code is usually extracted into chapter folders. When you start the extraction process, ensure your software (WinZip or PKUnzip) is set to use folder names.
Errata
We've made every effort to make sure that there are no errors in the text or in the code. However, no one is perfect and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save another reader hours of frustration, and of course, you will be helping us provide even higher quality information. Simply e-mail the information to support@wrox.com; your information will be checked and if correct, posted to the errata page for that title, or used in subsequent editions of the book.
To find errata on the web site, go to http://www.wrox.com/, and simply locate the title through our Advanced Search or title list. Click on the Book Errata link, which is below the cover graphic on the book's detail page.
E-Mail Support
If you wish to directly query a problem in the book with an expert who knows the book in detail then e- mail support@wrox.com, with the title of the book and the last four numbers of the ISBN(5288) in the subject field of the e-mail. A typical e-mail should include the following things:
The title of the book, last four digits of the ISBN, and page number of the problem in the Subject field
Your name, contact information, and the problem in the body of the message
We won't send you junk mail. We need the details to save your time and ours. When you send an e-mail message, it will go through the following chain of support:
Customer Support - Your message is delivered to our customer support staff who are the first people to read it. They have files on most frequently asked questions and will answer anything general about the book or the web site immediately.
Editorial - Deeper queries are forwarded to the technical editor responsible for that book. They have experience with the programming language or particular product, and are able to answer detailed technical questions on the subject.
The Authors - Finally, in the unlikely event that the editor cannot answer your problem, they will forward the request to the author. We do try to protect the authors from any distractions to their writing; however, we are quite happy to forward specific requests to them. All Wrox authors help with the support on their books. They will e-mail the customer and the editor with their response, and again all readers should benefit.
The Wrox Support process can only offer support to issues directly pertinent to the content of our published title. Support for questions that fall outside the scope of normal book support is provided via the community lists of our http://p2p.wrox.com/forum.
p2p.wrox.com
For author and peer discussion join the P2P mailing lists. Our unique system provides programmer to programmerTM contact on mailing lists, forums, and newsgroups, all in addition to our one-to-one e-mail support system. If you post a query to P2P, you can be confident that it is being examined by the many Nrox authors and other industry experts who are present on our mailing lists. At p2p.wrox.com you rill find a number of different lists to help you, not only while you read this book, but also as you tevelop your applications.
Fo subscribe to a mailing list just follow these steps:
1. Go to http://p2p.wrox.com/
2. Choose the appropriate category from the left menu bar
3. Click on the mailing list you wish to join
4. Follow the instructions to subscribe and fill in your e-mail address and password
5. Reply to the confirmation e-mail you receive
6. Use the subscription manager to join more lists and set your e-mail preferences
Why this System Offers the Best Support
You can choose to join the mailing lists or you can receive them as a weekly digest. If you don't have the time, or facility, to receive the mailing list, then you can search our archives. Junk and spam mails are deleted, and your own e-mail address is protected by the unique Lyris system. Queries about joining or leaving lists, and any other general queries about lists, should be sent to }istsupport@wrox,com,