SlideShare une entreprise Scribd logo
1  sur  196
Télécharger pour lire hors ligne
Component Oriented Development in OSGi with
         Declarative Services, Spring Dynamic Modules and
                           Apache iPOJO

                                  Neil Bartlett1            Heiko Seeberger2

                                                 1 Weigle   Wilczek UK
                                              2 Weigle   Wilczek GmbH


                                                March 24, 2009




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   1 / 174
Part I

                                                Introduction




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   2 / 174
The (Partial) Failure of Object Orientation


 Contents


      The (Partial) Failure of Object Orientation
  1



      What is a Component?
  2



      Implementing Components
  3



      Example Application
  4




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   3 / 174
The (Partial) Failure of Object Orientation


 The (Partial) Failure of Object Oriented




         One of the primary goals of object oriented programming (OOP) was,
         and still is, re-use.
         It has mostly failed in that goal.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   4 / 174
The (Partial) Failure of Object Orientation


 The (Partial) Failure of Object Oriented




         One of the primary goals of object oriented programming (OOP) was,
         and still is, re-use.
         It has mostly failed in that goal.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   4 / 174
The (Partial) Failure of Object Orientation


 The (Partial) Failure of Object Oriented




  Peter Kriens
  “Object Oriented technology was going to change the world. . . we would
  have all these objects in our library and building a new system would be a
  snap. Just get a few classes, bunch them together. . . and voila!”




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   5 / 174
The (Partial) Failure of Object Orientation


 What Went Wrong??




         Coupling
         Classes can almost never be used in isolation – they depend on other
         classes.
         Those classes depend on other packages, which depend on other
         JARs. . .




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   6 / 174
The (Partial) Failure of Object Orientation


 What Went Wrong??




         Coupling
         Classes can almost never be used in isolation – they depend on other
         classes.
         Those classes depend on other packages, which depend on other
         JARs. . .




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   6 / 174
The (Partial) Failure of Object Orientation


 What Went Wrong??




         Coupling
         Classes can almost never be used in isolation – they depend on other
         classes.
         Those classes depend on other packages, which depend on other
         JARs. . .




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   6 / 174
The (Partial) Failure of Object Orientation


 What Went Wrong??

  Eventually just to use one small class we end up doing this:
  Maven




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   7 / 174
The (Partial) Failure of Object Orientation


 A Solution?




         Re-use of classes outside their original context is hard, so. . .
         Give up!
         Leave them where they are, and call over the network!
         This is sometimes called “SOA”.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   8 / 174
The (Partial) Failure of Object Orientation


 A Better Solution




         Component Oriented Programming
         Builds on OOP.
         OOP is not bad, it’s just not the whole answer.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   9 / 174
What is a Component?


 Contents


      The (Partial) Failure of Object Orientation
  1



      What is a Component?
  2



      Implementing Components
  3



      Example Application
  4




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   10 / 174
What is a Component?


 Components




  But wait, what is a “component”, vs an object?
         Good question! Many attempts have been made to define the term.
         The following is not a formal academic definition, just a working
         definition that we have found useful in practice.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   11 / 174
What is a Component?


 Components




  But wait, what is a “component”, vs an object?
         Good question! Many attempts have been made to define the term.
         The following is not a formal academic definition, just a working
         definition that we have found useful in practice.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   11 / 174
What is a Component?


 The Most Common Analogy

  Lego?




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   12 / 174
What is a Component?


 Lego is a Poor Analogy




         Just dead lumps of plastic.
         All look and act the same.
         Not even very re-usable (try using a Duplo block in a Lego Technics
         model!).




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   13 / 174
What is a Component?


 A Better Analogy

  Bees/Animals




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   14 / 174
What is a Component?


 Components Are:. . .




         Active participants in the system.
         Aware of and adapt to their environment.
         May provide services to other components and use services from other
         components.
         Have a life cycle.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   15 / 174
What is a Component?


 Components Are:. . .




         Active participants in the system.
         Aware of and adapt to their environment.
         May provide services to other components and use services from other
         components.
         Have a life cycle.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   15 / 174
What is a Component?


 Components Are:. . .




         Active participants in the system.
         Aware of and adapt to their environment.
         May provide services to other components and use services from other
         components.
         Have a life cycle.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   15 / 174
What is a Component?


 Components Are:. . .




         Active participants in the system.
         Aware of and adapt to their environment.
         May provide services to other components and use services from other
         components.
         Have a life cycle.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   15 / 174
What is a Component?


 What Does the “Environment” Mean?




         Services provided by other components.
         Resource, devices, etc.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   16 / 174
What is a Component?


 Adaptation




         When the environment is good, the component flourishes.
         When the environment is harsh, the component survives.
         When very harsh, the component sleeps or dies.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   17 / 174
Implementing Components


 Contents


      The (Partial) Failure of Object Orientation
  1



      What is a Component?
  2



      Implementing Components
  3



      Example Application
  4




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   18 / 174
Implementing Components


 Implementing Components




         OSGi is the perfect environment for implementing components.
         The module layer allow us to minimise our static dependencies.
         Fewer static dependencies means less stuff that must be present for
         our component to work.
         Services allow our component to interact with other components.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   19 / 174
Implementing Components


 Implementing Components




  POJOs
  Components should be implemented as POJOs (Plain Old Java Objects)
  “glued” together with OSGi services.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   20 / 174
Example Application


 Contents


      The (Partial) Failure of Object Orientation
  1



      What is a Component?
  2



      Implementing Components
  3



      Example Application
  4




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   21 / 174
Example Application


 The Example Application




         The example is a simplistic “contact manager” application.
         Capable of displaying the content of multiple contact “repositories”.
         Each repository appears in its own tab.
         Repositories are implemented as OSGi services.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   22 / 174
Example Application


 Example Architecture

  Services Diagram
                 app.ui.*


                                                                                       0..1
                            0..N
                                                               core.inmemory


                                                                                                      LogService

                                                                                       0..1
                            ContactRepository
                                                                core.directory



                                                        0..N
                         ContactRepository
                             Listener




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.    March 24, 2009   23 / 174
Example Application


 The Example Application

  Contact Repository Viewer




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   24 / 174
Example Application


 Running the Example Application


  First start Equinox using the “Core” launcher from Eclipse’s Run menu.
  Equinox Console
  osgi> install file : contacts_api . jar
  Bundle id is 3

  osgi> install file : swingui . jar
  Bundle id is 4

  osgi> start 4


  The UI should now appear, but with no contact repository tabs.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   25 / 174
Example Application


 Installing a Basic Contact Repository



  Equinox Console
  osgi> install file : . . / ComponentsRaw / contacts . core . inmemory_raw . jar
  Bundle id is 5

  osgi> start 5


  You should see two repositories: “Some Dummies” and “OSGi Nerds”.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   26 / 174
Example Application


 The “Raw” Components




         The ComponentsRaw project contains two repository implementations.
         They are implemented using “raw” OSGi APIs, i.e. ServiceTracker.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   27 / 174
Example Application


 Implementing Components with Raw OSGi

         It is possible to implement components using just the raw OSGi APIs.
         But there are several challenges.
         We need to be cautious to separate component code from OSGi
         “glue”. If OSGi APIs are used in our components it makes them
         difficult to test and non-POJO.
         It is much harder to access services and configuration using raw APIs.
         Small changes in desired behaviour – e.g. a switch from mandatory to
         optional dependence on a service – require large changes in our glue
         code.
         Much of the glue code is repetitive – mostly the same for many
         different components.



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   28 / 174
Example Application


 Component Frameworks




         We would like to use a framework to ease implementation of
         components in OSGi.
         There is more than one to choose from!
         In this tutorial we look at three popular choices: Declarative Services;
         Spring Dynamic Modules; and Apache iPOJO.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   29 / 174
Example Application


 A More Interesting Component

  For most of this tutorial we will focus on a more interesting and complex
  example.
         The DirectoryRepository component implements a contact
         repository backed by files in a directory.
         It is active: it creates a thread to monitor the directory.
         It needs configuration: the directory to monitor.
         It consumes services provided by other components: the log service
         and some listeners.
         It provides a service to other components: the contact repository
         functionality.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   30 / 174
Example Application


 Getting Stuff



         Exercises:
         http://neilbartlett.name/downloads/eclipsecon2009/labs.zip
         Solutions:
         http://neilbartlett.name/downloads/eclipsecon2009/solutions.zip
         These Slides:
         http://neilbartlett.name/downloads/eclipsecon2009/slides.pdf
  NB these are temporary URLs until the files have been uploaded to
  eclipsecon.org.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   31 / 174
