COMPONENT-ORIENTED PROGRAMMING
COMPONENT-ORIENTED PROGRAMMING ANDY JU AN WANG KAI QIAN Southern Polytechnic State University Marietta, Georgia A JOHN WILEY & SONS, INC., PUBLICATION
Copyright 2005 by John Wiley & Sons, Inc. All rights reserved. Published by John Wiley & Sons, Inc., Hoboken, New Jersey. 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, or on the web at www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008. 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 merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives 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 limited to special, incidental, consequential, or other damages. For general information on our other products and services please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993 or fax 317-572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print, however, may not be available in electronic format. Library of Congress Cataloging-in-Publication Data: Wang, Andy Ju An. Component-oriented programming / Andy Ju An Wang & Kai Qian. p. cm. A Wiley-Interscience publication. Includes bibliographical references. ISBN 0-471-64446-3 1. Component software. 2. Computer programming. I. Qian, Kai. II. Title. QA76.76.C66W36 2005 005.3 dc22 2004015681 Printed in the United States of America. 10987654321
CONTENTS Preface ix 1 Introduction 1 1.1 What Is COP? 1 1.2 Why Is COP Important? 3 1.3 What Is a Component? 5 1.4 Hardware Components and Software Components, 7 1.5 From OOP to COP, 8 1.6 Component-Based Software Engineering, 10 1.7 Summary, 11 1.8 Self-Review Questions, 12 1.9 Exercises, 14 References, 14 2 Theory of Components 16 2.1 Principles of COP, 16 2.2 Infrastructures of COP, 18 2.3 Component Models, 20 2.4 Connection Models, 21 2.5 Deployment Models, 26 2.6 Unifying Component Technologies, 26 2.7 Summary, 32 2.8 Self-Review Questions, 33 2.9 Exercises, 35 References, 35 3 COP with JavaBeans 37 3.1 Overview of JavaBeans Technology, 37 3.2 Component Model of JavaBeans, 38 v
vi CONTENTS 3.3 Connection Model of JavaBeans, 64 3.4 Deployment Model of JavaBeans, 72 3.5 Examples and Lab Practice, 76 3.6 Summary, 83 3.7 Self-Review Questions, 85 3.8 Exercises, 86 References, 87 4 Enterprise JavaBeans Components 88 4.1 EJB Architecture, 88 4.2 Component Model of EJB, 90 4.3 Connection Model of EJB, 100 4.4 Deployment Model of EJB, 104 4.5 Examples and Lab Practice, 105 4.6 Summary, 142 4.7 Self-Review Questions, 143 4.8 Exercises, 143 4.9 Programming Exercises, 144 References, 145 5 CORBA Components 146 5.1 CORBA Component Infrastructure, 146 5.2 CORBA Component Model (CCM), 149 5.3 Connection Model of CORBA and CCM, 173 5.4 Deployment Model of CORBA and CCM, 176 5.5 Examples and Lab Practice, 184 5.6 Summary, 189 5.7 Self-Review Questions, 190 5.8 Exercises, 191 5.9 Programming Exercises, 192 References, 192 6.NET Components 194 6.1.NET Framework, 194 6.2 Component Model of.net, 198 6.3 Connection Model of.net, 204 6.4.NET Component Deployments, 212 6.5 Visual Studio.NET, 215 6.6 Examples and Lab Practice, 224 6.7 Summary, 234 6.8 Self-Review Questions, 234 6.9 Exercises, 235 6.10 Programming Exercises, 236 References, 237 7 COP with OSGi Components 238 7.1 Overview of OSGi Technology, 238 7.2 Component Model of OSGi, 239
CONTENTS vii 7.3 Connection Model of OSGi, 247 7.4 Deployment Model of OSGi, 250 7.5 Examples and Lab Practice, 252 7.6 Summary, 261 7.7 Self-Review Questions, 263 7.8 Exercises, 265 References, 266 8 Web Services Components 267 8.1 Web Services Framework, 267 8.2 Component Model of Web Services, 281 8.3 Connection Model of Web Services, 293 8.4 Web Services Component Deployment, 297 8.5 Examples and Lab Practice, 299 8.6 Summary, 311 8.7 Self-Review Questions, 312 8.8 Exercises, 313 8.9 Programming Exercises, 314 References, 314 Index 315
PREFACE This book is about component-oriented programming (COP for short), a new programming paradigm beyond object-oriented programming. Component-oriented programming offers higher reusability and better modular structure with greater flexibility, compared with object-oriented programming or library-based programming approaches. This book discusses principles of component-oriented programming and provides a unified component infrastructure to describe different component technologies. Hands-on programming practice is emphasized in this book. Readers of this book will learn how to develop reusable software components and how to build a software system out of prebuilt software components. Both authors have been teaching Component-Based Software Development (CBSD) to graduate and undergraduate students for several years. We strongly believe that we need a textbook emphasizing the programming aspect of CBSD or CBSE (Component- Based Software Engineering). Thus, students majoring in computer science, software engineering, or related areas can take this course as one of the major concentrations following OOP (Object-Oriented Programming) and elementary software engineering courses. CBSD is not just a philosophy, a theory, or some standard. It represents a new programming paradigm beyond OOP and library-based programming paradigms this is partially why we chose the title Component Oriented Programming. In a librarybased programming paradigm, multiple layers of software abstractions stack up one on top of the other, whereas in a component-based model, multiple software components plug in side-by-side with one another. In a library-based model, once a module is compiled into the software, it is always available at runtime. In a component-based model, a component can come and go dynamically at runtime. In a library-based paradigm, one most likely must rebuild and repackage the entire set of libraries to fix bugs and add features at compile-time. In a component-based paradigm, new components can be added or existing components can be updated in an incremental way at runtime. In a library-based paradigm, changes made to public interfaces have less impact because one has to rebuild the software in its entirety. In a component-based paradigm, the cost of ix
x PREFACE redoing public interfaces can be prohibitive because other components may have relied on them at runtime. We can make a long list of differences between traditional, library-based programming and new, component-based programming. Many software engineers have not realized or appreciated this shift in programming paradigms. As many experts have predicted, CBSE and component-oriented programming will be the future of software development following structured programming, library-based programming, and object-oriented programming. It is essential to make students realize that componentbased software development requires more discipline on the part of developers during the design and programming stages to provide more stability, extensibility, and flexibility. Such disciplined activity and techniques have to be established through well-developed training and practice following component-based software development principles. However, books published on CBSE are not suited to provide such training. Some are highly theoretical, targeting academic researchers, while others emphasize software life cycle and project management; yet others are too specific for particular component technology. In a typical computer science or software engineering curriculum setting in colleges and universities, the course for component-based software development is usually designed for students with previous training on OOP and fundamental software engineering principles. Hence, it is challenging to shift students from the mindset of library-based programming and OOP to the mindset of component-oriented programming. Both authors have followed a programming-centered approach to teaching the Component-Based Software Development course, which has turned out to be successful in emphasizing programming methodology and programming practice in the CBSE context. The programming paradigm is changing from object-oriented programming to component-oriented programming. We predict that in the next few years, component-based development will be the mainstream practice in software engineering. This book will provide in-depth knowledge about component-based development. It intends to introduce component-oriented programming to college students majoring in computer science, software engineering, information technology and to professional software engineers working in the industry as well. We assume that the readers of the book are familiar with at least one high-level programming language. The emphasis will be on component-oriented programming principles, methods, and techniques. Any reader who is interested in component-based software engineering in general will find useful information in this book. Readers can expect to gain the comprehensive knowledge necessary for component software development. Three efforts have been made to help readers understand and master componentoriented programming techniques. First, a unified component infrastructure is used for all component technologies covered in this book, namely, JavaBeans, EJB, COM/DCOM/.NET, CORBA, OSGi, and Web Services. This common component infrastructure contains a component model, a connection model, and a deployment model. It represents standard conventions for component specification, interfaces, composing connectors, architecture, packaging, and deployment. It also helps to apply the component principles to practical component software development following a well-defined format. The unified component infrastructure also serves as a pedagogical tool to keep readers concentrating on technical essentials rather than being distracted by those technical details.
PREFACE xi Second, we have paid careful attention to the pedagogical organization of the book. For instance, we put forward briefly at the beginning of each chapter its main objectives, and we explain all potential tricky points in detail in the process of presenting the content. At the end, a brief summary is provided to highlight the key points of the chapter. Self-Review Questions are designed for readers to test their understanding of the materials in each chapter, and key solutions for these questions are supplied. Exercises and Lab Practice are provided at the end of each chapter, serving as an integral part of the book. Third, a supplementary Web site (ftp://ftp.wiley.com/public/sci tech med/component/) is provided containing all examples, laboratory project guidelines, software packages, and developing tools used in the book. Overview of Chapters Chapter 1 is a brief introduction to component-oriented programming and the benefits of component-based software development. Chapter 2 discusses the principles of component-oriented programming, and provides a unified framework for describing different component technologies. Chapter 3 introduces the component infrastructure of JavaBeans. Both BDK (Bean Development Kit) and Bean Builder are discussed. Chapter 4 covers J2EE framework and EJB component architecture. Chapter 5 discusses CORBA (Common Object Request Broker Architecture) component infrastructure. Chapter 6 introduces Microsoft.NET framework and Visual Studio.NET component technology. Chapter 7 covers the component technology proposed by OSGi (Open Service Gateway initiative). Chapter 8 investigates Web services component technology. Acknowledgments Thanks to Cassie Craig of John Wiley & Sons, Inc., for her incredibly positive support in the writing of this text. We want to thank all the other people who helped improve the text, from the reviewers and editors to those who sent suggestions and feedback in the early drafts of this book.