Search in sources :

Example 11 with Promise

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

the class ChainTest method testThenSuccessException.

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

        @Override
        public Promise<String> call(Promise<String> resolved) throws Exception {
            throw thenException;
        }
    });
    assertFalse("chain not resolved", chain.isDone());
    def.resolve("ok");
    assertTrue("chain resolved", chain.isDone());
    assertEquals("chain failure matches", thenException, chain.getFailure());
}
Also used : Promise(org.osgi.util.promise.Promise) Deferred(org.osgi.util.promise.Deferred) TimeoutException(org.osgi.util.promise.TimeoutException) Test(org.junit.Test)

Example 12 with Promise

use of org.osgi.util.promise.Promise 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 13 with Promise

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

the class OSGiIndex method readIndexes.

private Promise<BridgeRepository> readIndexes(boolean refresh) throws Exception {
    List<Promise<List<Resource>>> promises = new ArrayList<>(getURIs().size());
    for (URI uri : getURIs()) {
        promises.add(download(uri, refresh));
    }
    Promise<List<List<Resource>>> all = Promises.all(promises);
    return all.map(new Function<List<List<Resource>>, BridgeRepository>() {

        @Override
        public BridgeRepository apply(List<List<Resource>> resources) {
            try {
                ResourcesRepository rr = new ResourcesRepository();
                for (List<Resource> p : resources) {
                    rr.addAll(p);
                }
                return new BridgeRepository(rr);
            } catch (Exception e) {
                throw Exceptions.duck(e);
            }
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) URI(java.net.URI) BridgeRepository(aQute.bnd.osgi.repository.BridgeRepository) Promise(org.osgi.util.promise.Promise) ArrayList(java.util.ArrayList) List(java.util.List) ResourcesRepository(aQute.bnd.osgi.repository.ResourcesRepository)

Example 14 with Promise

use of org.osgi.util.promise.Promise 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)

Example 15 with Promise

use of org.osgi.util.promise.Promise in project felix by apache.

the class ConfigurableComponentHolder method enableComponents.

public Promise<Void> enableComponents(final boolean async) {
    synchronized (enableLock) {
        if (m_enablePromise != null) {
            return m_enablePromise;
        }
        wait(m_disablePromise);
        List<AbstractComponentManager<S>> cms = new ArrayList<AbstractComponentManager<S>>();
        synchronized (m_components) {
            if (isSatisfied()) {
                if (m_factoryPidIndex == null || (m_componentMetadata.isObsoleteFactoryComponentFactory() && !m_componentMetadata.isConfigurationRequired())) {
                    m_singleComponent = createComponentManager(false);
                    cms.add(m_singleComponent);
                    m_singleComponent.reconfigure(mergeProperties(null), false, null);
                }
                if (m_factoryPidIndex != null) {
                    for (String pid : m_factoryConfigurations.keySet()) {
                        AbstractComponentManager<S> scm = createComponentManager(true);
                        m_components.put(pid, scm);
                        scm.reconfigure(mergeProperties(pid), false, new TargetedPID(pid));
                        cms.add(scm);
                    }
                }
            }
            m_enabled = true;
        }
        List<Promise<Void>> promises = new ArrayList<Promise<Void>>();
        for (AbstractComponentManager<S> cm : cms) {
            promises.add(cm.enable(async));
        }
        m_enablePromise = new Deferred<List<Void>>().resolveWith(Promises.<Void, Void>all(promises));
        m_disablePromise = null;
        return m_enablePromise;
    }
}
Also used : Promise(org.osgi.util.promise.Promise) Deferred(org.osgi.util.promise.Deferred) ArrayList(java.util.ArrayList) TargetedPID(org.apache.felix.scr.impl.metadata.TargetedPID)

Aggregations

Promise (org.osgi.util.promise.Promise)21 Failure (org.osgi.util.promise.Failure)11 ArrayList (java.util.ArrayList)9 TimeoutException (org.osgi.util.promise.TimeoutException)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 List (java.util.List)8 NoSuchElementException (java.util.NoSuchElementException)8 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)8 File (java.io.File)4 URI (java.net.URI)4 Deferred (org.osgi.util.promise.Deferred)4 ResourcesRepository (aQute.bnd.osgi.repository.ResourcesRepository)3 Resource (org.osgi.resource.Resource)3 BridgeRepository (aQute.bnd.osgi.repository.BridgeRepository)2 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)2 DownloadListenerPromise (aQute.bnd.util.repository.DownloadListenerPromise)2 PromiseCollectors.toPromise (aQute.lib.promise.PromiseCollectors.toPromise)2 Reporter (aQute.service.reporter.Reporter)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2