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.opendaylight.netconf.topology.pipeline.tx.ProxyWriteOnlyTransaction.java

@Override
public ListenableFuture<RpcResult<TransactionStatus>> commit() {
    final Future<RpcResult<TransactionStatus>> commit = delegate.commit();
    final SettableFuture<RpcResult<TransactionStatus>> settableFuture = SettableFuture.create();
    commit.onComplete(new OnComplete<RpcResult<TransactionStatus>>() {
        @Override//from  w  w  w  .ja  va2 s  .  c  o m
        public void onComplete(Throwable throwable, RpcResult<TransactionStatus> transactionStatusRpcResult)
                throws Throwable {
            if (throwable == null) {
                settableFuture.set(transactionStatusRpcResult);
            } else {
                settableFuture.setException(throwable);
            }
        }
    }, actorSystem.dispatcher());
    return settableFuture;
}

From source file:org.opendaylight.controller.cluster.sharding.ShardProxyTransaction.java

@Override
public ListenableFuture<Boolean> validate() {
    LOG.debug("Validating transaction for shard {}", shardRoot);

    checkTransactionReadied();//www .  jav a2 s . co m
    final List<ListenableFuture<Boolean>> futures = cohorts.stream()
            .map(DOMStoreThreePhaseCommitCohort::canCommit).collect(Collectors.toList());
    final SettableFuture<Boolean> ret = SettableFuture.create();

    Futures.addCallback(Futures.allAsList(futures), new FutureCallback<List<Boolean>>() {
        @Override
        public void onSuccess(final List<Boolean> result) {
            ret.set(true);
        }

        @Override
        public void onFailure(final Throwable throwable) {
            ret.setException(throwable);
        }
    }, MoreExecutors.directExecutor());

    return ret;
}

From source file:org.opendaylight.netconf.topology.impl.OnlySuccessStateAggregator.java

@Override
public ListenableFuture<Void> combineDeleteAttempts(List<ListenableFuture<Void>> stateFutures) {
    final SettableFuture<Void> future = SettableFuture.create();
    final ListenableFuture<List<Void>> allAsList = Futures.allAsList(stateFutures);
    Futures.addCallback(allAsList, new FutureCallback<List<Void>>() {
        @Override/*  w  w  w. j a  va 2s  .c  o m*/
        public void onSuccess(List<Void> result) {
            future.set(null);
        }

        @Override
        public void onFailure(Throwable t) {
            LOG.error("One of the combined delete attempts failed {}", t);
            future.setException(t);
        }
    });
    return future;
}

From source file:io.crate.executor.transport.TableCreator.java

private void setException(SettableFuture<Long> result, Throwable e, CreateTableAnalyzedStatement statement) {
    e = Exceptions.unwrap(e);//from  w ww.  j  ava2s . com
    String message = e.getMessage();
    if ("mapping [default]".equals(message) && e.getCause() != null) {
        // this is a generic mapping parse exception,
        // the cause has usually a better more detailed error message
        result.setException(e.getCause());
    } else if (statement.ifNotExists() && (e instanceof IndexAlreadyExistsException
            || (e instanceof IndexTemplateAlreadyExistsException && statement.templateName() != null))) {
        result.set(null);
    } else {
        result.setException(e);
    }
}

From source file:com.google.gapid.server.ChildProcess.java

protected void runProcess(final SettableFuture<T> result, final ProcessBuilder pb) {
    try {//from ww w  .j a  v a 2s .  c  o  m
        // This will throw IOException if the executable is not found.
        LOG.log(INFO, "Starting " + name + " as " + pb.command());
        process = pb.start();
    } catch (IOException e) {
        LOG.log(WARNING, "IO Error running process", e);
        result.setException(e);
        return;
    }

    int exitCode = -1;
    try (OutputHandler<T> stdout = createStdoutHandler(); OutputHandler<T> stderr = createStderrHandler()) {
        stdout.start(process.getInputStream(), result);
        stderr.start(process.getErrorStream(), result);
        exitCode = process.waitFor();
        stderr.join();
        stdout.join();
        stderr.finish(result);
        stdout.finish(result);
        if (!result.isDone()) {
            result.setException(new Exception(name + " has exited"));
        }
    } catch (InterruptedException e) {
        LOG.log(INFO, "Killing " + name);
        result.setException(e);
        process.destroy();
    } finally {
        onExit(exitCode);
    }
}

From source file:com.android.builder.internal.aapt.AbstractProcessExecutionAapt.java

