www.it-ebooks.info Professional Java ® JDK ® 6 Edition W. Clay Richardson, Donald Avondolio, Scot Schrager, Mark W. Mitchell, and Jeff Scanlon 01_777106 ffirs.qxp 12/5/06 5:58 PM Page i www.it-ebooks.info Professional Java, ® JDK ® 6 Edition Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN-13: 978-0-471-77710-6 ISBN-10: 0-471-77710-2 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 1O/RU/RS/QW/IN 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 Sections 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, 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-4355, or online at http://www.wiley.com/go/permissions. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REP- RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CON- TENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTAND- ING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PRO- FESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMA- TION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READ- ERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services or to obtain technical support, please contact our Cus- tomer Care Department within the U.S. at (800) 762-2974, outside the U.S. at (317) 572-3993 or fax (317) 572-4002. Library of Congress Cataloging-in-Publication Data Professional Java JDK, 6 Edition / W. Clay Richardson [et al.]. p. cm. Includes index. ISBN-13: 978-0-471-77710-6 (paper/website) ISBN-10: 0-471-77710-2 (paper/website) 1. Java (Computer program language) I. Richardson, W. Clay, 1976- QA76.73.J38P7623 2007 005.13’3—dc22 2006032740 Trademarks: Wiley and related trade dress are registered trademarks of Wiley Publishing, Inc., in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, 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. 01_777106 ffirs.qxp 12/5/06 5:58 PM Page ii www.it-ebooks.info This book is dedicated to all those who make the daily sacrifices, especially those who have made the ultimate sacrifice, to ensure our freedom and security. 01_777106 ffirs.qxp 12/5/06 5:58 PM Page iii www.it-ebooks.info About the Authors W. Clay Richardson is a software consultant concentrating on agile Java solutions for highly specialized business processes. He has fielded many Java solutions, serving in roles including senior architect, development lead, and program manager. He is a co-author of More Java Pitfalls and Professional Portal Development with Open Source Tools (Wiley), and Professional Java, JDK 5 Edition. As an adjunct professor of computer science for Virginia Tech, Richardson teaches graduate-level coursework in object-oriented development with Java. He holds degrees from Virginia Tech and the Virginia Military Institute. Donald Avondolio currently serves in a lead position as an architect/developer on an enterprise devel- opment project. In his spare time, Donald loves fly-fishing, watching baseball and lacrosse, running triathlons (not very well), and sitting around his house complaining about things. Scot Schrager has consulted extensively in the domains of pharmaceuticals, supply chain management, and the national security market. He has led and participated in various project teams using Java and Object Oriented Analysis & Design techniques. Most recently, Schrager has been focused on distributed application architecture using J2EE technology. Mark W. Mitchell has extensive experience in enterprise application integration, particularly Web Services integration between Java and the Microsoft platform. He has developed and deployed several mission-critical web applications. Mitchell holds a degree in computer science from the University of Virginia. Jeff Scanlon is a software development consultant from Virginia. He holds both the Sun Certified Java Developer and Microsoft Certified Solutions Developer certifications, and has been published in Software Development magazine. 01_777106 ffirs.qxp 12/5/06 5:58 PM Page iv www.it-ebooks.info Credits Acquisitions Editor Robert Elliott Development Editor Brian Herrmann Technical Editor David Parks Production Editor Kathryn Duggan Copy Editor Kim Cofer Editorial Manager Mary Beth Wakefield Production Manager Tim Tate Vice President and Executive Group Publisher Richard Swadley Vice President and Executive Publisher Joseph B. Wikert Project Coordinator Kristie Rees Graphics and Production Specialists Carrie A. Foster Brooke Gracyzk Denny Hager Joyce Haughey Barbara Moore Barry Offringa Alicia B. South Ronald Terry Quality Control Technicians John Greenough Charles Spencer Brian H. Walls Proofreading and Indexing Techbooks and Stephen Ingle Anniversary Logo Design Richard Pacifico 01_777106 ffirs.qxp 12/5/06 5:58 PM Page v www.it-ebooks.info 01_777106 ffirs.qxp 12/5/06 5:58 PM Page vi www.it-ebooks.info Contents Acknowledgments xv Introduction xvii Part I: Thinking Like a Java Developer 1 Chapter 1: Key Java Language Features and Libraries 3 Introducing Derby 3 Using Derby 4 Language Features Added in Java 5 7 Generics 7 Enhanced for Loop 16 Additions to the Java Class Library 17 Variable Arguments 18 Boxing and Unboxing Conversions 19 Static Imports 21 Enumerations 24 Metadata 26 Important Java Utility Libraries 34 Java Logging 35 Java Preferences 70 Summary 77 Chapter 2: Tools and Techniques for Developing Java Solutions 79 Principles of Quality Software Development 80 Habits of Effective Software Development 81 Communicate 81 Model 81 Be Agile 81 Be Disciplined 82 Trace Your Actions to Need 82 Don’t Be Afraid to Write Code 83 Think of Code as a Design, not a Product 83 02_777106 ftoc.qxp 11/28/06 10:35 PM Page vii www.it-ebooks.info viii Contents Read a Lot 84 Build Your Process from the Ground Up 84 Manage Your Configuration 84 Unit Test Your Code 85 Continuously Integrate 85 Maintaining Short Iterations 86 Measure What You Accomplished — Indirectly 87 Track Your Issues 87 Development Methodology 88 Waterfall Methodology 88 Unified Process 90 Extreme Programming 91 Observations on Methodology 92 Practical Development Scenarios 93 Ant 93 Maven 2 101 TestNG 106 XDoclet 110 JMeter 117 Summary 120 Chapter 3: Exploiting Patterns in Java 123 Why Patterns Are Important 124 Keys to Understanding the Java Programming Language 124 Keys to Understanding Tools Used in Java Development 125 Keys to Developing Effective Java Solutions 126 Building Patterns with Design Principles 127 Designing a Single Class 127 Creating an Association between Classes 128 Creating an Interface 129 Creating an Inheritance Loop 129 Important Java Patterns 131 Adapter 131 Model-View-Controller 134 Command 142 Strategy 146 Composite 150 Summary 154 02_777106 ftoc.qxp 11/28/06 10:35 PM Page viii www.it-ebooks.info ix Contents Part II: A Broad Understanding of Java APIs, Tools, and Techniques 155 Chapter 4: Developing Effective User Interfaces with JFC 157 Layout Managers 158 BorderLayout 158 BoxLayout 164 FlowLayout 173 GridLayout 177 GridBagLayout 189 SpringLayout 194 CardLayout 202 GroupLayout 208 Mustang Release Desktop Enhancements 214 Managing Navigation Flows in Swing Applications 225 Summary 235 Chapter 5: Persisting Your Application Using Files 237 Application Data 237 Saving Application Data 239 Sample Configuration Data Model for an Application 239 Java Serialization: Persisting Object Graphs 241 Key Classes 242 Serializing Your Objects 243 Extending and Customizing Serialization 257 When to Use Java Serialization 261 JavaBeans Long-Term Serialization: XMLEncoder/Decoder 262 Design Differences 262 Serializing Your JavaBeans 265 When to Use XMLEncoder/Decoder 269 Flexible XML Serialization: Java API for XML Binding (JAXB) 270 Sample XML Document for the Configuration Object 271 Defining Your XML Format with an XML Schema 273 JAXB API Key Classes 280 Marshalling and Unmarshalling XML Data 281 Creating New XML Content with JAXB-Generated Classes 283 Using JAXB-Generated Classes in Your Application 283 02_777106 ftoc.qxp 11/28/06 10:35 PM Page ix www.it-ebooks.info [...]... Sending and Receiving Messages 61 0 61 0 61 1 System Integration Patterns 61 9 Processing Chain Request-Reply Split-Aggregate 61 9 61 9 62 1 Summary 62 3 xii www.it-ebooks.info Contents Chapter 13: Java Security 62 5 Java Cryptography Architecture and Java Cryptography Extension (JCA/JCE) 62 5 JCA Design and Architecture Java Cryptography Extension 62 6 65 6 Program Security Using JAAS 66 6 User Identification Executing... Authorization Summary 66 7 66 8 67 2 67 3 Chapter 14: Packaging and Deploying Your Java Applications Examining Java Classpaths Investigating the Endorsed Directory Exploring Java Archives Manipulating JAR Files Examining the Basic Manifest File Examining Applets and JARs Signing JAR Files Examining the JAR Index Option Creating an Executable JAR Analyzing Applets 67 5 67 5 68 0 68 1 68 1 68 4 68 5 68 6 69 0 69 1 69 1 Basic Anatomy... Services 547 547 551 551 553 554 563 Random-Weather.org Platform-Independent RPC 564 566 Summary 597 Chapter 12: Service Oriented Integration Service Oriented Architecture Enabling Technology Java Management Extensions 599 599 60 0 60 0 Why Is JMX Important? The JMX Architecture Creating and Managing a Standard MBean JMX Management Java Messaging Service 60 1 60 1 60 2 60 6 61 0 Why Is JMS Important? Endpoints:... Examining Applet Security Exploring Web Applications 69 2 69 3 69 4 69 4 Examining the WAR Directory Structure Understanding the WAR Deployment Descriptor Packaging Enterprise JavaBeans Inspecting Enterprise Archives The EAR Descriptor File Deployment Scenario 69 5 69 6 69 8 69 9 700 700 Jumping into Java Web Start Examining the TicTacToe Example Summarizing Java Web Start Using Ant with Web Archives Installing... real Java solutions using Java APIs and open source tools Professional Java JDK 6 Edition leaves the reader with a well-rounded survey of the professional Java development landscape, without losing focus in exhaustive coverage of individual APIs This book is the bridge between Java language texts, methodology books, and specialized Java API books For example, once you have mastered the basics of the Java. .. the java. util branch of the class library that are useful for professional programming in Java Introducing Derby New to Java 6 is a lightweight database called Derby, a product of the Apache Database project Derby is a transactional, relational database and provides a small footprint on disk As of the July 13, 20 06 release of the Java 6 JDK, Derby is installed (by default) in C:\Program Files \Java\ jdk1 .6. 0\db... developers What This Book Covers Professional Java JDK 6 Edition builds upon Ivor Horton’s Beginning Java 2, to provide the reader with an understanding of how professionals use Java to develop software solutions It starts with a discussion of the tools and techniques of the Java developer, continues with a discussion of the more sophisticated and nuanced parts of the Java SDK, and concludes with several... derbytools.jar contains the ij tool itself: c:\Program Files \Java\ jdk1 .6. 0\db\lib\derby.jar c:\Program Files \Java\ jdk1 .6. 0\db\lib\derbytools.jar After your classpath is configured, start the tool and connect to the example database (toursdb) included with Derby: c:\ >java org.apache.derby.tools.ij ij version 10.2 ij> connect ‘jdbc:derby:c:\Program Files \Java\ jdk1 .6. 0\db\demo\databases\toursdb’; ij> Don’t forget... Enterprise JavaBeans (EJB) is Java s distributed component technology and the cornerstone of the Java 2 Enterprise Edition platform EJB 3.0 represents a significant improvement in the Java language by leveraging the Plain Old Java Object (POJO) paradigm with the Java Persistence API to provide reliable, robust, and transparent object persistence This chapter explains the EJB 3 specification and the Java. .. Developing an Email Client 432 432 4 36 442 449 451 455 460 System Design User Interface 460 461 Summary 471 Chapter 10: EJB 3 and the Java Persistence API New Features Java Persistence API (JPA) 473 474 475 Entities Query Language EntityManager What Are Session Beans? The Demise of Entity Beans? Interceptors EJB 3 and Java Persistence API Web Component Examples Summary 475 475 4 76 480 481 485 517 xi www.it-ebooks.info . 13: Java Security 62 5 Java Cryptography Architecture and Java Cryptography Extension (JCA/JCE) 62 5 JCA Design and Architecture 62 6 Java Cryptography Extension 65 6 Program Security Using JAAS 66 6 User. Identification 66 7 Executing Code with Security Checks 66 8 Authorization 67 2 Summary 67 3 Chapter 14: Packaging and Deploying Your Java Applications 67 5 Examining Java Classpaths 67 5 Investigating. Management Extensions 60 0 Why Is JMX Important? 60 1 The JMX Architecture 60 1 Creating and Managing a Standard MBean 60 2 JMX Management 60 6 Java Messaging Service 61 0 Why Is JMS Important? 61 0 Endpoints: