Search in sources :

Example 6 with Failure

use of org.osgi.util.promise.Failure in project aries by apache.

the class ChainTest method testThenFail.

@Test
public void testThenFail() throws Exception {
    Deferred<String> def = new Deferred<String>();
    final Promise<String> promise = def.getPromise();
    Promise<String> chain = promise.then(null, new Failure() {

        @Override
        public void fail(Promise<?> resolved) throws Exception {
        }
    });
    assertFalse("chain not resolved", chain.isDone());
    Throwable failure = new Throwable("fail!");
    def.fail(failure);
    assertTrue("chain resolved", chain.isDone());
    assertEquals("chain failure matches", failure, chain.getFailure());
}
Also used : Deferred(org.osgi.util.promise.Deferred) Failure(org.osgi.util.promise.Failure) TimeoutException(org.osgi.util.promise.TimeoutException) Test(org.junit.Test)

Example 7 with Failure

use of org.osgi.util.promise.Failure in project bnd by bndtools.

the class IndexFile method sync.

private void sync() throws Exception {
    List<Promise<Void>> sync = new ArrayList<>(promises.size());
    for (Iterator<Entry<Archive, Promise<File>>> i = promises.entrySet().iterator(); i.hasNext(); ) {
        Entry<Archive, Promise<File>> entry = i.next();
        final Archive archive = entry.getKey();
        Promise<File> promise = entry.getValue();
        i.remove();
        sync.add(promise.<Void>then(null, new Failure() {

            @Override
            public void fail(Promise<?> resolved) throws Exception {
                reporter.exception(resolved.getFailure(), "Failed to sync %s", archive);
            }
        }));
    }
    // block until all promises resolved
    Promises.all(sync).getFailure();
}
Also used : Promise(org.osgi.util.promise.Promise) Entry(java.util.Map.Entry) Archive(aQute.maven.api.Archive) ArrayList(java.util.ArrayList) File(java.io.File) Failure(org.osgi.util.promise.Failure)

Example 8 with Failure

use of org.osgi.util.promise.Failure in project aries by apache.

the class MethodCall method fireAndForget.

public Promise<Void> fireAndForget(Bundle clientBundle, ExecutorService executor, ScheduledExecutorService ses) {
    PromiseImpl<Void> started = new PromiseImpl<Void>(executor, ses);
    Object svc;
    try {
        svc = getService();
    } catch (Exception e) {
        logError("Unable to obtain the service object", e);
        started.fail(e);
        return started;
    }
    if (svc instanceof AsyncDelegate) {
        try {
            if (((AsyncDelegate) svc).execute(method, arguments)) {
                releaseService();
                started.resolve(null);
                return started;
            }
        } catch (Exception e) {
            releaseService();
            logError("The AsyncDelegate rejected the fire-and-forget invocation with an exception", e);
            started.fail(e);
            return started;
        }
    }
    //If we get here then svc is either not an async delegate, or it rejected the call
    PromiseImpl<Void> cleanup = new PromiseImpl<Void>();
    try {
        executor.execute(new FireAndForgetWork(this, cleanup, started));
        cleanup.onResolve(new Runnable() {

            public void run() {
                releaseService();
            }
        }).then(null, new Failure() {

            public void fail(Promise<?> resolved) throws Exception {
                logError("The fire-and-forget invocation failed", resolved.getFailure());
            }
        });
    } catch (RejectedExecutionException ree) {
        logError("The Async Service threadpool rejected the fire-and-forget invocation", ree);
        started.fail(new ServiceException("Unable to enqueue the fire-and forget task", 7, ree));
    }
    return started;
}
Also used : AsyncDelegate(org.osgi.service.async.delegate.AsyncDelegate) ServiceException(org.osgi.framework.ServiceException) PromiseImpl(org.apache.aries.async.promise.PromiseImpl) ServiceException(org.osgi.framework.ServiceException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Failure(org.osgi.util.promise.Failure) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 9 with Failure

use of org.osgi.util.promise.Failure in project aries by apache.

the class PromiseImpl method recover.

@Override
public Promise<T> recover(final Function<Promise<?>, ? extends T> recovery) {
    if (recovery == null)
        throw new NullPointerException();
    final PromiseImpl<T> result = new PromiseImpl<T>(exec, ses);
    then(new Success<T, T>() {

        @Override
        public Promise<T> call(Promise<T> resolved) throws Exception {
            result.resolve(resolved.getValue());
            return null;
        }
    }, new Failure() {

        @Override
        public void fail(Promise<?> resolved) throws Exception {
            try {
                T recover = recovery.apply(resolved);
                if (recover != null) {
                    result.resolve(recover);
                } else {
                    result.fail(resolved.getFailure());
                }
            } catch (Throwable t) {
                result.fail(t);
            }
        }
    });
    return result;
}
Also used : Promise(org.osgi.util.promise.Promise) InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException) TimeoutException(org.osgi.util.promise.TimeoutException) Failure(org.osgi.util.promise.Failure)

Example 10 with Failure

use of org.osgi.util.promise.Failure in project aries by apache.

the class PromiseImpl method resolveWith.

public Promise<Void> resolveWith(final Promise<? extends T> with) {
    if (with == null)
        throw new NullPointerException();
    final PromiseImpl<Void> result = new PromiseImpl<Void>(exec, ses);
    with.then(new Success<T, T>() {

        @Override
        public Promise<T> call(Promise<T> resolved) throws Exception {
            if (isDone()) {
                result.fail(new IllegalStateException("associated Promise already resolved"));
            }
            PromiseImpl.this.resolve(resolved.getValue());
            result.resolve(null);
            return null;
        }
    }, new Failure() {

        @Override
        public void fail(Promise<?> resolved) throws Exception {
            if (isDone()) {
                result.fail(new IllegalStateException("associated Promise already resolved"));
            }
            PromiseImpl.this.fail(resolved.getFailure());
            result.resolve(null);
        }
    });
    return result;
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) NoSuchElementException(java.util.NoSuchElementException) TimeoutException(org.osgi.util.promise.TimeoutException) Promise(org.osgi.util.promise.Promise) Failure(org.osgi.util.promise.Failure)

Aggregations

Failure (org.osgi.util.promise.Failure)14 Promise (org.osgi.util.promise.Promise)11 TimeoutException (org.osgi.util.promise.TimeoutException)10 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 NoSuchElementException (java.util.NoSuchElementException)8 Deferred (org.osgi.util.promise.Deferred)3 Archive (aQute.maven.api.Archive)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Success (org.osgi.util.promise.Success)2 BundleDescriptor (aQute.bnd.repository.maven.provider.IndexFile.BundleDescriptor)1 TaggedData (aQute.bnd.service.url.TaggedData)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 List (java.util.List)1 Entry (java.util.Map.Entry)1