Example usage for com.google.common.util.concurrent Futures makeChecked

List of usage examples for com.google.common.util.concurrent Futures makeChecked

Introduction

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

Prototype

@GwtIncompatible("TODO")
@CheckReturnValue
public static <V, X extends Exception> CheckedFuture<V, X> makeChecked(ListenableFuture<V> future,
        Function<? super Exception, X> mapper) 

Source Link

Document

Creates a CheckedFuture out of a normal ListenableFuture and a Function that maps from Exception instances into the appropriate checked type.

Usage

From source file:com.google.gerrit.server.index.change.ChangeIndexer.java

public static CheckedFuture<?, IOException> allAsList(List<? extends ListenableFuture<?>> futures) {
    // allAsList propagates the first seen exception, wrapped in
    // ExecutionException, so we can reuse the same mapper as for a single
    // future. Assume the actual contents of the exception are not useful to
    // callers. All exceptions are already logged by IndexTask.
    return Futures.makeChecked(Futures.allAsList(futures), MAPPER);
}

From source file:org.opendaylight.controller.md.sal.dom.broker.impl.legacy.sharded.adapter.ShardedDOMDataBrokerDelegatingWriteTransaction.java

@Override
public CheckedFuture<Void, TransactionCommitFailedException> submit() {
    return Futures.makeChecked(delegateTx.submit(), TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER);
}

From source file:org.opendaylight.yangtools.yang.model.repo.util.AbstractSchemaRepository.java

private static <T extends SchemaSourceRepresentation> CheckedFuture<T, SchemaSourceException> fetchSource(
        final SourceIdentifier id, final Iterator<AbstractSchemaSourceRegistration<?>> it) {
    final AbstractSchemaSourceRegistration<?> reg = it.next();

    @SuppressWarnings("unchecked")
    final CheckedFuture<? extends T, SchemaSourceException> f = ((SchemaSourceProvider<T>) reg.getProvider())
            .getSource(id);//from  w ww .  ja v a  2  s .c  o  m

    return Futures.makeChecked(Futures.withFallback(f, new FutureFallback<T>() {
        @Override
        public ListenableFuture<T> create(@Nonnull final Throwable t) throws SchemaSourceException {
            LOG.debug("Failed to acquire source from {}", reg, t);

            if (it.hasNext()) {
                return fetchSource(id, it);
            }

            throw new MissingSchemaSourceException("All available providers exhausted", id, t);
        }
    }), FETCH_MAPPER);
}

From source file:org.opendaylight.controller.cluster.schema.provider.impl.RemoteSchemaProvider.java

@Override
public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(SourceIdentifier sourceIdentifier) {
    LOG.trace("Getting yang schema source for {}", sourceIdentifier.getName());

    Future<YangTextSchemaSourceSerializationProxy> result = remoteRepo
            .getYangTextSchemaSource(sourceIdentifier);

    final SettableFuture<YangTextSchemaSource> res = SettableFuture.create();
    result.onComplete(new OnComplete<YangTextSchemaSourceSerializationProxy>() {
        @Override// www  .  j  a va 2s  . c  om
        public void onComplete(Throwable throwable,
                YangTextSchemaSourceSerializationProxy yangTextSchemaSourceSerializationProxy) {
            if (yangTextSchemaSourceSerializationProxy != null) {
                res.set(yangTextSchemaSourceSerializationProxy.getRepresentation());
            }
            if (throwable != null) {
                res.setException(throwable);
            }
        }

    }, executionContext);

    return Futures.makeChecked(res, MAPPER);
}

From source file:org.opendaylight.mdsal.mount.cache.impl.tx.CachedDOMReadOnlyTransaction.java

@Override
public CheckedFuture<Boolean, ReadFailedException> exists(LogicalDatastoreType store,
        YangInstanceIdentifier path) {//from w w  w.jav a 2  s .co m
    LOG.debug("{}: Exists store={} path={}", nodeId, store, path);

    final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = read(store, path);

    final ListenableFuture<Boolean> transform = Futures.transform(read,
            new Function<Optional<NormalizedNode<?, ?>>, Boolean>() {
                @Nullable
                @Override
                public Boolean apply(final Optional<NormalizedNode<?, ?>> input) {
                    return input.isPresent();
                }
            });

    return Futures.makeChecked(transform, new Function<Exception, ReadFailedException>() {
        @Nullable
        @Override
        public ReadFailedException apply(final Exception e) {
            return new ReadFailedException("%s: Unable to read from cache" + nodeId, e);
        }
    });
}

From source file:org.opendaylight.yangtools.yang.model.repo.util.SchemaSourceTransformer.java

@Override
public CheckedFuture<D, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
    final CheckedFuture<S, SchemaSourceException> f = provider.getSchemaSource(sourceIdentifier, srcClass);
    return Futures.makeChecked(Futures.transform(f, function), MAPPER);
}

From source file:org.opendaylight.mdsal.dom.broker.TransactionChainReadTransaction.java