Part II

                                         Declarative Services




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   32 / 174
Introduction


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivation
  7


      References to Services
  8


      Optional vs Mandatory Service References
  9


      Static vs Dynamic Components
 10


      Publishing a Service
 11


      Lazy Service Creation
 12



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   33 / 174
Introduction


 Declarative Services


  “Declarative Services” (DS) is a specification from the OSGi
  Compendium, section 112. It was introduced in Release 4.0 and is based
  on the extender model.
         Like all extenders, DS performs tasks on behalf of other bundles.
         The DS spec defines this extender and it is implemented by
         frameworks. The extender bundle itself is called the “Service
         Component Runtime” or SCR.
  The terms DS and SCR are sometimes confused. Remember, DS is the
  specification, SCR is the actual bundle that implements the specification.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   34 / 174
Introduction


 Declarative Services




         There are significant improvements in DS in OSGi R4.2.
         Many of these changes are supported in Equinox 3.5M5+.
         We will use the R4.2 features, but mention when we do so.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   35 / 174
Introduction


 Declarative Services



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


         “Binds” them to services and configuration.
     2


         Manages the component’s lifecycle in response to bound services
     3

         coming and going.
         Optionally, publishes our components as services themselves.
     4




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   36 / 174
Introduction


 Declarative Services



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


         “Binds” them to services and configuration.
     2


         Manages the component’s lifecycle in response to bound services
     3

         coming and going.
         Optionally, publishes our components as services themselves.
     4




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   36 / 174
Introduction


 Declarative Services



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


         “Binds” them to services and configuration.
     2


         Manages the component’s lifecycle in response to bound services
     3

         coming and going.
         Optionally, publishes our components as services themselves.
     4




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   36 / 174
Introduction


 Declarative Services



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


         “Binds” them to services and configuration.
     2


         Manages the component’s lifecycle in response to bound services
     3

         coming and going.
         Optionally, publishes our components as services themselves.
     4




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   36 / 174
Introduction


 Declarative Services



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


         “Binds” them to services and configuration.
     2


         Manages the component’s lifecycle in response to bound services
     3

         coming and going.
         Optionally, publishes our components as services themselves.
     4




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   36 / 174
A Minimal Example


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivation
  7


      References to Services
  8


      Optional vs Mandatory Service References
  9


      Static vs Dynamic Components
 10


      Publishing a Service
 11


      Lazy Service Creation
 12



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   37 / 174
A Minimal Example


 A Minimal Component Declaration



  minimal.xml
 <? xml version=quot; 1.0 quot; e n c o d i n g=quot; UTF -8 quot; ?>
 <s c r : c o m p o n e n t x m l n s : s c r=quot; http: // www . osgi . org / xmlns / scr / v1 .1.0 quot;>

     <i m p l e m e n t a t i o n c l a s s=quot; org . example . osgi . ds . HelloCom ponent quot; />

 </ s c r : c o m p o n e n t>


  NB: the namespace is required in order to use R4.2 features. If not
  included then SCR will default to the prior version.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   38 / 174
A Minimal Example


 A Minimal Component Declaration




         This declaration requests SCR to simply instantiate the named class,
         which must be visible to the declaring bundle’s classloader.
         We need to tell the SCR extender bundle about this file.
         We use a manifest header, Service-Component.
         A comma-separated list of XML declaration files.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   39 / 174
A Minimal Example


 A Minimal Component Declaration




  MANIFEST.MF
  Bundle−SymbolicName : mybundle
  Bundle−Version : 1 . 0 . 0
  Service−Component : OSGI−INF / minimal . xml
  ...




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   40 / 174
A Minimal Example


 A Minimal Component Declaration


  HelloComponent
  package org . example . osgi . ds ;

  public class H elloCom ponent {
     public Hello Componen t ( ) {
        System . out . println ( quot; He lloCompo nent created quot; ) ;
     }

       // . . .
  }


  Note: a Plain Old Java Object (POJO)! No OSGi API dependencies.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   41 / 174
A Minimal Example


 Building the Bundle


  Internal Bundle Structure
  minimal_ds . jar /
      META−INF /
            MANIFEST . MF
      OSGI−INF /
            minimal . xml
      org /
            example /
                osgi /
                      ds /
                                     Hello Compone nt . class




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   42 / 174
A Minimal Example


 Running the Example




  We need to install the SCR bundle from Equinox, and also a utility bundle
  used by Equinox’s SCR:
         org.eclipse.equinox.ds <version>.jar
         org.eclipse.equinox.util <version>.jar
  The SCR bundle needs to be started, but the util bundle does not.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   43 / 174
A Minimal Example


 Running the Example
  Console
  osgi> install file : org . eclipse . equinox . ds_1 . 1 . 0 . v20090112 −1800. jar
  Bundle id is 2

  osgi> install file : org . eclipse . equinox . util_1 . 0 . 1 . v20081205 − 1 8 0 . . .
  Bundle id is 3

  osgi> start 2

  osgi> ss

  Framework is launched .

  id State                  Bundle
                             org . eclipse . osgi_3 . 4 . 2 . R34x_v20080826 −1230
  0   ACTIVE
                             org . eclipse . osgi . services_3 . 2 . 0 . v20081205 −1800
  1   RESOLVED
                             org . eclipse . equinox . ds_1 . 1 . 0 . v20090112 −1800
  2   ACTIVE
                             org . eclipse . equinox . util_1 . 0 . 1 . v20081205 −1800
  3   RESOLVED

  osgi>


c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   44 / 174
A Minimal Example


 Running the Example

  Now install and start the example bundle:
  Console
  osgi> install file : minimal_ds . jar
  Bundle id is 4

  osgi> start 4
  Hello Compone nt created

  osgi>


  Note that SCR ignores our bundle until it is in ACTIVE state. DS bundles
  must be activated even though they often have no bundle activator! This
  is quite different from Eclipse Extensions.



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   45 / 174
Activation and Deactivation


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivation
  7


      References to Services
  8


      Optional vs Mandatory Service References
  9


      Static vs Dynamic Components
 10


      Publishing a Service
 11


      Lazy Service Creation
 12



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   46 / 174
Activation and Deactivation


 Activation and Deactivation



         That was a very long-winded way to merely instantiate a class!
         This component is not useful because it cannot even do anything.
         However, DS allows us to define lifecycle methods.
         We can be notified when the component starts and stops.
         This allows us to do interesting things like start threads, open
         sockets, etc.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   47 / 174
Activation and Deactivation


 An Active Component
  PollingComponent
  public class P o l l i n g C o m p o n e n t {

       private static final int DEFAUL T_PERIOD = 2 0 0 0 ;
       private PollingThread thread ;

       protected void activate ( Map<String , Object> config ) {
          System . out . println ( quot; Polling Component Activated quot; ) ;
          Integer period = ( Integer ) config . get ( quot; period quot; ) ;
          thread = new PollingThread (
                 period != null ? period : DEFAU LT_PERI OD ) ;
          thread . start ( ) ;
       }
       protected void deactivate ( ) {
          System . out . println ( quot; Polling Component Deactivated quot; ) ;
          thread . interrupt ( ) ;
       }
  }



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   48 / 174
Activation and Deactivation


 Activation and Deactivation




         SCR will find our activate/deactivate methods automatically.
         We can call them something else by adding attributes to the XML
         declaration.
  NB: Before R4.2, the method names could not be changed and they had
  to take a parameter of type ComponentContext, from the DS API. This
  broke the pojocity of the component.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   49 / 174
Activation and Deactivation


 Activation and Deactivation




  Why didn’t we just write this as a bundle activator??
         This is a POJO!
         Easier access to configuration – the Map parameter to activate.
         Easier access to service registry – we will see this soon.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   50 / 174
References to Services


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivation
  7


      References to Services
  8


      Optional vs Mandatory Service References
  9


      Static vs Dynamic Components
 10


      Publishing a Service
 11


      Lazy Service Creation
 12



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   51 / 174
References to Services


 References to Services




         Using lower level APIs we must write a lot of “glue” code to bind to
         services.
         DS replaces the glue code with simple declarations.
         We declare references to services.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   52 / 174
References to Services


 References to Services

  Example reference Element
        < r e f e r e n c e name=quot; LOG quot;
                i n t e r f a c e=quot; org . osgi . service . log . LogService quot;
                b i n d=quot; setLog quot; u n b i n d=quot; unsetLog quot; />



  Reference Attributes
                            The name of the reference.
      name
                            The service interface name.
      interface
                            The name of the “set” method associated with the ref-
      bind
                            erence.
                            The name of the “unset” method associated with the
      unbind
                            reference.


