Osgi in Practice
Total Page:16
File Type:pdf, Size:1020Kb
OSGi In Practice Neil Bartlett January 11, 2009 Contents Preface xiii I Nuts and Bolts1 1 Introduction3 1.1 What is a Module?.........................4 1.2 The Problem(s) with JARs....................5 1.2.1 Class Loading and the Global Classpath.........6 1.2.2 Conflicting Classes.....................8 1.2.3 Lack of Explicit Dependencies...............9 1.2.4 Lack of Version Information................ 10 1.2.5 Lack of Information Hiding Across JARs......... 12 1.2.6 Recap: JARs Are Not Modules.............. 12 1.3 J2EE Class Loading........................ 13 1.4 OSGi: A Simple Idea........................ 15 1.4.1 From Trees to Graphs................... 16 1.4.2 Information Hiding in OSGi Bundles........... 18 1.4.3 Versioning and Side-by-Side Versions........... 19 1.5 Dynamic Modules......................... 19 1.6 The OSGi Alliance and Standards................ 20 1.7 OSGi Implementations....................... 21 1.8 Alternatives to OSGi........................ 21 1.8.1 Build Tools: Maven and Ivy................ 22 1.8.2 Eclipse Plug-in System................... 22 1.8.3 JSR 277........................... 23 2 First Steps in OSGi 25 2.1 Bundle Construction........................ 25 2.2 OSGi Development Tools..................... 26 2.2.1 Eclipse Plug-in Development Environment........ 26 2.2.2 Bnd............................. 27 2.3 Installing a Framework....................... 28 2.4 Setting up Eclipse......................... 29 2.5 Running Felix............................ 31 2.6 Installing bnd............................ 33 2.7 Hello, World!............................ 34 DRAFT PREVIEW iv Contents 2.8 Bundle Lifecycle.......................... 36 2.9 Incremental Development..................... 39 2.10 Interacting with the Framework.................. 40 2.11 Starting and Stopping Threads.................. 43 2.12 Manipulating Bundles....................... 43 2.13 Exercises.............................. 44 3 Bundle Dependencies 47 3.1 Introducing the Example Application............... 48 3.2 Defining an API.......................... 48 3.3 Exporting the API......................... 51 3.4 Importing the API......................... 53 3.5 Interlude: How Bnd Works.................... 57 3.6 Requiring a Bundle......................... 59 3.7 Version Numbers and Ranges................... 61 3.7.1 Version Numbers...................... 62 3.7.2 Versioning Bundles..................... 63 3.7.3 Versioning Packages.................... 63 3.7.4 Version Ranges....................... 64 3.7.5 Versioning Import-Package and Require-Bundle ... 65 3.8 Class Loading in OSGi....................... 66 3.9 JRE Packages............................ 69 3.10 Execution Environments...................... 70 3.11 Fragment Bundles......................... 72 3.12 Class Space Consistency and “Uses” Constraints........ 73 4 Introduction to Services 75 4.1 Late Binding in Java........................ 75 4.1.1 Dependency Injection Frameworks............ 76 4.1.2 Dynamic Services...................... 77 4.2 Registering a Service........................ 79 4.3 Unregistering a Service....................... 81 4.4 Looking up a Service........................ 84 4.5 Service Properties......................... 86 4.6 Introduction to Service Trackers................. 88 4.7 Listening to Services........................ 90 4.8 Tracking Services.......................... 92 4.9 Filtering on Properties....................... 95 4.10 Cardinality and Selection Rules.................. 96 4.10.1 Optional, Unary...................... 98 4.10.2 Optional, Multiple..................... 101 4.10.3 Mandatory, Unary..................... 101 4.10.4 Mandatory, Multiple.................... 101 5 Example: Mailbox Reader GUI 103 DRAFT PREVIEW Contents v 5.1 The Mailbox Table Model and Panel............... 103 5.2 The Mailbox Tracker........................ 103 5.3 The Main Window......................... 106 5.4 The Bundle Activator....................... 109 5.5 Putting it Together......................... 111 6 Concurrency and OSGi 115 6.1 The Price of Freedom....................... 115 6.2 Shared Mutable State....................... 117 6.3 Safe Publication.......................... 119 6.3.1 Safe Publication in Services................ 121 6.3.2 Safe Publication in Framework Callbacks........ 124 6.4 Don’t Hold Locks when Calling Foreign Code.......... 126 6.5 GUI Development......................... 129 6.6 Using Executors.......................... 131 6.7 Interrupting Threads........................ 138 6.8 Exercises.............................. 141 7 The Whiteboard Pattern and Event Admin 143 7.1 The Classic Observer Pattern................... 143 7.2 Problems with the Observer Pattern............... 144 7.3 Fixing the Observer Pattern.................... 145 7.4 Using the Whiteboard Pattern.................. 146 7.4.1 Registering the Listener.................. 149 7.4.2 Sending Events....................... 151 7.5 Event Admin............................ 154 7.5.1 Sending Events....................... 154 7.5.2 The Event Object..................... 155 7.5.3 Receiving Events...................... 158 7.5.4 Running the Example................... 159 7.5.5 Synchronous versus Asynchronous Delivery....... 161 7.5.6 Ordered Delivery...................... 162 7.5.7 Reliable Delivery...................... 162 7.6 Exercises.............................. 163 8 The Extender Model 165 8.1 Looking for Bundle Entries.................... 166 8.2 Inspecting Headers......................... 168 8.3 Tracking Bundles.......................... 169 8.4 Synchronous and Asynchronous Bundle Listeners........ 176 8.5 The Eclipse Extension Registry.................. 181 8.6 Impersonating a Bundle...................... 183 8.7 Conclusion............................. 187 9 Configuration and Metadata 189 DRAFT PREVIEW vi Contents II Component Oriented Development 191 10 Component Oriented Development 193 11 Declarative Services 195 III Practical OSGi 197 12 Testing OSGi Bundles 199 13 Using Third-Party Libraries 201 14 Building Web Applications 203 IV Appendices 205 A Bundle Tracker 207 B ANT Build System for Bnd 211 DRAFT PREVIEW List of Figures 1.1 The standard Java class loader hierarchy.............7 1.2 Example of an internal JAR dependency.............9 1.3 A broken internal JAR dependency................ 10 1.4 Clashing Version Requirements.................. 11 1.5 A typical J2EE class loader hierarchy............... 14 1.6 The OSGi class loader graph.................... 17 1.7 Different Versions of the Same Bundle............... 20 2.1 Adding Felix as a User Library in Eclipse............ 30 2.2 Creating a new Java project in Eclipse: adding the Felix library 32 2.3 A New OSGi Project, Ready to Start Work........... 33 2.4 Bundle Lifecycle.......................... 37 3.1 The runtime resolution of matching import and export..... 57 3.2 The runtime resolution of a Required Bundle.......... 60 3.3 Simplified OSGi Class Search Order............... 66 3.4 Full OSGi Search Order...................... 68 4.1 Service Oriented Architecture................... 78 4.2 Updating a Service Registration in Response to Another Service 91 5.1 The Mailbox GUI (Windows XP and Mac OS X)........ 112 5.2 The Mailbox GUI with a Mailbox Selected............ 113 6.1 Framework Calls and Callbacks in OSGi............. 116 6.2 The Dining Philosophers Problem, Simplified.......... 128 7.1 The Classic Observer Pattern................... 144 7.2 An Event Broker.......................... 146 7.3 A Listener Directory........................ 147 7.4 The Event Admin Service..................... 155 8.1 Inclusion Relationships of Bundle States............. 171 8.2 Bundle Transitions and Events.................. 175 8.3 Synchronous Event Delivery when Starting a Bundle...... 179 8.4 Asynchronous Event Delivery after Starting a Bundle..... 180 8.5 Editing an Extension Point in Eclipse PDE........... 182 DRAFT PREVIEW viii List of Figures 8.6 Editing an Extension in Eclipse PDE............... 183 B.1 OSGi Project Structure...................... 212 DRAFT PREVIEW List of Code Listings 2.1 A Typical OSGi MANIFEST.MF File.............. 25 2.2 A Typical Bnd Descriptor File.................. 27 2.3 Hello World Activator....................... 35 2.4 Bnd Descriptor for the Hello World Activator.......... 35 2.5 Bundle Counter Activator..................... 42 2.6 Bnd Descriptor for the Bundle Counter............. 42 2.7 Heartbeat Activator........................ 43 2.8 Hello Updater Activator...................... 45 3.1 The Message Interface....................... 49 3.2 The Mailbox Interface....................... 50 3.3 Mailbox API Exceptions...................... 51 3.4 Bnd Descriptor for the Mailbox API............... 52 3.5 String Message........................... 54 3.6 Fixed Mailbox........................... 55 3.7 MANIFEST.MF generated from fixed_mailbox.bnd ....... 56 3.8 Bnd Sample: Controlling Bundle Contents............ 58 3.9 Bnd Sample: Controlling Imports................. 58 4.1 Naïve Solution to Instantiating an Interface........... 76 4.2 Welcome Mailbox Activator.................... 79 4.3 Bnd Descriptor for the Welcome Mailbox Bundle........ 80 4.4 File Mailbox Activator....................... 82 4.5 File Mailbox (Stub Implementation)............... 83 4.6 Bnd Descriptor for File Mailbox Bundle............. 83 4.7 Message Count Activator..................... 85 4.8 Bnd Descriptor for the Message Counter Bundle........ 86 4.9 Adding Service Properties to the Welcome Mailbox....... 87 4.10 Message Count