Search code examples
osgiipojoblueprint-osgi

Allowed signatures for iPOJO @Bind/@Unbind methods


In all iPOJO examples I've seen, the @Bind and @Unbind callbacks take only the service instance as an argument, i.e.

// @Requires
// private Foo foo;

@Bind
public void bindFoo(Foo foo) { ... }

@Unbind
public void unbindFoo(Foo foo) { ... }

Blueprint also allows you to have

public void bindFoo(ServiceReference reference) { ... }

public void bindFoo(Foo foo, Map<String, Object> properties) { ... }

Can iPOJO callbacks also get access to service properties or ServiceReference? Or should whiteboard handler be used for this instead?


Solution

  • Callbacks can have one of these signatures:

    1. Without any argument: the method is just a notification (method())
    2. With the service object : the object is the implicated service object (method(Service svc))
    3. With an OSGi service reference: the service reference appearing or disappearing (method(ServiceReference ref))
    4. With the service object and the OSGi service reference (method(Service svc, ServiceReference ref))
    5. With the service object and the service properties inside a Map (method(Service svc, Map properties))
    6. With the service object and the service properties inside a Dictionary (method(Service svc, Dictionary properties))

    So, are supported:

    @Bind
    public void bind() { ... }
    
    @Bind
    public void bind(Service svc) { ... }
    
    @Bind
    public void bind(ServiceReference ref) { ... }
    
    @Bind
    public void bind(Service svc, ServiceReference ref) { ... }
    
    @Bind
    public void bind(Service svc, Map properties) { ... }
    
    @Bind
    public void bind(Service svc, Dictionary properties) { ... }
    

    So no problem to access the service properties. Except if you really need the ServiceReference, it's probably better to not use this OSGi-specific object (reduce testability).