c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   53 / 174
Optional vs Mandatory Service References


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivation
  7


      References to Services
  8


      Optional vs Mandatory Service References
  9


      Static vs Dynamic Components
 10


      Publishing a Service
 11


      Lazy Service Creation
 12



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   54 / 174
Optional vs Mandatory Service References


 Optional vs Mandatory References




         We previously discussed the idea of optional and mandatory service
         dependencies.
         To model these with ServiceTracker required completely different
         code patterns.
         With DS we can simply change a single attribute on the reference
         element to switch between optional and mandatory.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   55 / 174
Optional vs Mandatory Service References


 Optional Service Reference


  Example reference Element
        < r e f e r e n c e name=quot; LOG quot;
                i n t e r f a c e=quot; org . osgi . service . log . LogService quot;
                b i n d=quot; setLog quot; u n b i n d=quot; unsetLog quot;
                 c a r d i n a l i t y =quot; 0..1 quot; />



         The default cardinality was “1..1” meaning that we must have
         exactly one instance, i.e. the reference is mandatory.
         A cardinality of “0..1” indicates that either zero or one instance is
         okay, i.e. the reference is optional.
         Yes, “0..n” and “1..n” do exist. We will look at these later.



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   56 / 174
Optional vs Mandatory Service References


  Lab 1 : Building the Directory Repository (Setup)

         Install and start the commands.jar bundle which gives us a useful
     1

         installDir command.

  osgi> install file : commands . jar
  Bundle id is 3

  osgi> start 3


         Install the Contacts API bundle, and the DS bundles:
     2



  osgi> install file : contacts_api . jar
  Bundle id is 4

  osgi> installDir . . / ComponentsDS / bundles




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   57 / 174
Optional vs Mandatory Service References


  Lab 1 : Building the Directory Repository



         Complete the implementation of the DirectoryRepositoryDS class
     1

         – TODO markers explain what is needed.
         Complete the DS XML descriptor directoryRepository.xml in the
     2

         dsxml directory. Remember to add at least:
                 A property element to set the dirName property to the name of the
                 contacts directory.
                 An optional reference to the OSGi log service
                 (org.osgi.service.log.LogService).
                 A zero-to-many reference to the repository listeners.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   58 / 174
Optional vs Mandatory Service References


  Lab 1 : Testing the Component




         Build the bundle using the supplied ANT build.
     1


         Install and start the bundle and also start the SCR extender bundle
     2

         (org.eclipse.equinox.ds...).
         Type the log command to see messages from the component.
     3

         Observe what happens when .contact files are added/removed in
         the contacts directory.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   59 / 174
Static vs Dynamic Components


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivation
  7


      References to Services
  8


      Optional vs Mandatory Service References
  9


      Static vs Dynamic Components
 10


      Publishing a Service
 11


      Lazy Service Creation
 12



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   60 / 174
Static vs Dynamic Components


 Static vs Dynamic Components




  If we stop the log bundle (org.eclipse.equinox.log...) we should see
  something like this:

  osgi> stop 6
  D i r e c t o r y R e p o s i t o r y D S constructed




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   61 / 174
Static vs Dynamic Components


 Static vs Dynamic Components



  . . . and when restarting the log bundle :

  osgi> start 6
  D i r e c t o r y R e p o s i t o r y D S constructed


  It seems our service is being destroyed and recreated each time the
  referent goes away or comes back. Why??




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   62 / 174
Static vs Dynamic Components


 Static Policy



         By default, DS uses a “static policy” for updating references to
         services.
         This means it hides the complexity of dealing with dynamically
         changing references. We need not worry about service references
         being replaced while we are using them or using synchronized.
         It does this simply by destroying and recreating the component
         instance each time the references change.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   63 / 174
Static vs Dynamic Components


 Efficiency of the Static Policy



         Much of the time, this isn’t too expensive, and it’s a reasonable trade
         off.
  Donald Knuth
  “Premature optimisation is the root of all evil.”

         Object instantiation in Java is cheap!
         However, sometimes this is a bad idea.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   64 / 174
Static vs Dynamic Components


 Efficiency of the Static Policy

  Dependent Services Graph




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   65 / 174
Static vs Dynamic Components


 Efficiency of the Static Policy

  Dependent Services Graph




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   66 / 174
Static vs Dynamic Components


 Efficiency of the Static Policy

  Dependent Services Graph




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   67 / 174
Static vs Dynamic Components


 Efficiency of the Static Policy

  Dependent Services Graph




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   68 / 174
Static vs Dynamic Components


 Dynamic Policy

         Fortunately “static” is only the default. We can change it simply by
         declaring policy=quot;dynamicquot;.
  Example reference Element
        < r e f e r e n c e name=quot; LOG quot;
                i n t e r f a c e=quot; org . example . osgi . log . Log quot;
                b i n d=quot; setLog quot; u n b i n d=quot; unsetLog quot;
                 c a r d i n a l i t y =quot; 0..1 quot;
                p o l i c y=quot; dynamic quot; />


         But now we need to worry about being “dynamic-safe”.
         The bind/unbind method might be called while a service method that
         uses the referent is executing.



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   69 / 174
Static vs Dynamic Components


 Coping with Dynamic Effects

         We already made our log field volatile for visibility purposes. But
         this is not enough to ensure we can cope with changes to the referred
         service.
  Accessing the Referred Service
  if ( log != null ) {
        log . log ( Log . INFO , quot; This is a message quot; ) ;
  }


         This is a pattern (or anti-pattern!) known as “check-then-act”.
         It fails because the log reference may be non-null on the first line and
         then be set to null before the second line executes.
         Result: NullPointerException.


c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   70 / 174
Static vs Dynamic Components


 Coping with Dynamic Effects

         A possible solution is to wrap this code in a synchronized block:
  Accessing the Referred Service
  synchronized ( this ) {
      if ( log != null ) {
            log . log ( Log . INFO , quot; This is a message quot; ) ;
      }
  }


         Note we would also have to make the bind and unbind methods
         synchronized.
         However this reduces the concurrency of our program, and it is
         unwise to hold a lock when calling a service.



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   71 / 174
Static vs Dynamic Components


 Coping with Dynamic Effects


         It is better to use a small synchronized block just to read the field,
         then act outside the block:
  Accessing the Referred Service Outside synchronized
  Log log = null ;
  synchronized ( this ) {
        log = this . log ;
  }
  if ( log != null ) {
        log . log ( Log . INFO , quot; This is a message quot; ) ;
  }




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   72 / 174
Static vs Dynamic Components


 Coping with Dynamic Effects


         A lightweight alternative solution is to use an AtomicReference.
  Accessing the Referred Service
  AtomicReference<Log> logRef ;

  // . . .

  Log log = logRef . get ( )
  if ( log != null ) {
        log . log ( Log . INFO , quot; This is a message quot; ) ;
  }




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   73 / 174
Static vs Dynamic Components


 Dynamic and Mandatory


  What is the effect of dynamic policy with a mandatory reference?
         When a mandatory reference is no longer satisfied, the component
         must be discarded. When the reference becomes satisfied again, the
         component is recreated.
         This is the case for both static and dynamic policy. So does dynamic
         policy make sense for a mandatory reference?
         Yes. With dynamic policy, the service reference can be replaced if
         there is another immediately available.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   74 / 174
Static vs Dynamic Components


 Dynamic and Mandatory


  What is the effect of dynamic policy with a mandatory reference?
         When a mandatory reference is no longer satisfied, the component
         must be discarded. When the reference becomes satisfied again, the
         component is recreated.
         This is the case for both static and dynamic policy. So does dynamic
         policy make sense for a mandatory reference?
         Yes. With dynamic policy, the service reference can be replaced if
         there is another immediately available.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   74 / 174
Static vs Dynamic Components


 Dynamic and Mandatory


  What is the effect of dynamic policy with a mandatory reference?
         When a mandatory reference is no longer satisfied, the component
         must be discarded. When the reference becomes satisfied again, the
         component is recreated.
         This is the case for both static and dynamic policy. So does dynamic
         policy make sense for a mandatory reference?
         Yes. With dynamic policy, the service reference can be replaced if
         there is another immediately available.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   74 / 174
Static vs Dynamic Components


 Dynamic and Mandatory


  What is the effect of dynamic policy with a mandatory reference?
         When a mandatory reference is no longer satisfied, the component
         must be discarded. When the reference becomes satisfied again, the
         component is recreated.
         This is the case for both static and dynamic policy. So does dynamic
         policy make sense for a mandatory reference?
         Yes. With dynamic policy, the service reference can be replaced if
         there is another immediately available.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   74 / 174
