else
templ = template;
final ServiceTemplate finalTemplate = templ;
LookupDiscovery lookupDiscovery = null;
try {
lookupDiscovery = new LookupDiscovery(groups);
// hook listener for finding the service
lookupDiscovery.addDiscoveryListener(new DiscoveryListener() {
public void discovered(DiscoveryEvent ev) {
log.debug("received discovery event " + ev);
ServiceRegistrar[] reg = ev.getRegistrars();
// once the proxy if found, bail out
for (int i = 0; i < reg.length && proxy == null; i++) {
findService(finalTemplate, reg[i]);
}
}
public void discarded(DiscoveryEvent ev) {
}
});
if (log.isDebugEnabled())
log.debug("Awaiting discovery event...");
if (proxy == null) {
synchronized (this) {
this.wait(timeout);
}
}
if (log.isDebugEnabled())
log.debug("Terminating discovery service");
}
catch (IOException e) {
throw new RemoteLookupFailureException("cannot create lookup discovery", e);
}
catch (InterruptedException e) {
throw new NestedRuntimeException("lookup interrupted", e) {
};
}
finally {
// make sure to close the lookup threads
if (lookupDiscovery != null)
lookupDiscovery.terminate();
}
return proxy;
}