Allowed signatures for iPOJO @Bind/@Unbind methods
-
05-06-2021 - |
Question
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:
- Without any argument: the method is just a notification (method())
- With the service object : the object is the implicated service object (method(Service svc))
- With an OSGi service reference: the service reference appearing or disappearing (method(ServiceReference ref))
- With the service object and the OSGi service reference (method(Service svc, ServiceReference ref))
- With the service object and the service properties inside a Map (method(Service svc, Map properties))
- 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).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow