Java Reference
In-Depth Information
private LinkedList<Bar> found = new LinkedList<Bar>();
private ServiceRegistration reg;
BarTracker(FooImpl foo, BundleContext ctx) {
super(ctx, Bar.class.getName(), null);
this.foo = foo;
this.ctx = ctx;
}
@Override
public Object addingService(ServiceReference reference) {
Bar bar = (Bar) super.addingService(reference);
found.add(bar);
if (foo.getBar() == null) {
foo.setBar(bar);
reg = ctx.registerService(Foo.class.getName(), foo, null);
}
return bar;
}
B
Stores
backups
First Bar
service found?
C
@Override
public void removedService(ServiceReference reference, Object service) {
found.remove(service);
if (foo.getBar() == service) {
if (found.isEmpty()) {
reg.unregister();
foo.setBar(null);
reg = null;
}
else {
foo.setBar(found.getFirst());
}
}
super.removedService(reference, service);
}
}
BarTracker tracks Bar services. If one is discovered, it checks whether this is the first
Bar service it has found C . If so, it calls the FooImpl.setBar() method prior to regis-
tering the Foo service of FooImpl . If more than one Bar service is found, backups are
stored B . If BarTracker detects that the Bar service being used has been removed D ,
it replaces that service with one of the backups E . If no backup is available, it unregis-
ters the Foo service and calls the FooImpl.setBar() method with null .
You may be looking at this code and thinking that it looks complicated. We agree
that it's reasonably so, particularly if you also consider that it covers only a single, one-
to-one service dependency. Things get more complex (and redundant) as you get
more dependencies. OSG i-based component frameworks allow you to describe these
types of issues; then the frameworks worry about it for us. Typically, the component
frameworks let you describe the following:
Bar service
removed?
D
E
Replaces removed
service with backup
Provided services —Services implemented by the component
Required services —Services needed by the component in order to provide its
services
 
Search WWH ::




Custom Search