use of ch.cyberduck.core.exception.ResolveCanceledException in project cyberduck by iterate-ch.
the class ResolverTest method testCancel.
@Test
public void testCancel() throws Exception {
final Resolver resolver = new Resolver();
try {
resolver.resolve("non.cyberduck.io", new CancelCallback() {
@Override
public void verify() throws ConnectionCanceledException {
throw new ConnectionCanceledException();
}
});
fail();
} catch (ResolveCanceledException e) {
//
}
assertNotNull(resolver.resolve("cyberduck.io", new DisabledCancelCallback()).getHostAddress());
}
use of ch.cyberduck.core.exception.ResolveCanceledException in project cyberduck by iterate-ch.
the class Resolver method resolve.
/**
* This method is blocking until the hostname has been resolved or the lookup has been canceled using #cancel
*
* @return The resolved IP address for this hostname
* @throws ResolveFailedException If the hostname cannot be resolved
* @throws ResolveCanceledException If the lookup has been interrupted
*/
public InetAddress resolve(final String hostname, final CancelCallback callback) throws ResolveFailedException, ResolveCanceledException {
final CountDownLatch signal = new CountDownLatch(1);
final AtomicReference<InetAddress> resolved = new AtomicReference<>();
final AtomicReference<UnknownHostException> failure = new AtomicReference<>();
final Thread resolver = threadFactory.newThread(new Runnable() {
@Override
public void run() {
try {
final InetAddress[] allByName = InetAddress.getAllByName(hostname);
Arrays.stream(allByName).findFirst().ifPresent(resolved::set);
if (preferIPv6) {
Arrays.stream(allByName).filter(inetAddress -> inetAddress instanceof Inet6Address).findFirst().ifPresent(resolved::set);
}
if (log.isInfoEnabled()) {
log.info(String.format("Resolved %s to %s", hostname, resolved.get().getHostAddress()));
}
} catch (UnknownHostException e) {
log.warn(String.format("Failed resolving %s", hostname));
failure.set(e);
} finally {
signal.countDown();
}
}
});
resolver.start();
log.debug(String.format("Waiting for resolving of %s", hostname));
// Wait for #run to finish
while (!Uninterruptibles.awaitUninterruptibly(signal, 500, TimeUnit.MILLISECONDS)) {
try {
callback.verify();
} catch (ConnectionCanceledException c) {
throw new ResolveCanceledException(MessageFormat.format(LocaleFactory.localizedString("DNS lookup for {0} failed", "Error"), hostname), c);
}
}
try {
callback.verify();
} catch (ConnectionCanceledException c) {
throw new ResolveCanceledException(MessageFormat.format(LocaleFactory.localizedString("DNS lookup for {0} failed", "Error"), hostname), c);
}
if (null == resolved.get()) {
if (null == failure.get()) {
log.warn(String.format("Canceled resolving %s", hostname));
throw new ResolveCanceledException(MessageFormat.format(LocaleFactory.localizedString("DNS lookup for {0} failed", "Error"), hostname));
}
throw new ResolveFailedException(MessageFormat.format(LocaleFactory.localizedString("DNS lookup for {0} failed", "Error"), hostname), failure.get());
}
return resolved.get();
}
Aggregations