OSGi in Action CREATING MODULAR APPLICATIONS IN JAVA RICHARD S. HALL KARL PAULS STUART McCULLOCH DAVID SAVAGE 11 MANNING Greenwich (74 w. long.)
contents foreword xiv preface xvii acknowledgments xix about this book xx about the authors xxv PART 1 INTRODUCING OSGI: MODULARITY, LIFECYCLE, AND SERVICES 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
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 JSR277 21 JSR294 22 Service Component Architecture 22.NET 22 1.5 Summary 23 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 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
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 A 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 1unget a service? 148 When should I unregister my service? 148 Should I bundle interfaces separately? 149 5 Delving 4.6 Standard services 149 Core services 150 Compendium services 151 4.7 Summary 152 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
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 Moving toward bundles 191 6.1 TurningJARs 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 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 Debugging applications 258 8.1 Debugging bundles 259 Debugging in action 261 Making things right with HotSwap 266
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 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 Metaiype Service 309 Preferences Service 312 9.3 Starting bundles lazily 314 Understanding activation policies 315 Using activation policies 316 9.4 Summary 317 ~f/i Managing applications 319 JL\J 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 Component models and frameworks 345 11.1 Understanding component orientation 346 What are components? 346 Why do we want components? 348
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 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 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 Securing your applications 438 14.1 To secure or not to secure 439
14.2 Security:justdoit 440 Java and OSGi security 440 14.3 OSGi-specific permissions 444 PackagePermission 444 BundlePermission 445 Admin- Permission 446 ServicePermission 447 Relative file permissions 448 14.4 Managing permissions with Conditional Permission Admin 449 Conditional permissions 449 Introducing the Conditional Permission Admin Service 450 Bundle location condition 451 Using ConditionalPermissionAdmin 452 Implementing a policy-file reader 456 14.5 Digitally signed bundles 457 Learning the terminology 458 Creating certificates and signing bundles 458 BundleSignerCondition 461 14.6 Local permissions 464 14.7 Advanced permission management 465 Custom conditions overview 465 Date-based condition 466 User-input condition 467 14.8 Bringing it all back home 471 14.9 Summary 475 Web applications and web services 477 15.1 Creating web applications 478 Using the HTTP Service specification 479 Using the Web Applications specification 488 Standard WARs: the Web URL Handler 492 15.2 Providing and consuming web services 493 Providing a web service 494 Consuming a web service 499 Distributing services 502 15.3 Summary 510 appendix A Building bundles 513 appendix B OSGi standard services 528 index 531