Java Reference
In-Depth Information
Having described the general registry architecture, let's look more closely at the
ExportedServiceTracker
, which handles the task of exporting local services with the
service.exported.interfaces
service property into the remote registry.
EXPORTEDSERVICETRACKER
The
ExportedServiceTracker
class extends the
ServiceTracker
class you met in
chapter 4. As the name implies, it tracks any services that have been marked for
export. Here's how you do this.
Listing 15.13 Constructing an exported service tracker
public ExportedServiceTracker(BundleContext ctx, Registry registry,
String[] intents, String[] configs) {
super(ctx, createFilter(ctx), null);
this.ctx = ctx;
this.registry = registry;
this.intents = intents == null ? new String[0] : intents;
this.configs = configs == null ? new String[0] : configs;
}
private static Filter createFilter(BundleContext ctx) {
try {
return ctx.createFilter("(service.exported.interfaces=*)");
} catch (InvalidSyntaxException e) {
throw new IllegalStateException(e);
}
}
You call the
ServiceTracker
constructor, passing in an
LDAP
filter that you create to
match all services with a
service.exported.interfaces
attribute of any value; the
*
is interpreted by the
LDAP
syntax to be a wildcard that matches any value. Then you
store the intents and configurations supported by your remote registry. You'll see a lit-
tle later how these are derived; for now, let's look at the overridden
addingService()
method in the next listing.
Listing 15.14 Dealing with new exported services
@Override
public Object addingService(ServiceReference ref) {
Object svc = super.addingService(ref);
if (isValidService(ref)) {
String[] ifaces = findExportedInterfaces(ref);
for (String iface : ifaces) {
registry.registerService(ref, iface, svc);
}
}
return svc;
}
This method is called by the
ServiceTracker
super-class whenever a service published
in the
OSG
i service registry matches the filter specified in listing 15.13. In this method,
you first get a reference to the matching service by calling the
addingService()