Discover how to develop full-scale J2EE?TM applications quickly and efficiently using the best Open Source tools Written by leading authorities in the field, this book shows you how to leverage a suite of best-of-breed Open Source development tools to take the pain out of J2EE and build a complete Web-based application. You'll combine these tools to actually reduce the points of failure in your application, while increasing overall system stability and robustness. Along with the tools introduced here, you'll develop the PetSoar application, which follows the PetStore application used by Sun Microsystems to demonstrate features of J2EE. With PetSoar, the authors focus on developing a maintainable and flexible application, rather than showcasing the end result, so that you can apply the material in your own projects. In addition, the authors provide methods for utilizing Open Source software components for each stage of the development process. The Open Source products covered include: • Hibernate to aid with simple,flexible, and speedy transparent object persistence • OpenSymphony WebWork to allow for pluggable view technologies and extensible configuration • JUnit and Mock Objects to assist with rapid and robust unit testing • XDoclet to assist with generating code and configuration files automatically • Jakarta Lucene to add Google-style smart search capabilities to data stores • OpenSymphony SiteMesh to aid in the creation of large sites with a common look and feel • OpenSymphony OSCache to easily cache slow dynamic sections of Web sites resulting in faster-loading pages Cover 1 Contents 6 Introduction 20 Part 1. Introduction 24 Overview of the Book 26 Using Open Source Technologies 26 Understanding Design and Development 28 Philosophies 28 Test First 28 Less Is More 28 Always Ask the Dumb Questions 29 Exploring the PetSoar Project 29 Sticking to the Basics 30 Summary 30 Application Overview 32 Looking at Yet Another Pet Store? 32 Understanding the Importance of Maintainability 34 Understanding the Requirements of PetSoar 34 Examining the Architecture and Technologies 35 Looking at the Architecture 35 Looking at Utility Libraries 37 Using Persistence and Searching 37 Using the Web Front End 38 Testing 39 Summary 39 Part 2. Building Your Open Source Toolbox 40 Unit Testing with JUnit 42 Types of Testing 42 Using JUnit 43 Features of JUnit 44 Writing a Unit Test 44 Running a Unit Test 46 Running Multiple Tests 48 Everything You Need to Know about JUnit 51 Summary 61 Testing Object Interactions with Mocks 62 Testing Object Interactions 62 Exploring Some Pitfalls of Testing State 62 Exploring the Alternative: Testing Interactions 64 Using Mock Objects 65 Example Scenario 66 Understanding the Role of a Mock Object 67 Understanding the Mock Objects Library 68 Using Dynamic Mocks 69 Summary 74 Storing Objects with Hibernate 76 Understanding the Complexities of Persistence 76 Persisting Objects with Hibernate 2 77 Creating the Persistent Classes 78 Mapping the Classes to a Database 80 Configuring Hibernate 83 Obtaining a Session 86 Storing Objects in the Database 87 Retrieving Objects from the Database 90 Querying Persistent Objects 91 Persisting Relationships Between Objects 93 Persisting Hierarchies of Objects 99 Understanding the Hibernate Toolset 102 Comparing Hibernate with Competing Technologies 102 Hibernate vs. EJB 102 Hibernate vs. JDO 104 Hibernate vs. DAO Frameworks 104 Summary 104 Model View Controller with WebWork 106 Understanding Model View Controller (MVC) 106 Examining the Model Layer 107 Examining the View Layer 108 Examining the Controller Layer 108 Tying It All Together 108 Looking at Reasons to Use MVC 109 Understanding MVC, WebWork, and XWork 110 Exploring XWork 110 Exploring WebWork 111 Taking an In- depth Look at Actions 112 A Simple XWork Example 113 Configuring XWork 114 Structuring Your Actions (Action Composition) 114 Calling an Action from XWork 116 Using Parameters and the ActionContext 117 Applying Newton’s Third Law of Physics 118 Understanding XWork Results and Action Chaining 119 Examining WebWork Results 119 and the Servlet Environment 119 Configuring WebWork 121 Understanding the Role of the Dispatcher 122 Exploring Example Views in JSP and Velocity 124 Looking at Component- Based Web Development 127 Expressing Yourself 132 Using Basic Expressions 133 Using Advanced Expressions 134 Understanding the ValueStack 136 Exploring Type Conversion 138 Digging into a Date Example 138 Specifying Default Conversion Rules 140 Specifying New Conversion Rules 140 Separating Concerns with Interceptors 141 Looking at Configuration and Interceptor Stacks 141 Using LoggingInterceptor 142 Building Your Own Interceptor 143 Validation — A Powerful Interceptor 143 Exploring an Example without 143 XWork Validation Framework 143 Exploring an Example with XWork Validation Framework 145 Using Built- In and Custom Validators 146 Using the Expression Validator 148 Summary 148 Simplifying Layout with SiteMesh 150 Identifying Problems with Layout 150 Using the Object- Oriented Solution 155 Decorator Design Pattern 156 Composite Design Pattern 157 Combining the Patterns 158 Implementing the Solution with SiteMesh 160 SiteMesh Fundamentals 161 Creating a Decorator 163 Composing Pages 165 Exploring SiteMesh 168 Using Tips and Tricks 172 Group Decorators Together 173 Don’t Be Afraid to Include 173 CSS Is Your Friend 173 Minimize HTML 174 Separate Your Concerns 174 Summary 174 Adding Search Capabilities with Lucene 176 Understanding the Complexities of Searching 176 Introducing Lucene 177 Understanding the Elements of Lucene 177 Indexing a Document 178 Searching Documents 182 Reindexing and Removing an Indexed Document 184 Using Advanced Searching 184 Customizing the Tokenization Process 185 Summary 187 Generating Configuration Files with XDoclet 188 Introducing XDoclet 188 Understanding Attribute Oriented 189 Programming with XDoclet 189 Understanding the Syntax of Attributes 191 Running XDoclet 193 Using Advanced Hibernate OR Mapping with XDoclet 196 Using XDoclet for Generating More 198 Sophisticated Artifacts 198 Understanding XDoclet Tasks and Subtasks 202 Using XDoclet Effectively 204 Summary 206 Communication and Tools 208 Exploring PetSoar Development 208 Managing Imperfect Communication 211 Communicating in Every Way 211 Using Source Communication 212 Using Communication as a Learning Tool 213 Exploring Our Toolbox 214 Source Configuration Management — CVS 214 Knowledge Management — Wiki 214 Mailing List — Majordomo 215 Real- time Discussion — IRC and Instant Messaging 215 IDEs — IDEA and Eclipse 216 Issue Tracking and Task Management — JIRA 216 Using Continuous Integration 216 Tying the Tools Together 218 Summary 218 Time- Saving Tools 220 Understanding Utility Components 220 Understanding OSCache 221 Introducing SampleNews. com 221 Using the Loop Test 222 Exploring the OSCache Tag Library 224 Understanding OSCache Concepts 225 Looking at a Caching Time Example 226 Looking at Advanced OSCache Features 227 Understanding Commons Lang 228 Exploring Most Useful Classes 229 Using Builder Classes 229 Understanding Commons Collections 232 Understanding Commons Logging 234 Looking at Advantages of Commons Logging 234 Looking at a Simple Example 235 Understanding Commons Digester 236 Looking at a Digester Example 236 Understanding Digester Rules 239 Summary 240 Part 3. Developing the Application 242 Setting Up the Development Environment 244 Working from Within the IDE 245 The Problem: IDEs Don’t Automate 246 The Solution: Automated Build Tools 247 Using Ant for All Your Building Needs 248 The Problem: Ant Isn’t the Silver Bullet 248 The Solution: Use What Makes Sense 248 Using the Hybrid Approach 248 Laying Out Your Project 250 Structuring by Type 250 Structuring by Deployment 252 Picking a Structure 253 And What about PetSoar? 253 Managing Unit Tests 254 Understanding Test Types 255 Examining Test Suites, JUnit, and Batch Testing 256 Using Version Control 257 Deploying PetSoar 258 Summary 260 Understanding Test Driven Development 262 Why Test First? 263 Testing First vs. Testing Last 263 Narrowing the Requirements 265 Understanding Testing Techniques 266 Place Unit Tests in the Same Package As Your Code 267 Never Skip Failing Tests 267 Isolate the Untestable Using Mock Objects 267 When to Use Interfaces and Classes 268 Stick with Simplicity 268 Work from the Top Down 269 Use Lots of Small Objects 269 Ensure That Your Test Suite Runs Quickly 270 Avoid Statics and Singletons 270 Testing the TDD Cycle 270 Example Scenario Using TDD 272 Example Scenario Revisited 274 Enhancing the Functionality 275 Summary 277 Managing Lifecycles and Dependencies of Components 278 Understanding Components and Services 278 Handling Dependencies 279 Using Direct Instantiation 279 Using a Factory 280 Using a Registry 280 Using a Container 281 Understanding the Component Lifecycle 282 Understanding Inversion of Control 283 Examining the Benefits 283 Exploring the Disadvantages 284 Understanding Separation of Concerns 285 Using Containers to Define Scope 285 Using XWork’s Container Implementation 286 Configuring the Container 286 Understanding How the Container Works 287 Testing XWork Components 289 Summary 291 Defining the Domain Model 292 Considering the Advantages of a Domain Model 292 Tackling the Layers 293 Comparing a Layer- Driven vs. Feature- Driven Approach 293 Choosing Bottom Up, Top Down, or Middle Out 295 Identifying the Current Goal 298 Adding a Pet to the Store 298 Implementing the PersistenceManager Using Hibernate 302 Where We Are 311 Retrieving Pets 312 Retrieving a Single Pet 315 Where We Are 318 Persisting the Categories 320 Where We Are 323 Implementing the Category- Pet Relation 324 Where We Are 329 Performance and Maintainability Considerations 329 Summary 331 Creating a Web- Based Interface 332 Adding a Pet 333 Creating the AddPet Action 333 Creating Views for AddPet 336 Tying It All Together 338 Displaying a Pet 340 Creating the ViewPet Action 340 Refactoring the Actions 344 Editing a Pet 347 Checking Validity 350 Tying It All Together — Take II 351 Get that Pet Out of Here! 352 Refactoring the CRUD 356 Removing Duplication in ViewPet and RemovePet 357 Odd One Out 359 Performing One Last Refactor 361 Decoupling the Validation 363 Changing the Pet 366 Summary 368 Defining Navigation, Layout, Look, and Feel 370 Componentizing Form Widgets 370 Forming a Better Look and Feel 374 Using a Touch of Style 378 Navigating to a Better User Interface 381 Summary 384 Implementing Browse and Search Capabilities 386 Defining the Application Requirements 386 Browsing the List of Categories 387 Browsing the List of Pets 389 Searching the Store for Pets 392 Implementing LuceneSearcher 395 Implementing LuceneIndexer 399 Where We Are 409 Implementing Searching of Any Type of Data 409 Where We Are 418 Implementing Full- Text Searches 418 Implementing Pagination 420 Tying It All Together 424 Summary 426 Adding a Shopping Cart 428 Creating a Shopping Cart 428 Creating the WebWork Actions 431 Checking Out the Shopping Cart 434 Summary 446 Securing the Application 448 Understanding J2EE Security 449 Simplifying Security 451 Using the HTTP Request Wrapper 452 Using the Security Filter 453 Using the Login Filter 453 Making It All Work in Harmony 455 Using a More Graceful Approach 457 Summary 459 Index 460 Cover......Page 1 Contents......Page 6 Introduction......Page 20 Part 1. Introduction......Page 24 Using Open Source Technologies......Page 26 Less Is More......Page 28 Exploring the PetSoar Project......Page 29 Summary......Page 30 Looking at Yet Another Pet Store?......Page 32 Understanding the Requirements of PetSoar......Page 34 Looking at the Architecture......Page 35 Using Persistence and Searching......Page 37 Using the Web Front End......Page 38 Summary......Page 39 Part 2. Building Your Open Source Toolbox......Page 40 Types of Testing......Page 42 Using JUnit......Page 43 Writing a Unit Test......Page 44 Running a Unit Test......Page 46 Running Multiple Tests......Page 48 Everything You Need to Know about JUnit......Page 51 Summary......Page 61 Exploring Some Pitfalls of Testing State......Page 62 Exploring the Alternative: Testing Interactions......Page 64 Using Mock Objects......Page 65 Example Scenario......Page 66 Understanding the Role of a Mock Object......Page 67 Understanding the Mock Objects Library......Page 68 Using Dynamic Mocks......Page 69 Summary......Page 74 Understanding the Complexities of Persistence......Page 76 Persisting Objects with Hibernate 2......Page 77 Creating the Persistent Classes......Page 78 Mapping the Classes to a Database......Page 80 Configuring Hibernate......Page 83 Obtaining a Session......Page 86 Storing Objects in the Database......Page 87 Retrieving Objects from the Database......Page 90 Querying Persistent Objects......Page 91 Persisting Relationships Between Objects......Page 93 Persisting Hierarchies of Objects......Page 99 Hibernate vs. EJB......Page 102 Summary......Page 104 Understanding Model View Controller (MVC)......Page 106 Examining the Model Layer......Page 107 Tying It All Together......Page 108 Looking at Reasons to Use MVC......Page 109 Exploring XWork......Page 110 Exploring WebWork......Page 111 Taking an In- depth Look at Actions......Page 112 A Simple XWork Example......Page 113 Structuring Your Actions (Action Composition)......Page 114 Calling an Action from XWork......Page 116 Using Parameters and the ActionContext......Page 117 Applying Newton’s Third Law of Physics......Page 118 and the Servlet Environment......Page 119 Configuring WebWork......Page 121 Understanding the Role of the Dispatcher......Page 122 Exploring Example Views in JSP and Velocity......Page 124 Looking at Component- Based Web Development......Page 127 Expressing Yourself......Page 132 Using Basic Expressions......Page 133 Using Advanced Expressions......Page 134 Understanding the ValueStack......Page 136 Digging into a Date Example......Page 138 Specifying New Conversion Rules......Page 140 Looking at Configuration and Interceptor Stacks......Page 141 Using LoggingInterceptor......Page 142 XWork Validation Framework......Page 143 Exploring an Example with XWork Validation Framework......Page 145 Using Built- In and Custom Validators......Page 146 Summary......Page 148 Identifying Problems with Layout......Page 150 Using the Object- Oriented Solution......Page 155 Decorator Design Pattern......Page 156 Composite Design Pattern......Page 157 Combining the Patterns......Page 158 Implementing the Solution with SiteMesh......Page 160 SiteMesh Fundamentals......Page 161 Creating a Decorator......Page 163 Composing Pages......Page 165 Exploring SiteMesh......Page 168 Using Tips and Tricks......Page 172 CSS Is Your Friend......Page 173 Summary......Page 174 Understanding the Complexities of Searching......Page 176 Understanding the Elements of Lucene......Page 177 Indexing a Document......Page 178 Searching Documents......Page 182 Using Advanced Searching......Page 184 Customizing the Tokenization Process......Page 185 Summary......Page 187 Introducing XDoclet......Page 188 Programming with XDoclet......Page 189 Understanding the Syntax of Attributes......Page 191 Running XDoclet......Page 193 Using Advanced Hibernate OR Mapping with XDoclet......Page 196 Sophisticated Artifacts......Page 198 Understanding XDoclet Tasks and Subtasks......Page 202 Using XDoclet Effectively......Page 204 Summary......Page 206 Exploring PetSoar Development......Page 208 Communicating in Every Way......Page 211 Using Source Communication......Page 212 Using Communication as a Learning Tool......Page 213 Knowledge Management — Wiki......Page 214 Real- time Discussion — IRC and Instant Messaging......Page 215 Using Continuous Integration......Page 216 Summary......Page 218 Understanding Utility Components......Page 220 Introducing SampleNews. com......Page 221 Using the Loop Test......Page 222 Exploring the OSCache Tag Library......Page 224 Understanding OSCache Concepts......Page 225 Looking at a Caching Time Example......Page 226 Looking at Advanced OSCache Features......Page 227 Understanding Commons Lang......Page 228 Using Builder Classes......Page 229 Understanding Commons Collections......Page 232 Looking at Advantages of Commons Logging......Page 234 Looking at a Simple Example......Page 235 Looking at a Digester Example......Page 236 Understanding Digester Rules......Page 239 Summary......Page 240 Part 3. Developing the Application......Page 242 Setting Up the Development Environment......Page 244 Working from Within the IDE......Page 245 The Problem: IDEs Don’t Automate......Page 246 The Solution: Automated Build Tools......Page 247 Using the Hybrid Approach......Page 248 Structuring by Type......Page 250 Structuring by Deployment......Page 252 And What about PetSoar?......Page 253 Managing Unit Tests......Page 254 Understanding Test Types......Page 255 Examining Test Suites, JUnit, and Batch Testing......Page 256 Using Version Control......Page 257 Deploying PetSoar......Page 258 Summary......Page 260 Understanding Test Driven Development......Page 262 Testing First vs. Testing Last......Page 263 Narrowing the Requirements......Page 265 Understanding Testing Techniques......Page 266 Isolate the Untestable Using Mock Objects......Page 267 Stick with Simplicity......Page 268 Use Lots of Small Objects......Page 269 Testing the TDD Cycle......Page 270 Example Scenario Using TDD......Page 272 Example Scenario Revisited......Page 274 Enhancing the Functionality......Page 275 Summary......Page 277 Understanding Components and Services......Page 278 Using Direct Instantiation......Page 279 Using a Registry......Page 280 Using a Container......Page 281 Understanding the Component Lifecycle......Page 282 Examining the Benefits......Page 283 Exploring the Disadvantages......Page 284 Using Containers to Define Scope......Page 285 Configuring the Container......Page 286 Understanding How the Container Works......Page 287 Testing XWork Components......Page 289 Summary......Page 291 Considering the Advantages of a Domain Model......Page 292 Comparing a Layer- Driven vs. Feature- Driven Approach......Page 293 Choosing Bottom Up, Top Down, or Middle Out......Page 295 Adding a Pet to the Store......Page 298 Implementing the PersistenceManager Using Hibernate......Page 302 Where We Are......Page 311 Retrieving Pets......Page 312 Retrieving a Single Pet......Page 315 Where We Are......Page 318 Persisting the Categories......Page 320 Where We Are......Page 323 Implementing the Category- Pet Relation......Page 324 Performance and Maintainability Considerations......Page 329 Summary......Page 331 Creating a Web- Based Interface......Page 332 Creating the AddPet Action......Page 333 Creating Views for AddPet......Page 336 Tying It All Together......Page 338 Creating the ViewPet Action......Page 340 Refactoring the Actions......Page 344 Editing a Pet......Page 347 Checking Validity......Page 350 Tying It All Together — Take II......Page 351 Get that Pet Out of Here!......Page 352 Refactoring the CRUD......Page 356 Removing Duplication in ViewPet and RemovePet......Page 357 Odd One Out......Page 359 Performing One Last Refactor......Page 361 Decoupling the Validation......Page 363 Changing the Pet......Page 366 Summary......Page 368 Componentizing Form Widgets......Page 370 Forming a Better Look and Feel......Page 374 Using a Touch of Style......Page 378 Navigating to a Better User Interface......Page 381 Summary......Page 384 Defining the Application Requirements......Page 386 Browsing the List of Categories......Page 387 Browsing the List of Pets......Page 389 Searching the Store for Pets......Page 392 Implementing LuceneSearcher......Page 395 Implementing LuceneIndexer......Page 399 Implementing Searching of Any Type of Data......Page 409 Implementing Full- Text Searches......Page 418 Implementing Pagination......Page 420 Tying It All Together......Page 424 Summary......Page 426 Creating a Shopping Cart......Page 428 Creating the WebWork Actions......Page 431 Checking Out the Shopping Cart......Page 434 Summary......Page 446 Securing the Application......Page 448 Understanding J2EE Security......Page 449 Simplifying Security......Page 451 Using the HTTP Request Wrapper......Page 452 Using the Login Filter......Page 453 Making It All Work in Harmony......Page 455 Using a More Graceful Approach......Page 457 Summary......Page 459 Index......Page 460 Annotation Discover how to develop full & ndash;scale J2EE?TM applications quickly and efficiently using the best Open Source toolsWritten by leading authorities in the field, this book shows you how to leverage a suite of best & ndash;of & ndash;breed Open Source development tools to take the pain out of J2EE and build a complete Web & ndash;based application. You & prime;ll combine these tools to actually reduce the points of failure in your application, while increasing overall system stability and robustness. Along with the tools introduced here, you & prime;ll develop the PetSoar application, which follows the PetStore application used by Sun Microsystems to demonstrate features of J2EE. With PetSoar, the authors focus on developing a maintainable and flexible application, rather than showcasing the end result, so that you can apply the material in your own projects. In addition, the authors provide methods for utilizing Open Source software components for each stage of the development process. The Open Source products covered include: & lowast; Hibernate to aid with simple, flexible, and speedy transparent object persistence & lowast; OpenSymphony WebWork to allow for pluggable view technologies and extensible configuration & lowast; JUnit and Mock Objects to assist with rapid and robust unit testing & lowast; XDoclet to assist with generating code and configuration files automatically & lowast; Jakarta Lucene to add Google & ndash;style smart search capabilities to data stores & lowast; OpenSymphony SiteMesh to aid in the creation of large sites with a common look and feel & lowast; OpenSymphony OSCache to easily cache slow dynamic sections of Web sites resulting in faster & ndash;loading pages
Discover how to develop full-scale J2EETM applications quickly and efficiently using the best Open Source tools
Written by leading authorities in the field, this book shows you how to leverage a suite of best-of-breed Open Source development tools to take the pain out of J2EE and build a complete Web-based application. You'll combine these tools to actually reduce the points of failure in your application, while increasing overall system stability and robustness. Along with the tools introduced here, you'll develop the PetSoar application, which follows the PetStore application used by Sun Microsystems to demonstrate features of J2EE. With PetSoar, the authors focus on developing a maintainable and flexible application, rather than showcasing the end result, so that you can apply the material in your own projects.
In addition, the authors provide methods for utilizing Open Source software components for each stage of the development process.
The Open Source products covered include:
* Hibernate to aid with simple,flexible, and speedy transparent object persistence
* OpenSymphony WebWork to allow for pluggable view technologies and extensible configuration
* JUnit and Mock Objects to assist with rapid and robust unit testing
* XDoclet to assist with generating code and configuration files automatically
* Jakarta Lucene to add Google-style smart search capabilities to data stores
* OpenSymphony SiteMesh to aid in the creation of large sites with a common look and feel
* OpenSymphony OSCache to easily cache slow dynamic sections of Web sites resulting in faster-loading pages