You are previewing Core J2EE™ Patterns: Best Practices and Design Strategies, Second Edition.
O'Reilly logo
Core J2EE™ Patterns: Best Practices and Design Strategies, Second Edition

Book Description

"The Java landscape is littered with libraries, tools, and specifications. What's been lacking is the expertise to fuse them into solutions to real-world problems. These patterns are the intellectual mortar for J2EE software construction."
--John Vlissides, co-author of Design Patterns, the "Gang of Four" book

"The authors of Core J2EE Patterns have harvested a really useful set of patterns. They show how to apply these patterns and how to refactor your system to take advantage of them. It's just like having a team of experts sitting at your side."
--Grady Booch, Chief Scientist, Rational Software Corporation

"The authors do a great job describing useful patterns for application architectures. The section on refactoring is worth the price of the entire book!"
--Craig McClanahan, Struts Lead Architect and Specification Lead for JavaServer Faces

"Core J2EE Patterns is the gospel that should accompany every J2EE application server...Built upon the in-the-trenches expertise of its veteran architect authors, this volume unites the platform's many technologies and APIs in a way that application architects can use, and provides insightful answers to the whys, whens, and hows of the J2EE platform."
--Sean Neville, JRun Enterprise Architect, Macromedia

Developers often confuse learning the technology with learning to design with the technology. In this book, senior architects from the Sun Java Center share their cumulative design experience on Java 2 Platform, Enterprise Edition (J2EE) technology.

The primary focus of the book is on patterns, best practices, design strategies, and proven solutions using the key J2EE technologies including JavaServer Pages(TM) (JSP(TM)), Servlets, Enterprise JavaBeans(TM) (EJB(TM)), and Java(TM) Message Service (JMS) APIs. The J2EE Pattern Catalog with 21 patterns and numerous strategies is presented to document and promote best practices for these technologies.

