16
Practical WebObjects CHARLES HILL AND SACHA MALLAIS APress Media, LLC

Practical WebObjects

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Practical WebObjects CHARLES HILL AND SACHA MALLAIS

APress Media, LLC

Practical WebObjects Copyright © 2004 by Charles Hill and Sacha Mallais

Originally published by Apress in 2004

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.

ISBN 978-1-59059-296-0 ISBN 978-1-4302-0751-1 (eBook) DOI 10.1007/978-1-4302-0751-1

Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

Lead Editor: Steve Anglin Technical Reviewers: Shehryar Khan and Gary Teter Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, John Franklin,

Jason Gilmore, Chris Mills, Steve Rycroft, Dominic Shakeshaft, Jim Sumser, Karen Watterson, Gavin Wray, John Zukowski

Project Manager: Tracy Brown Collins Copy Edit Manager: Nicole LeClerc Copy Editor: Suzanne Go raj Production Manager: Kari Brooks Production Editor: Ellie Fountain Interior Book Designer: Diana Van Winkle, Van Winkle Design Group Compositor: Diana Van Winkle, Van Winkle Design Group Proofreader: Gregory Teague Indexer: Michael Brinkman Artist: Diana Van Winkle, Van Winkle Design Group Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski

The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.

To my parents Lome and Rachel and to my wife Laksanavadee, for their love and support.

-Chuck

To my family and friends for their love and support, to my colleagues for their good work and expertise, and to the entire WebObjects community

for helping keep WebObjects alive and ticking. -Sacha

Contents at a Glance

About the Authors ... ..... . ........................................................ xv About the Technical Reviewers ... ... . . .. . .......................................... xvii Acknowledgments ...... . ... ..... ..... . ........................................... xix

CHAPTER 1 CHAPTER 2 CHAPTER 3 CHAPTER 4 CHAPTER 5 CHAPTER 6 CHAPTER 7 CHAPTER 8 CHAPTER 9 CHAPTER 10 CHAPTER 11

Making Your Code Better ...... . ......................... .. ...... 1

EO Modeling Techniques ................ . ...................... 31

Managing the Object Graph ... .. .. ............... . ... .... .... .. 61

Authentication and Security .................................... 99

Input and State Validation of Enterprise Objects ..... .. ....... . 139

The Secret Life of Components ................................ 159

Components and Elements ........ . ......... . ................. 187

Localization ... .......................................... .... . 221

Copying Enterprise Objects ................................... 237

WebObjects in a J2EE World .................................. 211

XML and WebObjects .............................. .... ..... .. 301

APPENDIX Setting Up the Code . . ........... . ..... . .. . ... ... .. . . ... .... .. 331

INDEX ............. .. ...... .. .................................... .. ............ 353

v

Contents

About the Authors .................................. . .............................. xv About the Technical Reviewers ..................................................... xvii Acknowledgments ................................................................ xix

HAPTER 1 Making Your Code Better .................................... 1

Eclipse and WOLips/WOProject ................... ... ........ . ...... 1 What Is Eclipse? ............................................. 1 What Is WOLips/WOProject? ... .. ............................. 3 Why Use Eclipse and WOLips/WOProject? ....................... 3 Issues with Eclipse .......................................... 4 Pros and Cons .............................................. 4

Automated UnitTesting ........................................... 5 The Case for Automated Unit Testing ........................... 5 JUnit: Java Unit Testing ....................................... 6 JUnit and EOF: DbUnit ........................................ 9

Design by Contract .............................................. 11 What Are Contracts? ................... . .................... 12 DBC and Unit Testing ........................................ 14 What Design by Contract Is Not. ..... ..... .................... 14 More on Design by Contract. ................................. 15 Why Contract Your Code? ............. . ...................... 15 Java and DBC .............................................. 17

Debugging Techniques: Logging in WebObjects .................. . ... 17 Using Subclasses ........................................... 17 Delegates ................................................. 19 Using Notifications ..................................... .. ... 19 NSLog and Other Logging Tools .............................. 21 Snooping on EOF ........................................... 24 Digging Deeper: Seeing the Fetched Data ...................... 25 The Last Log Entry .......................................... 27

