OSGi in Action: Creating Modular Applications in Java ppt

573 539 0
OSGi in Action: Creating Modular Applications in Java ppt

Đ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

OSGi in Action CREATING MODULAR APPLICATIONS IN JAVA RICHARD S. HALL KARL PAULS STUART McCULLOCH DAVID SAVAGE MANNING Greenwich (74° w. long.) www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 180 Broad Street, Suite 1323 Stamford, CT 06901 Email: orders@manning.com ©2011 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Cynthia Kane 180 Broad Street, Suite 1323 Copyeditor: Tiffany Taylor Stamford, CT 06901 Typesetter: Gordan Salinovic Illustrator: Martin Murtonen Cover designer: Marija Tudor ISBN 9781933988917 Printed in the United States of America 12345678910 –MAL –161514131211 www.it-ebooks.info v brief contents PART 1 INTRODUCING OSGI: MODULARITY, LIFECYCLE, AND SERVICES 1 1 ■ OSGi revealed 3 2 ■ Mastering modularity 24 3 ■ Learning lifecycle 69 4 ■ Studying services 117 5 ■ Delving deeper into modularity 154 PART 2 OSGI IN PRACTICE . 189 6 ■ Moving toward bundles 191 7 ■ Testing applications 230 8 ■ Debugging applications 258 9 ■ Managing bundles 292 10 ■ Managing applications 319 PART 3 ADVANCED TOPICS 343 11 ■ Component models and frameworks 345 12 ■ Advanced component frameworks 373 www.it-ebooks.info BRIEF CONTENTS vi 13 ■ Launching and embedding an OSGi framework 412 14 ■ Securing your applications 438 15 ■ Web applications and web services 477 www.it-ebooks.info vii contents foreword xiv preface xvii acknowledgments xix about this book xx about the authors xxv PART 1 INTRODUCING OSGI: MODULARITY, LIFECYCLE, AND SERVICES 1 1 OSGi revealed 3 1.1 The what and why of OSGi 4 Java’s modularity limitations 5 ■ Can OSGi help you? 8 1.2 An architectural overview of OSGi 9 The OSGi framework 9 ■ Putting it all together 12 1.3 “Hello, world!” examples 12 Module layer example 12 ■ Lifecycle layer example 14 ■ Service layer example 16 ■ Setting the stage 18 www.it-ebooks.info CONTENTS viii 1.4 Putting OSGi in context 19 Java Enterprise Edition 19 ■ Jini 20 ■ NetBeans 20 ■ Java Management Extensions 20 ■ Lightweight containers 21 ■ Java Business Integration 21 ■ JSR 277 21 ■ JSR 294 22 ■ Service Component Architecture 22 ■ .NET 22 1.5 Summary 23 2 Mastering modularity 24 2.1 What is modularity? 25 Modularity vs. object orientation 25 2.2 Why modularize? 27 2.3 Modularizing a simple paint program 28 2.4 Introducing bundles 31 The bundle’s role in physical modularity 32 ■ The bundle’s role in logical modularity 33 2.5 Defining bundles with metadata 34 Human-readable information 35 ■ Bundle identification 36 Code visibility 39 ■ Class-search order 48 2.6 Finalizing the paint program design 50 Improving the paint program’s modularization 51 ■ Launching the new paint program 52 2.7 OSGi dependency resolution 53 Resolving dependencies automatically 53 ■ Ensuring consistency with uses constraints 59 2.8 Reviewing the benefits of the modular paint program 64 2.9 Summary 68 3 Learning lifecycle 69 3.1 Introducing lifecycle management 70 What is lifecycle management? 70 ■ Why lifecycle management? 72 3.2 OSGi bundle lifecycle 72 Introducing lifecycle to the paint program 73 ■ The OSGi framework’s role in the lifecycle 75 ■ The bundle activator manifest entry 76 ■ Introducing the lifecycle API 77 ■ Lifecycle state diagram 83 ■ Bundle cache and framework restarts 84 3.3 Using the lifecycle API in your bundles 85 Configuring bundles 86 ■ Deploying bundles 88 ■ Inspecting framework state 92 ■ Persisting bundle state 93 ■ Listening for events 96 ■ Bundle suicide 99 www.it-ebooks.info CONTENTS ix 3.4 Dynamically extending the paint program 101 3.5 Lifecycle and modularity 108 Resolving bundles 108 ■ Refreshing bundles 110 ■ When updating isn’t updated 114 3.6 Summary 115 4 Studying services 117 4.1 The what, why, and when of services 118 What is a service? 118 ■ Why use services? 119 ■ When to use services 123 ■ When not to use services 124 ■ Still not sure? 124 4.2 OSGi services in action 125 Publishing a service 126 ■ Finding and binding services 128 4.3 Dealing with dynamics 132 Avoiding common pitfalls 133 ■ Listening for services 136 Tracking services 141 4.4 Using services in the paint example 143 Defining a shape service 144 ■ Publishing a shape service 144 Tracking shape services 145 4.5 Relating services to modularity and lifecycle 146 Why can’t I see my service? 147 ■ Can I provide a bundle-specific service? 147 ■ When should I unget a service? 148 ■ When should I unregister my service? 148 ■ Should I bundle interfaces separately? 149 4.6 Standard services 149 Core services 150 ■ Compendium services 151 4.7 Summary 152 5 Delving deeper into modularity 154 5.1 Managing your exports 155 Importing your exports 155 ■ Implicit export attributes 158 Mandatory export attributes 160 ■ Export filtering 161 Duplicate exports 162 5.2 Loosening your imports 164 Optional imports 164 ■ Dynamic imports 165 ■ Optional vs. dynamic imports 166 ■ Logging example 167 5.3 Requiring bundles 171 Declaring bundle dependencies 171 ■ Aggregating split packages 173 ■ Issues with bundle dependencies 176 www.it-ebooks.info CONTENTS x 5.4 Dividing bundles into fragments 177 Understanding fragments 177 ■ Using fragments for localization 180 5.5 Dealing with your environment 183 Requiring execution environments 184 ■ Bundling native libraries 185 5.6 Summary 187 PART 2 OSGI IN PRACTICE 189 6 Moving toward bundles 191 6.1 Turning JARs into bundles 192 Choosing an identity 192 ■ Exporting packages 195 Discovering what to import 199 ■ Embedding vs. importing 203 Adding lifecycle support 204 ■ JAR file to bundle cheat sheet 205 6.2 Splitting an application into bundles 206 Making a mega bundle 206 ■ Slicing code into bundles 216 Loosening things up 221 ■ To bundle or not to bundle? 226 6.3 Summary 229 7 Testing applications 230 7.1 Migrating tests to OSGi 231 In-container testing 231 ■ Bundling tests 232 ■ Covering all the bases 235 7.2 Mocking OSGi 237 Testing expected behavior 237 ■ Mocking in action 238 Mocking unexpected situations 240 ■ Coping with multithreaded tests 241 ■ Exposing race conditions 243 7.3 Advanced OSGi testing 244 OSGi test tools 245 ■ Running tests on multiple frameworks 246 Unit testing 250 ■ Integration testing 251 ■ Management testing 254 7.4 Summary 257 8 Debugging applications 258 8.1 Debugging bundles 259 Debugging in action 261 ■ Making things right with HotSwap 266 www.it-ebooks.info CONTENTS xi 8.2 Solving class-loading issues 271 ClassNotFoundException vs. NoClassDefFoundError 272 ■ Casting problems 274 ■ Using uses constraints 275 ■ Staying clear of Class.forName() 278 ■ Following the Thread Context Class Loader 280 8.3 Tracking down memory leaks 283 Analyzing OSGi heap dumps 283 8.4 Dangling services 287 Finding a dangling service 287 ■ Protecting against dangling services 288 8.5 Summary 290 9 Managing bundles 292 9.1 Versioning packages and bundles 293 Meaningful versioning 293 ■ Package versioning 295 Bundle versioning 297 9.2 Configuring bundles 299 Configuration Admin Service 299 ■ Metatype Service 309 Preferences Service 312 9.3 Starting bundles lazily 314 Understanding activation policies 315 ■ Using activation policies 316 9.4 Summary 317 10 Managing applications 319 10.1 Deploying bundles 320 Introducing management agents 320 ■ OSGi Bundle Repository 321 Deployment Admin 330 10.2 Ordering bundle activation 337 Introducing the Start Level Service 338 ■ Using the Start Level Service 339 10.3 Summary 342 PART 3 ADVANCED TOPICS 343 11 Component models and frameworks 345 11.1 Understanding component orientation 346 What are components? 346 ■ Why do we want components? 348 www.it-ebooks.info CONTENTS xii 11.2 OSGi and components 349 OSGi’s service-oriented component model 349 ■ Improving upon OSGi’s component model 351 ■ Painting with components 355 11.3 Declarative Services 355 Building Declarative Services components 356 ■ Providing services with Declarative Services 357 ■ Consuming services with Declarative Services 359 ■ Declarative Services component lifecycle 364 11.4 Summary 371 12 Advanced component frameworks 373 12.1 Blueprint Container 374 Blueprint architecture 374 ■ Providing services with Blueprint 375 Consuming services with Blueprint 378 ■ Blueprint component lifecycle 382 ■ Advanced Blueprint features 387 12.2 Apache Felix iPOJO 391 Building iPOJO components 392 ■ Providing services with iPOJO 393 Consuming services with iPOJO 395 ■ iPOJO component lifecycle 400 ■ Instantiating components with iPOJO 404 12.3 Mix and match 408 12.4 Summary 411 13 Launching and embedding an OSGi framework 412 13.1 Standard launching and embedding 413 Framework API overview 413 ■ Creating a framework instance 415 ■ Configuring a framework 417 ■ Starting a framework instance 419 ■ Stopping a framework instance 420 13.2 Launching the framework 421 Determining which bundles to install 422 ■ Shutting down cleanly 422 ■ Configuring, creating, and starting the framework 423 Installing the bundles 424 ■ Starting the bundles 424 ■ Starting the main bundle 425 ■ Waiting for shutdown 426 13.3 Embedding the framework 427 Inside vs. outside 427 ■ Who’s in control? 431 ■ Embedded framework example 432 13.4 Summary 437 14 Securing your applications 438 14.1 To secure or not to secure 439 www.it-ebooks.info [...]... bundles, including how to define bundle metadata, package your bundle content, and add lifecycle support We also describe how to go about dividing an application into bundles, demonstrating techniques on an existing open source project Chapter 7 shows how to test bundles and OSGi- based applications We look into running your existing tests in OSGi and mocking OSGi APIs In addition to unit and integration... our OSGi discussion is in the following chapters We also look at the state of modularity support in Java as well as in some related technologies Chapter 2 explores the module layer of the OSGi framework We start with a general discussion of modularity in computing and then continue by describing OSGi s module concept, called a bundle We present OSGi s declarative metadata-based approach for creating. .. at launching the OSGi framework We describe the standard approach for configuring and creating OSGi frameworks We also show how you can use the standard API to embed an OSGi framework into an existing application Chapter 14 delves into operating OSGi in a secure environment We describe the issues involved and approaches to alleviating them We explain how OSGi extends the standard Java security architecture... public interface in one package, a private implementation in another, and a main class in yet another Listing 1.1 Example of the limitations of Java s object-orientated encapsulation Greeting .java package org.foo.hello; public interface Greeting { void sayHello(); } B package org.foo.hello.impl; import org.foo.hello.Greeting; www.it-ebooks.info Simple interface GreetingImpl .java 6 CHAPTER 1 OSGi revealed... class GreetingImpl implements Greeting { final String m_name; C public GreetingImpl(String name) { m_name = name; } Interface implementation public void sayHello() { System.out.println("Hello, " + m_name + "!"); } } Main .java package org.foo.hello.main; import org.foo.hello.Greeting; import org.foo.hello.impl.GreetingImpl; public class Main { public static void main(String[] args) { Greeting greet =... OSGi technology www.it-ebooks.info www.it-ebooks.info OSGi revealed This chapter covers ■ Understanding Java s built -in support for modularity ■ Introducing OSGi technology and how it improves Java modularity ■ Positioning OSGi with respect to other technologies The Java platform is an unqualified success story It’s used to develop applications for everything from small mobile devices to massive enterprise... lack of modularization in Java These include the following: ■ ■ ■ Programming practices to capture logical structure Tricks with multiple class loaders Serialization between in- process components 3 www.it-ebooks.info www.it-ebooks.info 5 The what and why of OSGi improve maintainability by enforcing the logical module boundaries; we’ll discuss more modularity details in chapter 2 The notion of modularity... naturally into three parts: 1 2 3 Explaining the core OSGi specification Describing how to work with the specification in practice Introducing advanced OSGi- related topics xx www.it-ebooks.info ABOUT THIS BOOK xxi In part 1 of the book, we focus on explaining the most common aspects of the OSGi core specification from the user’s perspective We introduce OSGi according to its three-layer architecture:... been designing and building OSGi applications since 2005 in many different areas including build tools, component models, data persistence, desktop UIs, management, messaging, provisioning, resolvers, and RPC He contributes to the Apache Felix project especially in the area of development tooling via the Sigil subproject He is also directly involved in developing specifications for the OSGi Alliance... presenting command or shell interaction, normal Courier typeface is used to indicate program output, while bold is used to indicate user input Code annotations accompany many of the listings, highlighting important concepts In some cases, numbered bullets link to explanations that follow the listing Author Online Purchase of OSGi in Action includes free access to a private web forum run by Manning Publications . Testing applications 230 7.1 Migrating tests to OSGi 231 In- container testing 231 ■ Bundling tests 232 ■ Covering all the bases 235 7.2 Mocking OSGi. We look into running your existing tests in OSGi and mocking OSGi APIs. In addition to unit and integration testing, we discuss management testing and

Ngày đăng: 15/03/2014, 11:20

Từ khóa liên quan

Mục lục

  • Team rebOOk

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

  • Đang cập nhật ...

Tài liệu liên quan