Core J2EE Patterns, Second Edition offers the following:

  • J2EE Pattern Catalog with 21 patterns--fully revised and newly documented patterns providing proven solutions for enterprise applications

  • Design strategies for the presentation tier, business tier, and integration tier

  • Coverage of servlets, JSP, EJB, JMS, and Web Services

  • J2EE technology bad practices

  • Refactorings to improve existing designs using patterns

  • Fully illustrated with UML diagrams

  • Extensive sample code for patterns, strategies, and refactorings

  • Table of Contents

    1. Copyright
      1. Dedication
    2.  
    3. Prentice Hall Core Series
    4. Foreword – Grady Booch
    5. Foreword – Martin Fowler
    6. Preface
      1. Sun Java Center and the J2EE Pattern Catalog
      2. What This Book Is About
      3. What This Book Is Not
      4. Who Should Read this Book
      5. How This Book Is Organized
        1. Part 1
        2. Part 2
      6. Companion Web Site and Contact Information
      7. Acknowledgments
      8. Acknowledgments – First Edition
    7. 1. Patterns and J2EE
      1. 1. Introduction
        1. What Is J2EE?
        2. What Is a Pattern?
          1. Historical References
          2. Defining a Pattern
          3. Categorizing Patterns
        3. J2EE Pattern Catalog
          1. Continuous Evolution
          2. How to Use the J2EE Pattern Catalog
          3. Benefits of Using Patterns
            1. Leverage a Proven Solution
            2. Common Vocabulary
            3. Constrains Solution Space
        4. Patterns, Frameworks, and Reuse
        5. Summary
      2. 2. Presentation Tier Design Considerations and Bad Practices
        1. Presentation Tier Design Considerations
          1. Session Management
            1. Session State on the Client
              1. HTML Hidden Fields
              2. HTTP Cookies
            2. Security Concerns of Client-Side Session State
            3. Session State in the Presentation Tier
          2. Controlling Client Access
            1. Guarding a View
              1. Embedding Guard Within View
              2. Including an All-or-Nothing Guard per View
              3. Including a Guard for Portions of a View
              4. Portions of View Not Displayed Based on User Role
              5. Portions of View Not Displayed Based on System State or Error Conditions
            2. Guarding by Configuration
              1. Resource Guards via Standard Security Constraints
              2. Resource Guards via Simple and Generic Configuration
            3. Duplicate Form Submissions
              1. Synchronizer (or Déjà vu) Token
          3. Validation
            1. Validation on Client
            2. Validation on Server
              1. Form-Centric Validation
              2. Validation Based on Abstract Types
          4. Helper Properties—Integrity and Consistency
        2. Presentation Tier Bad Practices
          1. Control Code in Multiple Views
            1. Problem Summary
            2. Solution Reference
          2. Exposing Presentation-Tier Data Structures to Business Tier
            1. Problem Summary
            2. Solution Reference
          3. Exposing Presentation-Tier Data Structures to Domain Objects
            1. Problem Summary
            2. Solution Reference
          4. Allowing Duplicate Form Submissions
            1. Problem Summary
            2. Solution Reference
          5. Exposing Sensitive Resources to Direct Client Access
            1. Problem Summary
            2. Solution Reference
          6. Assuming <jsp:setProperty> Will Reset Bean Properties
            1. Problem Summary
            2. Solution Reference
          7. Creating Fat Controllers
            1. Problem Summary
            2. Solution Reference
          8. Using Helpers as Scriplets
            1. Problem Summary
            2. Solution Reference
      3. 3. Business Tier Design Considerations and Bad Practices
        1. Business Tier Design Considerations
          1. Using Session Beans
            1. Session Bean—Stateless Versus Stateful
            2. Storing State on the Business Tier
          2. Using Entity Beans
            1. Entity Bean Primary Keys
            2. Business Logic in Entity Beans
          3. Caching Enterprise Bean Remote References and Handles
        2. Business and Integration Tiers Bad Practices
          1. Mapping the Object Model Directly to the Entity Bean Model
            1. Problem Summary
            2. Solution Reference
          2. Mapping the Relational Model Directly to the Entity Bean Model
            1. Problem Summary
            2. Solution Reference
          3. Mapping Each Use Case to a Session Bean
            1. Problem Summary
            2. Solution Reference
          4. Exposing All Enterprise Bean Attributes via Getter/Setter Methods
            1. Problem Summary
            2. Solution Reference
          5. Embedding Service Lookup in Clients
            1. Problem Summary
            2. Solution Reference
          6. Using Entity Beans as Read-Only Objects
            1. Problem Summary
            2. Solution Reference
          7. Using Entity Beans as Fine-Grained Objects
            1. Problem Summary
            2. Solution Reference
          8. Storing Entire Entity Bean-Dependent Object Graph
            1. Problem Summary
            2. Solution Reference
          9. Exposing EJB-related Exceptions to Non-EJB Clients
            1. Problem Summary
            2. Solution Reference
          10. Using Entity Bean Finder Methods to Return a Large Results Set
            1. Problem Summary
            2. Solution Reference
          11. Client Aggregates Data from Business Components
            1. Problem Summary
            2. Solution Reference
          12. Using Enterprise Beans for Long-Lived Transactions
            1. Problem Summary
            2. Solution Reference
          13. Stateless Session Bean Reconstructs Conversational State for Each Invocation
            1. Problem Summary
            2. Solution Reference
      4. 4. J2EE Refactorings
        1. Presentation Tier Refactorings
          1. Introduce a Controller
            1. Motivation
            2. Mechanics
            3. Example
          2. Introduce Synchronizer Token
            1. Motivation
            2. Mechanics
            3. Example
          3. Localize Disparate Logic
            1. Motivation
            2. Mechanics
            3. Example
          4. Hide Presentation Tier-Specific Details From the Business Tier
            1. Motivation
            2. Mechanics
            3. Example
          5. Remove Conversions from View
            1. Motivation
            2. Mechanics
            3. Example
          6. Hide Resources From a Client
            1. Motivation
            2. Mechanics
            3. Example
              1. Restricted by Container Configuration
              2. Restricted by Using a Control Component
        2. Business and Integration Tier Refactorings
          1. Wrap Entities With Session
            1. Motivation
            2. Mechanics
          2. Introduce Business Delegate
            1. Motivation
            2. Mechanics
          3. Merge Session Beans
            1. Motivation
            2. Mechanics
          4. Reduce Inter-Entity Bean Communication
            1. Motivation
            2. Mechanics
          5. Move Business Logic to Session
            1. Motivation
            2. Mechanics
        3. General Refactorings
          1. Separate Data Access Code
            1. Motivation
            2. Mechanics
            3. Example
          2. Refactor Architecture by Tiers
            1. Motivation
            2. Mechanics
          3. Use A Connection Pool
            1. Motivation
            2. Mechanics
    8. 2. J2EE Pattern Catalog
      1. 5. J2EE Patterns Overview
        1. What Is a Pattern?
          1. Identifying a Pattern
          2. Patterns Versus Strategies
        2. The Tiered Approach
          1. Client Tier
          2. Presentation Tier
          3. Business Tier
          4. Integration Tier
          5. Resource Tier
        3. J2EE Patterns
          1. Presentation Tier Patterns
          2. Business Tier Patterns
          3. Integration Tier Patterns
        4. Guide to the Catalog
          1. Terminology
          2. Use of UML
            1. UML Stereotypes
          3. Pattern Template
        5. J2EE Pattern Relationships
        6. Relationship to Known Patterns
        7. Patterns Roadmap
        8. Summary
      2. 6. Presentation Tier Patterns
        1. Intercepting Filter
          1. Problem\
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. Client
              2. FilterManager
              3. FilterChain
              4. FilterOne, FilterTwo
              5. Target
            3. Strategies
              1. Standard Filter Strategy
              2. Custom Filter Strategy
              3. Base Filter Strategy
              4. Template Filter Strategy
              5. Web Service Message-Handling Strategies
              6. Custom SOAP Filter StrategyWeb Service
              7. JAX-RPC Filter StrategyWeb Service
          4. Consequences
          5. Related Patterns
        2. Front Controller
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. FrontController
              2. ApplicationController
              3. Command
              4. View
            3. Strategies
              1. Servlet Front Strategy
              2. JSP Front Strategy
              3. Command and Controller Strategy
              4. Physical Resource Mapping Strategy
              5. Logical Resource Mapping Strategy
              6. Multiplexed Resource Mapping Strategy
              7. Dispatcher in Controller Strategy
              8. Base Front Strategy
              9. Filter Controller Strategy
          4. Consequences
          5. Related Patterns
        3. Context Object
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. Client
              2. ProtocolInterface
              3. ContextFactory
              4. ContextObject
            3. Strategies
              1. Request Context Strategies
              2. Request Context Map Strategy
              3. Request Context POJO Strategy
              4. Request Context Validation Strategy
            4. Configuration Context Strategies
              1. JSTL Configuration Strategy
              2. Security Context Strategy
            5. General Context Object Strategies
              1. Context Object Factory Strategy
              2. Context Object Auto-Population Strategy
          4. Consequences
          5. Related Patterns
        4. Application Controller
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. Client
              2. ApplicationController
              3. Mapper
              4. Map
              5. Target
            3. Strategies
              1. Command Handler Strategy
              2. View Handler Strategy
              3. Transform Handler Strategy
              4. Navigation and Flow Control Strategy
            4. Message Handling StrategiesWeb Service
              1. Custom SOAP Message Handling StrategyWeb Service
              2. JAX-RPC Message Handling StrategyWeb Service
          4. Consequences
          5. Related Patterns
        5. View Helper
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. Client
              2. View
              3. Helper1, Helper2
              4. PresentationModel
            3. Strategies
              1. Template-Based View Strategy
              2. Controller-Based View Strategy
              3. JavaBean Helper Strategy
              4. Custom Tag Helper Strategy
              5. Tag File Helper Strategy
              6. Business Delegate as Helper Strategy
          4. Consequences
          5. Related Patterns
        6. Composite View
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. Client
              2. View
              3. SimpleView
              4. CompositeView
              5. Template
              6. ViewManager
            3. Strategies
              1. JavaBean View Management Strategy
              2. Standard Tag View Management Strategy
              3. Custom Tag View Management Strategy
              4. Transformer View Management Strategy
              5. Early-Binding Resource Strategy
              6. Late-Binding Resource Strategy
          4. Consequences
          5. Sample Code
          6. Related Patterns
        7. Service to Worker
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. FrontController
              2. ApplicationController
              3. View
              4. BusinessHelper, ViewHelper
              5. PresentationModel
              6. BusinessService
            3. Strategies
          4. Consequences
          5. Sample Code
          6. Related Patterns
        8. Dispatcher View
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. FrontController
              2. ApplicationController
              3. View
              4. BusinessHelper, ViewHelper
              5. PresentationModel
              6. BusinessService
            3. Strategies
          4. Consequences
          5. Sample Code
          6. Related Patterns
      3. 7. Business Tier Patterns
        1. Business Delegate
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. BusinessDelegate
              2. ServiceLocator
              3. BusinessService
            3. Strategies
              1. Delegate Proxy Strategy
              2. Delegate Adapter Strategy
          4. Consequences
          5. Sample Code
            1. Implementing the Business Delegate Pattern
          6. Related Patterns
        2. Service Locator
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. Client
              2. ServiceLocator
              3. Cache
              4. InitialContext
              5. Target
              6. RegistryService
            3. Strategies
              1. EJB Service Locator Strategy
              2. JDBC DataSource Service Locator Strategy
              3. JMS Service Locator Strategies
              4. JMS Queue Service Locator Strategy
              5. JMS Topic Service Locator Strategy
              6. Web Service Locator Strategy
          4. Consequences
          5. Sample Code
            1. Implementing Service Locator
            2. Implementing EJB Service Locator
            3. Implementing JMS Service Locator
            4. Implementing JDBC DataSource Service Locator
            5. Implementing Web Service Locator
          6. Related Patterns
        3. Session Façade
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Collaborations
              1. Client
              2. SessionFacade
              3. BusinessComponent
              4. ApplicationService
              5. DataAccessObject
            3. Session Façade Strategies
              1. Stateless Session Façade Strategy
              2. Stateful Session Façade Strategy
          4. Consequences
          5. Sample Code
            1. Implementing the Session Façade
          6. Related Patterns
        4. Application Service
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. Client
              2. ApplicationService
              3. BusinessObject
              4. Service
              5. DataAccessObject
            3. Strategies
              1. Application Service Command Strategy
            4. GoF Strategy for Application Service Strategy
            5. Application Service Layer Strategy
          4. Consequences
          5. Sample Code
          6. Related Patterns
        5. Business Object
          1. Problem
          2. Forces
          3. Solution
            1. Identifying Candidate Business Objects
            2. Business Objects and Persistence
            3. Structure
            4. Participants and Responsibilities
              1. Client
              2. ParentBO
              3. DependentBO
            5. Strategies
              1. POJO Business Object Strategy
            6. Composite Entity Business Object Strategy
          4. Consequences
          5. Sample Code
            1. Implementing POJO Business Object
            2. Implementing Entity Bean Business Object
          6. Related Patterns
        6. Composite Entity
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. CompositeEntity
              2. DependentBO, DependentEntityBO, and DependentPOJOBO
              3. DataStore
              4. EJBContainer
            3. Strategies
              1. Composite Entity Remote Facade Strategy
              2. Composite Entity BMP Strategies
              3. Lazy Loading Strategy
              4. Store Optimization (Dirty Marker) Strategy
              5. Composite Transfer Object Strategy
          4. Consequences
          5. Sample Code
            1. Implementing the Composite Entity Pattern
            2. Implementing the Lazy Loading Strategy
            3. Implementing the Store Optimization (Dirty Marker) Strategy
            4. Implementing the Composite Transfer Object Strategy
          6. Related Patterns
        7. Transfer Object
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. Client
              2. Component
              3. PresComponent
              4. BizComponent
              5. IntComponent
              6. TransferObject
            3. Strategies
              1. Updatable Transfer Objects Strategy
              2. Multiple Transfer Objects Strategy
              3. Entity Inherits Transfer Object Strategy
          4. Consequences
          5. Sample Code
            1. Implementing the Transfer Object Pattern
            2. Implementing the Updatable Transfer Objects Strategy
            3. Implementing the Multiple Transfer Objects Strategy
            4. Implementing the Entity Inherits Transfer Object Strategy
          6. Related Patterns
        8. Transfer Object Assembler
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. Client
              2. TransferObjectAssembler
              3. ApplicationModel
              4. BusinessObject
              5. SessionFacade
              6. DataAccessObject
              7. Service
            3. Strategies
              1. POJO Transfer Object Assembler Strategy
              2. Session Bean Transfer Object Assembler Strategy
          4. Consequences
          5. Sample Code
            1. Implementing the Transfer Object Assembler
          6. Related Patterns
        9. Value List Handler
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Collaborations
              1. Client
              2. ValueListIterator
              3. ValueListHandler
              4. DataAccessObject
              5. ValueList
              6. Value
            3. Strategies
              1. POJO Handler Strategy
              2. Value List Handler Session Façade Strategy
              3. Value List from Data Access Object Strategy
          4. Consequences
          5. Sample Code
            1. Implementing the Value List Handler
          6. Related Patterns
      4. 8. Integration Tier Patterns
        1. Data Access Object
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. Client
              2. DataAccessObject
              3. DataSource
              4. ResultSet
              5. Data
            3. Strategies
              1. Custom Data Access Object Strategy
            4. Data Access Object Factory Strategies
              1. Transfer Object Collection Strategy
              2. Cached RowSet Strategy
              3. Read Only RowSet Strategy
              4. RowSet Wrapper List Strategy
          4. Consequences
          5. Related Patterns
        2. Service Activator
          1. Problem
          2. Forces
          3. Solution
            1. Participants and Responsibilities
              1. Client
              2. Request
              3. ServiceActivator
              4. BusinessService
              5. Response
            2. Strategies
              1. POJO Service Activator Strategy
              2. MDB Service Activator Strategy
              3. Command Request Strategy
              4. Service Activator Aggregator Strategy
            3. Response Strategies
              1. Database Response Strategy
              2. Email Response Strategy
              3. JMS Message Response Strategy
          4. Consequences
          5. Related Patterns
        3. Domain Store
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
            3. Object Creation and Persistence
            4. Strategies
              1. Custom Persistence Strategy
              2. On-demand State Retrieval
              3. JDO Strategy
          4. Consequences
          5. Related Patterns
        4. Web Service Broker
          1. Problem
          2. Forces
          3. Solution
            1. Structure
            2. Participants and Responsibilities
              1. Client
              2. EndpointProcessor
              3. WebServiceBroker
              4. POJOBroker
              5. SessionBeanJAXBroker
              6. POJOJAXBroker
              7. BusinessService
            3. Strategies
              1. Custom XML Messaging Strategy
              2. Java Binder Strategy
              3. Java Binder Strategy Example
              4. JAX-RPC Strategy
              5. JAX-RPC Style Strategy Example
              6. Code Example
          4. Consequences
          5. Related Patterns
      5. Epilogue
        1. Web Worker Micro-Architecture
          1. Workflow Mini-Primer
          2. Web Worker Micro ArchitectureProblem
            1. Problem Story
          3. Forces
          4. Solution
            1. Structure
              1. Action Adapter
              2. Application Controller (205) Using Command Handler Strategy
              3. Business Delegate
              4. Service Locator
              5. Work Adapter
              6. Application Controller With Command Handler strategy
              7. Service Activator
            2. Participants and Responsibilities
              1. Action Adapter
              2. Interactions and Method Call Information
                1. Create Candidate
                2. Review Candidate
                3. Interview Candidate
              3. Business Event, Command String, and Type Returned
                1. Create Candidate
                2. Review Candidate
                3. Interview Candidate
              4. WorkflowCommand and WorkflowDelegate Actions
                1. Create Candidate
                2. Review Candidate
                3. Interview Candidate
              5. Work Adapter Components
              6. Code for Work Adapter
              7. Interactions and HireEmployeeWA workItemAcquired() Information
                1. Create Candidate
                2. Review Candidate
                3. Interview Candidate
              8. Refactoring Required for Web Application
                1. Presentation Tier
                2. Business Tier
              9. Refactorings for Workflow Event
                1. Start Workflow Process
                2. Stop, Pause and Terminate Workflow Process
                3. Acquire WorkItem
                4. Commit and Abort WorkItem
          5. Consequences
    9. Bibliography
      1. Printed Works
      2. Online References
    10. The Apache Software License, Version 1.1