/** * Determines if the specified {@code data} is authorized for this service. If the result resolves * to {@code true}, the request is authorized, or {@code false} otherwise. If the future resolves * exceptionally, the request will not be authorized. */ static <T> CompletionStage<Boolean> authorize(Authorizer<T> authorizer, ServiceRequestContext ctx, T data) { try { final CompletionStage<Boolean> f = authorizer.authorize(ctx, data); if (f == null) { throw new NullPointerException("An " + Authorizer.class.getSimpleName() + " returned null: " + authorizer); } return f; } catch (Throwable cause) { return CompletableFutures.exceptionallyCompletedFuture(cause); } }
@Override public CompletableFuture<AggregatedHttpFile> aggregateWithPooledObjects(Executor fileReadExecutor, ByteBufAllocator alloc) { try { final HttpFile file = getFile(); return file != null ? file.aggregateWithPooledObjects(fileReadExecutor, alloc) : CompletableFuture.completedFuture(HttpFile.nonExistent()); } catch (Exception e) { return CompletableFutures.exceptionallyCompletedFuture(e); } }
@Override public CompletableFuture<AggregatedHttpFile> aggregate(Executor fileReadExecutor) { try { final HttpFile file = getFile(); return file != null ? file.aggregate(fileReadExecutor) : CompletableFuture.completedFuture(HttpFile.nonExistent()); } catch (Exception e) { return CompletableFutures.exceptionallyCompletedFuture(e); } }
case STARTED: if (failIfStarted) { return exceptionallyCompletedFuture( new IllegalStateException("must be stopped to start; currently " + state)); } else { submitted = true; } catch (Exception e) { return exceptionallyCompletedFuture(e); } finally { if (!submitted) { }); return rollbackFuture.<V>thenCompose(unused -> exceptionallyCompletedFuture(cause)); } else { enter(State.STARTED, result);
submitted = true; } catch (Exception e) { return exceptionallyCompletedFuture(e); } finally { if (!submitted) {
@Test public void transferFailure_Throwable() { @SuppressWarnings("unchecked") final AsyncMethodCallback<String> callback = mock(AsyncMethodCallback.class); AsyncMethodCallbacks.transfer(exceptionallyCompletedFuture(new Throwable("foo")), callback); verify(callback, only()).onError(argThat(argument -> { return argument instanceof CompletionException && "foo".equals(argument.getCause().getMessage()); })); }
@Test public void transferFailure_Exception() { @SuppressWarnings("unchecked") final AsyncMethodCallback<String> callback = mock(AsyncMethodCallback.class); AsyncMethodCallbacks.transfer(exceptionallyCompletedFuture(new AnticipatedException()), callback); verify(callback, only()).onError(isA(AnticipatedException.class)); }
attrs = readAttributes(); } catch (IOException e) { return CompletableFutures.exceptionallyCompletedFuture(e); return CompletableFutures.exceptionallyCompletedFuture( new IOException("too large to aggregate: " + attrs.length() + " bytes")); in = newStream(); } catch (IOException e) { return CompletableFutures.exceptionallyCompletedFuture(e);
CompletableFuture<Chunk> createCompletableFuture() { LoadState currentState = state.get(); if (currentState == LoadState.COMPLETED) { return CompletableFuture.completedFuture(loaded.get()); } else if (currentState == LoadState.EXCEPTIONAL) { return CompletableFutures.exceptionallyCompletedFuture(loadException.get()); } CompletableFuture<Chunk> completableFuture = new CompletableFuture<>(); futuresToComplete.add(completableFuture); return completableFuture; } }
private <T> CompletionStage<T> fail() { return CompletableFutures.exceptionallyCompletedFuture( new MemcacheClosedException("Not connected")); } }
/** * Returns the {@link CompletableFuture} whose value is the absolute {@link Revision} of the * specified {@link Revision}. * * @deprecated Use {@link #normalizeNow(Revision)}. */ @Deprecated default CompletableFuture<Revision> normalize(Revision revision) { try { return CompletableFuture.completedFuture(normalizeNow(revision)); } catch (Exception e) { return CompletableFutures.exceptionallyCompletedFuture(e); } }
@Override public CompletableFuture<Map<String, Entry<?>>> find(Revision revision, String pathPattern, Map<FindOption<?>, ?> options) { requireNonNull(revision, "revision"); requireNonNull(pathPattern, "pathPattern"); requireNonNull(options, "options"); final Revision normalizedRevision; try { normalizedRevision = normalizeNow(revision); } catch (Exception e) { return CompletableFutures.exceptionallyCompletedFuture(e); } return unsafeCast(cache.get(new CacheableFindCall(repo, normalizedRevision, pathPattern, options))); }
/** * the callable function will run as soon as the currently active set of futures is less than the * maxConcurrency limit. * * @param callable - a function that creates a future. * @return a proxy future that completes with the future created by the input function. This * future will be immediately failed with {@link CapacityReachedException} if the soft queue * size limit is exceeded. */ public CompletableFuture<T> add(final Callable<? extends CompletionStage<T>> callable) { requireNonNull(callable); final CompletableFuture<T> response = new CompletableFuture<>(); final Job<T> job = new Job<>(callable, response); if (!queue.offer(job)) { final String message = "Queue size has reached capacity: " + maxQueueSize; return CompletableFutures.exceptionallyCompletedFuture(new CapacityReachedException(message)); } pump(); return response; }
/** * the callable function will run as soon as the currently active set of futures is less than the * maxConcurrency limit. * * @param callable - a function that creates a future. * @return a proxy future that completes with the future created by the input function. This * future will be immediately failed with {@link CapacityReachedException} if the soft queue * size limit is exceeded. */ public CompletableFuture<T> add(final Callable<? extends CompletionStage<T>> callable) { requireNonNull(callable); final CompletableFuture<T> response = new CompletableFuture<>(); final Job<T> job = new Job<>(callable, response); if (!queue.offer(job)) { final String message = "Queue size has reached capacity: " + maxQueueSize; return CompletableFutures.exceptionallyCompletedFuture(new CapacityReachedException(message)); } pump(); return response; }
@Override public CompletableFuture<Map<String, Change<?>>> diff(Revision from, Revision to, String pathPattern) { requireNonNull(from, "from"); requireNonNull(to, "to"); requireNonNull(pathPattern, "pathPattern"); final RevisionRange range; try { range = normalizeNow(from, to).toAscending(); } catch (Exception e) { return CompletableFutures.exceptionallyCompletedFuture(e); } return unsafeCast(cache.get(new CacheableMultiDiffCall(repo, range.from(), range.to(), pathPattern))); }
@Override public CompletableFuture<Change<?>> diff(Revision from, Revision to, Query<?> query) { requireNonNull(from, "from"); requireNonNull(to, "to"); requireNonNull(query, "query"); final RevisionRange range; try { range = normalizeNow(from, to).toAscending(); } catch (Exception e) { return CompletableFutures.exceptionallyCompletedFuture(e); } return unsafeCast(cache.get(new CacheableSingleDiffCall(repo, range.from(), range.to(), query))); }
@Override public CompletableFuture<Revision> findLatestRevision(Revision lastKnownRevision, String pathPattern) { requireNonNull(lastKnownRevision, "lastKnownRevision"); requireNonNull(pathPattern, "pathPattern"); final RevisionRange range; try { range = normalizeNow(lastKnownRevision, Revision.HEAD); } catch (Exception e) { return CompletableFutures.exceptionallyCompletedFuture(e); } if (range.from().equals(range.to())) { return CompletableFuture.completedFuture(null); } final CompletableFuture<Object> future = cache.get(new CacheableFindLatestRevCall(repo, range.from(), range.to(), pathPattern)) .thenApply(result -> result != CacheableFindLatestRevCall.EMPTY ? result : null); return unsafeCast(future); }
@Override public <T> CompletableFuture<MergedEntry<T>> mergeFiles(Revision revision, MergeQuery<T> query) { requireNonNull(revision, "revision"); requireNonNull(query, "query"); final Revision normalizedRevision; try { normalizedRevision = normalizeNow(revision); } catch (Exception e) { return CompletableFutures.exceptionallyCompletedFuture(e); } final CacheableMergeQueryCall key = new CacheableMergeQueryCall(repo, normalizedRevision, query); final CompletableFuture<Object> value = cache.getIfPresent(key); if (value != null) { return unsafeCast(value); } return Repository.super.mergeFiles(normalizedRevision, query).thenApply(mergedEntry -> { key.computedValue(mergedEntry); cache.get(key); return mergedEntry; }); }
@Override public <T> CompletionStage<T> send(final Request<T> request) { final CompletionStage<T> future = delegate.send(request); return CompletableFutures.exceptionallyCompose( future, e -> { e = unwrap(e); if (e instanceof MemcacheClosedException && delegate.isConnected()) { return delegate.send(request); } else { return CompletableFutures.exceptionallyCompletedFuture(e); } }); }
private static <T> CompletableFuture<T> run(ThriftCall<T> call) { final ThriftCompletableFuture<T> future = new ThriftCompletableFuture<>(); try { call.apply(future); return future.exceptionally(cause -> Exceptions.throwUnsafely(convertCause(cause))); } catch (Exception e) { return CompletableFutures.exceptionallyCompletedFuture(convertCause(e)); } }