MANNING Richard S. Hall Karl Pauls Stuart McCulloch David Savage FOREWORD BY PETER KRIENS C reating m odular a pplications in J ava OSGi in Action OSGi in Action CREATING MODULAR APPLICATIONS IN JAVA RICHARD S. HALL KARL PAULS STUART McCULLOCH DAVID SAVAGE MANNING Greenwich (74° w. long.) Download from Wow! eBook <www.wowebook.com> 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 Download from Wow! eBook <www.wowebook.com> 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 Download from Wow! eBook <www.wowebook.com> BRIEF CONTENTS vi 13 ■ Launching and embedding an OSGi framework 412 14 ■ Securing your applications 438 15 ■ Web applications and web services 477 Download from Wow! eBook <www.wowebook.com> 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 Download from Wow! eBook <www.wowebook.com> 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 Download from Wow! eBook <www.wowebook.com> 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 Download from Wow! eBook <www.wowebook.com> [...]... 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 7 Summary 229 Testing applications 7.1 230 Migrating tests to OSGi In- container testing the bases 235 7.2 Mocking OSGi 231 231 Bundling tests... Covering all 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 8 Summary 257 Debugging applications 8.1 ■ Debugging bundles Debugging... 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 framework... 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 testing, we discuss management testing and explore some tools to help you along the way Chapter 8 follows testing by describing how to debug your bundles We look into simple, command-line debugging as... dumps 8.4 Dangling services 9 Summary 283 287 Finding a dangling service services 288 8.5 283 287 ■ 290 Managing bundles 292 9.1 Versioning packages and bundles Meaningful versioning 293 Bundle versioning 297 9.2 Configuring bundles ■ 9.3 Package versioning Starting bundles lazily 299 Summary Metatype Service ■ 315 Deploying bundles Ordering bundle activation Summary Using activation 320 ■ OSGi Bundle... CHAPTER 1 OSGi revealed public 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[]... Sometimes I think he physically feels pain if we have to compromise on a dirty feature As an invited OSGi researcher, he has became one of the key people behind the specifications, making sure we don’t bloat the framework and always follow our principles When Manning sent a flattering email proposing an OSGi in Action book to the key OSGi people, Richard was among them This email triggered intense discussions... 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 Download from Wow! eBook 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:... Providing services with iPOJO Consuming services with iPOJO 395 iPOJO component lifecycle 400 Instantiating components with iPOJO 404 ■ 393 ■ ■ 12.3 12.4 13 Mix and match 408 Summary 411 Launching and embedding an OSGi framework 13.1 412 Standard launching and embedding 413 Framework API overview 413 Creating a framework instance 415 Configuring a framework 417 Starting a framework instance 419 Stopping... 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 . 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. 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