Static vs Dynamic Components


 Dynamic Service Replacement



  Beware!
  When a service is dynamically replaced, SCR first calls the bind method
  with the new service, then calls the unbind method with the old service.
  Therefore the na¨ unbind method as follows is wrong:
                  ıve
  protected synchronized void unbind ( Log log ) {
      this . log = null ;
  }




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   75 / 174
Static vs Dynamic Components


 Dynamic Service Replacement



         To do this correctly in a synchronized block we would have to check
         the value as follows:
  Correct Unbinding with synchronized
  protected synchronized void unsetLog ( Log log ) {
      if ( this . log == log ) this . log = null ;
  }




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   76 / 174
Static vs Dynamic Components


 Dynamic Service Replacement



         With AtomicReference it can be done more elegantly:
  Correct Unbinding with AtomicReference
  protected void unsetLog ( Log log ) {
      logRef . compareAndSet ( log , null ) ;
  }


         This atomically sets the value of logRef to null iff the current value
         is log.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   77 / 174
Static vs Dynamic Components


 Multiple References



         So far we have looked only at unary references, 0..1 or 1..1.
         We noted that 0..n and 1..n also exist.
         At this point there is nothing difficult or surprising in going from
         unary to multiple references.
         With a multiple reference the bind/unbind methods are called for
         each individual service instance.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   78 / 174
Static vs Dynamic Components


 Multiple References



         So far we have looked only at unary references, 0..1 or 1..1.
         We noted that 0..n and 1..n also exist.
         At this point there is nothing difficult or surprising in going from
         unary to multiple references.
         With a multiple reference the bind/unbind methods are called for
         each individual service instance.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   78 / 174
Static vs Dynamic Components


 Multiple References



         So far we have looked only at unary references, 0..1 or 1..1.
         We noted that 0..n and 1..n also exist.
         At this point there is nothing difficult or surprising in going from
         unary to multiple references.
         With a multiple reference the bind/unbind methods are called for
         each individual service instance.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   78 / 174
Static vs Dynamic Components


 Multiple References



         So far we have looked only at unary references, 0..1 or 1..1.
         We noted that 0..n and 1..n also exist.
         At this point there is nothing difficult or surprising in going from
         unary to multiple references.
         With a multiple reference the bind/unbind methods are called for
         each individual service instance.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   78 / 174
Static vs Dynamic Components


 Multiple References




  Important
  Multiple references must use the dynamic policy. Static policy simply
  makes no sense. Thread safety is therefore important and unavoidable.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   79 / 174
Publishing a Service


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivation
  7


      References to Services
  8


      Optional vs Mandatory Service References
  9


      Static vs Dynamic Components
 10


      Publishing a Service
 11


      Lazy Service Creation
 12



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   80 / 174
Publishing a Service


 Publishing a Service


         The final piece of the puzzle is publishing our component as a service
         itself.
         This is done with the <service> element in our XML descriptor.
  Service Element
        < s e r v i c e>
               <p r o v i d e i n t e r f a c e=quot; net ... C o n t a c t R e p o s i t o r y quot; />
        </ s e r v i c e>


         Provide multiple services simply by adding additional <provide>
         elements.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   81 / 174
Publishing a Service


 Service Properties


  We can specify service properties using the <property> element. These
  properties are passed to the component in activation and published to the
  service registry.
  Property Element
 <scr : component xmlns : scr=quot; http :// www . osgi . org / xmlns / scr / v1 .1.0 quot;>

       <imple mentati on class=quot; ... quot;/>

       <property name=quot; foo quot; value=quot; bar quot;/>
       ....




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   82 / 174
Publishing a Service


  Lab 2 : Using the Dynamic Policy




         Switch both references in the directoryRepository.xml to use
     1

         dynamic policy.
         Fix the component implementation to properly reflect dynamics.
     2


         Repeat the tests from the previous exercise.
     3




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   83 / 174
Publishing a Service


  Lab 2 : Publishing as a Service




         Publish the component under the ContactRepository interface.
     1


         Set the service property name to the value Directory.
     2


         Check the output of the services command in the console.
     3




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   84 / 174
Publishing a Service


 Expected Results



  osgi> start 7

  osgi> services
  ...
  { net . e cl ip se t ra in i ng . osgi . contacts . api . C o n t a c t R e p o s i t o r y}=
        { name=Directory , dirName=contacts ,
        component . name=DirectoryRepository ,
        component . id =1 , service . id=35}
  Registered by bundle : contacts . core . direc tory_ds _0 . 0 . 0 [ 7 ]
  No bundles using service .
  ...




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   85 / 174
Publishing a Service


 Two Important Points




         The service appears to have been registered by our bundle even
     1

         though it was really registered by SCR. Other bundles just see a
         normal service, they are not aware we are using DS.
         Something was missing in the output on the previous slide. . . the
     2

         print statement from the constructor, “DirectoryRepositoryDS
         constructed”. Why?




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   86 / 174
Lazy Service Creation


 Contents

      Introduction
  5


      A Minimal Example
  6


      Activation and Deactivation
  7


      References to Services
  8


      Optional vs Mandatory Service References
  9


      Static vs Dynamic Components
 10


      Publishing a Service
 11


      Lazy Service Creation
 12



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   87 / 174
Lazy Service Creation


 Lazy Service Creation




         By default, SCR creates “delayed services”.
         These are services that are registered in the service registry but the
         implementation object has not yet been instantiated.
         It will be instantiated “on-demand” when the first client attempts to
         actually use the service.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   88 / 174
Lazy Service Creation


 The Problem with Eager Service Registration




         Programmatic registration from a bundle activator results in services
         being registered eagerly.
         As soon as our bundle starts, our service is created.
         But we have no idea if any consumer will ever need our service!




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   89 / 174
Lazy Service Creation


 The Problem with Eager Service Registration




         Programmatic registration from a bundle activator results in services
         being registered eagerly.
         As soon as our bundle starts, our service is created.
         But we have no idea if any consumer will ever need our service!




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   89 / 174
Lazy Service Creation


 The Problem with Eager Service Registration




         Programmatic registration from a bundle activator results in services
         being registered eagerly.
         As soon as our bundle starts, our service is created.
         But we have no idea if any consumer will ever need our service!




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   89 / 174
Lazy Service Creation


 The Cost of Eager Service Registration



         Most simple service objects are cheap to create. . .
         . . . but the class loader for the bundle is not quite so cheap!
         Hundreds of bundles providing services that are never used creates
         significant classloading overhead.
         If our bundle has no activator and uses “delayed” services, there is no
         need for the framework to create a classloader until the service is
         actually used.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   90 / 174
Lazy Service Creation


 Immediate Services




  Delayed creation is the default for services, but it can be turned off.
         Service components can be declared with immediate=quot;truequot; to
         make SCR instantiate them immediately (assuming their
         dependencies are satisfied).
         Non-service components are always “immediate”.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   91 / 174
Part III

                                                  Spring-DM




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   92 / 174
Introduction


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deactivation
 15


      References to Services
 16


      Optional vs Mandatory Service References
 17


      Publishing Services
 18


      Advantages and Disadvantages
 19




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   93 / 174
Introduction


 Spring Dynamic Modules (DM)




         The Spring Framework is an extremely popular Dependency Injection
         (DI) framework.
         It is widely used in many Java deployment scenarios, but particularly
         in J2EE.
         It has a strong focus on POJOs, interface-based development, and
         testability.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   94 / 174
Introduction


 The Spring Framework
  Spring creates a container called the Application Context. Within the
  container are multiple beans.
  Spring Application Context

                                                         Customer
                                                         Manager




                                              Customer
                                                DAO




                                          DataSource                     Log




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   95 / 174
Introduction


 Spring Beans

  Beans in Spring tend to be pure POJOs following the JavaBean pattern.
  CustomerDAO
  public class CustomerDAO {

         private DataSource dataSource ;
         private Log log ;

         public void setDataSource ( DataSource dataSource ) {
              this . dataSource = dataSource ;
         }
         public void setLog ( Log log ) {
              this . log = log ;
         }
         // . . .
  }




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   96 / 174
Introduction


 Spring XML Configuration

  Beans are instantiated, configured and “wired” together using an XML
  declaration file.
  Spring XML
 <b e a n s>
       <bean i d=quot; customerDb quot; c l a s s=quot; org . spring ... DbDataSource quot;>
             <p r o p e r t y name=quot; url quot; v a l u e=quot; j d b c : h s q l d b : h s q l : // localhost quot; />
             <p r o p e r t y name=quot; username quot; v a l u e=quot; sa quot; />
       </ bean>

       <bean i d=quot; customerDao quot; c l a s s=quot; org . example ... CustomerDAO quot;>
              <p r o p e r t y name=quot; dataSource quot; r e f=quot; customerDb quot; />
       </ bean>
 </ b e a n s>




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   97 / 174
Introduction


 Spring Framework Limitations



         Spring works well and a huge number of components and patterns are
         available.
         However it can be painful to scale up to large complex applications.
         Thousands of beans in a flat namespace.
         Start-up ordering concerns.
         Lack of modularity.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   98 / 174
