Example usage for com.google.common.util.concurrent SettableFuture setException

List of usage examples for com.google.common.util.concurrent SettableFuture setException

Introduction

In this page you can find the example usage for com.google.common.util.concurrent SettableFuture setException.

Prototype

@Override
    public boolean setException(Throwable throwable) 

Source Link

Usage

From source file:org.testfx.util.WaitForAsyncUtils.java

private static <T> void callCallableAndSetFuture(Callable<T> callable, SettableFuture<T> future) {
    try {/*  w  w w. j a v a2 s.  c  om*/
        future.set(callable.call());
    } catch (Throwable exception) {
        future.setException(exception);
    }
}

From source file:org.jenkinsci.plugins.workflow.support.concurrent.Futures.java

/**
 * Returns a {@code ListenableFuture} which has an exception set immediately
 * upon construction.//from w w  w.  j  a v  a  2  s.c o m
 *
 * <p>The returned {@code Future} can't be cancelled, and its {@code isDone()}
 * method always returns {@code true}. Calling {@code get()} will immediately
 * throw the provided {@code Throwable} wrapped in an {@code
 * ExecutionException}.
 *
 * @throws Error if the throwable is an {@link Error}.
 */
public static <V> ListenableFuture<V> immediateFailedFuture(Throwable throwable) {
    checkNotNull(throwable);
    SettableFuture<V> future = SettableFuture.create();
    future.setException(throwable);
    return future;
}

From source file:com.microsoft.office365.http.SharepointCookieCredentials.java

public static ListenableFuture<CookieCredentials> requestCredentials(String sharepointSiteUrl,
        Activity activity) {//from  w ww.  jav a2 s.  c o  m
    final SettableFuture<CookieCredentials> future = SettableFuture.create();

    ListenableFuture<String> login = showLoginForCookies(activity, sharepointSiteUrl);

    Futures.addCallback(login, new FutureCallback<String>() {
        @Override
        public void onFailure(Throwable t) {
            future.setException(t);
        }

        @Override
        public void onSuccess(String cookies) {
            future.set(new CookieCredentials(cookies));
        }
    });

    return future;
}

From source file:io.airlift.drift.client.DriftInvocationHandler.java

private static ListenableFuture<Object> unwrapUserException(ListenableFuture<Object> future) {
    SettableFuture<Object> result = SettableFuture.create();
    Futures.addCallback(future, new FutureCallback<Object>() {
        @Override//  w w w .  j  a  v  a  2 s . com
        public void onSuccess(Object value) {
            result.set(value);
        }

        @Override
        public void onFailure(Throwable t) {
            result.setException(unwrapUserException(t));
        }
    }, directExecutor());
    return result;
}

From source file:com.microsoft.sharepointservices.http.SharepointCookieCredentials.java

/**
 * Request credentials./*from   w w w.j av  a2  s  .c o m*/
 *
 * @param sharepointSiteUrl the sharepoint site url
 * @param activity the activity
 * @return the listenable future
 */
public static ListenableFuture<CookieCredentials> requestCredentials(String sharepointSiteUrl,
        Activity activity) {
    final SettableFuture<CookieCredentials> future = SettableFuture.create();

    ListenableFuture<String> login = showLoginForCookies(activity, sharepointSiteUrl);

    Futures.addCallback(login, new FutureCallback<String>() {
        @Override
        public void onFailure(Throwable t) {
            future.setException(t);
        }

        @Override
        public void onSuccess(String cookies) {
            future.set(new CookieCredentials(cookies));
        }
    });

    return future;
}

From source file:com.eviware.loadui.ui.fx.util.test.FXTestUtils.java

/**
 * Runs the given Callable in the JavaFX thread, waiting for it to complete
 * before returning. Also attempts to wait for any other JavaFX events that
 * may have been queued in the Callable to complete. If any Exception is
 * thrown during execution of the Callable, that exception will be re-thrown
 * from invokeAndWait./*from www  .j  ava2  s  .  c  o m*/
 * 
 * @param task
 * @param timeoutInSeconds
 * @throws Throwable
 */
