Search in sources :

Example 51 with CancellationException

use of java.util.concurrent.CancellationException in project linuxtools by eclipse.

the class AbstractRegistry method retrieveTagsFromRegistryV1.

/**
 * Retrieves the list of tags for a given repository, assuming that the
 * target registry is a registry v2 instance.
 *
 * @param client
 *            the client to use
 * @param repository
 *            the repository to look-up
 * @return the list of tags for the given repository
 * @throws CancellationException
 *             - if the computation was cancelled
 * @throws ExecutionException
 *             - if the computation threw an exception
 * @throws InterruptedException
 *             - if the current thread was interrupted while waiting
 */
private List<IRepositoryTag> retrieveTagsFromRegistryV1(final Client client, final String repository) throws InterruptedException, ExecutionException {
    final GenericType<Map<String, String>> REPOSITORY_TAGS_RESULT_LIST = new GenericType<Map<String, String>>() {
    };
    final WebTarget queryTagsResource = client.target(getHTTPServerAddress()).path(// $NON-NLS-1$
    "v1").path("repositories").path(repository).path(// $NON-NLS-1$ //$NON-NLS-2$
    "tags");
    return queryTagsResource.request(APPLICATION_JSON_TYPE).async().method(GET, REPOSITORY_TAGS_RESULT_LIST).get().entrySet().stream().map(e -> new RepositoryTag(e.getKey(), e.getValue())).collect(Collectors.toList());
}
Also used : GET(javax.ws.rs.HttpMethod.GET) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) TimeoutException(java.util.concurrent.TimeoutException) ArrayList(java.util.ArrayList) RepositoryTag(org.eclipse.linuxtools.internal.docker.core.RepositoryTag) ClientBuilder(javax.ws.rs.client.ClientBuilder) ImageSearchResultV1(org.eclipse.linuxtools.internal.docker.core.ImageSearchResultV1) Map(java.util.Map) Status(javax.ws.rs.core.Response.Status) ObjectMapperProvider(com.spotify.docker.client.ObjectMapperProvider) CancellationException(java.util.concurrent.CancellationException) NLS(org.eclipse.osgi.util.NLS) JacksonFeature(org.glassfish.jersey.jackson.JacksonFeature) Collectors(java.util.stream.Collectors) ImageSearchResultV2(org.eclipse.linuxtools.internal.docker.core.ImageSearchResultV2) BearerTokenResponse(org.eclipse.linuxtools.internal.docker.core.OAuth2Utils.BearerTokenResponse) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) GenericType(javax.ws.rs.core.GenericType) List(java.util.List) ImageSearchResult(com.spotify.docker.client.messages.ImageSearchResult) Response(javax.ws.rs.core.Response) DockerImageSearchResult(org.eclipse.linuxtools.internal.docker.core.DockerImageSearchResult) RepositoryTagV2(org.eclipse.linuxtools.internal.docker.core.RepositoryTagV2) APPLICATION_JSON_TYPE(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE) WebTarget(javax.ws.rs.client.WebTarget) OAuth2Utils(org.eclipse.linuxtools.internal.docker.core.OAuth2Utils) GenericType(javax.ws.rs.core.GenericType) RepositoryTag(org.eclipse.linuxtools.internal.docker.core.RepositoryTag) WebTarget(javax.ws.rs.client.WebTarget) Map(java.util.Map)

Example 52 with CancellationException

use of java.util.concurrent.CancellationException in project pravega by pravega.

the class FutureReadResultEntryCollection method cancelAll.

/**
 * Cancels all Reads in this collection..
 */
void cancelAll() {
    List<FutureReadResultEntry> toCancel;
    synchronized (this.reads) {
        toCancel = new ArrayList<>(this.reads);
        this.reads.clear();
    }
    CancellationException ce = new CancellationException();
    toCancel.forEach(e -> e.fail(ce));
}
Also used : CancellationException(java.util.concurrent.CancellationException)

Example 53 with CancellationException

use of java.util.concurrent.CancellationException in project cruise-control by linkedin.

the class OperationFutureTest method testCancelInProgressFuture.

