Java Reference
In-Depth Information
Hashtable props = new Hashtable(ref.getProperties());
return ctx.registerService(
ref.getInterface(), ref.getService() ,props);
}
}
...
}
This class passes through the service properties of the remote service and registers the
service object in the OSG i service registry.
The final area to look at is what happens if a new remote service is discovered by
the watcher.
Listing 15.19 RegistryListener event handling
private void handleAdd(RemoteServiceReference ref) {
synchronized (m_watches) {
if (!m_regs.containsKey(ref)) {
for (Watch w : m_watches.keySet()) {
if (w.matches(ref)) {
Future<ServiceRegistration> future = exec
.submit(new Registration(ref));
m_regs.put(ref, future);
break;
}
}
}
}
}
This method is called as a result of a RegistryListener event indicating that a new
remote-service reference has been added to your RemoteRegistry . You check whether
this is a new service reference and whether any existing watch has been created for
this service. If so, you create another background registration and store the future
OSG i service registration.
In summary, using your helper class and the service-hook interfaces, you can find
out when a remote service is needed and inject it into the local OSG i service registry
on demand. Let's see how this works in practice.
IMPORTEDSERVICELISTENERHOOK
ImportedServiceListenerHook tracks service-listener registrations and adds a watch in
the remote registry for the associated services. You keep track of which types of services
other bundles are interested in so you know which types of remote services you should
import. The following listing shows how to process service-listener registrations.
Listing 15.20 ListenerHook that tracks registered service listeners
public void added(Collection listeners) {
for (final ListenerInfo info : (Collection<ListenerInfo>) listeners) {
if (!info.isRemoved()) {
LDAPExpr expr = LDAPParser.parseExpression(info.getFilter());
expr.visit(new ExprVisitor() {
 
Search WWH ::




Custom Search