0
Component Oriented Development in OSGi with
         Declarative Services, Spring Dynamic Modules and
                    ...
Part I

                                                Introduction




c 2009 Neil Bartlett & WeigleWilczek GmbH. Releas...
The (Partial) Failure of Object Orientation


 Contents


      The (Partial) Failure of Object Orientation
  1



      W...
The (Partial) Failure of Object Orientation


 The (Partial) Failure of Object Oriented




         One of the primary go...
The (Partial) Failure of Object Orientation


 The (Partial) Failure of Object Oriented




         One of the primary go...
The (Partial) Failure of Object Orientation


 The (Partial) Failure of Object Oriented




  Peter Kriens
  “Object Orien...
The (Partial) Failure of Object Orientation


 What Went Wrong??




         Coupling
         Classes can almost never b...
The (Partial) Failure of Object Orientation


 What Went Wrong??




         Coupling
         Classes can almost never b...
The (Partial) Failure of Object Orientation


 What Went Wrong??




         Coupling
         Classes can almost never b...
The (Partial) Failure of Object Orientation


 What Went Wrong??

  Eventually just to use one small class we end up doing...
The (Partial) Failure of Object Orientation


 A Solution?




         Re-use of classes outside their original context i...
The (Partial) Failure of Object Orientation


 A Better Solution




         Component Oriented Programming
         Buil...
What is a Component?


 Contents


      The (Partial) Failure of Object Orientation
  1



      What is a Component?
  2...
What is a Component?


 Components




  But wait, what is a “component”, vs an object?
         Good question! Many attem...
What is a Component?


 Components




  But wait, what is a “component”, vs an object?
         Good question! Many attem...
What is a Component?


 The Most Common Analogy

  Lego?




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the...
What is a Component?


 Lego is a Poor Analogy




         Just dead lumps of plastic.
         All look and act the same...
What is a Component?


 A Better Analogy

  Bees/Animals




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the...
What is a Component?


 Components Are:. . .




         Active participants in the system.
         Aware of and adapt t...
What is a Component?


 Components Are:. . .




         Active participants in the system.
         Aware of and adapt t...
What is a Component?


 Components Are:. . .




         Active participants in the system.
         Aware of and adapt t...
What is a Component?


 Components Are:. . .




         Active participants in the system.
         Aware of and adapt t...
What is a Component?


 What Does the “Environment” Mean?




         Services provided by other components.
         Res...
What is a Component?


 Adaptation




         When the environment is good, the component flourishes.
         When the e...
Implementing Components


 Contents


      The (Partial) Failure of Object Orientation
  1



      What is a Component?
...
Implementing Components


 Implementing Components




         OSGi is the perfect environment for implementing component...