Creating Java Classes from EOModels ............................. 27 The Generation Gap Pattern ..... . .... . ..... .. ................ 27 The Trusty EOGenerator ..................................... 27 Alternatives to EOGenerator ... . . ... ..................... ..... 29

Summary ...................................................... 30 vii

viii

EO Modeling Techniques ................................... 31

Inheritance ..................................................... 31 Inheritance Implementation Details ........................... 31 Inheritance and Primary Keys ................................ 32 Inheritance and the Overly Required Attribute .................. 33 Inheritance and the Impossible Constraint ..................... 33 Inheritance and Multiple Models .............................. 34 Inheritance, Multiple Models, and SQL Generation .............. 34

Relationships ................................................... 35 Optimizing Relationships with Batch Faulting ................... 35 One-Sided Relationships .................................... 36 AddObjectsToBothSidesOfRelationshipWithKeyO ................ 37

Attribute Value Types ............................................. 38 Noninteger Numbers ........................................ 38 Custom Types .............................................. 41 Handling LOB Data Efficiently ................................ 44

Prototypes ...................................................... 45 JDBC Prototypes (Old vs. New Prototypes) ..................... 46 Strategies for Managing Prototypes ........................... 47 One Last Consideration: The Connection Dictionary ............. 53

Debugging JDBC Connection Problems ............................. 55 Understanding the Source of the Problem ...................... 55 Finding and Fixing the Problem ............................... 57

EO Modeling with Eclipse ......................................... 60 Summary ...................................................... 60

Managing the Object Graph ................................ 61

Understanding the Object Graph ................................... 61

Viewing the Object Graph in Context .......................... 62 Understanding the Varieties of Editing Contexts ................. 64

Rules of Engagement ............................................ 65

Seeing State Changes in the Object Graph .......................... 67

Introducing the EOStateTransition Interface .................... 69 The CooperatingEditingContext ............................... 10

Debugging Object Graph Problems ................................ 78 Finding and Preventing Cross-Editing Context Relationships ...... 78 Defeating Referential Integrity Problems ....................... 80

Locking, Concurrency, and EOF ................................... 83 What Needs to Be Locked? .................................. 84

CONTENTS lx

How to Lock Correctly ....................................... 84 Debugging Locking Problems ................................ 91

Managing Object Freshness ...................................... 94 Fetching Fresh Objects ...................................... 94 Using Snapshot Timestamps to Ensure Fresh Data .............. 95 Refreshing and Relationships ................................ 96 Freshening Data Through Distributed Change Notifications ....... 97

Summary ...................................................... 97

cHAPTER 4 Authentication and Security ............................... 99

Beginning with the Basics ........................................ 99 The Technical Details of Secure HTIP ......................... 99 Practical Use of HTIPS ..................................... 1 oo Using HTIPS with WOHyperlink .............................. 100 Using HTIPS with WOForm ................................. 1 02

Authenticating Users ............................................ 105 Introducing the AbstractUser ................................ 105 Introducing the SimpleUser ................................. 1 08 Tracking the User After Authentication ........................ 108 Producing a Login Form .................................... 11 o Handling Logout ........................................... 111

Advanced Authentication Methods ................................ 112 Obscuring the Password .................................... 113 Encrypting the Password ................................... 115 Using LDAP for Authentication ............................... 119 Using Kerberos for Authentication ............................ 122

Advanced Login Topics .......................................... 126 Just in Time Login ......................................... 126 Automating User Login ..................................... 131

Advanced Security Topics ....................................... 135 Preventing Session Hijacking ................................ 135 Preventing Direct Component Access ........................ 137

Summary ..................................................... 138

HAPTEIR 5 Input and State Validation of Enterprise Objects ........ 139

Validation Basics ............................................... 139 NSValidation .............................................. 139 EOValidation .............................................. 141

X

Getting into the Details .......................................... 141 Validation During Take Values ............................... 142 Validation on saveChanges ................................. 144 Formatters ................................................ 145

