Search in sources :

Example 1 with ResolveCanceledException

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());
}
Also used : ResolveCanceledException(ch.cyberduck.core.exception.ResolveCanceledException) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) CancelCallback(ch.cyberduck.core.threading.CancelCallback) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 2 with ResolveCanceledException

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();
}
Also used : ResolveCanceledException(ch.cyberduck.core.exception.ResolveCanceledException) Arrays(java.util.Arrays) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) CancelCallback(ch.cyberduck.core.threading.CancelCallback) PreferencesFactory(ch.cyberduck.core.preferences.PreferencesFactory) ResolveFailedException(ch.cyberduck.core.exception.ResolveFailedException) UnknownHostException(java.net.UnknownHostException) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageFormat(java.text.MessageFormat) InetAddress(java.net.InetAddress) TimeUnit(java.util.concurrent.TimeUnit) Inet6Address(java.net.Inet6Address) CountDownLatch(java.util.concurrent.CountDownLatch) Logger(org.apache.logging.log4j.Logger) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) NamedThreadFactory(ch.cyberduck.core.threading.NamedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) LogManager(org.apache.logging.log4j.LogManager) ResolveCanceledException(ch.cyberduck.core.exception.ResolveCanceledException) UnknownHostException(java.net.UnknownHostException) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Inet6Address(java.net.Inet6Address) CountDownLatch(java.util.concurrent.CountDownLatch) ResolveFailedException(ch.cyberduck.core.exception.ResolveFailedException) InetAddress(java.net.InetAddress)

Aggregations

ConnectionCanceledException (ch.cyberduck.core.exception.ConnectionCanceledException)2 ResolveCanceledException (ch.cyberduck.core.exception.ResolveCanceledException)2 CancelCallback (ch.cyberduck.core.threading.CancelCallback)2 ResolveFailedException (ch.cyberduck.core.exception.ResolveFailedException)1 PreferencesFactory (ch.cyberduck.core.preferences.PreferencesFactory)1 NamedThreadFactory (ch.cyberduck.core.threading.NamedThreadFactory)1 IntegrationTest (ch.cyberduck.test.IntegrationTest)1 Uninterruptibles (com.google.common.util.concurrent.Uninterruptibles)1 Inet6Address (java.net.Inet6Address)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 MessageFormat (java.text.MessageFormat)1 Arrays (java.util.Arrays)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 Test (org.junit.Test)1