Introduction


 Spring Dynamic Modules

         Spring-DM uses OSGi to modularise large Spring applications.
         Does not fundamentally change the Spring container model. . . just
         allows the creation of multiple interoperating containers.
  Spring-DM




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   99 / 174
Introduction


 Bridging Application Contexts




         Spring-DM allows a large Application Context to be broken down into
         many small ones.
         It also offers “glue” between App Contexts based on importing and
         exporting beans.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   100 / 174
Introduction


 Bridging Application Contexts


  Exporting a Bean




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   101 / 174
Introduction


 Bridging Application Contexts


  Importing a Bean




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   102 / 174
Introduction


 Bridging Application Contexts


  Exporting and Importing a Bean




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   103 / 174
A Minimal Example


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deactivation
 15


      References to Services
 16


      Optional vs Mandatory Service References
 17


      Publishing Services
 18


      Advantages and Disadvantages
 19




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   104 / 174
A Minimal Example


 A Minimal Example



  minimal.xml
 <?xml version=quot; 1.0 quot; encoding=quot; UTF -8 quot;?>
 <beans xmlns=quot; http :// www . s pr in g fr am ew o rk . org / schema / beans quot;/>

        <bean id=quot; hello quot; class=quot; org . example . osgi . ds . HelloCom ponent quot;/>

  </beans>




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   105 / 174
A Minimal Example


 Spring-DM Application Context



         Like DS, Spring-DM uses an extender bundle which looks for XML
         files.
         Placed in a common location (META-INF/spring/*.xml) or
         referenced by a Spring-Context manifest header.
  MANIFEST.MF
  Spring−Context : config / context . xml , config / context−osgi . xml




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   106 / 174
A Minimal Example


 Running the Example

         We need to install rather more bundles for Spring-DM to work... 12!
         One of them is the extender bundle and must be started:
         org.springframework.osgi.extender <version>.jar

  Starting Spring-DM

  osgi> ss
  ...
  17 RESOLVED               org . s pr in gf r am ew or k . osgi . extender_1 . 2 . 0 . m2
  ...

  osgi> start 17
  INFO [ org . spring . . . ] − Starting [ . . . extender ] bundle v . [ 1 . 2 . 0 . m2 ]
  INFO [ org . spring . . . ] − No custom extender configuration detected . . .
  INFO [ org . spring . . . ] − Initializing Timer




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   107 / 174
A Minimal Example


 Spring-DM Application Context



         Unlike DS, each XML file is not a single component.
         Spring-DM creates one Application Context for the whole bundle.
         The contents is the union of all the listed XML files.
         Some parts of this XML will be OSGi-specific.
  Best Practice
  OSGi-specific parts of the Spring XML should be placed in a separate file.
  This enables re-use of the generics parts when used in a non-OSGi runtime.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   108 / 174
Activation and Deactivation


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deactivation
 15


      References to Services
 16


      Optional vs Mandatory Service References
 17


      Publishing Services
 18


      Advantages and Disadvantages
 19




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   109 / 174
Activation and Deactivation


 Activation and Deactivation


  There are no default method names for activation and deactivation, we
  must specify them via the bean declaration:
  Init and Destroy Methods
 <bean id=quot; polling quot; class=quot; org . example . osgi . dm . P o l l i n g C o m p o n e n t quot;
         init−method=quot; activate quot;
         destroy−method=quot; deactivate quot;>
     <property name=quot; period quot; value=quot; 2000 quot;/>
 </bean>


  These methods are always zero-arg. Configuration is supplied via our
  setter methods, or constructor injection.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   110 / 174
References to Services


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deactivation
 15


      References to Services
 16


      Optional vs Mandatory Service References
 17


      Publishing Services
 18


      Advantages and Disadvantages
 19




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   111 / 174
References to Services


 References to Services


         “Importing” a bean from another context means referencing it as a
         service.
  Service Imports
 < o s g i : r e f e r e n c e i d=quot; imp ortedLo gSvc quot;
        i n t e r f a c e=quot; org . osgi . service . log . LogService quot; />

 <bean i d=quot; l o g g i n g P o l l i n g B e a n quot; c l a s s=quot; org . example ... quot;>
      <p r o p e r t y name=quot; log quot; r e f=quot; im portedLo gSvc quot; />
 </ bean>


         The reference element looks just like another bean, and can be
         referenced by other beans using its ID as normal.



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   112 / 174
References to Services


 Proxy Beans



         In fact the reference really is just another bean. It is a generated
         proxy that continuously tracks the backing service.
         The proxy is injected once into each bean that refers to it.
  Important!
  Spring-DM does not directly inject service objects into its beans, but
  injects a proxy object. The reference from the bean to the proxy remains
  constant, so the bean does not have to deal with the dynamics.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   113 / 174
References to Services


 Dynamics and Thread Safety



         This swaps one problem – thread safety – for another: the beans using
         the service have no idea whether the service is presently available.
         What happens when a bean calls the proxy and no backing service is
         present?
         DM uses an (optional) timeout to wait for a suitable service to appear.
         Ultimately it must throw an unchecked exception,
         ServiceUnavilableException.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   114 / 174
References to Services


 Listening to Services




         This is nice and simple, but sometimes we really need to know the
         state of the service. For example we might be able to offer alternative
         functionality when the backing service is unavailable.
         E.g. when there is no LogService, print to the console.
         For this we can nominate a bean as a service listener.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   115 / 174
References to Services


 Listening to Services


  Service Listener
 <osgi : reference id=quot; imp ortedLo gSvc quot;
           interface=quot; org . osgi . service . log . LogService quot;>
     <listener ref=quot; log quot; bind−method=quot; setLog quot;
                              unbind−method=quot; unsetLog quot;/>
 </osgi : reference>

 <bean id=quot; log quot; class=quot; org ... MyLog quot;/>


  Note that the log bean declaration could be in the non-OSGi part of our
  application context.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   116 / 174
References to Services


 Listening to Services



         Just like DS with dynamic policy, the bind/unbind methods can be
         called concurrently with other methods accessing the bean.
         Therefore beans configured as listeners must be thread-safe.
         There is no equivalent of static policy.
         NB: when service replacement occurs, unbind is not called at all.
         Unbind is only called when the service is going away and there is no
         available replacement.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   117 / 174
References to Services


 Multiple References

         The <reference> element is used for references to single services.
         For multiple services, there are two choices: <list> and <set>.
         Each of these gives us a managed collection object which can be
         injected as a constant bean into other beans.
  List and Sets
 <osgi : list id=quot; listenerList quot;
     interface=quot; org . osgi . service . log . LogListener quot;/>

 <osgi : set id=quot; listenerSet quot;
     interface=quot; org . osgi . service . log . LogListener quot;/>

 <bean id=quot; log quot; class=quot; org ... MyLog quot;>
     <property name=quot; listeners quot; ref=quot; listenerList quot;/>
 </bean>




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   118 / 174
References to Services


 Multiple References




         Spring-DM adds and removes service objects in this collection
         transparently.
         A listener bean can be nominated, as with singular references.
         Iterators over these collections are stable. If a call to hasNext returns
         true then the subsequent call to next is guaranteed to return
         non-null.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   119 / 174
Optional vs Mandatory Service References


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deactivation
 15


      References to Services
 16


      Optional vs Mandatory Service References
 17


      Publishing Services
 18


      Advantages and Disadvantages
 19




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   120 / 174
Optional vs Mandatory Service References


 Mandatory Dependencies


         Like DS, Spring-DM supports both optional and mandatory services
         via a simple declaration.
  Optional Service Reference
 <osgi : reference id=quot; logSvc quot; interface=quot; org ... log . LogService quot;
           cardinality=quot; 0..1 quot;/>

 <osgi : list id=quot; listeners quot; interface=quot; org ... log . LogListener quot;
           cardinality=quot; 0.. N quot;/>


         The default cardinality can be controlled in the outer <beans>
         element, but the default default is 1..1 or 1..N, i.e. mandatory.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   121 / 174
Optional vs Mandatory Service References


 Mandatory Dependencies



         If there are any unsatisfied mandatory references, the entire
         application context will not start.
         Obviously we cannot have a mandatory ref to a service exported by
         the same application context!
         If a mandatory reference becomes unsatisfied, the application context
         is not deactivated and no active components will be deactivated.
         Instead any service exports that depend on the reference (whether
         directly or transitively) will be unregistered.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   122 / 174
Publishing Services


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deactivation
 15


      References to Services
 16


      Optional vs Mandatory Service References
 17


      Publishing Services
 18


      Advantages and Disadvantages
 19




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   123 / 174
Publishing Services


 Publishing a Service




         Any bean within the application context can be published as a service.
  Simple Publication
 <osgi : service ref=quot; contacts quot; interface=quot; net ... C o n t a c t R e p o s i t o r y quot;/>




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   124 / 174
Publishing Services


 More Publication Options


 <osgi : service ref=quot; contacts quot;>
     <osgi : interfaces>
           <value>net . . . ContactRepository </value>
           <value>net . . . EventHandler </value>
     </osgi : interfaces>
 </osgi : service>

 <osgi : service ref=quot; contacts quot; auto−export=quot; interfaces quot;/>

 <osgi : service ref=quot; contacts quot; auto−export=quot; interfaces quot;>
     <osgi : service−properties>
           <entry key=quot; foo quot; value=quot; bar quot;/>
     </osgi : service−properties>
 </osgi : service>




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   125 / 174
Publishing Services


  Lab 3 : Building the Directory Repository (Setup)


         Install the Spring-DM bundles:
     1



  osgi> installDir . . / C o m p o n e n t s S p r i n g D M / bundles


         Start the Spring-DM Extender bundle
     2

         (org.springframework.osgi.extender...)

  osgi> start 17
  INFO [ org . spring . . . ] − Starting . . .




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   126 / 174
Publishing Services


  Lab 3 : Building the Directory Repository



         Complete the implementation of the DirectoryRepositoryDM class
     1

         – TODO markers explain what is needed.
         Complete the Spring XML descriptor osgi-context.xml in the
     2

         spring directory. Add references to the log and listener services.
         Complete context.xml in the same directory. Add properties wired
     3

         to the service reference beans. Declare the init and destroy methods
         on the main component bean.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   127 / 174
Publishing Services


  Lab 3 : Publishing as a Service




         Publish the component under the ContactRepository interface.
     1


         Add the property name=Directory.
     2


         Test as before with the services command and in the Swing GUI.
     3




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   128 / 174
Advantages and Disadvantages


 Contents

      Introduction
 13


      A Minimal Example
 14


      Activation and Deactivation
 15


      References to Services
 16


      Optional vs Mandatory Service References
 17


      Publishing Services
 18


      Advantages and Disadvantages
 19




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   129 / 174
Advantages and Disadvantages


 Advantages of Spring-DM



         Spring-DM is clearly the best approach for modularising an existing
         Spring-based application.
         It provides a more mature Dependency Injection framework than DS
         with features not present in DS (e.g. constructor injection).
         Stronger focus on POJOs than DS.
         Standardised in the OSGi compendium in R4.2 (the Blueprint
         Service).




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   130 / 174
Advantages and Disadvantages


 Disadvantages of Spring-DM



         Heavy! Twelve JARs totalling 2.1Mb (DS is 170k)
         The “application context” idea has been adapted to fit the OSGi
         model and it feels like legacy. The result seems to be higher
         complexity.
         No separate bean life-cycle.
         Lack of laziness.
         A “standard” but will we ever see a competing implementation?




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   131 / 174
Advantages and Disadvantages


 Lack Of Bean Life-cycle


  Bundle with 2 Mandatory References

                                                            A


                                                                               1..1




                                                                               1..1

                                                                B




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   132 / 174
Advantages and Disadvantages


 Lack Of Bean Life-cycle
  Partially Satisfied

                                A


                                                    1..1




                                                    1..1

                                      B




         The App Context on the left will not be activated because one
         mandatory reference is missing.
         However the individual bean A has no missing references.
         Why not activate just A and the beans that use it?
c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   133 / 174
Advantages and Disadvantages


 Lack of Bean Life-cycle


         When mandatory references become unsatisfied, Spring-DM avoids
         destroying the application context.
         Beans that are “active” will not be deactivated. . . even if their
         “mandatory” references are unsatisfied!
         Have to use service listeners if there is active state (e.g. a polling
         thread) which must be deactivated when a referenced service goes
         away.
  Why does Spring-DM not deactivate the context? Probably because it is
  expensive to recreate, rather than because it is the right thing to do.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   134 / 174
Advantages and Disadvantages


 Lack of Laziness




         The application context is created by the Spring-DM extender bundle
         as soon as our bundle enters ACTIVE state.
         This causes classloading events, forcing the framework to create a
         classloader for our bundle.
         There does not appear to be a mechanism for deferring this activity
         until the published services are needed by a consumer.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   135 / 174
Part IV

                                              Apache iPOJO




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   136 / 174
Introduction


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactivation
 22

      References to Services
 23

      Dynamics and Thread-Safety
 24

      Publishing Services
 25

      Annotation-Based Declarations
 26

      iPOJO Handlers
 27

      Advantages and Disadvantages
 28



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   137 / 174
Introduction


 Apache iPOJO



         iPOJO is a sub-project of Apache Felix, however it works on other
         R4.1 frameworks.
         Like Spring-DM it focuses on creating a pure POJO programming
         model for components.
         It supports individual “bean” component lifecycle.
         Supports both XML and annotation-based metadata.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   138 / 174
A Minimal Example


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactivation
 22

      References to Services
 23

      Dynamics and Thread-Safety
 24

      Publishing Services
 25

      Annotation-Based Declarations
 26

      iPOJO Handlers
 27

      Advantages and Disadvantages
 28



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   139 / 174
A Minimal Example


 A Minimal Example


  Minimal iPOJO Declaration
 < i p o j o>
        <component name=quot; hello quot; c l a s s n a m e=quot; org ... HelloCom ponent quot; />
        < i n s t a n c e component=quot; hello quot; />
 </ i p o j o>



         iPOJO separates the concepts of a component definition and
         component instances.
         The former is like a factory.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   140 / 174
A Minimal Example


 Running the Example




  iPOJO is just one extender bundle, which must be installed and started:
         org.apache.felix.ipojo-<version>.jar
  BUT we need an additional build step – an ANT task which
  post-processes the bundle JAR. We sill see why this is necessary shortly.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   141 / 174
Activation and Deactivation


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactivation
 22

      References to Services
 23

      Dynamics and Thread-Safety
 24

      Publishing Services
 25

      Annotation-Based Declarations
 26

      iPOJO Handlers
 27

      Advantages and Disadvantages
 28



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   142 / 174
Activation and Deactivation


 Activation and Deactivation


  Validate/Invalidate Callbacks
 <ipojo>
     <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;>
         <callback transition=quot; validate quot; method=quot; start quot;/>
         <callback transition=quot; invalidate quot; method=quot; stop quot;/>
     </component>

      <instance component=quot; org ... P o l l i n g C o m p o n e n t quot;/>
  </ipojo>


  Like Spring-DM, the activation method is zero-arg, so how are
  configuration properties set?




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   143 / 174
Activation and Deactivation


 Configuration Properties


  Properties
 <ipojo>
     <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;>
         <callback transition=quot; validate quot; method=quot; start quot;/>
         <callback transition=quot; invalidate quot; method=quot; stop quot;/>
         <properties>
             <property name=quot; period quot; field=quot; period quot;/>
         </properties>
     </component>

      <instance component=quot; org ... P o l l i n g C o m p o n e n t quot;>
          <property name=quot; period quot; value=quot; 2000 quot;/>
      </instance>
  </ipojo>




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   144 / 174
Activation and Deactivation


 Component Implementation

  Receiving a Property
  public class P o l l i n g C o m p o n e n t {

         private int period ;

         protected void start ( ) {
             // . . .
         }

         protected void stop ( ) {
             // . . .
         }
  }


  Direct injection into the private field!


c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   145 / 174
References to Services


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactivation
 22

      References to Services
 23

      Dynamics and Thread-Safety
 24

      Publishing Services
 25

      Annotation-Based Declarations
 26

      iPOJO Handlers
 27

      Advantages and Disadvantages
 28



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   146 / 174
References to Services


 References to Services

  Referring to a service is done with similar magic. Just declare a private
  field with the type of the service, and use it!
  Receiving a Service
  public class P o l l i n g C o m p o n e n t {

         private int period ;
         private LogService log ;

         protected void start ( ) {
             if ( log != null )
                   log . log ( LogService . LOG_INFO , quot; Starting polling quot; ) ;
             // . . .
         }




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   147 / 174
References to Services


 Declaring a Service Reference




        <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;>
            <requires field=quot; log quot;/>

                 ...

         </component>




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   148 / 174
References to Services


 Optional Service References



  References are mandatory by default, to make optional:

        <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;>
            <requires field=quot; log quot; optional=quot; true quot;/>

                 ...

         </component>




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   149 / 174
Dynamics and Thread-Safety


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactivation
 22

      References to Services
 23

      Dynamics and Thread-Safety
 24

      Publishing Services
 25

      Annotation-Based Declarations
 26

      iPOJO Handlers
 27

      Advantages and Disadvantages
 28



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   150 / 174
Dynamics and Thread-Safety


 Dynamic Services




  Notice when we used the optional log service:

                if ( log != null )
                      log . log ( LogService . LOG_INFO , quot; Starting polling quot; ) ;


  This is classic check-then-act, and looks completely unsafe!




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   151 / 174
Dynamics and Thread-Safety


 Thread Safety



         iPOJO “manages” the required synchronisation for us.
         As soon as a method “touches” a dependency (i.e. an injected field) ,
         iPOJO ensures that these objects are kept until the end of the
         method.
         All iPOJO components therefore follow a dynamic policy as defined
         by DS.
         But we can write our code without regard for thread-safety.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   152 / 174
Dynamics and Thread-Safety


 Bytecode Instrumentation



  Perhaps now you see what the additional build step is for.
         iPOJO makes heavy use of bytecode instrumentation.
         Methods are generated giving access to private fields.
         Methods are inspected for access to shared variables, and
         synchronization instructions inserted.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   153 / 174
Publishing Services


 Contents
      Introduction
 20

      A Minimal Example
 21

      Activation and Deactivation
 22

      References to Services
 23

      Dynamics and Thread-Safety
 24

      Publishing Services
 25

      Annotation-Based Declarations
 26

      iPOJO Handlers
 27

      Advantages and Disadvantages
 28



c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   154 / 174
Publishing Services


 Publishing Services


  Publishing is done with the <provides> element. In the simplest cases,
  just adding this element is all that is necessary:
  Provides Element
        <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;>
            <provides/>
        </component>


  This is like Spring-DM’s auto-export=quot;interfacesquot;. Reflection is used
  to publish under all interfaces implemented by the component.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   155 / 174
Publishing Services


 Service Properties


  Service properties are added to the provides element as follows:
  Properties on Provides
        <component classname=quot; org ... P o l l i n g C o m p o n e n t quot;>
            <provides>
                <property name=quot; foo quot; type=quot; string quot; value=quot; bar quot;/>
                <property name=quot; period quot; field=quot; period quot;/>
            </provides>
        </component>


  Properties can be explicitly provided here, or sourced from fields in the
  component.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   156 / 174
Publishing Services


  Lab 4 : Building the Directory Repository (Setup)



         Install the iPOJO bundle:
     1



  osgi> installDir . . / C om po ne n ts iP OJ O / bundles


         Start the iPOJO bundle (org.apache.felix.ipojo...)
     2




  osgi> start 5




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   157 / 174
Publishing Services


  Lab 4 : Building the Directory Repository



         Complete the implementation of the DirectoryRepositoryiPOJO
     1

         class – TODO markers explain what is needed.
         Complete the XML descriptor
     2

         core.contacts.directory ipojo.xml. Add the following:
                 An optional reference to the log service.
                 A zero-to-many reference to the listener service.
                 Callbacks for the activate/deactivate methods.




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   158 / 174
Publishing Services


  Lab 4 : Publishing as a Service




         Publish the component under the ContactRepository interface.
     1


         Add the service property name=Directory.
     2


         Test as before with the services command and in the Swing GUI.
     3




c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0.   March 24, 2009   159 / 174
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO
OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO

Contenu connexe

Similaire à OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO

Together in Eclipse
Together in EclipseTogether in Eclipse
Together in EclipseTomasz Zarna
 
Tutorial storybook
Tutorial storybookTutorial storybook
Tutorial storybookVinay H G
 
Don't Ship That Container - Dirk Hohndel
Don't Ship That Container - Dirk HohndelDon't Ship That Container - Dirk Hohndel
Don't Ship That Container - Dirk HohndelShane Coughlan
 
Building Eclipse Plugins and RCP Applications with Tycho - ECE 2012
Building Eclipse Plugins and RCP Applications with Tycho - ECE 2012Building Eclipse Plugins and RCP Applications with Tycho - ECE 2012
Building Eclipse Plugins and RCP Applications with Tycho - ECE 2012jsievers
 
EclipseCon 2009: TmL Tutorial
EclipseCon 2009: TmL TutorialEclipseCon 2009: TmL Tutorial
EclipseCon 2009: TmL TutorialEric Cloninger
 
Building Eclipse Plugins and RCP applications with Tycho
Building Eclipse Plugins and RCP applications with TychoBuilding Eclipse Plugins and RCP applications with Tycho
Building Eclipse Plugins and RCP applications with Tychojsievers
 
Ellip Studio - Training session
Ellip Studio - Training sessionEllip Studio - Training session
Ellip Studio - Training sessionterradue
 
Declare and write your own ta action
Declare and write your own ta actionDeclare and write your own ta action
Declare and write your own ta actionRobin0590
 
OSDC 2018 | Ops hates containers. Why? by Martin Alfke
OSDC 2018 |  Ops hates containers. Why? by Martin AlfkeOSDC 2018 |  Ops hates containers. Why? by Martin Alfke
OSDC 2018 | Ops hates containers. Why? by Martin AlfkeNETWAYS
 
How to become an Eclipse committer in 20 minutes and fork the IDE
How to become an Eclipse committer in 20 minutes and fork the IDEHow to become an Eclipse committer in 20 minutes and fork the IDE
How to become an Eclipse committer in 20 minutes and fork the IDELars Vogel
 
JavaOne 2013: Garbage Collection Unleashed - Demystifying the Wizardry
JavaOne 2013: Garbage Collection Unleashed - Demystifying the WizardryJavaOne 2013: Garbage Collection Unleashed - Demystifying the Wizardry
JavaOne 2013: Garbage Collection Unleashed - Demystifying the WizardryRyan Sciampacone
 
OSGi Versioning & Testing
OSGi Versioning & TestingOSGi Versioning & Testing
OSGi Versioning & TestingChris Aniszczyk
 
Eclipse Plug-in Develompent Tips And Tricks
Eclipse Plug-in Develompent Tips And TricksEclipse Plug-in Develompent Tips And Tricks
Eclipse Plug-in Develompent Tips And TricksChris Aniszczyk
 
EclipseCon 08 - Agile RCP
EclipseCon 08 - Agile RCPEclipseCon 08 - Agile RCP
EclipseCon 08 - Agile RCPHeiko Seeberger
 
Jubula tutorial slides
Jubula tutorial slidesJubula tutorial slides
Jubula tutorial slidesbredex
 
MarkLogic Developer Community Resources, September 2013
MarkLogic Developer Community Resources, September 2013MarkLogic Developer Community Resources, September 2013
MarkLogic Developer Community Resources, September 2013Eric Bloch
 
Using Technology in the Classroom, Chapter 11
Using Technology in the Classroom, Chapter 11Using Technology in the Classroom, Chapter 11
Using Technology in the Classroom, Chapter 11Sandra Burns, M.Ed
 
stackconf 2022: It’s Time to Debloat the Cloud with Unikraft
stackconf 2022: It’s Time to Debloat the Cloud with Unikraftstackconf 2022: It’s Time to Debloat the Cloud with Unikraft
stackconf 2022: It’s Time to Debloat the Cloud with UnikraftNETWAYS
 
Where Do I Click
Where Do I ClickWhere Do I Click
Where Do I Clickbredex
 

Similaire à OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO (20)

Together in Eclipse
Together in EclipseTogether in Eclipse
Together in Eclipse
 
Tutorial storybook
Tutorial storybookTutorial storybook
Tutorial storybook
 
Don't Ship That Container - Dirk Hohndel
Don't Ship That Container - Dirk HohndelDon't Ship That Container - Dirk Hohndel
Don't Ship That Container - Dirk Hohndel
 
Building Eclipse Plugins and RCP Applications with Tycho - ECE 2012
Building Eclipse Plugins and RCP Applications with Tycho - ECE 2012Building Eclipse Plugins and RCP Applications with Tycho - ECE 2012
Building Eclipse Plugins and RCP Applications with Tycho - ECE 2012
 
EclipseCon 2009: TmL Tutorial
EclipseCon 2009: TmL TutorialEclipseCon 2009: TmL Tutorial
EclipseCon 2009: TmL Tutorial
 
Compare framework
Compare frameworkCompare framework
Compare framework
 
Building Eclipse Plugins and RCP applications with Tycho
Building Eclipse Plugins and RCP applications with TychoBuilding Eclipse Plugins and RCP applications with Tycho
Building Eclipse Plugins and RCP applications with Tycho
 
Ellip Studio - Training session
Ellip Studio - Training sessionEllip Studio - Training session
Ellip Studio - Training session
 
Declare and write your own ta action
Declare and write your own ta actionDeclare and write your own ta action
Declare and write your own ta action
 
OSDC 2018 | Ops hates containers. Why? by Martin Alfke
OSDC 2018 |  Ops hates containers. Why? by Martin AlfkeOSDC 2018 |  Ops hates containers. Why? by Martin Alfke
OSDC 2018 | Ops hates containers. Why? by Martin Alfke
 
How to become an Eclipse committer in 20 minutes and fork the IDE
How to become an Eclipse committer in 20 minutes and fork the IDEHow to become an Eclipse committer in 20 minutes and fork the IDE
How to become an Eclipse committer in 20 minutes and fork the IDE
 
JavaOne 2013: Garbage Collection Unleashed - Demystifying the Wizardry
JavaOne 2013: Garbage Collection Unleashed - Demystifying the WizardryJavaOne 2013: Garbage Collection Unleashed - Demystifying the Wizardry
JavaOne 2013: Garbage Collection Unleashed - Demystifying the Wizardry
 
OSGi Versioning & Testing
OSGi Versioning & TestingOSGi Versioning & Testing
OSGi Versioning & Testing
 
Eclipse Plug-in Develompent Tips And Tricks
Eclipse Plug-in Develompent Tips And TricksEclipse Plug-in Develompent Tips And Tricks
Eclipse Plug-in Develompent Tips And Tricks
 
EclipseCon 08 - Agile RCP
EclipseCon 08 - Agile RCPEclipseCon 08 - Agile RCP
EclipseCon 08 - Agile RCP
 
Jubula tutorial slides
Jubula tutorial slidesJubula tutorial slides
Jubula tutorial slides
 
MarkLogic Developer Community Resources, September 2013
MarkLogic Developer Community Resources, September 2013MarkLogic Developer Community Resources, September 2013
MarkLogic Developer Community Resources, September 2013
 
Using Technology in the Classroom, Chapter 11
Using Technology in the Classroom, Chapter 11Using Technology in the Classroom, Chapter 11
Using Technology in the Classroom, Chapter 11
 
stackconf 2022: It’s Time to Debloat the Cloud with Unikraft
stackconf 2022: It’s Time to Debloat the Cloud with Unikraftstackconf 2022: It’s Time to Debloat the Cloud with Unikraft
stackconf 2022: It’s Time to Debloat the Cloud with Unikraft
 
Where Do I Click
Where Do I ClickWhere Do I Click
Where Do I Click
 

Plus de Heiko Seeberger

Scaladays 2011 - The Ease of Scalaz
Scaladays 2011 - The Ease of ScalazScaladays 2011 - The Ease of Scalaz
Scaladays 2011 - The Ease of ScalazHeiko Seeberger
 
RheinJUG 2010 - Sprechen Sie Scala?
RheinJUG 2010 - Sprechen Sie Scala?RheinJUG 2010 - Sprechen Sie Scala?
RheinJUG 2010 - Sprechen Sie Scala?Heiko Seeberger
 
Objektforum 2010 - Sprechen Sie Scala?
Objektforum 2010 - Sprechen Sie Scala?Objektforum 2010 - Sprechen Sie Scala?
Objektforum 2010 - Sprechen Sie Scala?Heiko Seeberger
 
JM 08/09 - Beginning Scala Review
JM 08/09 - Beginning Scala ReviewJM 08/09 - Beginning Scala Review
JM 08/09 - Beginning Scala ReviewHeiko Seeberger
 
OSGi DevCon Europe 09 - OSGi on Scala
OSGi DevCon Europe 09 - OSGi on ScalaOSGi DevCon Europe 09 - OSGi on Scala
OSGi DevCon Europe 09 - OSGi on ScalaHeiko Seeberger
 
JAX 09 - OSGi Service Components Models
JAX 09 - OSGi Service Components ModelsJAX 09 - OSGi Service Components Models
JAX 09 - OSGi Service Components ModelsHeiko Seeberger
 
Eclipse Magazin 12 - Design by Contract
Eclipse Magazin 12 - Design by ContractEclipse Magazin 12 - Design by Contract
Eclipse Magazin 12 - Design by ContractHeiko Seeberger
 
Eclipse Magazin 16 - Die Stärke der Drei
Eclipse Magazin 16 - Die Stärke der DreiEclipse Magazin 16 - Die Stärke der Drei
Eclipse Magazin 16 - Die Stärke der DreiHeiko Seeberger
 
Eclipse Magazin15 - Performance Logging
Eclipse Magazin15 - Performance LoggingEclipse Magazin15 - Performance Logging
Eclipse Magazin15 - Performance LoggingHeiko Seeberger
 
Eclipse Magazin 14 - Getting hooked on Equinox
Eclipse Magazin 14 - Getting hooked on EquinoxEclipse Magazin 14 - Getting hooked on Equinox
Eclipse Magazin 14 - Getting hooked on EquinoxHeiko Seeberger
 

Plus de Heiko Seeberger (20)

Scaladays 2011 - The Ease of Scalaz
Scaladays 2011 - The Ease of ScalazScaladays 2011 - The Ease of Scalaz
Scaladays 2011 - The Ease of Scalaz
 
Java Magazin - Lift
Java Magazin - LiftJava Magazin - Lift
Java Magazin - Lift
 
JavaSPEKTRUM - Scala 3
JavaSPEKTRUM - Scala 3JavaSPEKTRUM - Scala 3
JavaSPEKTRUM - Scala 3
 
JavaSPEKTRUM - Scala 2
JavaSPEKTRUM - Scala 2JavaSPEKTRUM - Scala 2
JavaSPEKTRUM - Scala 2
 
JavaSPEKTRUM - Scala 1
JavaSPEKTRUM - Scala 1JavaSPEKTRUM - Scala 1
JavaSPEKTRUM - Scala 1
 
RheinJUG 2010 - Sprechen Sie Scala?
RheinJUG 2010 - Sprechen Sie Scala?RheinJUG 2010 - Sprechen Sie Scala?
RheinJUG 2010 - Sprechen Sie Scala?
 
Objektforum 2010 - Sprechen Sie Scala?
Objektforum 2010 - Sprechen Sie Scala?Objektforum 2010 - Sprechen Sie Scala?
Objektforum 2010 - Sprechen Sie Scala?
 
W-JAX 09 - ScalaModules
W-JAX 09 - ScalaModulesW-JAX 09 - ScalaModules
W-JAX 09 - ScalaModules
 
W-JAX 09 - Lift
W-JAX 09 - LiftW-JAX 09 - Lift
W-JAX 09 - Lift
 
JM 08/09 - Beginning Scala Review
JM 08/09 - Beginning Scala ReviewJM 08/09 - Beginning Scala Review
JM 08/09 - Beginning Scala Review
 
JM 08/09 - ScalaModules
JM 08/09 - ScalaModulesJM 08/09 - ScalaModules
JM 08/09 - ScalaModules
 
OSGi DevCon Europe 09 - OSGi on Scala
OSGi DevCon Europe 09 - OSGi on ScalaOSGi DevCon Europe 09 - OSGi on Scala
OSGi DevCon Europe 09 - OSGi on Scala
 
JAX 09 - OSGi on Scala
JAX 09 - OSGi on ScalaJAX 09 - OSGi on Scala
JAX 09 - OSGi on Scala
 
JAX 09 - OSGi Service Components Models
JAX 09 - OSGi Service Components ModelsJAX 09 - OSGi Service Components Models
JAX 09 - OSGi Service Components Models
 
JAX 08 - Agile RCP
JAX 08 - Agile RCPJAX 08 - Agile RCP
JAX 08 - Agile RCP
 
Eclipse Magazin 12 - Design by Contract
Eclipse Magazin 12 - Design by ContractEclipse Magazin 12 - Design by Contract
Eclipse Magazin 12 - Design by Contract
 
JUGM 07 - AspectJ
JUGM 07 - AspectJJUGM 07 - AspectJ
JUGM 07 - AspectJ
 
Eclipse Magazin 16 - Die Stärke der Drei
Eclipse Magazin 16 - Die Stärke der DreiEclipse Magazin 16 - Die Stärke der Drei
Eclipse Magazin 16 - Die Stärke der Drei
 
Eclipse Magazin15 - Performance Logging
Eclipse Magazin15 - Performance LoggingEclipse Magazin15 - Performance Logging
Eclipse Magazin15 - Performance Logging
 
Eclipse Magazin 14 - Getting hooked on Equinox
Eclipse Magazin 14 - Getting hooked on EquinoxEclipse Magazin 14 - Getting hooked on Equinox
Eclipse Magazin 14 - Getting hooked on Equinox
 

Dernier

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 

Dernier (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 

OSGi DevCon 09 - Component Oriented Development in OSGi with DS, Spring and iPOJO

  • 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. Part I Introduction c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 2 / 174
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Part II Declarative Services c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 32 / 174
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Part III Spring-DM c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 92 / 174
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Part IV Apache iPOJO c 2009 Neil Bartlett & WeigleWilczek GmbH. Released under the Eclipse Public License v1.0. March 24, 2009 136 / 174
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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