Table Of Contentwww.it-ebooks.info
www.it-ebooks.info
Building Modular Cloud Apps
with OSGi
Paul Bakker and Bert Ertman
www.it-ebooks.info
Building Modular Cloud Apps with OSGi
by Paul Bakker and Bert Ertman
Copyright © 2013 Paul Bakker and Bert Ertman. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editor: Meghan Blanchette Indexer: BIM Indexing and Proofreading Services
Production Editor: Rachel Steely Cover Designer: Randy Comer
Copyeditor: Amanda Kersey Interior Designer: David Futato
Proofreader: Linley Dolby Illustrator: Rebecca Demarest
September 2013: First Edition
Revision History for the First Edition:
2013-09-05: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449345150 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. Building Modular Cloud Apps with OSGi, the image of a European Roller, and related trade dress
are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-34515-0
[LSI]
www.it-ebooks.info
To Qiushi, my love, for supporting all my crazy ideas and always being there for me.
— Paul Bakker
To Jorien, my incredible wife, and to Amber, my beautiful and joyful daughter. Thank you
girls for supporting me throughout my career and for putting up with me. I love you!
— Bert Ertman
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Part I. Introducing Modularity in Java
1. Modularity Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Dealing with Increasing Complexity 4
Divide and Conquer 4
Service Oriented Architecture All Over Again? 5
A Better Look at Modularity and What It Really Means 6
Design Time Modularity 6
Runtime Modularity 6
Modularity Solutions 7
OSGi 8
Jigsaw 8
JBoss Modules 9
Maven 9
Choosing a Solution: OSGi 9
What Is OSGi? 9
OSGi in the Real World 10
Tooling 11
Bndtools 11
Maven with the BND Maven Plug-in 12
Eclipse Tycho 12
NetBeans and IntelliJ 12
2. Basic Concepts of OSGi. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Hiding Implementations 13
Import Package Myths 16
Depending on Other Modules 16
v
www.it-ebooks.info
Service Dynamics 17
The Bundle Format 18
Bundle-SymbolicName 18
Bundle-Name 18
Bundle-Version 18
Import-Package 19
Export-Package 20
Bundle-Activator 21
Running an OSGi Application 22
The Framework Lifecycle 23
3. Creating the First OSGi Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Prerequisites 25
Getting Started 26
Creating an API Bundle 27
Creating an OSGi Service 28
Running the Code 30
Using the Agenda Service 32
Service Instances 34
Understanding Service Dynamics 34
Services and Bundle States 36
Debugging Services 36
Having Multiple Service Implementations 37
Service Properties 39
Service Ranking 40
Service Registration and Deregistration Callbacks 40
Injecting Multiple Service Implementations and the Whiteboard Pattern 41
Lifecycle Callbacks 42
Injecting BundleContext and DependencyManager 43
4. Advanced OSGi. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Semantic Versioning 45
Provider and Consumer Types 46
Baselining in Bndtools 47
Semantic Bundle Versioning 48
Integration Testing 48
Writing Integration Tests 48
Running Integration Tests 51
Configuring Services 52
Managed Services 53
Configuring a Managed Service 55
Required Configuration 56
vi | Table of Contents
www.it-ebooks.info
Managed Service Factories 57
MetaType 61
Providing Configuration 62
Log Service 63
Installing and Using the LogService 64
Logging Performance 64
Extender Pattern 65
Event Admin 68
Using Event Admin 68
Aspect Services 70
The Bundle Cache 72
5. Pointers and Pitfalls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Better Understanding OSGi Specifications 75
Require Bundle 76
Fragment Bundles 77
Loading Extra System Packages 77
Profilers, Coverage Tools, and Bootpath Delegation 78
Dealing with Non-OSGi Libraries 79
Transitive Dependencies 79
Classloading Problems in Libraries and Frameworks 80
Passing the Bundle Classloader Manually 81
Handling Classpath Scanning 81
The Low-Level Service API 82
Registering Services 82
Using Services 82
Service Tracker 84
Dynamic Classloading 86
DynamicImport-Package 87
Optional Import-Package 88
Part II. Developing Cloud Applications
6. Cloud Application Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Technology Stack 91
The Amdatu Project 92
The Layered Architecture 93
Inside a Bundle 96
API Bundles 96
Separation of Concerns 97
Services Are Responsible for Their Own Data 98
Table of Contents | vii
www.it-ebooks.info
Bndtools Project Structure 98
Comparing with SOA Again 99
Remoting 99
7. Architectural Capabilities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Maintainability 101
Extensibility 101
Scalability 102
Testability 104
8. Creating Web Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Modular Web Applications 107
HTTP Service 108
Taking It One Step Further with Servlets 110
Adding Filters into the Mix 111
Handling Web Resources Automatically 113
Rethinking Web Applications 113
RESTful Web Services 114
Getting Started 114
Implementing an Agenda Resource 116
Extending the Agenda Resource 118
Simplified Object Mapping 118
Self-Documenting RESTful Endpoints 119
Modularizing RESTful Resources 120
Modular User Interfaces Using HTML 5 121
Token Based Security 122
Using the Token Provider 122
Web Application Bundles 124
OpenSocial 125
Getting Started 126
Creating Gadgets 126
9. Persistence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Relational Databases 129
JDBC 130
Object Relational Mapping with JPA 133
NoSQL 139
Document Stores 142
Using MongoDB as a Document Store 142
Getting Started 143
Implementing a Persistent Service 143
Using the MongoDB Query System 146
viii | Table of Contents
www.it-ebooks.info
Description:If you're an experienced Java developer in the enterprise, this practical, hands-on book shows you how to use OSGi to design, develop, and deploy modular cloud applications. You'll quickly learn how to use OSGi, through concise code examples and a set of best practices derived from the authors' expe