Java Reference
In-Depth Information
(continued)
This also applies to the reference-list approach of aggregating services. The Blueprint
specification ensures that the hasNext() and getNext() operations are safe with
respect to changes in the OSGi service registry. But if a service is removed and has-
Next() has already been called, a dummy object is returned that throws Service-
UnavailableException s when any methods are called, instead of throwing a
ConcurrentModificationException during the iteration. These types of issues
aren't specific to Blueprint, but to how Blueprint handles OSGi's service dynamism.
This covers the basics of service injection. Now let's look at how Blueprint lets compo-
nents have a dynamic view of services using reference listeners.
REFERENCE LISTENERS
Reference listeners allow a component to receive services via bind and unbind call-
backs. Consider the following example:
public class E {
private volatile A a;
public void addService(A a) { this.a = a }
public void removeService(A a) { this.a = null }
public void someAction() {
A a = this.a;
if ( a != null ) a.doit();
}
}
In this example, class E receives callbacks via the addService() and removeService()
methods when an A service is registered or unregistered in the OSG i service registry,
respectively. The body of the someAction() method must guard against the fact that the
service may be null . You express this sort of dependency in Blueprint XML as follows:
<bean id="e" class="E"/>
<reference id="a" interface="A">
<reference-listener
bind-method="addService"
unbind-method="removeService"
ref="e"/>
</reference>
A reference-listener callback can have any of the following signatures, which have the
same semantics as their Declarative Services equivalents (from the last chapter):
public void <method-name>(ServiceReference)
public void <method-name>(<parameter-type>)
public void <method-name>(<parameter-type>, Map)
One issue to keep in mind: the Blueprint specification mandates that binding meth-
ods have public method access. Although the risk is probably minor in most scenar-
ios, it does open Blueprint components exposed as services to the possibility that
external code using reflection can inject dependencies even if a security manager is
Search WWH ::




Custom Search