Implementing Components


 Implementing Components




  POJOs
  Components should be implemented as POJOs (Plain Old Java...
Example Application


 Contents


      The (Partial) Failure of Object Orientation
  1



      What is a Component?
  2
...
Example Application


 The Example Application




         The example is a simplistic “contact manager” application.
   ...
Example Application


 Example Architecture

  Services Diagram
                 app.ui.*


                              ...
Example Application


 The Example Application

  Contact Repository Viewer




c 2009 Neil Bartlett & WeigleWilczek GmbH....
Example Application


 Running the Example Application


  First start Equinox using the “Core” launcher from Eclipse’s Ru...
Example Application


 Installing a Basic Contact Repository



  Equinox Console
  osgi> install file : . . / ComponentsR...
Example Application


 The “Raw” Components




         The ComponentsRaw project contains two repository implementations...
Example Application


 Implementing Components with Raw OSGi

         It is possible to implement components using just t...
Example Application


 Component Frameworks




         We would like to use a framework to ease implementation of
      ...
Example Application


 A More Interesting Component

  For most of this tutorial we will focus on a more interesting and c...
Example Application


 Getting Stuff



         Exercises:
         http://neilbartlett.name/downloads/eclipsecon2009/labs...
Part II

                                         Declarative Services




c 2009 Neil Bartlett & WeigleWilczek GmbH. Rele...
Introduction


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivation
  7


 ...
Introduction


 Declarative Services


  “Declarative Services” (DS) is a specification from the OSGi
  Compendium, section...
Introduction


 Declarative Services




         There are significant improvements in DS in OSGi R4.2.
         Many of t...
Introduction


 Declarative Services



  What does the SCR extender bundle do on our behalf?
         Creates Components....
Introduction


 Declarative Services



  What does the SCR extender bundle do on our behalf?
         Creates Components....
Introduction


 Declarative Services



  What does the SCR extender bundle do on our behalf?
         Creates Components....
Introduction


 Declarative Services



  What does the SCR extender bundle do on our behalf?
         Creates Components....
Introduction


 Declarative Services



  What does the SCR extender bundle do on our behalf?
         Creates Components....
A Minimal Example


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivation
  ...
A Minimal Example


 A Minimal Component Declaration



  minimal.xml
 <? xml version=quot; 1.0 quot; e n c o d i n g=quot...
A Minimal Example


 A Minimal Component Declaration




         This declaration requests SCR to simply instantiate the ...
A Minimal Example


 A Minimal Component Declaration




  MANIFEST.MF
  Bundle−SymbolicName : mybundle
  Bundle−Version :...
A Minimal Example


 A Minimal Component Declaration


  HelloComponent
  package org . example . osgi . ds ;

  public cl...
A Minimal Example


 Building the Bundle


  Internal Bundle Structure
  minimal_ds . jar /
      META−INF /
            M...
A Minimal Example


 Running the Example




  We need to install the SCR bundle from Equinox, and also a utility bundle
 ...
A Minimal Example


 Running the Example
  Console
  osgi> install file : org . eclipse . equinox . ds_1 . 1 . 0 . v200901...
A Minimal Example


 Running the Example

  Now install and start the example bundle:
  Console
  osgi> install file : min...
Activation and Deactivation


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deact...
Activation and Deactivation


 Activation and Deactivation



         That was a very long-winded way to merely instantia...
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 {

  ...
Activation and Deactivation


 Activation and Deactivation




         SCR will find our activate/deactivate methods autom...
Activation and Deactivation


 Activation and Deactivation




  Why didn’t we just write this as a bundle activator??
   ...
References to Services


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivati...
References to Services


 References to Services




         Using lower level APIs we must write a lot of “glue” code to...
References to Services


 References to Services

  Example reference Element
        < r e f e r e n c e name=quot; LOG q...
Optional vs Mandatory Service References


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activat...
Optional vs Mandatory Service References


 Optional vs Mandatory References




         We previously discussed the idea...
Optional vs Mandatory Service References


 Optional Service Reference


  Example reference Element
        < r e f e r e...
Optional vs Mandatory Service References


  Lab 1 : Building the Directory Repository (Setup)

         Install and start...
Optional vs Mandatory Service References


  Lab 1 : Building the Directory Repository



         Complete the implementa...
Optional vs Mandatory Service References


  Lab 1 : Testing the Component




         Build the bundle using the supplie...
Static vs Dynamic Components


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deac...
Static vs Dynamic Components


 Static vs Dynamic Components




  If we stop the log bundle (org.eclipse.equinox.log...) ...
Static vs Dynamic Components


 Static vs Dynamic Components



  . . . and when restarting the log bundle :

  osgi> star...
Static vs Dynamic Components


 Static Policy



         By default, DS uses a “static policy” for updating references to...
Static vs Dynamic Components


 Efficiency of the Static Policy



         Much of the time, this isn’t too expensive, and ...
Static vs Dynamic Components


 Efficiency of the Static Policy

  Dependent Services Graph




c 2009 Neil Bartlett & Weigl...
Static vs Dynamic Components


 Efficiency of the Static Policy

  Dependent Services Graph




c 2009 Neil Bartlett & Weigl...
Static vs Dynamic Components


 Efficiency of the Static Policy

  Dependent Services Graph




c 2009 Neil Bartlett & Weigl...
Static vs Dynamic Components


 Efficiency of the Static Policy

  Dependent Services Graph




c 2009 Neil Bartlett & Weigl...
Static vs Dynamic Components


 Dynamic Policy

         Fortunately “static” is only the default. We can change it simply...
Static vs Dynamic Components


 Coping with Dynamic Effects

         We already made our log field volatile for visibility ...
Static vs Dynamic Components


 Coping with Dynamic Effects

         A possible solution is to wrap this code in a synchro...
Static vs Dynamic Components


 Coping with Dynamic Effects


         It is better to use a small synchronized block just ...
Static vs Dynamic Components


 Coping with Dynamic Effects


         A lightweight alternative solution is to use an Atom...
Static vs Dynamic Components


 Dynamic and Mandatory


  What is the effect of dynamic policy with a mandatory reference?
...
Static vs Dynamic Components


 Dynamic and Mandatory


  What is the effect of dynamic policy with a mandatory reference?
...
Static vs Dynamic Components


 Dynamic and Mandatory


  What is the effect of dynamic policy with a mandatory reference?
...
Static vs Dynamic Components


 Dynamic and Mandatory


  What is the effect of dynamic policy with a mandatory reference?
...
Static vs Dynamic Components


 Dynamic Service Replacement



  Beware!
  When a service is dynamically replaced, SCR firs...
Static vs Dynamic Components


 Dynamic Service Replacement



         To do this correctly in a synchronized block we wo...
Static vs Dynamic Components


 Dynamic Service Replacement



         With AtomicReference it can be done more elegantly...
Static vs Dynamic Components


 Multiple References



         So far we have looked only at unary references, 0..1 or 1....
Static vs Dynamic Components


 Multiple References



         So far we have looked only at unary references, 0..1 or 1....
Static vs Dynamic Components


 Multiple References



         So far we have looked only at unary references, 0..1 or 1....
Static vs Dynamic Components


 Multiple References



         So far we have looked only at unary references, 0..1 or 1....
Static vs Dynamic Components


 Multiple References




  Important
  Multiple references must use the dynamic policy. Sta...
Publishing a Service


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivation...
Publishing a Service


 Publishing a Service


         The final piece of the puzzle is publishing our component as a serv...
Publishing a Service


 Service Properties


  We can specify service properties using the <property> element. These
  pro...
Publishing a Service


  Lab 2 : Using the Dynamic Policy




         Switch both references in the directoryRepository.x...
Publishing a Service


  Lab 2 : Publishing as a Service




         Publish the component under the ContactRepository in...
Publishing a Service


 Expected Results



  osgi> start 7

  osgi> services
  ...
  { net . e cl ip se t ra in i ng . os...
Publishing a Service


 Two Important Points




         The service appears to have been registered by our bundle even
 ...
Lazy Service Creation


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivatio...
Lazy Service Creation


 Lazy Service Creation




         By default, SCR creates “delayed services”.
         These are...
Lazy Service Creation


 The Problem with Eager Service Registration




         Programmatic registration from a bundle ...
Lazy Service Creation


 The Problem with Eager Service Registration




         Programmatic registration from a bundle ...
Lazy Service Creation


 The Problem with Eager Service Registration




         Programmatic registration from a bundle ...
Lazy Service Creation


 The Cost of Eager Service Registration



         Most simple service objects are cheap to creat...
Lazy Service Creation


 Immediate Services




  Delayed creation is the default for services, but it can be turned off.
 ...
Part III

                                                  Spring-DM




c 2009 Neil Bartlett & WeigleWilczek GmbH. Relea...
Introduction


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deactivation
 15


 ...
Introduction


 Spring Dynamic Modules (DM)




         The Spring Framework is an extremely popular Dependency Injection...
Introduction


 The Spring Framework
  Spring creates a container called the Application Context. Within the
  container a...
Introduction


 Spring Beans

  Beans in Spring tend to be pure POJOs following the JavaBean pattern.
  CustomerDAO
  publ...
Introduction


 Spring XML Configuration

  Beans are instantiated, configured and “wired” together using an XML
  declarati...
Introduction


 Spring Framework Limitations



         Spring works well and a huge number of components and patterns ar...
Introduction


 Spring Dynamic Modules

         Spring-DM uses OSGi to modularise large Spring applications.
         Doe...
Introduction


 Bridging Application Contexts




         Spring-DM allows a large Application Context to be broken down ...
Introduction


 Bridging Application Contexts


  Exporting a Bean




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released...
Introduction


 Bridging Application Contexts


  Importing a Bean




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released...
Introduction


 Bridging Application Contexts


  Exporting and Importing a Bean




c 2009 Neil Bartlett & WeigleWilczek ...
A Minimal Example


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deactivation
 1...
A Minimal Example


 A Minimal Example



  minimal.xml
 <?xml version=quot; 1.0 quot; encoding=quot; UTF -8 quot;?>
 <bea...
A Minimal Example


 Spring-DM Application Context



         Like DS, Spring-DM uses an extender bundle which looks for ...
A Minimal Example


 Running the Example

         We need to install rather more bundles for Spring-DM to work... 12!
   ...
A Minimal Example


 Spring-DM Application Context



         Unlike DS, each XML file is not a single component.
        ...
Activation and Deactivation


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deact...
Activation and Deactivation


 Activation and Deactivation


  There are no default method names for activation and deacti...
References to Services


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deactivati...
References to Services


 References to Services


         “Importing” a bean from another context means referencing it a...
References to Services


 Proxy Beans



         In fact the reference really is just another bean. It is a generated
   ...
References to Services


 Dynamics and Thread Safety



         This swaps one problem – thread safety – for another: the...
References to Services


 Listening to Services




         This is nice and simple, but sometimes we really need to know...
References to Services


 Listening to Services


  Service Listener
 <osgi : reference id=quot; imp ortedLo gSvc quot;
  ...
References to Services


 Listening to Services



         Just like DS with dynamic policy, the bind/unbind methods can ...
References to Services


 Multiple References

         The <reference> element is used for references to single services....
References to Services


 Multiple References




         Spring-DM adds and removes service objects in this collection
 ...
Optional vs Mandatory Service References


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activat...
Optional vs Mandatory Service References


 Mandatory Dependencies


         Like DS, Spring-DM supports both optional an...
Optional vs Mandatory Service References


 Mandatory Dependencies



         If there are any unsatisfied mandatory refer...
Publishing Services


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deactivation
...
Publishing Services


 Publishing a Service




         Any bean within the application context can be published as a ser...
Publishing Services


 More Publication Options


 <osgi : service ref=quot; contacts quot;>
     <osgi : interfaces>
    ...
Publishing Services


  Lab 3 : Building the Directory Repository (Setup)


         Install the Spring-DM bundles:
     1...
Publishing Services


  Lab 3 : Building the Directory Repository



         Complete the implementation of the Directory...
Publishing Services


  Lab 3 : Publishing as a Service




         Publish the component under the ContactRepository int...
Advantages and Disadvantages


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deac...
Advantages and Disadvantages


 Advantages of Spring-DM



         Spring-DM is clearly the best approach for modularisin...
Advantages and Disadvantages


 Disadvantages of Spring-DM



         Heavy! Twelve JARs totalling 2.1Mb (DS is 170k)
   ...
Advantages and Disadvantages


 Lack Of Bean Life-cycle


  Bundle with 2 Mandatory References

                          ...
Advantages and Disadvantages


 Lack Of Bean Life-cycle
  Partially Satisfied

                                A


        ...
Advantages and Disadvantages


 Lack of Bean Life-cycle


         When mandatory references become unsatisfied, Spring-DM ...
Advantages and Disadvantages


 Lack of Laziness




         The application context is created by the Spring-DM extender...
Part IV

                                              Apache iPOJO




c 2009 Neil Bartlett & WeigleWilczek GmbH. Release...
Introduction


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactivation
 22

     ...
Introduction


 Apache iPOJO



         iPOJO is a sub-project of Apache Felix, however it works on other
         R4.1 f...
A Minimal Example


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactivation
 22

...
A Minimal Example


 A Minimal Example


  Minimal iPOJO Declaration
 < i p o j o>
        <component name=quot; hello quo...
A Minimal Example


 Running the Example




  iPOJO is just one extender bundle, which must be installed and started:
   ...
Activation and Deactivation


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactiva...
Activation and Deactivation


 Activation and Deactivation


  Validate/Invalidate Callbacks
 <ipojo>
     <component clas...
Activation and Deactivation


 Configuration Properties


  Properties
 <ipojo>
     <component classname=quot; org ... P o...
Activation and Deactivation


 Component Implementation

  Receiving a Property
  public class P o l l i n g C o m p o n e...
References to Services


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactivation
...
References to Services


 References to Services

  Referring to a service is done with similar magic. Just declare a priv...
References to Services


 Declaring a Service Reference




        <component classname=quot; org ... P o l l i n g C o m...
References to Services


 Optional Service References



  References are mandatory by default, to make optional:

       ...
Dynamics and Thread-Safety


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactivat...
Dynamics and Thread-Safety


 Dynamic Services




  Notice when we used the optional log service:

                if ( l...
Dynamics and Thread-Safety


 Thread Safety



         iPOJO “manages” the required synchronisation for us.
         As s...
Dynamics and Thread-Safety


 Bytecode Instrumentation



  Perhaps now you see what the additional build step is for.
   ...
Publishing Services


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactivation
 22...
Publishing Services


 Publishing Services


  Publishing is done with the <provides> element. In the simplest cases,
  ju...
Publishing Services


 Service Properties


  Service properties are added to the provides element as follows:
  Propertie...
Publishing Services


  Lab 4 : Building the Directory Repository (Setup)



         Install the iPOJO bundle:
     1



...
Publishing Services


  Lab 4 : Building the Directory Repository



         Complete the implementation of the Directory...
Publishing Services


  Lab 4 : Publishing as a Service




         Publish the component under the ContactRepository int...
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
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
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
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
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
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
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
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
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
Upcoming SlideShare
Loading in...5
×

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

9,309

Published on

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

Published in: Technology
1 Comment
32 Likes
Statistics
Notes
  • 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
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
9,309
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
716
Comments
1
Likes
32
Embeds 0
No embeds

No notes for slide

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

  1. 1. 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
  2. 2. Part I Introduction c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 2 / 174
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 18. 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
  19. 19. 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
  20. 20. 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
  21. 21. 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
  22. 22. 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
  23. 23. 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
  24. 24. 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
  25. 25. 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
  26. 26. 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
  27. 27. 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
  28. 28. 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
  29. 29. 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
  30. 30. 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
  31. 31. 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
  32. 32. 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
  33. 33. 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
  34. 34. 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
  35. 35. 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
  36. 36. 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
  37. 37. 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
  38. 38. 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
  39. 39. Part II Declarative Services c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 32 / 174
  40. 40. 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
  41. 41. 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
  42. 42. 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
  43. 43. 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
  44. 44. 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
  45. 45. 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
  46. 46. 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
  47. 47. 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
  48. 48. 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
  49. 49. 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
  50. 50. 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
  51. 51. 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
  52. 52. 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
  53. 53. 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
  54. 54. 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
  55. 55. 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
  56. 56. 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
  57. 57. 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
  58. 58. 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
  59. 59. 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
  60. 60. 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
  61. 61. 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
  62. 62. 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
  63. 63. 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
  64. 64. 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
  65. 65. 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
  66. 66. 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
  67. 67. 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
  68. 68. 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
  69. 69. 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
  70. 70. 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
  71. 71. 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
  72. 72. 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
  73. 73. 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
  74. 74. 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
  75. 75. 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
  76. 76. 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
  77. 77. 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
  78. 78. 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
  79. 79. 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
  80. 80. 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
  81. 81. 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
  82. 82. 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
  83. 83. 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
  84. 84. 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
  85. 85. 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
  86. 86. 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
  87. 87. 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
  88. 88. 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
  89. 89. 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
  90. 90. 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
  91. 91. 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
  92. 92. 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
  93. 93. 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
  94. 94. 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
  95. 95. 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
  96. 96. 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
  97. 97. 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
  98. 98. 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
  99. 99. 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
  100. 100. 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
  101. 101. 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
  102. 102. 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
  103. 103. 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
  104. 104. 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
  105. 105. 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
  106. 106. 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
  107. 107. 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
  108. 108. 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
  109. 109. 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
  110. 110. 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
  111. 111. Part III Spring-DM c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 92 / 174
  112. 112. 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
  113. 113. 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
  114. 114. 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
  115. 115. 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
  116. 116. 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
  117. 117. 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
  118. 118. 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
  119. 119. 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
  120. 120. 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
  121. 121. 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
  122. 122. 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
  123. 123. 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
  124. 124. 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
  125. 125. 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
  126. 126. 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
  127. 127. 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
  128. 128. 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
  129. 129. 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
  130. 130. 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
  131. 131. 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
  132. 132. 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
  133. 133. 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
  134. 134. 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
  135. 135. 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
  136. 136. 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
  137. 137. 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
  138. 138. 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
  139. 139. 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
  140. 140. 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
  141. 141. 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
  142. 142. 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
  143. 143. 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
  144. 144. 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
  145. 145. 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
  146. 146. 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
  147. 147. 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
  148. 148. 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
  149. 149. 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
  150. 150. 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
  151. 151. 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
  152. 152. 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
  153. 153. 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
  154. 154. 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
  155. 155. Part IV Apache iPOJO c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 136 / 174
  156. 156. 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
  157. 157. 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
  158. 158. 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
  159. 159. 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
  160. 160. 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
  161. 161. 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
  162. 162. 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
  163. 163. 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
  164. 164. 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
  165. 165. 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
  166. 166. 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
  167. 167. 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
  168. 168. 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
  169. 169. 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
  170. 170. 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
  171. 171. 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
  172. 172. 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
  173. 173. 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
  174. 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
  175. 175. 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
  176. 176. 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
  177. 177. 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
  178. 178. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×