Limitations of WebObjects Validation .............................. 145 Values Are Not Assigned If Not Valid .......................... 145 Only One Validation Error per Attribute ........................ 148 No Immediate Validation .................................... 148 Making Changes While Validating an EO ...................... 149 Lack of Customization of Validation Messages ................. 150 Lack of Localization of Validation Messages ................... 150 Missing Database Validations ............................... 151

The Practical Solution ........................................... 152 The Classes ............................................... 152 Getting Started ............................................ 153 Using Message Key Paths .................................. 153 Message Key Path Search Order. ............................ 155 Messages and Variable Substitution .......................... 155 Validation Methods ........................................ 156

Summary ..................................................... 157

The Secret Life of Components ........................... 159

The Hypertext Transfer Protocol .................................. 159 Spying on HTIP ........................................... 161

The Request-Response Loop, Briefly ............................ 164 The Role of the WebObjects Frameworks ..................... 164 The Big Picture ............................................ 165

The Request-Response Loop, Deeply ............................ 166 The Beginning: The Request Is Made ......................... 167 The Web Server Receives the Request. ....................... 168 The woadaptor and wotaskd Forward the Request ............. 168

The Application Receives the Request ........................ 169 The Application Forwards the Request to the Request Handler ... 169 The Request Handler Forwards the Request to the Component .. 170 The Main Event. ........................................... 111 Detour: ComponentTemplates .............................. 173 The Take Values Phase ..................................... 173 Performing the Requested Action ............................ 179 Generating the Response ................................... 181 The End of the World As WO Knows It ........................ 183

'CONTENTS xl

... And Beyond ............................................ 183 Complications ............................................. 184

Summary ..................................................... 185

CHAPTER 7 Components and Elements ................................ 187

The Case of the Missing Element ................................. 187 Request-Response and Component Debugging .............. 188 The Golden Rule ........................................... 190 The Mystery of the Missing Submit .......................... 191

Dynamic Elements ............................................. 193 Dynamic Elements, Components, and Associations ............ 193 How Dynamic Elements Work ............................... 194 An Example Dynamic Element. .............................. 195 Replacing the WebObjects Dynamic Elements ................. 199

Mysteries of Binding Synchronization ............................. 201 Timing ................................................... 201 A Binding Synchronization Example .......................... 201 Binding Instance Variables and Methods ...................... 205 Debugging Binding Synchronization .......................... 205 Manual Synchronization .................................... 206 The Carat Notation ......................................... 207

Stateless Components .......................................... 207 The Mapped List Popup Stateless Component ................. 209

Debugging and Optimization with WOEvent ........................ 212 Preparing for WOEvent Logging .............................. 213 Displaying the Logged Events ............................... 214 Some Practice Reading the Event Log ........................ 214 Creating Custom Events .................................... 215

Summary ..................................................... 219

cHAPTER a Localization ................................................ 221

Parlez-vous Anglais? ............................................ 221 Text Encoding ............................................. 222 Localize Early ............................................. 223

Detecting User Language Preferences ............................ 223 Encoding in Responses ......................................... 226 Encoding in Requests ........................................... 228 Database Localization Issues .................................... 229

Sorting It All Out ........................................... 230

xll CONTENTS

The Biggest Problem of Our Time (Zone) ........................... 230 Standard WebObjects Localization ................................ 231 An Alternate Localization Strategy ................................ 232 Strings, All Shapes and Sizes .................................... 235 Debugging Issues .............................................. 236 Summary ..................................................... 236

Copying Enterprise Objects ............................... 237

Understanding the Problem ...................................... 237 Types of Copies ................................................ 238 Naive Copy .................................................... 239 EOEnterpriseObject Metadata .................................... 240

EOAttribute ............................................... 240 EORelationship ............................................ 240 EOEntity .................................................. 240 EOModel ................................................. 241 EOModeiGroup ............................................ 241 EOCiassDescription ........................................ 241

EOModei-Based Copy ........................................... 241 The EOCopyable Default Implementation ..................... 248 Making Use of the Default Implementation .................... 250 The EOCopyable Utility Methods ............................. 250 Tweaking the Copy Process ................................. 264

Object Migration ............................................... 266 Using javaeoutil ........................................... 266 Using BulkMover .......................................... 269 Using Dedicated Migration Tool .............................. 269