@Override
@NonNull//from   w w w .ja  v  a  2s  . co  m
protected ListenableFuture<Void> makeValidatedPackage(@NonNull AaptPackageConfig config) throws AaptException {
    ProcessInfoBuilder builder = makePackageProcessBuilder(config);

    final ProcessInfo processInfo = builder.createProcess();
    ListenableFuture<ProcessResult> execResult = mProcessExecutor.submit(processInfo, mProcessOutputHandler);

    final SettableFuture<Void> result = SettableFuture.create();
    Futures.addCallback(execResult, new FutureCallback<ProcessResult>() {
        @Override
        public void onSuccess(ProcessResult processResult) {
            try {
                processResult.rethrowFailure().assertNormalExitValue();
                result.set(null);
            } catch (Exception e) {
                result.setException(e);
            }
        }

        @Override
        public void onFailure(@NonNull Throwable t) {
            result.setException(t);
        }
    });

    return result;
}

From source file:co.cask.cdap.common.twill.AbstractMasterTwillRunnable.java

private Service.Listener createServiceListener(final String name, final SettableFuture<String> future) {
    return new ServiceListenerAdapter() {
        @Override//from w  ww.  j av a 2 s .c  o  m
        public void terminated(Service.State from) {
            LOG.info("Service " + name + " terminated");
            future.set(name);
        }

        @Override
        public void failed(Service.State from, Throwable failure) {
            LOG.error("Service " + name + " failed", failure);
            future.setException(failure);
        }
    };
}

From source file:com.android.ddmlib.AndroidDebugBridge.java

public static ListenableFuture<AdbVersion> getAdbVersion(@NonNull final File adb) {
    final SettableFuture<AdbVersion> future = SettableFuture.create();
    new Thread(new Runnable() {
        @Override/*from   ww  w . j  av a 2  s  . c  o  m*/
        public void run() {
            ProcessBuilder pb = new ProcessBuilder(adb.getPath(), "version");
            pb.redirectErrorStream(true);

            Process p = null;
            try {
                p = pb.start();
            } catch (IOException e) {
                future.setException(e);
                return;
            }

            StringBuilder sb = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            try {
                String line;
                while ((line = br.readLine()) != null) {
                    AdbVersion version = AdbVersion.parseFrom(line);
                    if (version != AdbVersion.UNKNOWN) {
                        future.set(version);
                        return;
                    }
                    sb.append(line);
                    sb.append('\n');
                }
            } catch (IOException e) {
                future.setException(e);
                return;
            } finally {
                try {
                    br.close();
                } catch (IOException e) {
                    future.setException(e);
                }
            }

            future.setException(
                    new RuntimeException("Unable to detect adb version, adb output: " + sb.toString()));
        }
    }, "Obtaining adb version").start();
    return future;
}

From source file:com.github.xose.persona.verifier.RemoteVerifier.java

@Override
public ListenableFuture<VerifyResult> verify(String assertion, String audience) {
    final SettableFuture<VerifyResult> future = SettableFuture.create();

    Request request = new RequestBuilder("POST").setUrl(verifyUrl).addParameter("assertion", assertion)
            .addParameter("audience", audience).build();

    try {/* w w w .  j a  v  a 2 s  .com*/
        client.executeRequest(request, new AsyncCompletionHandler<Response>() {
            @Override
            public Response onCompleted(Response response) throws IOException {
                if (200 != response.getStatusCode()) {
                    future.setException(new Exception("HTTP Code " + response.getStatusCode()));
                    return response;
                }

                try {
                    future.set(VerifyResultParser.fromJSONString(response.getResponseBody()));
                } catch (Throwable e) {
                    future.setException(new Exception("JSON parsing error", e));
                }

                return response;
            }

            @Override
            public void onThrowable(Throwable t) {
                future.setException(t);
            }
        });
    } catch (IOException e) {
        future.setException(e);
    }

    return future;
}

From source file:org.apache.qpid.server.model.AbstractConfiguredObjectTypeFactory.java

@Override
public ListenableFuture<X> createAsync(final ConfiguredObjectFactory factory,
        final Map<String, Object> attributes, final ConfiguredObject<?> parent) {
    final SettableFuture<X> returnVal = SettableFuture.create();
    final X instance = createInstance(attributes, parent);
    final ListenableFuture<Void> createFuture = instance.createAsync();
    AbstractConfiguredObject.addFutureCallback(createFuture, new FutureCallback<Void>() {
        @Override/*from w ww .j a  v a2 s . co m*/
        public void onSuccess(final Void result) {
            returnVal.set(instance);
        }

        @Override
        public void onFailure(final Throwable t) {
            returnVal.setException(t);
        }
    }, MoreExecutors.directExecutor());

    return returnVal;
}