public static void invokeAndWait(final Callable<?> task, int timeoutInSeconds) throws Exception {
    final SettableFuture<Void> future = SettableFuture.create();

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            try {
                task.call();
                future.set(null);
            } catch (Throwable e) {
                future.setException(e);
            }
        }
    });

    try {
        future.get(timeoutInSeconds, TimeUnit.SECONDS);
        awaitEvents();
    } catch (ExecutionException e) {
        if (e.getCause() instanceof Exception) {
            throw (Exception) e.getCause();
        } else {
            throw e;
        }
    }
}

From source file:org.apache.twill.internal.Services.java

/**
 * Returns a {@link ListenableFuture} that will be completed when the given service is stopped. If the service
 * stopped due to error, the failure cause would be reflected in the future.
 *
 * @param service The {@link Service} to block on.
 * @return A {@link ListenableFuture} that will be completed when the service is stopped.
 *///from   www. j  a v  a 2 s  . c  om
public static ListenableFuture<Service.State> getCompletionFuture(Service service) {
    final SettableFuture<Service.State> resultFuture = SettableFuture.create();

    service.addListener(new ServiceListenerAdapter() {
        @Override
        public void terminated(Service.State from) {
            resultFuture.set(Service.State.TERMINATED);
        }

        @Override
        public void failed(Service.State from, Throwable failure) {
            resultFuture.setException(failure);
        }
    }, Threads.SAME_THREAD_EXECUTOR);

    Service.State state = service.state();
    if (state == Service.State.TERMINATED) {
        return Futures.immediateFuture(state);
    } else if (state == Service.State.FAILED) {
        return Futures
                .immediateFailedFuture(new IllegalStateException("Service failed with unknown exception."));
    }

    return resultFuture;
}

From source file:org.glowroot.central.util.MoreFutures.java

public static <V> ListenableFuture<V> onFailure(ListenableFuture<V> future, Runnable onFailure) {
    SettableFuture<V> outerFuture = SettableFuture.create();
    Futures.addCallback(future, new FutureCallback<V>() {
        @Override//from  www.  ja  va 2s . c o  m
        public void onSuccess(V result) {
            outerFuture.set(result);
        }

        @Override
        public void onFailure(Throwable t) {
            logger.debug(t.getMessage(), t);
            onFailure.run();
            outerFuture.setException(t);
        }
    }, MoreExecutors.directExecutor());
    return outerFuture;
}

From source file:org.glowroot.central.util.MoreFutures.java

public static <V> ListenableFuture<V> onSuccessAndFailure(ListenableFuture<V> future, Runnable onSuccess,
        Runnable onFailure) {/*from w w w  . j a  v  a  2  s  . c  om*/
    SettableFuture<V> outerFuture = SettableFuture.create();
    Futures.addCallback(future, new FutureCallback<V>() {
        @Override
        public void onSuccess(V result) {
            onSuccess.run();
            outerFuture.set(result);
        }

        @Override
        public void onFailure(Throwable t) {
            logger.debug(t.getMessage(), t);
            onFailure.run();
            outerFuture.setException(t);
        }
    }, MoreExecutors.directExecutor());
    return outerFuture;
}

From source file:com.android.tools.idea.ddms.adb.AdbService.java

/** Returns a future that wraps the given future for obtaining the debug bridge with a timeout. */
private static ListenableFuture<AndroidDebugBridge> makeTimedFuture(
        @NotNull final Future<BridgeConnectionResult> delegate, final long timeout,
        @NotNull final TimeUnit unit) {
    final SettableFuture<AndroidDebugBridge> future = SettableFuture.create();

    ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
        @Override/*from   w ww  .  j  a va  2  s . c  o  m*/
        public void run() {
            try {
                BridgeConnectionResult value = delegate.get(timeout, unit);
                if (value.error != null) {
                    future.setException(new RuntimeException("Unable to create Debug Bridge: " + value.error));
                } else {
                    future.set(value.bridge);
                }
            } catch (ExecutionException e) {
                future.setException(e.getCause());
            } catch (InterruptedException e) {
                delegate.cancel(true);
                future.setException(e);
            } catch (TimeoutException e) {
                delegate.cancel(true);
                future.setException(e);
            }
        }
    });

    return future;
}