Summary ..................................................... 270

WebObjects in a J2EE World .............................. 211

What Is J2EE and Why Is It Important? ............................ 271 Servlets .................................................. 272 JavaServer Pages ......................................... 273 JSP Tag Libraries .......................................... 274 Enterprise JavaBeans ...................................... 27 4

Understanding WebObjects Servlet Deployment .................... 275 Single Servlet Deployment vs. Standard Deployment ........... 276

Class Loading ............................................. 276 Resource Location ......................................... 277

CONTENTS xill

Differences Between WebObjects Applications and WebObjects Servlets ................................... 278 Multithreaded Servlet Containers ............................ 279 EAR, WAR, WOA ........................................... 280 Tips and Tricks ............................................ 283

Using WebObjects with JavaServer Pages ......................... 284

WOComponent Integration with JSP Example ................. 284

Deploying the Sample Application ........................... 291 JSP/WO Best Practices ..................................... 297

Summary ..................................................... 300

R 11 ...................................... 301

What About XML? .............................................. 301

Document Type Definitions .................................. 303

XML Schema .............................................. 304

XSL and Related Technologies ............................... 304

XML Parsers ................................................... 305

DOM Parsers .............................................. 305

SAX Parsers .............................................. 306

Other Parsers ............................................. 306 XML and Object Serialization ..................................... 307

WebObjects Application Support ............................. 307

Sample Application ........................................ 308 Serializing EO Data into XML ................................ 308

Step 2: Serializing the Cart .................................. 311

Step 3: XML Transformation ................................. 314

Step 4: HTML Transformation ............................... 321 Step 5: PDF Generation ..................................... 324 Step 6: Deserialization of XML Back to EO .................... 329

Summary ..................................................... 330

DIX ....................................... 331

Installing FrontBase ............................................. 331

Installing Eclipse ............................................... 331

A Word on Eclipse Directories ............................... 332

Patching Ant ................................................... 332 Installing WOLips ............................................... 333

Installing EOGenerator .......................................... 333

xlv CONTENTS

Getting Ready to Install the Code ................................ 334 Get the Practical WebObjects File ............................ 334 Install the Jass4Eclipse Plug-in .............................. 334 Configuring Eclipse ........................................ 335

Installing the Practical WebObjects Frameworks .................... 335 Importing the Projects ...................................... 335 Setting up the Database .................................... 336 Run Unit Tests to Verify Installation ........................... 337

Setup for Each Chapter ......................................... 339 Chapter 1 ................................................. 339 Chapter 2 ................................................. 339 Chapter 3 ................................................. 340 Chapter 4 ................................................. 340 Chapter 5 ................................................. 349 Chapter 6 ................................................. 349 Chapter 7 ................................................. 350 Chapter 8 ................................................. 350 Chapter 9 ................................................. 350 Chapter 1 0 ............................................... 350 Chapter 11 ............................................... 351

IND ....................................................................... 353

About the Authors

SACHA MALLAIS has been the president of Global Village Consulting, Inc. since it was founded in 1995. He has been usingWebObjects since 1998 and Java since 1999. He received his education from the University of British Columbia, where he primarily studied database design and networking. He lives in British Columbia, where the constant rain gives him a ready-made excuse for playing on his computer instead of going outside.

HUCK HILL was raised in Manitoba where he learned programming in the days of punched cards. He attended Brandon University, majoring in both Mathematics and Computer Science with a minor in Physics to fill his idle hours. Upon graduation, realizing that a career in COBOL beckoned, he set off to tour the world. After wandering through Africa, India, and Central America, he ended up living in Thailand and Taiwan for almost seven years. He now makes his home in the Canadian Wet Coast city of Vancouver, where Global Village Consulting employs him. On those rare

occasions when he is not sitting in front of a computer, he collects and restores old tools and tries to fit in the occasional bit of woodworking.

XV

About the Technical ·Reviewers

SHEHRYAR KHAN is a Senior Architect at Ubermind Inc. He's been doingWebObjects develop­ment since the days ofWebObjects 2.0, and did EOF/AppKit-based client server application development before that. Shehryar has successfully led teams ofWebObjects developers on large projects for the U.S. government and the financial and telecommunications industries.

