java open source programming

482 2.6K 0
java open source programming

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info Joe Walnes Ara Abrahamian Mike Cannon-Brookes Pat Lightbody Java ™ Open Source Programming With XDoclet, JUnit, WebWork, Hibernate 01 463620 FM.qxd 10/28/03 8:49 AM Page i www.it-ebooks.info 01 463620 FM.qxd 10/28/03 8:49 AM Page xii www.it-ebooks.info Joe Walnes Ara Abrahamian Mike Cannon-Brookes Pat Lightbody Java ™ Open Source Programming With XDoclet, JUnit, WebWork, Hibernate 01 463620 FM.qxd 10/28/03 8:49 AM Page i www.it-ebooks.info Vice President and Executive Group Publisher: Richard Swadley Vice President and Executive Publisher: Bob Ipsen Vice President and Publisher: Joseph B. Wikert Executive Editorial Director: Mary Bednarek Editorial Manager: Kathryn A. Malm Executive Editor: Robert Elliott Senior Production Editor: Fred Bernardi Development Editor: Kevin Shafer Production Editor: Pamela Hanley Media Development Specialist: Kit Malone Permissions Editor: Carmen Krikorian Text Design & Composition: Wiley Composition Services Copyright © 2004 by Wiley Publishing, Inc., Indianapolis, Indiana. All rights reserved. Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, E-mail: permcoordinator@wiley.com. Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of mer- chantability or fitness for a particular purpose. No warranty may be created or extended by sales rep- resentatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any loss of profit or any other commercial damages, including but not lim- ited to special, incidental, consequential, or other damages. For general information on our other products and services please contact our Customer Care Depart- ment within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Trademarks: Wiley and the Wiley Publishing logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries. Java is a trademark of Sun Microsystems, Inc. All other trademarks are the property of their respective owners. Wiley Pub- lishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Cataloging-in-Publication Data: Java Open source programming : with Xdoclet, JUnit, WebWork, Hibernate (Java Open Source Library) / Joe Walnes [et al.]. p. cm. ISBN 0-471-46362-0 (PAPER/WEBSITE) 1. Java (Computer program language) 2. Open source software. I. Walnes, Joe, 1978- QA76.73.J38J3785 2003 005.2'762 dc22 2003020242 ISBN: 0-471-46362-0 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 01 463620 FM.qxd 10/28/03 8:49 AM Page ii www.it-ebooks.info Contents iii Acknowledgments xiii About the Authors xv Introduction xvii Part One Introduction 1 Chapter 1 Overview of the Book 3 Using Open Source Technologies 3 Understanding Design and Development Philosophies 5 Test First 5 Less Is More 5 Always Ask the Dumb Questions 6 Exploring the PetSoar Project 6 Sticking to the Basics 7 Summary 7 Chapter 2 Application Overview 9 Looking at Yet Another Pet Store? 9 Understanding the Importance of Maintainability 11 Understanding the Requirements of PetSoar 11 Examining the Architecture and Technologies 12 Looking at the Architecture 12 Looking at Utility Libraries 14 Using Persistence and Searching 14 Using the Web Front End 15 Testing 16 Summary 16 01 463620 FM.qxd 10/28/03 8:49 AM Page iii www.it-ebooks.info Part Two Building Your Open Source Toolbox 17 Chapter 3 Unit Testing with JUnit 19 Types of Testing 19 Using JUnit 20 Features of JUnit 21 Writing a Unit Test 21 Running a Unit Test 23 Running Multiple Tests 25 Everything You Need to Know about JUnit 28 Assertion Methods 28 Exception Handling 29 Test Suites 30 Test Runners 30 Setting Up and Tearing Down the Environment 34 Extensions 37 Summary 38 Chapter 4 Testing Object Interactions with Mocks 39 Testing Object Interactions 39 Exploring Some Pitfalls of Testing State 39 Too Many Dependencies 40 Too Much Exposure 40 Too Much State to Manage 40 Too Hard to Test 41 Exploring the Alternative: Testing Interactions 41 Using Mock Objects 42 Example Scenario 43 Understanding the Role of a Mock Object 44 Understanding the Mock Objects Library 45 Using Dynamic Mocks 46 Creating Mocks 47 Substituting Objects 47 Defining the Expectations 48 Understanding Argument Constraints 48 Verifying Expectations 49 Setting Up Return Values 50 Summary 51 Chapter 5 Storing Objects with Hibernate 53 Understanding the Complexities of Persistence 53 Persisting Objects with Hibernate 2 54 Creating the Persistent Classes 55 Mapping the Classes to a Database 57 <hibernate-mapping> 58 <class> 58 <property> 59 <id> 59 <component> 60 iv Contents 01 463620 FM.qxd 10/28/03 8:49 AM Page iv www.it-ebooks.info Configuring Hibernate 60 Obtaining a Session 63 Storing Objects in the Database 64 Retrieving Objects from the Database 67 Querying Persistent Objects 68 Persisting Relationships Between Objects 70 Persisting Hierarchies of Objects 76 Understanding the Hibernate Toolset 79 Comparing Hibernate with Competing Technologies 79 Hibernate vs. EJB 79 Hibernate vs. JDO 81 Hibernate vs. DAO Frameworks 81 Summary 81 Chapter 6 Model View Controller with WebWork 83 Understanding Model View Controller (MVC) 83 Examining the Model Layer 84 Examining the View Layer 85 Examining the Controller Layer 85 Tying It All Together 85 Looking at Reasons to Use MVC 86 Understanding MVC, WebWork, and XWork 87 Exploring XWork 87 Exploring WebWork 88 Taking an In-depth Look at Actions 89 A Simple XWork Example 90 Configuring XWork 91 Structuring Your Actions (Action Composition) 91 Calling an Action from XWork 93 Using Parameters and the ActionContext 94 Applying Newton’s Third Law of Physics 95 Understanding XWork Results and Action Chaining 96 Examining WebWork Results and the Servlet Environment 96 Configuring WebWork 98 Understanding the Role of the Dispatcher 99 Namespaces 100 Exploring Example Views in JSP and Velocity 101 HelloWorld in JSP 101 JSP Tags 102 Looking at Component-Based Web Development 104 Themes 107 Writing Your Own Component 108 One Small Problem 108 Expressing Yourself 109 Using Basic Expressions 110 Properties 110 Method Calls 111 Static Fields and Method Calls 111 Contents v 01 463620 FM.qxd 10/28/03 8:49 AM Page v www.it-ebooks.info Using Advanced Expressions 111 Dealing with Collections 112 Constructors 113 Context Variables and the Root Variable 113 Understanding the ValueStack 113 Accessing Stack Elements 114 Examples Using the JSP Tags 114 Exploring Type Conversion 115 Digging into a Date Example 115 Specifying Default Conversion Rules 117 Specifying New Conversion Rules 117 Separating Concerns with Interceptors 118 Looking at Configuration and Interceptor Stacks 118 Using LoggingInterceptor 119 Building Your Own Interceptor 120 Validation — A Powerful Interceptor 120 Exploring an Example without XWork Validation Framework 120 Exploring an Example with XWork Validation Framework 122 Using Built-In and Custom Validators 123 Using the Expression Validator 125 Summary 125 Chapter 7 Simplifying Layout with SiteMesh 127 Identifying Problems with Layout 127 Using the Object-Oriented Solution 132 Decorator Design Pattern 133 Composite Design Pattern 134 Combining the Patterns 135 Implementing the Solution with SiteMesh 137 SiteMesh Fundamentals 138 Creating a Decorator 140 Composing Pages 142 Exploring SiteMesh 145 Getting to the Content 145 Mapping Decorators 148 Using Tips and Tricks 149 Group Decorators Together 150 Don’t Be Afraid to Include 150 CSS Is Your Friend 150 Minimize HTML 151 Separate Your Concerns 151 Summary 151 Chapter 8 Adding Search Capabilities with Lucene 153 Understanding the Complexities of Searching 153 Introducing Lucene 154 Understanding the Elements of Lucene 154 Indexing a Document 155 vi Contents 01 463620 FM.qxd 10/28/03 8:49 AM Page vi www.it-ebooks.info Searching Documents 159 Reindexing and Removing an Indexed Document 161 Using Advanced Searching 161 Customizing the Tokenization Process 162 Summary 164 Chapter 9 Generating Configuration Files with XDoclet 165 Introducing XDoclet 165 Understanding Attribute Oriented Programming with XDoclet 166 Understanding the Syntax of Attributes 168 Running XDoclet 170 Using Advanced Hibernate OR Mapping with XDoclet 173 Using XDoclet for Generating More Sophisticated Artifacts 175 Understanding XDoclet Tasks and Subtasks 179 EJBDoclet 179 WebDoclet 180 JMXDoclet 180 JDODoclet 180 HibernateDoclet 181 Using XDoclet Effectively 181 Summary 183 Chapter 10 Communication and Tools 185 Exploring PetSoar Development 185 Managing Imperfect Communication 188 Communicating in Every Way 188 Using Source Communication 189 Using Communication as a Learning Tool 190 Exploring Our Toolbox 191 Source Configuration Management — CVS 191 Knowledge Management — Wiki 191 Mailing List — Majordomo 192 Real-time Discussion — IRC and Instant Messaging 192 IDEs — IDEA and Eclipse 193 Issue Tracking and Task Management — JIRA 193 Using Continuous Integration 193 Tying the Tools Together 195 Summary 195 Chapter 11 Time-Saving Tools 197 Understanding Utility Components 197 Understanding OSCache 198 Introducing SampleNews.com 198 Using the Loop Test 199 Exploring the OSCache Tag Library 201 Understanding OSCache Concepts 202 Cache Key 202 Scope 202 Duration 203 Contents vii 01 463620 FM.qxd 10/28/03 8:49 AM Page vii www.it-ebooks.info [...]... technology since publication time Also, consider checking out J2EE Open Source Toolkit: Building an Enterprise Platform with Open Source Tools by John T Bell, James Lambros, and Stan Ng (John Wiley & Sons: Hoboken, NJ, 2003), another title in the Java Open Source Library that shows you how to build an enterprise development platform using Open Source tools, including many of the same tools discussed in this... covering Using Open Source Technologies Open Source Software (OSS) is an interesting phenomenon that, in the last few years, has really begun to show its incredible staying power With the widespread usage of the Apache HTTP Server, the Linux operating system, and lately the JBoss application server, it is clear that Open Source technology can be as good as or better than commercial offerings Open Source Software... project-management system Mike specializes in building brilliantly simple, usable J2EE Web applications and also founded JavaBlogs (www.javablogs.com), a Web-based, Java- focused blog aggregator He also founded the OpenSymphony (www.opensymphony.com) project and works on WebWork, SiteMesh, and other Open Source projects On weekends, he likes to be distinctly Australian — drinking, gambling, and enjoying the best... overview and architecture Part II: Building Your Open Source Toolbox The second part of the book will introduce you to the many Open Source projects that we have come to include in our “developer’s toolbox” over the years of working with J2EE We will discuss reasons why each technology is useful, lessons learned from using the product, and finally alternative Open Source projects that may offer similar or... (although usually these two groups actually consist of the same set of people) The first group consists of anyone who wishes to learn about cutting-edge Open Source Java components In this book, we will take an in-depth look into several prominent Open Source projects that aid in enterprise development, including JUnit, Ant, Hibernate, WebWork, SiteMesh, XDoclet, and Lucene If you’re not already familiar... design, we will show you how utilizing Open Source technology can actually reduce the points of failure in your application and increase overall system stability and robustness while your application remains maintainable and flexible In this book, we draw upon several Open Source products Some will be offered under an umbrella organization, such as Apache Jakarta or OpenSymphony, while others are more... service is provided by Java API for XML Messaging (JAXM) All technologies powering this application are either standard J2EE technologies or Open Source products No stored procedures or code generation is used ◆ XPetStore: http://xpetstore.sourceforge.net/ An Open Source implementation that minimizes the lines of code by making heavy use of code generation, there are two versions of this available One... California, San Diego, where he received his B.S in Computer Science He also works on various Open Source projects, primarily WebWork, XWork, and OSWorkflow, all of which can be found at www.opensymphony.com You can read his blog at: http://blogs.atlassian.com/psquad www.it-ebooks.info Introduction In today’s IT environment, Java is a leading technology in the world of enterprise development As management demands... they evolved It was painful, but over time, we started making use of reusable Open Source tools that dramatically reduced the time taken to develop these applications We also started questioning which technologies were actually helping us and which had become a hindrance This book is about using development techniques and Open Source tools to lower the cost of building enterprise applications We aim to... Abrahamian is a freelance consultant specializing in developing Enterprise Java solutions He’s been involved in various J2EE projects all around the globe, as a consultant or technical leader His areas of expertise are code generation, attribute-oriented programming, and software automation techniques He is also active in many Open Source projects, including XDoclet as the leader of the project His other . Cataloging-in-Publication Data: Java Open source programming : with Xdoclet, JUnit, WebWork, Hibernate (Java Open Source Library) / Joe Walnes [et al.]. p. cm. ISBN 0-471-46362-0 (PAPER/WEBSITE) 1. Java (Computer. founded JavaBlogs (www.javablogs.com), a Web-based, Java- focused blog aggregator. He also founded the OpenSymphony (www.opensymphony.com) project and works on WebWork, SiteMesh, and other Open Source. www.it-ebooks.info Joe Walnes Ara Abrahamian Mike Cannon-Brookes Pat Lightbody Java ™ Open Source Programming With XDoclet, JUnit, WebWork, Hibernate 01 463620 FM.qxd 10/28/03 8:49 AM

Ngày đăng: 01/08/2014, 17:06

Mục lục

  • Part 1. Introduction

    • Overview of the Book

      • Using Open Source Technologies

      • Understanding Design and Development

      • Always Ask the Dumb Questions

      • Exploring the PetSoar Project

      • Sticking to the Basics

      • Application Overview

        • Looking at Yet Another Pet Store?

        • Understanding the Importance of Maintainability

        • Understanding the Requirements of PetSoar

        • Examining the Architecture and Technologies

          • Looking at the Architecture

          • Looking at Utility Libraries

          • Using Persistence and Searching

          • Using the Web Front End

          • Part 2. Building Your Open Source Toolbox

            • Unit Testing with JUnit

              • Types of Testing

              • Using JUnit

                • Features of JUnit

                • Writing a Unit Test

                • Running a Unit Test

                • Everything You Need to Know about JUnit

                • Testing Object Interactions with Mocks

                  • Testing Object Interactions

                    • Exploring Some Pitfalls of Testing State

                    • Exploring the Alternative: Testing Interactions

                    • Using Mock Objects

                      • Example Scenario

Tài liệu cùng người dùng

Tài liệu liên quan