Java Reference
In-Depth Information
public void visitExpr(LDAPExpr expr) {
if (expr instanceof SimpleTerm) {
SimpleTerm term = (SimpleTerm) expr;
if (term.getName().equals(Constants.OBJECTCLASS)) {
watcher.addWatch(term.getRval(), info.getFilter());
}
}
}
});
}
}
}
You first check whether the listener is removed. This may seem a little odd, given that
it happens in the
added()
method, but it protects your listener against race condi-
tions due to asynchronous event delivery. You then inspect the body of the
LDAP
expression by using a utility class to walk your way through the filter expression to
find references to the
objectClass
service property, indicating the service interfaces
of interest. Finally, you add a watch in your remote registry for the discovered service
interfaces specified.
Now, when another bundle registers a service listener, your listener hook will find
any matching remote services in the remote registry and add them to the local
OSG
i
service registry. This lets you handle asynchronous service lookup; but how do you
handle direct service queries? We'll look at this next.
IMPORTEDSERVICEFINDHOOK
When a bundle invokes
BundleContext.getServiceReference()
, you'd like to be
able to intercept it and inject a remote service into the
OSG
i service registry. You can
achieve this using a find hook:
public class ImportedServiceFindHook implements FindHook {
...
public void find(BundleContext ctx, java.lang.String name,
java.lang.String filter, boolean allServices, Collection references)
{
watcher.findServices(name, filter);
}
}
This implementation is trivial because it asks the registry watcher to find any matching
services in the remote registry, which then adds the services to the local
OSG
i service
registry.
PUTTING IT ALL TOGETHER
We'll skip over the implementation of the
DummyRegistry
, because it's indeed trivial
(the curious can look in the companion code). You can complete the example by
creating a test bundle that exports a
Foo
service using the
service.exported.
interfaces=*
service property as follows:
Hashtable props = new Hashtable();
props.put("service.exported.interfaces","*");
context.registerService(Foo.class.getName(), new FooImpl(), props);