ARY TETER is the architect and lead developer ofWireHose Server, the leading personaliza­tion and smart metadata framework for WebObjects. Formerly CEO and CTO of Bulldog Beach Interactive, he is now focused on refining Wire Hose and creating other technologies to make the world a better place.

XVII

About the Authors

Writing a book is hard work. We've read that many times in the front of other books and we now know it to be true. It's certainly not a job for one or even two people. Writing a book is a collective effort requiring the professional skills of many people. This book would not have been written without the support and assistance of these people.

We will start by thanking Lucas Rockwell of U C Berkeley, who first suggested to Apress that they might want to publish a book on WebObjects. Lucas went on to name names of likely authors, and we were tapped to write the book.

Although their names do not appear on the cover, two additional authors had a hand in three chapters of the book. We would like to thank our two collaborators for sharing their unique WebObjects knowledge and experience with our readers and with us.

Dov Rosenberg ([email protected], http: I lwww. convi veon. com) wrote chapters 10 and 11 on J2EE and XML. Dov is the president of Conviveon Corporation, an Orlando, Florida-based company. Conviveon sells a WebObjects-based enterprise content management tool called SiteConnect Server, which heavily leverages WebObjects/EOF, XML, and JSP Servlet technology. He also helped design and develop the American Automobile Association's web site, one of the largest non-Apple WebObjects applications on the Internet. Since he discovered WebObjects/Java in 1997 he can't bring himself to suffer lesser technolo­gies. He is married to Cindy (his very understanding wife of nearly 20 years) and has two teenagers, Andrew and Molly.

Pierre Frisch ([email protected]) wrote Chapter 8 on localization and internationaliza­tion. Pierre, an Apple developer since the beginning of Apple's developer program, founded sPearWay Limited (http: I lwww. spearway. com) in 1992 to offer comprehensive solutions to serve businesses. He developed the first commercial WebObjects application for luggage lockers in 1997, localized in 37languages. The most recent sPearWay product is sPearCat, a stand-alone, easy-to-use, multilingual Web catalogue-building tool.

Special thanks are due to our two technical reviewers, Gary Teter and Shehryar Khan. Gary Teter is the genius behind the WebObjects-based WrreHose platform (http: I lwww. wirehose. com) for building web applications. WrreHose can personalize access to any kind of enterprise object; it's a next -generation portal and smart metadata API that's light years ahead of anything offered by Sun, IBM, or BEA. Gary's knowledge ofWebObjects is enviable. Shehryar Khan is a senior architect at Uberrnind (http: I lwww. ubermind. com/), a WebObjects and J2EE consulting house. If you are looking for WebObjects training, Uberrnind offers an excellent WebObjects course. Shehryar was tireless in his suggestions of ways to improve the text. His knowledge of J2EE technologies brought a more informed view to comparisons of those technologies.

Shehryar and Gary's WebObjects expertise benefited the accuracy and quality of this book considerably. Any remaining mistakes are ours alone (and probably due to our not listening to them). Gentlemen, we thank you.

The wonderful people at Apress made writing this book as pleasurable as possible. Tracy Brown Collins worked hard to keep us on some semblance of a schedule despite our best attempts to keep writing forever. If it wasn't for Tracy, we'd probably still be typing and editing. xlx

XX ~ACKNOWLEDGMENTS

Thanks to our editor Steve Anglin for supporting our many, many changes as we tried to make the best book we could. Thanks also to Suzanne Goraj, Ellie Fountain, Beth Christmas, and all the unsung heroes who toiled away in the background.

Thanks to Leon Salvail, CEO of Global Village Consulting, for supporting our efforts in writing this book. We also thank Josephine Garcia and Andrew "Muskie" McKay for helping us better understand some of the difficulties in mastering WebObjects.

Chuck would like to thank Jeff Martirano for helping him to truly appreciate the Eclipse IDE, and Sacha would like to thank Steven K. Besler for introducing him to WebObjects in the first place.

Finally, we would like to thank our family and friends for tolerating our absences and preoccupation while writing this book. We promise that we won't do it again!