Search in sources :

Example 11 with Failure

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

the class PromiseImpl method delay.

@Override
public Promise<T> delay(final long milliseconds) {
    final PromiseImpl<T> p = new PromiseImpl<T>();
    then(new Success<T, T>() {

        @Override
        public Promise<T> call(final Promise<T> resolved) throws Exception {
            ses.schedule(new Runnable() {

                @Override
                public void run() {
                    try {
                        p.resolve(resolved.getValue());
                    } catch (IllegalStateException ise) {
                    // Someone else resolved our promise?
                    } catch (Exception e) {
                        p.fail(e);
                    }
                }
            }, milliseconds, MILLISECONDS);
            return null;
        }
    }, new Failure() {

        @Override
        public void fail(final Promise<?> resolved) throws Exception {
            ses.schedule(new Runnable() {

                @Override
                public void run() {
                    try {
                        p.fail(resolved.getFailure());
                    } catch (Exception e) {
                        p.fail(e);
                    }
                }
            }, milliseconds, MILLISECONDS);
        }
    });
    return p;
}
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 12 with Failure

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

the class ChainTest method testThenFailException.

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

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

Example 13 with Failure

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

the class OSGiIndex method isStale.

/**
	 * Check any of the URL indexes are stale.
	 * 
	 * @return
	 * @throws Exception
	 */
boolean isStale() throws Exception {
    final Deferred<List<Void>> freshness = new Deferred<>();
    List<Promise<Void>> promises = new ArrayList<>(getURIs().size());
    for (final URI uri : getURIs()) {
        if (freshness.getPromise().isDone()) {
            // early exit if staleness already detected
            break;
        }
        try {
            Promise<TaggedData> async = client.build().useCache().asTag().async(uri);
            promises.add(async.then(new Success<TaggedData, Void>() {

                @Override
                public Promise<Void> call(Promise<TaggedData> resolved) throws Exception {
                    switch(resolved.getValue().getState()) {
                        case OTHER:
                            // in the offline case
                            // ignore might be best here
                            logger.debug("Could not verify {}", uri);
                            break;
                        case UNMODIFIED:
                            break;
                        case NOT_FOUND:
                        case UPDATED:
                        default:
                            logger.debug("Found {} to be stale", uri);
                            freshness.fail(new Exception("stale"));
                    }
                    return null;
                }
            }, new Failure() {

                @Override
                public void fail(Promise<?> resolved) throws Exception {
                    logger.debug("Could not verify {}: {}", uri, resolved.getFailure());
                    freshness.fail(resolved.getFailure());
                }
            }));
        } catch (Exception e) {
            logger.debug("Checking stale status: {}: {}", uri, e);
        }
    }
    // Resolve when all uris checked
    Promise<List<Void>> all = Promises.all(promises);
    freshness.resolveWith(all);
    // Block until freshness is resolved
    return freshness.getPromise().getFailure() != null;
}
Also used : Deferred(org.osgi.util.promise.Deferred) ArrayList(java.util.ArrayList) TaggedData(aQute.bnd.service.url.TaggedData) URI(java.net.URI) Success(org.osgi.util.promise.Success) Promise(org.osgi.util.promise.Promise) ArrayList(java.util.ArrayList) List(java.util.List) Failure(org.osgi.util.promise.Failure)

Example 14 with Failure

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

the class MavenBndRepository method get.

@Override
public File get(String bsn, Version version, Map<String, String> properties, final DownloadListener... listeners) throws Exception {
    init();
    BundleDescriptor descriptor = index.getDescriptor(bsn, version);
    if (descriptor == null)
        return null;
    Archive archive = descriptor.archive;
    if (archive != null) {
        final File file = storage.toLocalFile(archive);
        final File withSources = new File(file.getParentFile(), "+" + file.getName());
        if (withSources.isFile() && withSources.lastModified() > file.lastModified()) {
            if (listeners.length == 0)
                return withSources;
            for (DownloadListener dl : listeners) dl.success(withSources);
            return withSources;
        }
        Promise<File> promise = index.updateAsync(descriptor, storage.get(archive));
        if (listeners.length == 0)
            return promise.getValue();
        promise.then(new Success<File, Void>() {

            @Override
            public Promise<Void> call(Promise<File> resolved) throws Exception {
                File value = resolved.getValue();
                if (value == null) {
                    throw new FileNotFoundException("Download failed");
                }
                for (DownloadListener dl : listeners) {
                    try {
                        dl.success(value);
                    } catch (Exception e) {
                        reporter.exception(e, "Download listener failed in success callback %s", dl);
                    }
                }
                return null;
            }
        }).then(null, new Failure() {

            @Override
            public void fail(Promise<?> resolved) throws Exception {
                String reason = Exceptions.toString(resolved.getFailure());
                for (DownloadListener dl : listeners) {
                    try {
                        dl.failure(file, reason);
                    } catch (Exception e) {
                        reporter.exception(e, "Download listener failed in failure callback %s", dl);
                    }
                }
            }
        });
        return file;
    }
    return null;
}
Also used : Archive(aQute.maven.api.Archive) FileNotFoundException(java.io.FileNotFoundException) Success(org.osgi.util.promise.Success) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) BundleDescriptor(aQute.bnd.repository.maven.provider.IndexFile.BundleDescriptor) Promise(org.osgi.util.promise.Promise) File(java.io.File) 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