@Override
public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
        final YangInstanceIdentifier path) {
    final SettableFuture<Optional<NormalizedNode<?, ?>>> readResult = SettableFuture.create();

    Futures.addCallback(previousWriteTxFuture, new FutureCallback<Void>() {
        @Override//from ww w. j  ava 2  s  . co m
        public void onSuccess(@Nullable final Void result) {
            Futures.addCallback(delegateReadTx.read(store, path),
                    new FutureCallback<Optional<NormalizedNode<?, ?>>>() {

                        @Override
                        public void onSuccess(@Nullable final Optional<NormalizedNode<?, ?>> result) {
                            readResult.set(result);
                        }

                        @Override
                        public void onFailure(final Throwable throwable) {
                            txChain.transactionFailed(TransactionChainReadTransaction.this, throwable);
                            readResult.setException(throwable);
                        }
                    });
        }

        @Override
        public void onFailure(final Throwable throwable) {
            // we don't have to notify txchain about this failure
            // failed write transaction should do this
            readResult.setException(throwable);
        }
    });

    return Futures.makeChecked(readResult, ReadFailedException.MAPPER);
}

From source file:org.opendaylight.netconf.topology.singleton.impl.tx.NetconfWriteOnlyTransaction.java

@Override
public CheckedFuture<Void, TransactionCommitFailedException> submit() {
    LOG.trace("{}: Submit", id);

    final Future<Void> submit = delegate.submit();
    final SettableFuture<Void> settFuture = SettableFuture.create();
    final CheckedFuture<Void, TransactionCommitFailedException> checkedFuture;
    checkedFuture = Futures.makeChecked(settFuture,
            new Function<Exception, TransactionCommitFailedException>() {
                @Nullable/*  ww  w  .  j a v a  2s.  c  om*/
                @Override
                public TransactionCommitFailedException apply(Exception input) {
                    return new TransactionCommitFailedException("Transaction commit failed", input);
                }
            });
    submit.onComplete(new OnComplete<Void>() {
        @Override
        public void onComplete(Throwable throwable, Void object) throws Throwable {
            if (throwable == null) {
                settFuture.set(object);
            } else {
                settFuture.setException(throwable);
            }
        }
    }, actorSystem.dispatcher());
    return checkedFuture;
}

From source file:org.opendaylight.netconf.topology.singleton.impl.ProxyDOMRpcService.java

@Nonnull
@Override//from www  .j  a va2s . c o  m
public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
        @Nullable final NormalizedNode<?, ?> input) {
    LOG.trace("{}: Rpc operation invoked with schema type: {} and node: {}.", id, type, input);

    final NormalizedNodeMessage normalizedNodeMessage = new NormalizedNodeMessage(YangInstanceIdentifier.EMPTY,
            input);
    final Future<Object> scalaFuture = Patterns.ask(masterActorRef,
            new InvokeRpcMessage(new SchemaPathMessage(type), normalizedNodeMessage), actorResponseWaitTime);

    final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();

    final CheckedFuture<DOMRpcResult, DOMRpcException> checkedFuture = Futures.makeChecked(settableFuture,
            new Function<Exception, DOMRpcException>() {

                @Nullable
                @Override
                public DOMRpcException apply(@Nullable final Exception exception) {
                    return new ClusteringRpcException(id + ": Exception during remote rpc invocation.",
                            exception);
                }
            });

    scalaFuture.onComplete(new OnComplete<Object>() {
        @Override
        public void onComplete(final Throwable failure, final Object success) throws Throwable {
            if (failure != null) {
                settableFuture.setException(failure);
                return;
            }
            if (success instanceof Throwable) {
                settableFuture.setException((Throwable) success);
                return;
            }
            if (success instanceof EmptyResultResponse || success == null) {
                settableFuture.set(null);
                return;
            }
            final Collection<RpcError> errors = ((InvokeRpcMessageReply) success).getRpcErrors();
            final NormalizedNodeMessage normalizedNodeMessageResult = ((InvokeRpcMessageReply) success)
                    .getNormalizedNodeMessage();
            final DOMRpcResult result;
            if (normalizedNodeMessageResult == null) {
                result = new DefaultDOMRpcResult(errors);
            } else {
                if (errors == null) {
                    result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode());
                } else {
                    result = new DefaultDOMRpcResult(normalizedNodeMessageResult.getNode(), errors);
                }
            }
            settableFuture.set(result);
        }
    }, actorSystem.dispatcher());

    return checkedFuture;

}

From source file:org.opendaylight.mdsal.dom.broker.TransactionChainReadTransaction.java

@Override
public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
        final YangInstanceIdentifier path) {
    final Function<Optional<NormalizedNode<?, ?>>, Boolean> transform = optionalNode -> optionalNode.isPresent()
            ? Boolean.TRUE//from  ww w . j  a  va 2  s. co  m
            : Boolean.FALSE;
    final ListenableFuture<Boolean> existsResult = Futures.transform(read(store, path), transform);
    return Futures.makeChecked(existsResult, ReadFailedException.MAPPER);
}