Component Oriented Development in OSGi with Declarative Services, Spring Dynamic Modules and Apache iPOJO
Upcoming SlideShare
Loading in...5
×
 

Component Oriented Development in OSGi with Declarative Services, Spring Dynamic Modules and Apache iPOJO

on

  • 14,463 views

Tutorial slides from EclipseCon 2009, by Neil Bartlett and Heiko Seeberger

Tutorial slides from EclipseCon 2009, by Neil Bartlett and Heiko Seeberger

Statistics

Views

Total Views
14,463
Views on SlideShare
13,231
Embed Views
1,232

Actions

Likes
31
Downloads
700
Comments
1

10 Embeds 1,232

http://neilbartlett.name 719
http://www.eclipsecon.org 396
http://www.slideshare.net 59
http://njbartlett.name 36
http://eclipsecon.org 16
http://www.feedage.com 2
http://74.125.155.132 1
http://203.208.39.132 1
http://webcache.googleusercontent.com 1
http://web.archive.org 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Some comments that I've made also at the EclipseCon 2009 presentation regarding the Spring DM disadvantages (slide 150):

    1. the Spring DM core+extender+io is ~600 K. With Spring it can build up to 2Mb but you get full support of the Spring IoC container including annotation support, component scanning, processing and the like (these are used in some of the samples).

    2. the application context concepts is not legacy - that's why it has been adopted in the OSGI 4.2 spec as well. It's a representation of the container at runtime - due to IoC, is your choice if you want to use it or not.

    3. it's up to you define a fine or coarse grained context. Having half of the context start while the other part is waiting is a recipe for disaster. An application context should start when all its mandatory dependencies are available (that's why they are mandatory after all) - if they are not, one can just mark them as optional. Also, you can split a context into two bundles and have two different bundles with two different contexts and independent lifecycles.
    At the end of the day it's your choice how big or small the context is and how many separate lifecycles you want to deal with.

    4. Spring DM 1.2.0 builds on OSGI 4.0 which does not include lazy activation (it was introduced in 4.1). Whether (and how) laziness can work is discussed in the Blueprint specification process. Note that beside Equinox, there are no other OSGi implementations that support lazy activation (note that technically lazy activation has little to do with actual class loading).

    5. Currently, there are 4 other implementations that I can think of. Even if there weren't, I fail to see how this is a disadvantage of Spring DM in the first place.

    Cheers,
    Costin Leau
    Spring DM Lead
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Component Oriented Development in OSGi with Declarative Services, Spring Dynamic Modules and Apache iPOJO Component Oriented Development in OSGi with Declarative Services, Spring Dynamic Modules and Apache iPOJO Presentation Transcript

  • Component Oriented Development in OSGi with Declarative Services, Spring Dynamic Modules and Apache iPOJO Neil Bartlett1 Heiko Seeberger2 1 Weigle Wilczek UK 2 Weigle Wilczek GmbH March 24, 2009 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 1 / 174
  • Part I Introduction c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 2 / 174
  • The (Partial) Failure of Object Orientation Contents The (Partial) Failure of Object Orientation 1 What is a Component? 2 Implementing Components 3 Example Application 4 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 3 / 174
  • The (Partial) Failure of Object Orientation The (Partial) Failure of Object Oriented One of the primary goals of object oriented programming (OOP) was, and still is, re-use. It has mostly failed in that goal. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 4 / 174
  • The (Partial) Failure of Object Orientation The (Partial) Failure of Object Oriented One of the primary goals of object oriented programming (OOP) was, and still is, re-use. It has mostly failed in that goal. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 4 / 174
  • The (Partial) Failure of Object Orientation The (Partial) Failure of Object Oriented Peter Kriens “Object Oriented technology was going to change the world. . . we would have all these objects in our library and building a new system would be a snap. Just get a few classes, bunch them together. . . and voila!” c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 5 / 174
  • The (Partial) Failure of Object Orientation What Went Wrong?? Coupling Classes can almost never be used in isolation – they depend on other classes. Those classes depend on other packages, which depend on other JARs. . . c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 6 / 174
  • The (Partial) Failure of Object Orientation What Went Wrong?? Coupling Classes can almost never be used in isolation – they depend on other classes. Those classes depend on other packages, which depend on other JARs. . . c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 6 / 174
  • The (Partial) Failure of Object Orientation What Went Wrong?? Coupling Classes can almost never be used in isolation – they depend on other classes. Those classes depend on other packages, which depend on other JARs. . . c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 6 / 174
  • The (Partial) Failure of Object Orientation What Went Wrong?? Eventually just to use one small class we end up doing this: Maven c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 7 / 174
  • The (Partial) Failure of Object Orientation A Solution? Re-use of classes outside their original context is hard, so. . . Give up! Leave them where they are, and call over the network! This is sometimes called “SOA”. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 8 / 174
  • The (Partial) Failure of Object Orientation A Better Solution Component Oriented Programming Builds on OOP. OOP is not bad, it’s just not the whole answer. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 9 / 174
  • What is a Component? Contents The (Partial) Failure of Object Orientation 1 What is a Component? 2 Implementing Components 3 Example Application 4 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 10 / 174
  • What is a Component? Components But wait, what is a “component”, vs an object? Good question! Many attempts have been made to define the term. The following is not a formal academic definition, just a working definition that we have found useful in practice. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 11 / 174
  • What is a Component? Components But wait, what is a “component”, vs an object? Good question! Many attempts have been made to define the term. The following is not a formal academic definition, just a working definition that we have found useful in practice. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 11 / 174
  • What is a Component? The Most Common Analogy Lego? c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 12 / 174
  • What is a Component? Lego is a Poor Analogy Just dead lumps of plastic. All look and act the same. Not even very re-usable (try using a Duplo block in a Lego Technics model!). c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 13 / 174
  • What is a Component? A Better Analogy Bees/Animals c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 14 / 174
  • What is a Component? Components Are:. . . Active participants in the system. Aware of and adapt to their environment. May provide services to other components and use services from other components. Have a life cycle. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 15 / 174
  • What is a Component? Components Are:. . . Active participants in the system. Aware of and adapt to their environment. May provide services to other components and use services from other components. Have a life cycle. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 15 / 174
  • What is a Component? Components Are:. . . Active participants in the system. Aware of and adapt to their environment. May provide services to other components and use services from other components. Have a life cycle. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 15 / 174
  • What is a Component? Components Are:. . . Active participants in the system. Aware of and adapt to their environment. May provide services to other components and use services from other components. Have a life cycle. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 15 / 174
  • What is a Component? What Does the “Environment” Mean? Services provided by other components. Resource, devices, etc. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 16 / 174
  • What is a Component? Adaptation When the environment is good, the component flourishes. When the environment is harsh, the component survives. When very harsh, the component sleeps or dies. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 17 / 174
  • Implementing Components Contents The (Partial) Failure of Object Orientation 1 What is a Component? 2 Implementing Components 3 Example Application 4 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 18 / 174
  • Implementing Components Implementing Components OSGi is the perfect environment for implementing components. The module layer allow us to minimise our static dependencies. Fewer static dependencies means less stuff that must be present for our component to work. Services allow our component to interact with other components. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 19 / 174
  • Implementing Components Implementing Components POJOs Components should be implemented as POJOs (Plain Old Java Objects) “glued” together with OSGi services. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 20 / 174
  • Example Application Contents The (Partial) Failure of Object Orientation 1 What is a Component? 2 Implementing Components 3 Example Application 4 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 21 / 174
  • Example Application The Example Application The example is a simplistic “contact manager” application. Capable of displaying the content of multiple contact “repositories”. Each repository appears in its own tab. Repositories are implemented as OSGi services. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 22 / 174
  • Example Application Example Architecture Services Diagram app.ui.* 0..1 0..N core.inmemory LogService 0..1 ContactRepository core.directory 0..N ContactRepository Listener c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 23 / 174
  • Example Application The Example Application Contact Repository Viewer c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 24 / 174
  • Example Application Running the Example Application First start Equinox using the “Core” launcher from Eclipse’s Run menu. Equinox Console osgi> install file : contacts_api . jar Bundle id is 3 osgi> install file : swingui . jar Bundle id is 4 osgi> start 4 The UI should now appear, but with no contact repository tabs. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 25 / 174
  • Example Application Installing a Basic Contact Repository Equinox Console osgi> install file : . . / ComponentsRaw / contacts . core . inmemory_raw . jar Bundle id is 5 osgi> start 5 You should see two repositories: “Some Dummies” and “OSGi Nerds”. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 26 / 174
  • Example Application The “Raw” Components The ComponentsRaw project contains two repository implementations. They are implemented using “raw” OSGi APIs, i.e. ServiceTracker. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 27 / 174
  • Example Application Implementing Components with Raw OSGi It is possible to implement components using just the raw OSGi APIs. But there are several challenges. We need to be cautious to separate component code from OSGi “glue”. If OSGi APIs are used in our components it makes them difficult to test and non-POJO. It is much harder to access services and configuration using raw APIs. Small changes in desired behaviour – e.g. a switch from mandatory to optional dependence on a service – require large changes in our glue code. Much of the glue code is repetitive – mostly the same for many different components. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 28 / 174
  • Example Application Component Frameworks We would like to use a framework to ease implementation of components in OSGi. There is more than one to choose from! In this tutorial we look at three popular choices: Declarative Services; Spring Dynamic Modules; and Apache iPOJO. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 29 / 174
  • Example Application A More Interesting Component For most of this tutorial we will focus on a more interesting and complex example. The DirectoryRepository component implements a contact repository backed by files in a directory. It is active: it creates a thread to monitor the directory. It needs configuration: the directory to monitor. It consumes services provided by other components: the log service and some listeners. It provides a service to other components: the contact repository functionality. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 30 / 174
  • Example Application Getting Stuff Exercises: http://neilbartlett.name/downloads/eclipsecon2009/labs.zip Solutions: http://neilbartlett.name/downloads/eclipsecon2009/solutions.zip These Slides: http://neilbartlett.name/downloads/eclipsecon2009/slides.pdf NB these are temporary URLs until the files have been uploaded to eclipsecon.org. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 31 / 174
  • Part II Declarative Services c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 32 / 174
  • Introduction Contents Introduction 5 A Minimal Example 6 Activation and Deactivation 7 References to Services 8 Optional vs Mandatory Service References 9 Static vs Dynamic Components 10 Publishing a Service 11 Lazy Service Creation 12 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 33 / 174
  • Introduction Declarative Services “Declarative Services” (DS) is a specification from the OSGi Compendium, section 112. It was introduced in Release 4.0 and is based on the extender model. Like all extenders, DS performs tasks on behalf of other bundles. The DS spec defines this extender and it is implemented by frameworks. The extender bundle itself is called the “Service Component Runtime” or SCR. The terms DS and SCR are sometimes confused. Remember, DS is the specification, SCR is the actual bundle that implements the specification. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 34 / 174
  • Introduction Declarative Services There are significant improvements in DS in OSGi R4.2. Many of these changes are supported in Equinox 3.5M5+. We will use the R4.2 features, but mention when we do so. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 35 / 174
  • Introduction Declarative Services What does the SCR extender bundle do on our behalf? Creates Components. 1 “Binds” them to services and configuration. 2 Manages the component’s lifecycle in response to bound services 3 coming and going. Optionally, publishes our components as services themselves. 4 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 36 / 174
  • Introduction Declarative Services What does the SCR extender bundle do on our behalf? Creates Components. 1 “Binds” them to services and configuration. 2 Manages the component’s lifecycle in response to bound services 3 coming and going. Optionally, publishes our components as services themselves. 4 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 36 / 174
  • Introduction Declarative Services What does the SCR extender bundle do on our behalf? Creates Components. 1 “Binds” them to services and configuration. 2 Manages the component’s lifecycle in response to bound services 3 coming and going. Optionally, publishes our components as services themselves. 4 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 36 / 174
  • Introduction Declarative Services What does the SCR extender bundle do on our behalf? Creates Components. 1 “Binds” them to services and configuration. 2 Manages the component’s lifecycle in response to bound services 3 coming and going. Optionally, publishes our components as services themselves. 4 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 36 / 174
  • Introduction Declarative Services What does the SCR extender bundle do on our behalf? Creates Components. 1 “Binds” them to services and configuration. 2 Manages the component’s lifecycle in response to bound services 3 coming and going. Optionally, publishes our components as services themselves. 4 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 36 / 174
  • A Minimal Example Contents Introduction 5 A Minimal Example 6 Activation and Deactivation 7 References to Services 8 Optional vs Mandatory Service References 9 Static vs Dynamic Components 10 Publishing a Service 11 Lazy Service Creation 12 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 37 / 174
  • A Minimal Example A Minimal Component Declaration minimal.xml <? xml version=quot; 1.0 quot; e n c o d i n g=quot; UTF -8 quot; ?> <s c r : c o m p o n e n t x m l n s : s c r=quot; http: // www . osgi . org / xmlns / scr / v1 .1.0 quot;> <i m p l e m e n t a t i o n c l a s s=quot; org . example . osgi . ds . HelloCom ponent quot; /> </ s c r : c o m p o n e n t> NB: the namespace is required in order to use R4.2 features. If not included then SCR will default to the prior version. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 38 / 174
  • A Minimal Example A Minimal Component Declaration This declaration requests SCR to simply instantiate the named class, which must be visible to the declaring bundle’s classloader. We need to tell the SCR extender bundle about this file. We use a manifest header, Service-Component. A comma-separated list of XML declaration files. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 39 / 174
  • A Minimal Example A Minimal Component Declaration MANIFEST.MF Bundle−SymbolicName : mybundle Bundle−Version : 1 . 0 . 0 Service−Component : OSGI−INF / minimal . xml ... c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 40 / 174
  • A Minimal Example A Minimal Component Declaration HelloComponent package org . example . osgi . ds ; public class H elloCom ponent { public Hello Componen t ( ) { System . out . println ( quot; He lloCompo nent created quot; ) ; } // . . . } Note: a Plain Old Java Object (POJO)! No OSGi API dependencies. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 41 / 174
  • A Minimal Example Building the Bundle Internal Bundle Structure minimal_ds . jar / META−INF / MANIFEST . MF OSGI−INF / minimal . xml org / example / osgi / ds / Hello Compone nt . class c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 42 / 174
  • A Minimal Example Running the Example We need to install the SCR bundle from Equinox, and also a utility bundle used by Equinox’s SCR: org.eclipse.equinox.ds <version>.jar org.eclipse.equinox.util <version>.jar The SCR bundle needs to be started, but the util bundle does not. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 43 / 174
  • A Minimal Example Running the Example Console osgi> install file : org . eclipse . equinox . ds_1 . 1 . 0 . v20090112 −1800. jar Bundle id is 2 osgi> install file : org . eclipse . equinox . util_1 . 0 . 1 . v20081205 − 1 8 0 . . . Bundle id is 3 osgi> start 2 osgi> ss Framework is launched . id State Bundle org . eclipse . osgi_3 . 4 . 2 . R34x_v20080826 −1230 0 ACTIVE org . eclipse . osgi . services_3 . 2 . 0 . v20081205 −1800 1 RESOLVED org . eclipse . equinox . ds_1 . 1 . 0 . v20090112 −1800 2 ACTIVE org . eclipse . equinox . util_1 . 0 . 1 . v20081205 −1800 3 RESOLVED osgi> c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 44 / 174
  • A Minimal Example Running the Example Now install and start the example bundle: Console osgi> install file : minimal_ds . jar Bundle id is 4 osgi> start 4 Hello Compone nt created osgi> Note that SCR ignores our bundle until it is in ACTIVE state. DS bundles must be activated even though they often have no bundle activator! This is quite different from Eclipse Extensions. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 45 / 174
  • Activation and Deactivation Contents Introduction 5 A Minimal Example 6 Activation and Deactivation 7 References to Services 8 Optional vs Mandatory Service References 9 Static vs Dynamic Components 10 Publishing a Service 11 Lazy Service Creation 12 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 46 / 174
  • Activation and Deactivation Activation and Deactivation That was a very long-winded way to merely instantiate a class! This component is not useful because it cannot even do anything. However, DS allows us to define lifecycle methods. We can be notified when the component starts and stops. This allows us to do interesting things like start threads, open sockets, etc. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 47 / 174
  • Activation and Deactivation An Active Component PollingComponent public class P o l l i n g C o m p o n e n t { private static final int DEFAUL T_PERIOD = 2 0 0 0 ; private PollingThread thread ; protected void activate ( Map<String , Object> config ) { System . out . println ( quot; Polling Component Activated quot; ) ; Integer period = ( Integer ) config . get ( quot; period quot; ) ; thread = new PollingThread ( period != null ? period : DEFAU LT_PERI OD ) ; thread . start ( ) ; } protected void deactivate ( ) { System . out . println ( quot; Polling Component Deactivated quot; ) ; thread . interrupt ( ) ; } } c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 48 / 174
  • Activation and Deactivation Activation and Deactivation SCR will find our activate/deactivate methods automatically. We can call them something else by adding attributes to the XML declaration. NB: Before R4.2, the method names could not be changed and they had to take a parameter of type ComponentContext, from the DS API. This broke the pojocity of the component. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 49 / 174
  • Activation and Deactivation Activation and Deactivation Why didn’t we just write this as a bundle activator?? This is a POJO! Easier access to configuration – the Map parameter to activate. Easier access to service registry – we will see this soon. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 50 / 174
  • References to Services Contents Introduction 5 A Minimal Example 6 Activation and Deactivation 7 References to Services 8 Optional vs Mandatory Service References 9 Static vs Dynamic Components 10 Publishing a Service 11 Lazy Service Creation 12 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 51 / 174
  • References to Services References to Services Using lower level APIs we must write a lot of “glue” code to bind to services. DS replaces the glue code with simple declarations. We declare references to services. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 52 / 174
  • References to Services References to Services Example reference Element < r e f e r e n c e name=quot; LOG quot; i n t e r f a c e=quot; org . osgi . service . log . LogService quot; b i n d=quot; setLog quot; u n b i n d=quot; unsetLog quot; /> Reference Attributes The name of the reference. name The service interface name. interface The name of the “set” method associated with the ref- bind erence. The name of the “unset” method associated with the unbind reference. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 53 / 174
  • Optional vs Mandatory Service References Contents Introduction 5 A Minimal Example 6 Activation and Deactivation 7 References to Services 8 Optional vs Mandatory Service References 9 Static vs Dynamic Components 10 Publishing a Service 11 Lazy Service Creation 12 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 54 / 174
  • Optional vs Mandatory Service References Optional vs Mandatory References We previously discussed the idea of optional and mandatory service dependencies. To model these with ServiceTracker required completely different code patterns. With DS we can simply change a single attribute on the reference element to switch between optional and mandatory. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 55 / 174
  • Optional vs Mandatory Service References Optional Service Reference Example reference Element < r e f e r e n c e name=quot; LOG quot; i n t e r f a c e=quot; org . osgi . service . log . LogService quot; b i n d=quot; setLog quot; u n b i n d=quot; unsetLog quot; c a r d i n a l i t y =quot; 0..1 quot; /> The default cardinality was “1..1” meaning that we must have exactly one instance, i.e. the reference is mandatory. A cardinality of “0..1” indicates that either zero or one instance is okay, i.e. the reference is optional. Yes, “0..n” and “1..n” do exist. We will look at these later. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 56 / 174
  • Optional vs Mandatory Service References Lab 1 : Building the Directory Repository (Setup) Install and start the commands.jar bundle which gives us a useful 1 installDir command. osgi> install file : commands . jar Bundle id is 3 osgi> start 3 Install the Contacts API bundle, and the DS bundles: 2 osgi> install file : contacts_api . jar Bundle id is 4 osgi> installDir . . / ComponentsDS / bundles c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 57 / 174
  • Optional vs Mandatory Service References Lab 1 : Building the Directory Repository Complete the implementation of the DirectoryRepositoryDS class 1 – TODO markers explain what is needed. Complete the DS XML descriptor directoryRepository.xml in the 2 dsxml directory. Remember to add at least: A property element to set the dirName property to the name of the contacts directory. An optional reference to the OSGi log service (org.osgi.service.log.LogService). A zero-to-many reference to the repository listeners. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 58 / 174
  • Optional vs Mandatory Service References Lab 1 : Testing the Component Build the bundle using the supplied ANT build. 1 Install and start the bundle and also start the SCR extender bundle 2 (org.eclipse.equinox.ds...). Type the log command to see messages from the component. 3 Observe what happens when .contact files are added/removed in the contacts directory. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 59 / 174
  • Static vs Dynamic Components Contents Introduction 5 A Minimal Example 6 Activation and Deactivation 7 References to Services 8 Optional vs Mandatory Service References 9 Static vs Dynamic Components 10 Publishing a Service 11 Lazy Service Creation 12 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 60 / 174
  • Static vs Dynamic Components Static vs Dynamic Components If we stop the log bundle (org.eclipse.equinox.log...) we should see something like this: osgi> stop 6 D i r e c t o r y R e p o s i t o r y D S constructed c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 61 / 174
  • Static vs Dynamic Components Static vs Dynamic Components . . . and when restarting the log bundle : osgi> start 6 D i r e c t o r y R e p o s i t o r y D S constructed It seems our service is being destroyed and recreated each time the referent goes away or comes back. Why?? c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 62 / 174
  • Static vs Dynamic Components Static Policy By default, DS uses a “static policy” for updating references to services. This means it hides the complexity of dealing with dynamically changing references. We need not worry about service references being replaced while we are using them or using synchronized. It does this simply by destroying and recreating the component instance each time the references change. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 63 / 174
  • Static vs Dynamic Components Efficiency of the Static Policy Much of the time, this isn’t too expensive, and it’s a reasonable trade off. Donald Knuth “Premature optimisation is the root of all evil.” Object instantiation in Java is cheap! However, sometimes this is a bad idea. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 64 / 174
  • Static vs Dynamic Components Efficiency of the Static Policy Dependent Services Graph c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 65 / 174
  • Static vs Dynamic Components Efficiency of the Static Policy Dependent Services Graph c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 66 / 174
  • Static vs Dynamic Components Efficiency of the Static Policy Dependent Services Graph c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 67 / 174
  • Static vs Dynamic Components Efficiency of the Static Policy Dependent Services Graph c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 68 / 174
  • Static vs Dynamic Components Dynamic Policy Fortunately “static” is only the default. We can change it simply by declaring policy=quot;dynamicquot;. Example reference Element < r e f e r e n c e name=quot; LOG quot; i n t e r f a c e=quot; org . example . osgi . log . Log quot; b i n d=quot; setLog quot; u n b i n d=quot; unsetLog quot; c a r d i n a l i t y =quot; 0..1 quot; p o l i c y=quot; dynamic quot; /> But now we need to worry about being “dynamic-safe”. The bind/unbind method might be called while a service method that uses the referent is executing. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 69 / 174
  • Static vs Dynamic Components Coping with Dynamic Effects We already made our log field volatile for visibility purposes. But this is not enough to ensure we can cope with changes to the referred service. Accessing the Referred Service if ( log != null ) { log . log ( Log . INFO , quot; This is a message quot; ) ; } This is a pattern (or anti-pattern!) known as “check-then-act”. It fails because the log reference may be non-null on the first line and then be set to null before the second line executes. Result: NullPointerException. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 70 / 174
  • Static vs Dynamic Components Coping with Dynamic Effects A possible solution is to wrap this code in a synchronized block: Accessing the Referred Service synchronized ( this ) { if ( log != null ) { log . log ( Log . INFO , quot; This is a message quot; ) ; } } Note we would also have to make the bind and unbind methods synchronized. However this reduces the concurrency of our program, and it is unwise to hold a lock when calling a service. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 71 / 174
  • Static vs Dynamic Components Coping with Dynamic Effects It is better to use a small synchronized block just to read the field, then act outside the block: Accessing the Referred Service Outside synchronized Log log = null ; synchronized ( this ) { log = this . log ; } if ( log != null ) { log . log ( Log . INFO , quot; This is a message quot; ) ; } c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 72 / 174
  • Static vs Dynamic Components Coping with Dynamic Effects A lightweight alternative solution is to use an AtomicReference. Accessing the Referred Service AtomicReference<Log> logRef ; // . . . Log log = logRef . get ( ) if ( log != null ) { log . log ( Log . INFO , quot; This is a message quot; ) ; } c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 73 / 174
  • Static vs Dynamic Components Dynamic and Mandatory What is the effect of dynamic policy with a mandatory reference? When a mandatory reference is no longer satisfied, the component must be discarded. When the reference becomes satisfied again, the component is recreated. This is the case for both static and dynamic policy. So does dynamic policy make sense for a mandatory reference? Yes. With dynamic policy, the service reference can be replaced if there is another immediately available. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 74 / 174
  • Static vs Dynamic Components Dynamic and Mandatory What is the effect of dynamic policy with a mandatory reference? When a mandatory reference is no longer satisfied, the component must be discarded. When the reference becomes satisfied again, the component is recreated. This is the case for both static and dynamic policy. So does dynamic policy make sense for a mandatory reference? Yes. With dynamic policy, the service reference can be replaced if there is another immediately available. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 74 / 174
  • Static vs Dynamic Components Dynamic and Mandatory What is the effect of dynamic policy with a mandatory reference? When a mandatory reference is no longer satisfied, the component must be discarded. When the reference becomes satisfied again, the component is recreated. This is the case for both static and dynamic policy. So does dynamic policy make sense for a mandatory reference? Yes. With dynamic policy, the service reference can be replaced if there is another immediately available. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 74 / 174
  • Static vs Dynamic Components Dynamic and Mandatory What is the effect of dynamic policy with a mandatory reference? When a mandatory reference is no longer satisfied, the component must be discarded. When the reference becomes satisfied again, the component is recreated. This is the case for both static and dynamic policy. So does dynamic policy make sense for a mandatory reference? Yes. With dynamic policy, the service reference can be replaced if there is another immediately available. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 74 / 174
  • Static vs Dynamic Components Dynamic Service Replacement Beware! When a service is dynamically replaced, SCR first calls the bind method with the new service, then calls the unbind method with the old service. Therefore the na¨ unbind method as follows is wrong: ıve protected synchronized void unbind ( Log log ) { this . log = null ; } c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 75 / 174
  • Static vs Dynamic Components Dynamic Service Replacement To do this correctly in a synchronized block we would have to check the value as follows: Correct Unbinding with synchronized protected synchronized void unsetLog ( Log log ) { if ( this . log == log ) this . log = null ; } c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 76 / 174
  • Static vs Dynamic Components Dynamic Service Replacement With AtomicReference it can be done more elegantly: Correct Unbinding with AtomicReference protected void unsetLog ( Log log ) { logRef . compareAndSet ( log , null ) ; } This atomically sets the value of logRef to null iff the current value is log. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 77 / 174
  • Static vs Dynamic Components Multiple References So far we have looked only at unary references, 0..1 or 1..1. We noted that 0..n and 1..n also exist. At this point there is nothing difficult or surprising in going from unary to multiple references. With a multiple reference the bind/unbind methods are called for each individual service instance. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 78 / 174
  • Static vs Dynamic Components Multiple References So far we have looked only at unary references, 0..1 or 1..1. We noted that 0..n and 1..n also exist. At this point there is nothing difficult or surprising in going from unary to multiple references. With a multiple reference the bind/unbind methods are called for each individual service instance. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 78 / 174
  • Static vs Dynamic Components Multiple References So far we have looked only at unary references, 0..1 or 1..1. We noted that 0..n and 1..n also exist. At this point there is nothing difficult or surprising in going from unary to multiple references. With a multiple reference the bind/unbind methods are called for each individual service instance. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 78 / 174
  • Static vs Dynamic Components Multiple References So far we have looked only at unary references, 0..1 or 1..1. We noted that 0..n and 1..n also exist. At this point there is nothing difficult or surprising in going from unary to multiple references. With a multiple reference the bind/unbind methods are called for each individual service instance. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 78 / 174
  • Static vs Dynamic Components Multiple References Important Multiple references must use the dynamic policy. Static policy simply makes no sense. Thread safety is therefore important and unavoidable. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 79 / 174
  • Publishing a Service Contents Introduction 5 A Minimal Example 6 Activation and Deactivation 7 References to Services 8 Optional vs Mandatory Service References 9 Static vs Dynamic Components 10 Publishing a Service 11 Lazy Service Creation 12 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 80 / 174
  • Publishing a Service Publishing a Service The final piece of the puzzle is publishing our component as a service itself. This is done with the <service> element in our XML descriptor. Service Element < s e r v i c e> <p r o v i d e i n t e r f a c e=quot; net ... C o n t a c t R e p o s i t o r y quot; /> </ s e r v i c e> Provide multiple services simply by adding additional <provide> elements. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 81 / 174
  • Publishing a Service Service Properties We can specify service properties using the <property> element. These properties are passed to the component in activation and published to the service registry. Property Element <scr : component xmlns : scr=quot; http :// www . osgi . org / xmlns / scr / v1 .1.0 quot;> <imple mentati on class=quot; ... quot;/> <property name=quot; foo quot; value=quot; bar quot;/> .... c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 82 / 174
  • Publishing a Service Lab 2 : Using the Dynamic Policy Switch both references in the directoryRepository.xml to use 1 dynamic policy. Fix the component implementation to properly reflect dynamics. 2 Repeat the tests from the previous exercise. 3 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 83 / 174
  • Publishing a Service Lab 2 : Publishing as a Service Publish the component under the ContactRepository interface. 1 Set the service property name to the value Directory. 2 Check the output of the services command in the console. 3 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 84 / 174
  • Publishing a Service Expected Results osgi> start 7 osgi> services ... { net . e cl ip se t ra in i ng . osgi . contacts . api . C o n t a c t R e p o s i t o r y}= { name=Directory , dirName=contacts , component . name=DirectoryRepository , component . id =1 , service . id=35} Registered by bundle : contacts . core . direc tory_ds _0 . 0 . 0 [ 7 ] No bundles using service . ... c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 85 / 174
  • Publishing a Service Two Important Points The service appears to have been registered by our bundle even 1 though it was really registered by SCR. Other bundles just see a normal service, they are not aware we are using DS. Something was missing in the output on the previous slide. . . the 2 print statement from the constructor, “DirectoryRepositoryDS constructed”. Why? c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 86 / 174
  • Lazy Service Creation Contents Introduction 5 A Minimal Example 6 Activation and Deactivation 7 References to Services 8 Optional vs Mandatory Service References 9 Static vs Dynamic Components 10 Publishing a Service 11 Lazy Service Creation 12 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 87 / 174
  • Lazy Service Creation Lazy Service Creation By default, SCR creates “delayed services”. These are services that are registered in the service registry but the implementation object has not yet been instantiated. It will be instantiated “on-demand” when the first client attempts to actually use the service. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 88 / 174
  • Lazy Service Creation The Problem with Eager Service Registration Programmatic registration from a bundle activator results in services being registered eagerly. As soon as our bundle starts, our service is created. But we have no idea if any consumer will ever need our service! c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 89 / 174
  • Lazy Service Creation The Problem with Eager Service Registration Programmatic registration from a bundle activator results in services being registered eagerly. As soon as our bundle starts, our service is created. But we have no idea if any consumer will ever need our service! c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 89 / 174
  • Lazy Service Creation The Problem with Eager Service Registration Programmatic registration from a bundle activator results in services being registered eagerly. As soon as our bundle starts, our service is created. But we have no idea if any consumer will ever need our service! c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 89 / 174
  • Lazy Service Creation The Cost of Eager Service Registration Most simple service objects are cheap to create. . . . . . but the class loader for the bundle is not quite so cheap! Hundreds of bundles providing services that are never used creates significant classloading overhead. If our bundle has no activator and uses “delayed” services, there is no need for the framework to create a classloader until the service is actually used. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 90 / 174
  • Lazy Service Creation Immediate Services Delayed creation is the default for services, but it can be turned off. Service components can be declared with immediate=quot;truequot; to make SCR instantiate them immediately (assuming their dependencies are satisfied). Non-service components are always “immediate”. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 91 / 174
  • Part III Spring-DM c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 92 / 174
  • Introduction Contents Introduction 13 A Minimal Example 14 Activation and Deactivation 15 References to Services 16 Optional vs Mandatory Service References 17 Publishing Services 18 Advantages and Disadvantages 19 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 93 / 174
  • Introduction Spring Dynamic Modules (DM) The Spring Framework is an extremely popular Dependency Injection (DI) framework. It is widely used in many Java deployment scenarios, but particularly in J2EE. It has a strong focus on POJOs, interface-based development, and testability. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 94 / 174
  • Introduction The Spring Framework Spring creates a container called the Application Context. Within the container are multiple beans. Spring Application Context Customer Manager Customer DAO DataSource Log c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 95 / 174
  • Introduction Spring Beans Beans in Spring tend to be pure POJOs following the JavaBean pattern. CustomerDAO public class CustomerDAO { private DataSource dataSource ; private Log log ; public void setDataSource ( DataSource dataSource ) { this . dataSource = dataSource ; } public void setLog ( Log log ) { this . log = log ; } // . . . } c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 96 / 174
  • Introduction Spring XML Configuration Beans are instantiated, configured and “wired” together using an XML declaration file. Spring XML <b e a n s> <bean i d=quot; customerDb quot; c l a s s=quot; org . spring ... DbDataSource quot;> <p r o p e r t y name=quot; url quot; v a l u e=quot; j d b c : h s q l d b : h s q l : // localhost quot; /> <p r o p e r t y name=quot; username quot; v a l u e=quot; sa quot; /> </ bean> <bean i d=quot; customerDao quot; c l a s s=quot; org . example ... CustomerDAO quot;> <p r o p e r t y name=quot; dataSource quot; r e f=quot; customerDb quot; /> </ bean> </ b e a n s> c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 97 / 174
  • Introduction Spring Framework Limitations Spring works well and a huge number of components and patterns are available. However it can be painful to scale up to large complex applications. Thousands of beans in a flat namespace. Start-up ordering concerns. Lack of modularity. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 98 / 174
  • Introduction Spring Dynamic Modules Spring-DM uses OSGi to modularise large Spring applications. Does not fundamentally change the Spring container model. . . just allows the creation of multiple interoperating containers. Spring-DM c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 99 / 174
  • Introduction Bridging Application Contexts Spring-DM allows a large Application Context to be broken down into many small ones. It also offers “glue” between App Contexts based on importing and exporting beans. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 100 / 174
  • Introduction Bridging Application Contexts Exporting a Bean c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 101 / 174
  • Introduction Bridging Application Contexts Importing a Bean c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 102 / 174
  • Introduction Bridging Application Contexts Exporting and Importing a Bean c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 103 / 174
  • A Minimal Example Contents Introduction 13 A Minimal Example 14 Activation and Deactivation 15 References to Services 16 Optional vs Mandatory Service References 17 Publishing Services 18 Advantages and Disadvantages 19 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 104 / 174
  • A Minimal Example A Minimal Example minimal.xml <?xml version=quot; 1.0 quot; encoding=quot; UTF -8 quot;?> <beans xmlns=quot; http :// www . s pr in g fr am ew o rk . org / schema / beans quot;/> <bean id=quot; hello quot; class=quot; org . example . osgi . ds . HelloCom ponent quot;/> </beans> c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 105 / 174
  • A Minimal Example Spring-DM Application Context Like DS, Spring-DM uses an extender bundle which looks for XML files. Placed in a common location (META-INF/spring/*.xml) or referenced by a Spring-Context manifest header. MANIFEST.MF Spring−Context : config / context . xml , config / context−osgi . xml c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 106 / 174
  • A Minimal Example Running the Example We need to install rather more bundles for Spring-DM to work... 12! One of them is the extender bundle and must be started: org.springframework.osgi.extender <version>.jar Starting Spring-DM osgi> ss ... 17 RESOLVED org . s pr in gf r am ew or k . osgi . extender_1 . 2 . 0 . m2 ... osgi> start 17 INFO [ org . spring . . . ] − Starting [ . . . extender ] bundle v . [ 1 . 2 . 0 . m2 ] INFO [ org . spring . . . ] − No custom extender configuration detected . . . INFO [ org . spring . . . ] − Initializing Timer c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 107 / 174
  • A Minimal Example Spring-DM Application Context Unlike DS, each XML file is not a single component. Spring-DM creates one Application Context for the whole bundle. The contents is the union of all the listed XML files. Some parts of this XML will be OSGi-specific. Best Practice OSGi-specific parts of the Spring XML should be placed in a separate file. This enables re-use of the generics parts when used in a non-OSGi runtime. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 108 / 174
  • Activation and Deactivation Contents Introduction 13 A Minimal Example 14 Activation and Deactivation 15 References to Services 16 Optional vs Mandatory Service References 17 Publishing Services 18 Advantages and Disadvantages 19 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 109 / 174
  • Activation and Deactivation Activation and Deactivation There are no default method names for activation and deactivation, we must specify them via the bean declaration: Init and Destroy Methods <bean id=quot; polling quot; class=quot; org . example . osgi . dm . P o l l i n g C o m p o n e n t quot; init−method=quot; activate quot; destroy−method=quot; deactivate quot;> <property name=quot; period quot; value=quot; 2000 quot;/> </bean> These methods are always zero-arg. Configuration is supplied via our setter methods, or constructor injection. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 110 / 174
  • References to Services Contents Introduction 13 A Minimal Example 14 Activation and Deactivation 15 References to Services 16 Optional vs Mandatory Service References 17 Publishing Services 18 Advantages and Disadvantages 19 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 111 / 174
  • References to Services References to Services “Importing” a bean from another context means referencing it as a service. Service Imports < o s g i : r e f e r e n c e i d=quot; imp ortedLo gSvc quot; i n t e r f a c e=quot; org . osgi . service . log . LogService quot; /> <bean i d=quot; l o g g i n g P o l l i n g B e a n quot; c l a s s=quot; org . example ... quot;> <p r o p e r t y name=quot; log quot; r e f=quot; im portedLo gSvc quot; /> </ bean> The reference element looks just like another bean, and can be referenced by other beans using its ID as normal. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 112 / 174
  • References to Services Proxy Beans In fact the reference really is just another bean. It is a generated proxy that continuously tracks the backing service. The proxy is injected once into each bean that refers to it. Important! Spring-DM does not directly inject service objects into its beans, but injects a proxy object. The reference from the bean to the proxy remains constant, so the bean does not have to deal with the dynamics. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 113 / 174
  • References to Services Dynamics and Thread Safety This swaps one problem – thread safety – for another: the beans using the service have no idea whether the service is presently available. What happens when a bean calls the proxy and no backing service is present? DM uses an (optional) timeout to wait for a suitable service to appear. Ultimately it must throw an unchecked exception, ServiceUnavilableException. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 114 / 174
  • References to Services Listening to Services This is nice and simple, but sometimes we really need to know the state of the service. For example we might be able to offer alternative functionality when the backing service is unavailable. E.g. when there is no LogService, print to the console. For this we can nominate a bean as a service listener. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 115 / 174
  • References to Services Listening to Services Service Listener <osgi : reference id=quot; imp ortedLo gSvc quot; interface=quot; org . osgi . service . log . LogService quot;> <listener ref=quot; log quot; bind−method=quot; setLog quot; unbind−method=quot; unsetLog quot;/> </osgi : reference> <bean id=quot; log quot; class=quot; org ... MyLog quot;/> Note that the log bean declaration could be in the non-OSGi part of our application context. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 116 / 174
  • References to Services Listening to Services Just like DS with dynamic policy, the bind/unbind methods can be called concurrently with other methods accessing the bean. Therefore beans configured as listeners must be thread-safe. There is no equivalent of static policy. NB: when service replacement occurs, unbind is not called at all. Unbind is only called when the service is going away and there is no available replacement. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 117 / 174
  • References to Services Multiple References The <reference> element is used for references to single services. For multiple services, there are two choices: <list> and <set>. Each of these gives us a managed collection object which can be injected as a constant bean into other beans. List and Sets <osgi : list id=quot; listenerList quot; interface=quot; org . osgi . service . log . LogListener quot;/> <osgi : set id=quot; listenerSet quot; interface=quot; org . osgi . service . log . LogListener quot;/> <bean id=quot; log quot; class=quot; org ... MyLog quot;> <property name=quot; listeners quot; ref=quot; listenerList quot;/> </bean> c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 118 / 174
  • References to Services Multiple References Spring-DM adds and removes service objects in this collection transparently. A listener bean can be nominated, as with singular references. Iterators over these collections are stable. If a call to hasNext returns true then the subsequent call to next is guaranteed to return non-null. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 119 / 174
  • Optional vs Mandatory Service References Contents Introduction 13 A Minimal Example 14 Activation and Deactivation 15 References to Services 16 Optional vs Mandatory Service References 17 Publishing Services 18 Advantages and Disadvantages 19 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 120 / 174
  • Optional vs Mandatory Service References Mandatory Dependencies Like DS, Spring-DM supports both optional and mandatory services via a simple declaration. Optional Service Reference <osgi : reference id=quot; logSvc quot; interface=quot; org ... log . LogService quot; cardinality=quot; 0..1 quot;/> <osgi : list id=quot; listeners quot; interface=quot; org ... log . LogListener quot; cardinality=quot; 0.. N quot;/> The default cardinality can be controlled in the outer <beans> element, but the default default is 1..1 or 1..N, i.e. mandatory. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 121 / 174
  • Optional vs Mandatory Service References Mandatory Dependencies If there are any unsatisfied mandatory references, the entire application context will not start. Obviously we cannot have a mandatory ref to a service exported by the same application context! If a mandatory reference becomes unsatisfied, the application context is not deactivated and no active components will be deactivated. Instead any service exports that depend on the reference (whether directly or transitively) will be unregistered. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 122 / 174
  • Publishing Services Contents Introduction 13 A Minimal Example 14 Activation and Deactivation 15 References to Services 16 Optional vs Mandatory Service References 17 Publishing Services 18 Advantages and Disadvantages 19 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 123 / 174
  • Publishing Services Publishing a Service Any bean within the application context can be published as a service. Simple Publication <osgi : service ref=quot; contacts quot; interface=quot; net ... C o n t a c t R e p o s i t o r y quot;/> c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 124 / 174
  • Publishing Services More Publication Options <osgi : service ref=quot; contacts quot;> <osgi : interfaces> <value>net . . . ContactRepository </value> <value>net . . . EventHandler </value> </osgi : interfaces> </osgi : service> <osgi : service ref=quot; contacts quot; auto−export=quot; interfaces quot;/> <osgi : service ref=quot; contacts quot; auto−export=quot; interfaces quot;> <osgi : service−properties> <entry key=quot; foo quot; value=quot; bar quot;/> </osgi : service−properties> </osgi : service> c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 125 / 174
  • Publishing Services Lab 3 : Building the Directory Repository (Setup) Install the Spring-DM bundles: 1 osgi> installDir . . / C o m p o n e n t s S p r i n g D M / bundles Start the Spring-DM Extender bundle 2 (org.springframework.osgi.extender...) osgi> start 17 INFO [ org . spring . . . ] − Starting . . . c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 126 / 174
  • Publishing Services Lab 3 : Building the Directory Repository Complete the implementation of the DirectoryRepositoryDM class 1 – TODO markers explain what is needed. Complete the Spring XML descriptor osgi-context.xml in the 2 spring directory. Add references to the log and listener services. Complete context.xml in the same directory. Add properties wired 3 to the service reference beans. Declare the init and destroy methods on the main component bean. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 127 / 174
  • Publishing Services Lab 3 : Publishing as a Service Publish the component under the ContactRepository interface. 1 Add the property name=Directory. 2 Test as before with the services command and in the Swing GUI. 3 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 128 / 174
  • Advantages and Disadvantages Contents Introduction 13 A Minimal Example 14 Activation and Deactivation 15 References to Services 16 Optional vs Mandatory Service References 17 Publishing Services 18 Advantages and Disadvantages 19 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 129 / 174
  • Advantages and Disadvantages Advantages of Spring-DM Spring-DM is clearly the best approach for modularising an existing Spring-based application. It provides a more mature Dependency Injection framework than DS with features not present in DS (e.g. constructor injection). Stronger focus on POJOs than DS. Standardised in the OSGi compendium in R4.2 (the Blueprint Service). c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 130 / 174
  • Advantages and Disadvantages Disadvantages of Spring-DM Heavy! Twelve JARs totalling 2.1Mb (DS is 170k) The “application context” idea has been adapted to fit the OSGi model and it feels like legacy. The result seems to be higher complexity. No separate bean life-cycle. Lack of laziness. A “standard” but will we ever see a competing implementation? c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 131 / 174
  • Advantages and Disadvantages Lack Of Bean Life-cycle Bundle with 2 Mandatory References A 1..1 1..1 B c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 132 / 174
  • Advantages and Disadvantages Lack Of Bean Life-cycle Partially Satisfied A 1..1 1..1 B The App Context on the left will not be activated because one mandatory reference is missing. However the individual bean A has no missing references. Why not activate just A and the beans that use it? c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 133 / 174
  • Advantages and Disadvantages Lack of Bean Life-cycle When mandatory references become unsatisfied, Spring-DM avoids destroying the application context. Beans that are “active” will not be deactivated. . . even if their “mandatory” references are unsatisfied! Have to use service listeners if there is active state (e.g. a polling thread) which must be deactivated when a referenced service goes away. Why does Spring-DM not deactivate the context? Probably because it is expensive to recreate, rather than because it is the right thing to do. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 134 / 174
  • Advantages and Disadvantages Lack of Laziness The application context is created by the Spring-DM extender bundle as soon as our bundle enters ACTIVE state. This causes classloading events, forcing the framework to create a classloader for our bundle. There does not appear to be a mechanism for deferring this activity until the published services are needed by a consumer. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 135 / 174
  • Part IV Apache iPOJO c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 136 / 174
  • Introduction Contents Introduction 20 A Minimal Example 21 Activation and Deactivation 22 References to Services 23 Dynamics and Thread-Safety 24 Publishing Services 25 Annotation-Based Declarations 26 iPOJO Handlers 27 Advantages and Disadvantages 28 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 137 / 174
  • Introduction Apache iPOJO iPOJO is a sub-project of Apache Felix, however it works on other R4.1 frameworks. Like Spring-DM it focuses on creating a pure POJO programming model for components. It supports individual “bean” component lifecycle. Supports both XML and annotation-based metadata. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 138 / 174
  • A Minimal Example Contents Introduction 20 A Minimal Example 21 Activation and Deactivation 22 References to Services 23 Dynamics and Thread-Safety 24 Publishing Services 25 Annotation-Based Declarations 26 iPOJO Handlers 27 Advantages and Disadvantages 28 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 139 / 174
  • A Minimal Example A Minimal Example Minimal iPOJO Declaration < i p o j o> <component name=quot; hello quot; c l a s s n a m e=quot; org ... HelloCom ponent quot; /> < i n s t a n c e component=quot; hello quot; /> </ i p o j o> iPOJO separates the concepts of a component definition and component instances. The former is like a factory. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 140 / 174
  • A Minimal Example Running the Example iPOJO is just one extender bundle, which must be installed and started: org.apache.felix.ipojo-<version>.jar BUT we need an additional build step – an ANT task which post-processes the bundle JAR. We sill see why this is necessary shortly. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 141 / 174
  • Activation and Deactivation Contents Introduction 20 A Minimal Example 21 Activation and Deactivation 22 References to Services 23 Dynamics and Thread-Safety 24 Publishing Services 25 Annotation-Based Declarations 26 iPOJO Handlers 27 Advantages and Disadvantages 28 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 142 / 174
  • Activation and Deactivation Activation and Deactivation Validate/Invalidate Callbacks <ipojo> <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;> <callback transition=quot; validate quot; method=quot; start quot;/> <callback transition=quot; invalidate quot; method=quot; stop quot;/> </component> <instance component=quot; org ... P o l l i n g C o m p o n e n t quot;/> </ipojo> Like Spring-DM, the activation method is zero-arg, so how are configuration properties set? c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 143 / 174
  • Activation and Deactivation Configuration Properties Properties <ipojo> <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;> <callback transition=quot; validate quot; method=quot; start quot;/> <callback transition=quot; invalidate quot; method=quot; stop quot;/> <properties> <property name=quot; period quot; field=quot; period quot;/> </properties> </component> <instance component=quot; org ... P o l l i n g C o m p o n e n t quot;> <property name=quot; period quot; value=quot; 2000 quot;/> </instance> </ipojo> c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 144 / 174
  • Activation and Deactivation Component Implementation Receiving a Property public class P o l l i n g C o m p o n e n t { private int period ; protected void start ( ) { // . . . } protected void stop ( ) { // . . . } } Direct injection into the private field! c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 145 / 174
  • References to Services Contents Introduction 20 A Minimal Example 21 Activation and Deactivation 22 References to Services 23 Dynamics and Thread-Safety 24 Publishing Services 25 Annotation-Based Declarations 26 iPOJO Handlers 27 Advantages and Disadvantages 28 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 146 / 174
  • References to Services References to Services Referring to a service is done with similar magic. Just declare a private field with the type of the service, and use it! Receiving a Service public class P o l l i n g C o m p o n e n t { private int period ; private LogService log ; protected void start ( ) { if ( log != null ) log . log ( LogService . LOG_INFO , quot; Starting polling quot; ) ; // . . . } c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 147 / 174
  • References to Services Declaring a Service Reference <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;> <requires field=quot; log quot;/> ... </component> c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 148 / 174
  • References to Services Optional Service References References are mandatory by default, to make optional: <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;> <requires field=quot; log quot; optional=quot; true quot;/> ... </component> c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 149 / 174
  • Dynamics and Thread-Safety Contents Introduction 20 A Minimal Example 21 Activation and Deactivation 22 References to Services 23 Dynamics and Thread-Safety 24 Publishing Services 25 Annotation-Based Declarations 26 iPOJO Handlers 27 Advantages and Disadvantages 28 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 150 / 174
  • Dynamics and Thread-Safety Dynamic Services Notice when we used the optional log service: if ( log != null ) log . log ( LogService . LOG_INFO , quot; Starting polling quot; ) ; This is classic check-then-act, and looks completely unsafe! c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 151 / 174
  • Dynamics and Thread-Safety Thread Safety iPOJO “manages” the required synchronisation for us. As soon as a method “touches” a dependency (i.e. an injected field) , iPOJO ensures that these objects are kept until the end of the method. All iPOJO components therefore follow a dynamic policy as defined by DS. But we can write our code without regard for thread-safety. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 152 / 174
  • Dynamics and Thread-Safety Bytecode Instrumentation Perhaps now you see what the additional build step is for. iPOJO makes heavy use of bytecode instrumentation. Methods are generated giving access to private fields. Methods are inspected for access to shared variables, and synchronization instructions inserted. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 153 / 174
  • Publishing Services Contents Introduction 20 A Minimal Example 21 Activation and Deactivation 22 References to Services 23 Dynamics and Thread-Safety 24 Publishing Services 25 Annotation-Based Declarations 26 iPOJO Handlers 27 Advantages and Disadvantages 28 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 154 / 174
  • Publishing Services Publishing Services Publishing is done with the <provides> element. In the simplest cases, just adding this element is all that is necessary: Provides Element <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;> <provides/> </component> This is like Spring-DM’s auto-export=quot;interfacesquot;. Reflection is used to publish under all interfaces implemented by the component. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 155 / 174
  • Publishing Services Service Properties Service properties are added to the provides element as follows: Properties on Provides <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;> <provides> <property name=quot; foo quot; type=quot; string quot; value=quot; bar quot;/> <property name=quot; period quot; field=quot; period quot;/> </provides> </component> Properties can be explicitly provided here, or sourced from fields in the component. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 156 / 174
  • Publishing Services Lab 4 : Building the Directory Repository (Setup) Install the iPOJO bundle: 1 osgi> installDir . . / C om po ne n ts iP OJ O / bundles Start the iPOJO bundle (org.apache.felix.ipojo...) 2 osgi> start 5 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 157 / 174
  • Publishing Services Lab 4 : Building the Directory Repository Complete the implementation of the DirectoryRepositoryiPOJO 1 class – TODO markers explain what is needed. Complete the XML descriptor 2 core.contacts.directory ipojo.xml. Add the following: An optional reference to the log service. A zero-to-many reference to the listener service. Callbacks for the activate/deactivate methods. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 158 / 174
  • Publishing Services Lab 4 : Publishing as a Service Publish the component under the ContactRepository interface. 1 Add the service property name=Directory. 2 Test as before with the services command and in the Swing GUI. 3 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 159 / 174
  • Annotation-Based Declarations Contents Introduction 20 A Minimal Example 21 Activation and Deactivation 22 References to Services 23 Dynamics and Thread-Safety 24 Publishing Services 25 Annotation-Based Declarations 26 iPOJO Handlers 27 Advantages and Disadvantages 28 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 160 / 174
  • Annotation-Based Declarations Annotation-Based Declarations As an alternative to XML, iPOJO supports annotations directly in the component source code. These annotations are retained in the compiled bytecode but not the runtime VM. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 161 / 174
  • Annotation-Based Declarations Annotation-Based Declarations MyCustomerDAO @Component @Provides public class MyCustomerDAO implements CustomerDAO { @Requires private DataSource dataSource ; public Customer loo kupCusto mer ( String name ) { Connection conn = dataSource . getConnection ( ) ; // . . . } @Validate public void starting ( ) { /∗ . . . ∗/ } @Invalidate public void stopping ( ) { /∗ . . . ∗/ } } c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 162 / 174
  • Annotation-Based Declarations Annotation-Based Declarations We can use annotations together with XML. The annotations control the component definition (i.e. <component> element). XML would then be used to produce instances. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 163 / 174
  • iPOJO Handlers Contents Introduction 20 A Minimal Example 21 Activation and Deactivation 22 References to Services 23 Dynamics and Thread-Safety 24 Publishing Services 25 Annotation-Based Declarations 26 iPOJO Handlers 27 Advantages and Disadvantages 28 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 164 / 174
  • iPOJO Handlers iPOJO Handlers DS and Spring-DM use a “monolithic container”, i.e. all the aspects of component control (service refs, service provides, configuration, life-cycle) are implemented in one indivisible runtime (the SCR or Spring-DM extender). iPOJO separates these tasks into individual handlers. Handlers are provided out of the box for the common use-cases (refs, provides, etc.). We can extend the behaviour of iPOJO with custom handlers. Example: JMX instrumentation of component state. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 165 / 174
  • Advantages and Disadvantages Contents Introduction 20 A Minimal Example 21 Activation and Deactivation 22 References to Services 23 Dynamics and Thread-Safety 24 Publishing Services 25 Annotation-Based Declarations 26 iPOJO Handlers 27 Advantages and Disadvantages 28 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 166 / 174
  • Advantages and Disadvantages Advantages of iPOJO Very clean POJO coding style for components. Extreme flexibility through handlers. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 167 / 174
  • Advantages and Disadvantages Disadvantages of iPOJO Bytecode engineering and extensive annotations: are these really still POJOs?? Is the blanket promise to “handle concurrency” really credible? Additional build steps. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 168 / 174
  • Advantages and Disadvantages Thread-Safety Yes, thread-safety is easy to screw up, and Java offers weak support for getting it right. Worse, too many Java programmers are unaccustomed to concurrent programming. Remember, J(2)EE literally forbids the use of threads. Is the solution to delegate to a container? No. Truly re-usable components should not rely on a special container to make them robust, they should be internally robust. Well-written DS components are safe in almost any context. iPOJO components are only safe in iPOJO or a non-threaded environment. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 169 / 174
  • Advantages and Disadvantages Thread-Safety Yes, thread-safety is easy to screw up, and Java offers weak support for getting it right. Worse, too many Java programmers are unaccustomed to concurrent programming. Remember, J(2)EE literally forbids the use of threads. Is the solution to delegate to a container? No. Truly re-usable components should not rely on a special container to make them robust, they should be internally robust. Well-written DS components are safe in almost any context. iPOJO components are only safe in iPOJO or a non-threaded environment. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 169 / 174
  • Advantages and Disadvantages Thread-Safety Yes, thread-safety is easy to screw up, and Java offers weak support for getting it right. Worse, too many Java programmers are unaccustomed to concurrent programming. Remember, J(2)EE literally forbids the use of threads. Is the solution to delegate to a container? No. Truly re-usable components should not rely on a special container to make them robust, they should be internally robust. Well-written DS components are safe in almost any context. iPOJO components are only safe in iPOJO or a non-threaded environment. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 169 / 174
  • Advantages and Disadvantages Thread-Safety Yes, thread-safety is easy to screw up, and Java offers weak support for getting it right. Worse, too many Java programmers are unaccustomed to concurrent programming. Remember, J(2)EE literally forbids the use of threads. Is the solution to delegate to a container? No. Truly re-usable components should not rely on a special container to make them robust, they should be internally robust. Well-written DS components are safe in almost any context. iPOJO components are only safe in iPOJO or a non-threaded environment. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 169 / 174
  • Part V Conclusion c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 170 / 174
  • Component Model Interoperability Contents Component Model Interoperability 29 c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 171 / 174
  • Component Model Interoperability Component Model Interoperability Great News! All of the discussed component models for OSGi (and others e.g. Peaberry for Google Guice) interoperate perfectly at the services level! c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 172 / 174
  • Component Model Interoperability Component Model Interoperability We can easily use a Spring-DM with DS or iPOJO bundles on either side: Exporting and Importing a Bean c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 173 / 174
  • Component Model Interoperability Component Model Interoperability In all cases, the imported and exported artefacts are just services. The use of DS, Spring-DM etc. can be considered merely an implementation detail of each bundle. We are not stuck with our choice of component model until the end of time! We can use third party components developed using other component models. c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 174 / 174