MANNING Holly Cummins Timothy Ward www.it-ebooks.info Enterprise OSGi in Action www.it-ebooks.info www.it-ebooks.info Enterprise OSGi in Action WITH EXAMPLES USING APACHE ARIES HOLLY CUMMINS TIMOTHY WARD MANNING S HELTER ISLAND 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. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2013 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. Development editors: Sebastian Stirling, Frank Pohlmann Manning Publications Co. Technical proofreader: Mark Nuttall 20 Baldwin Road Copyeditor: Benjamin Berg PO Box 261 Proofreader: Katie Tennant Shelter Island, NY 11964 Typesetter: Gordan Salinovic Cover designer: Marija Tudor ISBN 9781617290138 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13 www.it-ebooks.info v brief contents PART 1 PROGRAMMING BEYOND HELLO WORLD . 1 1 ■ OSGi and the enterprise—why now? 3 2 ■ Developing a simple OSGi-based web application 26 3 ■ Persistence pays off 55 4 ■ Packaging your enterprise OSGi applications 86 PART 2 BUILDING BETTER ENTERPRISE OSGI APPLICATIONS . 111 5 ■ Best practices for enterprise applications 113 6 ■ Building dynamic applications with OSGi services 132 7 ■ Provisioning and resolution 164 8 ■ Tools for building and testing 190 9 ■ IDE development tools 222 PART 3 INTEGRATING ENTERPRISE OSGI WITH EVERYTHING ELSE 239 10 ■ Hooking up remote systems with distributed OSGi 241 11 ■ Migration and integration 270 12 ■ Coping with the non-OSGi world 291 13 ■ Choosing a stack 318 www.it-ebooks.info www.it-ebooks.info vii contents preface xiii acknowledgments xv about this book xvii about the authors xx about the cover illustration xxi PART 1 PROGRAMMING BEYOND HELLO WORLD. 1 1 OSGi and the enterprise—why now? 3 1.1 Java’s missing modularity 4 Thinking about modularity 5 ■ Enterprise Java and modularity— even worse! 7 1.2 OSGi to the rescue 9 Modularity, versioning, and compatibility 9 ■ Dynamism and lifecycle management 14 ■ Why isn’t everyone using OSGi? 19 Why OSGi and Java EE don’t work together 21 1.3 Programming with enterprise OSGi 23 Enterprise OSGi and OSGi in the enterprise 23 ■ Dependency injection 24 ■ Java EE integration 24 1.4 Summary 25 www.it-ebooks.info CONTENTS viii 2 Developing a simple OSGi-based web application 26 2.1 The development sandbox 27 Introducing Apache Aries 27 ■ My first enterprise OSGi runtime 28 2.2 Writing an OSGi web application 31 Building a simple OSGi web application bundle 31 ■ Deploying and testing 34 ■ A familiar feeling—and important differences 35 Spicing things up with fragments 38 2.3 Decoupling with dependency injection 41 Inversion of control 42 ■ Introducing the Blueprint service 43 Coupling two components with Blueprint 43 2.4 Bridging JNDI and OSGi 50 2.5 Summary 54 3 Persistence pays off 55 3.1 Java and persistence 56 Making persistence easy 56 ■ The problems with traditional persistence in OSGi 57 3.2 Building a persistent application 59 Setting up a datasource 59 ■ Creating a persistence bundle 65 Making use of the data 72 3.3 Transactions—the secret ingredient 75 What is a transaction? 75 ■ Handling multipart transactions 76 3.4 Summary 85 4 Packaging your enterprise OSGi applications 86 4.1 The need for more than modules 87 Java EE applications 88 ■ Enterprise OSGi applications 90 4.2 Enterprise OSGi subsystems 90 ESA structure 91 ■ Subsystem metadata 92 ■ Provisioning and resolution 94 4.3 The Enterprise Bundle Archive (EBA) 99 Sharing and isolation 99 4.4 Alternative approaches 100 Spring plan and PAR files 101 ■ Apache Karaf features 102 4.5 Developing an enterprise OSGi application 103 Building your metadata 104 4.6 Summary 109 www.it-ebooks.info CONTENTS ix PART 2 BUILDING BETTER ENTERPRISE OSGI APPLICATIONS 111 5 Best practices for enterprise applications 113 5.1 The benefits of sharing—and how to achieve them in your bundles 114 Versioning bundles and packages 115 ■ Scoping your bundles 116 ■ Why isolation is important to sharing 123 5.2 Structuring for flexibility 124 Separate interfaces from implementation 124 ■ If you can’t separate out your API 124 ■ Avoid static factory classes 125 Building a better listener with the whiteboard pattern 126 5.3 A better enterprise application architecture 127 Use small WABs 128 ■ Make a persistence bundle 128 The rewards of well-written bundles 131 5.4 Summary 131 6 Building dynamic applications with OSGi services 132 6.1 OSGi dynamism 133 6.2 Using OSGi services 134 Registering and looking up services the old-fashioned way 135 Blueprint 138 ■ Declarative Services 138 ■ iPojo 141 Google Guice and Peaberry 141 6.3 Getting the most out of Blueprint 142 Using Blueprint for things other than services 142 ■ Values 143 Understanding bean scopes 144 ■ Constructing beans 146 Lifecycle callbacks 147 ■ Service ranking 149 ■ Registering services under multiple interfaces 149 6.4 Blueprint and service dynamism 150 The remarkable appearing and disappearing services 150 Multiplicity and optionality 156 ■ Monitoring the lifecycle 160 6.5 Summary 163 7 Provisioning and resolution 164 7.1 Describing OSGi bundles 165 Describing bundles as resources 165 ■ Requirements and capabilities 167 Directives affecting the resolver 168 ■ Repositories 172 7.2 Provisioning bundles 174 Package-based provisioning 174 ■ Service-based provisioning 179 www.it-ebooks.info [...]... Migration and integration 270 11.1 271 Using SCA to integrate heterogeneous systems using an ESB 274 11.2 Managing heterogeneous applications 271 Migrating from Java EE ■ Integrating 276 Moving from WARs to WABs 276 Using persistence bundles EJBs in OSGi 283 Moving to Blueprint from the Spring Framework 286 ■ ■ 11.3 12 Summary 289 Coping with the non -OSGi world 291 12.1 Turning normal JARs into OSGi bundles... INTEGRATING ENTERPRISE OSGI WITH EVERYTHING ELSE .239 10 Hooking up remote systems with distributed OSGi 10.1 241 The principles of remoting 242 The benefits of remoting 242 The drawbacks of remoting Good practices and the fallacies of remoting 249 ■ 10.2 The Remote Services Specification 249 Exposing endpoints 10.3 246 250 ■ Discovering endpoints 251 Writing a remotable service 251 Coding a special... data in a database, controlling the flow of transactions, and packaging bundles into a single unit Chapter 1 starts off gently by introducing OSGi and explaining why modularity—which is what OSGi provides—is so important If you’re itching to get coding, don’t worry Chapter 2 shows you how to develop your first enterprise OSGi application You’ll write a modular web application and connect it to OSGi. .. well together ■ How enterprise OSGi fixes this, and what the enterprise OSGi programming model looks like Enterprise OSGi combines two of Java’s most enduringly popular programming models: enterprise Java and OSGi Enterprise Java is a loosely defined set of libraries, APIs, and frameworks built on top of core Java that turn it into a powerful platform for distributed, transactional, interactive, and persistent... via a link found on the book’s homepage on the Manning website, www.manning.com/EnterpriseOSGiinAction The SourceCodeEnterpriseOSGiinAction.zip archive includes source code for an application with a web frontend and a JPA backend, as well as distributed variations There’s a Maven build that produces bundles and a eba application that can be installed into an OSGi framework See section 2.1.2 for instructions... As well as developing Aries itself, I was speaking at conferences about enterprise OSGi Manning contacted me and asked me if I’d be interested in writing a book on the subject I was excited by the idea, but scared—after all, there were lots of people who’d been working with enterprise OSGi for much longer than I had What I did have was insight into what people learning enterprise OSGi needed to know... covers the basics of OSGi It explains why OSGi is such a necessary technology, and provides grounding in versioning, bundles, bundle lifecycles, and OSGi services It includes some practical hints on OSGi frameworks and consoles www.it-ebooks.info ABOUT THIS BOOK xix Appendix B describes the broader OSGi ecosystem It explains how the OSGi alliance works and what’s in the various OSGi specifications Code... (Manning Publications, 2011) In a sense, OSGi takes the Java programming model closer to an “ideal” programming model—one that’s robust, powerful, and elegant The way it does this is by encouraging good software engineering practice through higher levels of modularity These, along with versioning, are the driving principles behind OSGi OSGi enables abstraction, encapsulation, decomposition, loose coupling,... explain the syntax we use for the diagrams later in the book This section covers all the important facts for writing enterprise OSGi applications, but if you’re new to OSGi, or if after reading it you’re bursting to know even more about the core OSGi platform, you should read appendixes A and B We can’t cover all of OSGi in two appendixes, so we’d also definitely recommend you get hold of OSGi in Action. .. bundles 292 Finding bundled libraries 292 Building your own bundles 294 Generating bundles automatically 296 ■ ■ www.it-ebooks.info 280 xii CONTENTS 12.2 Common problems for OSGi- unaware libraries 302 Reflection in OSGi 303 Using and abusing the thread context ClassLoader 306 META-INF services 308 Serialization and deserialization 309 ■ ■ 12.3 ■ An example library conversion—logging frameworks in OSGi 310 . MANNING Holly Cummins Timothy Ward www.it-ebooks.info Enterprise OSGi in Action www.it-ebooks.info www.it-ebooks.info Enterprise OSGi in Action WITH EXAMPLES USING APACHE ARIES HOLLY CUMMINS TIMOTHY. link found on the book’s homepage on the Manning website, www.manning.com/EnterpriseOSGiinAction. The SourceCodeEnterpriseOSGiinAction.zip archive includes source code for an application with. familiar feeling—and important differences 35 Spicing things up with fragments 38 2.3 Decoupling with dependency injection 41 Inversion of control 42 ■ Introducing the Blueprint service 43 Coupling two