@Test
public void testCancelInProgressFuture() throws InterruptedException, ExecutionException {
    OperationFuture<Integer> future = new OperationFuture<>("testCancelInProgressFuture");
    AtomicBoolean interrupted = new AtomicBoolean(false);
    // An execution thread that should be interrupted before completing the future.
    Thread executionThread = new Thread(new OperationRunnable<Integer>(null, future) {

        @Override
        protected Integer getResult() throws Exception {
            try {
                synchronized (this) {
                    while (!interrupted.get()) {
                        this.wait();
                    }
                }
                return 100;
            } catch (InterruptedException ie) {
                interrupted.set(true);
                throw ie;
            }
        }
    });
    executionThread.start();
    TestThread t = new TestThread(future::get);
    t.start();
    future.cancel(true);
    t.join();
    executionThread.join();
    assertEquals(0, t.result());
    assertTrue(t.exception() instanceof CancellationException);
    assertTrue(future.isDone());
    assertTrue(future.isCancelled());
    assertTrue(interrupted.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 54 with CancellationException

use of java.util.concurrent.CancellationException in project assertj-core by joel-costigliola.

the class StandardRepresentation method toStringOf.

protected String toStringOf(CompletableFuture<?> future) {
    String className = future.getClass().getSimpleName();
    if (!future.isDone())
        return concat(className, "[Incomplete]");
    try {
        Object joinResult = future.join();
        // avoid stack overflow error if future join on itself or another future that cycles back to the first
        Object joinResultRepresentation = joinResult instanceof CompletableFuture ? joinResult : toStringOf(joinResult);
        return concat(className, "[Completed: ", joinResultRepresentation, "]");
    } catch (CompletionException e) {
        return concat(className, "[Failed: ", toStringOf(e.getCause()), "]", String.format("%n%s", getStackTrace(e)));
    } catch (CancellationException e) {
        return concat(className, "[Cancelled]");
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CancellationException(java.util.concurrent.CancellationException) CompletionException(java.util.concurrent.CompletionException) Integer.toHexString(java.lang.Integer.toHexString)

Example 55 with CancellationException

use of java.util.concurrent.CancellationException in project android_packages_apps_Dialer by LineageOS.

the class ActivationTask method onExecuteInBackgroundThread.

@Override
@WorkerThread
public void onExecuteInBackgroundThread() {
    Assert.isNotMainThread();
    LoggerUtils.logImpressionOnMainThread(getContext(), DialerImpression.Type.VVM_ACTIVATION_STARTED);
    PhoneAccountHandle phoneAccountHandle = getPhoneAccountHandle();
    if (phoneAccountHandle == null) {
        // This should never happen
        VvmLog.e(TAG, "null PhoneAccountHandle");
        return;
    }
    PreOMigrationHandler.migrate(getContext(), phoneAccountHandle);
    if (!VisualVoicemailSettingsUtil.isEnabled(getContext(), phoneAccountHandle)) {
        VvmLog.i(TAG, "VVM is disabled");
        return;
    }
    OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(getContext(), phoneAccountHandle);
    if (!helper.isValid()) {
        VvmLog.i(TAG, "VVM not supported on phoneAccountHandle " + phoneAccountHandle);
        VvmAccountManager.removeAccount(getContext(), phoneAccountHandle);
        return;
    }
    // a good chance of being started up.
    if (!VoicemailStatus.edit(getContext(), phoneAccountHandle).setType(helper.getVvmType()).apply()) {
        VvmLog.e(TAG, "Failed to configure content provider - " + helper.getVvmType());
        fail();
    }
    VvmLog.i(TAG, "VVM content provider configured - " + helper.getVvmType());
    if (VvmAccountManager.isAccountActivated(getContext(), phoneAccountHandle)) {
        VvmLog.i(TAG, "Account is already activated");
        onSuccess(getContext(), phoneAccountHandle);
        return;
    }
    helper.handleEvent(VoicemailStatus.edit(getContext(), phoneAccountHandle), OmtpEvents.CONFIG_ACTIVATING);
    if (!hasSignal(getContext(), phoneAccountHandle)) {
        VvmLog.i(TAG, "Service lost during activation, aborting");
        // Restore the "NO SIGNAL" state since it will be overwritten by the CONFIG_ACTIVATING
        // event.
        helper.handleEvent(VoicemailStatus.edit(getContext(), phoneAccountHandle), OmtpEvents.NOTIFICATION_SERVICE_LOST);
        // Don't retry, a new activation will be started after the signal returned.
        return;
    }
    helper.activateSmsFilter();
    VoicemailStatus.Editor status = mRetryPolicy.getVoicemailStatusEditor();
    VisualVoicemailProtocol protocol = helper.getProtocol();
    Bundle data;
    if (mMessageData != null) {
        // The content of STATUS SMS is provided to launch this task, no need to request it
        // again.
        data = mMessageData;
    } else {
        try (StatusSmsFetcher fetcher = new StatusSmsFetcher(getContext(), phoneAccountHandle)) {
            protocol.startActivation(helper, fetcher.getSentIntent());
            // Both the fetcher and OmtpMessageReceiver will be triggered, but
            // OmtpMessageReceiver will just route the SMS back to ActivationTask, which will be
            // rejected because the task is still running.
            data = fetcher.get();
        } catch (TimeoutException e) {
            // The carrier is expected to return an STATUS SMS within STATUS_SMS_TIMEOUT_MILLIS
            // handleEvent() will do the logging.
            helper.handleEvent(status, OmtpEvents.CONFIG_STATUS_SMS_TIME_OUT);
            fail();
            return;
        } catch (CancellationException e) {
            VvmLog.e(TAG, "Unable to send status request SMS");
            fail();
            return;
        } catch (InterruptedException | ExecutionException | IOException e) {
            VvmLog.e(TAG, "can't get future STATUS SMS", e);
            fail();
            return;
        }
    }
    StatusMessage message = new StatusMessage(data);
    VvmLog.d(TAG, "STATUS SMS received: st=" + message.getProvisioningStatus() + ", rc=" + message.getReturnCode());
    if (message.getProvisioningStatus().equals(OmtpConstants.SUBSCRIBER_READY)) {
        VvmLog.d(TAG, "subscriber ready, no activation required");
        updateSource(getContext(), phoneAccountHandle, message);
    } else {
        if (helper.supportsProvisioning()) {
            VvmLog.i(TAG, "Subscriber not ready, start provisioning");
            helper.startProvisioning(this, phoneAccountHandle, status, message, data);
        } else if (message.getProvisioningStatus().equals(OmtpConstants.SUBSCRIBER_NEW)) {
            VvmLog.i(TAG, "Subscriber new but provisioning is not supported");
            // Ignore the non-ready state and attempt to use the provided info as is.
            // This is probably caused by not completing the new user tutorial.
            updateSource(getContext(), phoneAccountHandle, message);
        } else {
            VvmLog.i(TAG, "Subscriber not ready but provisioning is not supported");
            helper.handleEvent(status, OmtpEvents.CONFIG_SERVICE_NOT_AVAILABLE);
        }
    }
    LoggerUtils.logImpressionOnMainThread(getContext(), DialerImpression.Type.VVM_ACTIVATION_COMPLETED);
}
Also used : PhoneAccountHandle(android.telecom.PhoneAccountHandle) StatusSmsFetcher(com.android.voicemail.impl.sms.StatusSmsFetcher) Bundle(android.os.Bundle) IOException(java.io.IOException) StatusMessage(com.android.voicemail.impl.sms.StatusMessage) CancellationException(java.util.concurrent.CancellationException) VisualVoicemailProtocol(com.android.voicemail.impl.protocol.VisualVoicemailProtocol) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) WorkerThread(android.support.annotation.WorkerThread)

Aggregations

CancellationException (java.util.concurrent.CancellationException)505 ExecutionException (java.util.concurrent.ExecutionException)150 Test (org.junit.Test)91 TimeoutException (java.util.concurrent.TimeoutException)76 ArrayList (java.util.ArrayList)47 CountDownLatch (java.util.concurrent.CountDownLatch)46 Future (java.util.concurrent.Future)46 IOException (java.io.IOException)42 ExecutorService (java.util.concurrent.ExecutorService)30 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)29 CompletableFuture (java.util.concurrent.CompletableFuture)28 Callable (java.util.concurrent.Callable)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 List (java.util.List)24 Map (java.util.Map)23 HashMap (java.util.HashMap)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 TimeUnit (java.util.concurrent.TimeUnit)20 CharStream (org.antlr.v4.runtime.CharStream)20 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)20