Search in sources :

Example 1 with Success

use of org.osgi.util.promise.Success in project bndtools by bndtools.

the class RepositorySelectionPart method reloadRepos.

private void reloadRepos() {
    final IMessageManager messages = getManagedForm().getMessageManager();
    messages.removeMessage(MESSAGE_KEY, runReposViewer.getControl());
    final List<Repository> allRepos = new ArrayList<>();
    try {
        allRepos.addAll(repositories.getOrdered());
        runReposViewer.setInput(allRepos);
    } catch (Exception e) {
        messages.addMessage(MESSAGE_KEY, "Repository List: Unable to load OSGi Repositories. " + e.getMessage(), e, IMessageProvider.ERROR, runReposViewer.getControl());
        // Load the repos and clear the error message if the Workspace is initialised later.
        Central.onWorkspaceInit(new Success<Workspace, Void>() {

            @Override
            public Promise<Void> call(final Promise<Workspace> resolved) throws Exception {
                final Deferred<Void> completion = new Deferred<>();
                SWTConcurrencyUtil.execForControl(runReposViewer.getControl(), true, new Runnable() {

                    @Override
                    public void run() {
                        try {
                            allRepos.clear();
                            allRepos.addAll(resolved.getValue().getPlugins(Repository.class));
                            runReposViewer.setInput(allRepos);
                            messages.removeMessage(MESSAGE_KEY, runReposViewer.getControl());
                            completion.resolve(null);
                        } catch (Exception e) {
                            completion.fail(e);
                        }
                    }
                });
                return completion.getPromise();
            }
        });
    }
    updateButtons();
}
Also used : Promise(org.osgi.util.promise.Promise) Repository(org.osgi.service.repository.Repository) WorkspaceR5Repository(bndtools.central.WorkspaceR5Repository) Deferred(org.osgi.util.promise.Deferred) ArrayList(java.util.ArrayList) IMessageManager(org.eclipse.ui.forms.IMessageManager) InvocationTargetException(java.lang.reflect.InvocationTargetException) Success(org.osgi.util.promise.Success)

Example 2 with Success

use of org.osgi.util.promise.Success in project bndtools by bndtools.

the class ReposTemplateLoader method findTemplates.

@Override
public Promise<List<Template>> findTemplates(String templateType, final Reporter reporter) {
    String filterStr = String.format("(%s=%s)", NS_TEMPLATE, templateType);
    final Requirement requirement = new CapReqBuilder(NS_TEMPLATE).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filterStr).buildSyntheticRequirement();
    // Try to get the repositories and BundleLocator from the workspace
    List<Repository> workspaceRepos;
    BundleLocator tmpLocator;
    try {
        if (workspace == null)
            workspace = Central.getWorkspace();
        workspaceRepos = workspace.getPlugins(Repository.class);
        tmpLocator = new RepoPluginsBundleLocator(workspace.getRepositories());
    } catch (Exception e) {
        workspaceRepos = Collections.emptyList();
        tmpLocator = new DirectDownloadBundleLocator();
    }
    final BundleLocator locator = tmpLocator;
    // Setup the repos
    List<Repository> repos = new ArrayList<>(workspaceRepos.size() + 1);
    repos.addAll(workspaceRepos);
    addPreferenceConfiguredRepos(repos, reporter);
    // Generate a Promise<List<Template>> for each repository and add to an accumulator
    Promise<List<Template>> accumulator = Promises.resolved((List<Template>) new LinkedList<Template>());
    for (final Repository repo : repos) {
        final Deferred<List<Template>> deferred = new Deferred<>();
        final Promise<List<Template>> current = deferred.getPromise();
        accumulator = accumulator.then(new Success<List<Template>, List<Template>>() {

            @Override
            public Promise<List<Template>> call(Promise<List<Template>> resolved) throws Exception {
                final List<Template> prefix = resolved.getValue();
                return current.map(new Function<List<Template>, List<Template>>() {

                    @Override
                    public List<Template> apply(List<Template> t) {
                        return CollectionUtils.append(prefix, t);
                    }
                });
            }
        });
        executor.submit(new Runnable() {

            @Override
            public void run() {
                List<Template> templates = new LinkedList<>();
                Map<Requirement, Collection<Capability>> providerMap = repo.findProviders(Collections.singleton(requirement));
                if (providerMap != null) {
                    Collection<Capability> candidates = providerMap.get(requirement);
                    if (candidates != null) {
                        for (Capability cap : candidates) {
                            IdentityCapability idcap = ResourceUtils.getIdentityCapability(cap.getResource());
                            Object id = idcap.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE);
                            Object ver = idcap.getAttributes().get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
                            try {
                                String engineName = (String) cap.getAttributes().get("engine");
                                if (engineName == null)
                                    engineName = "stringtemplate";
                                TemplateEngine engine = engines.get(engineName);
                                if (engine != null)
                                    templates.add(new CapabilityBasedTemplate(cap, locator, engine));
                                else
                                    reporter.error("Error loading template from resource '%s' version %s: no Template Engine available matching '%s'", id, ver, engineName);
                            } catch (Exception e) {
                                reporter.error("Error loading template from resource '%s' version %s: %s", id, ver, e.getMessage());
                            }
                        }
                    }
                }
                deferred.resolve(templates);
            }
        });
    }
    return accumulator;
}
Also used : Deferred(org.osgi.util.promise.Deferred) ArrayList(java.util.ArrayList) Success(org.osgi.util.promise.Success) Template(org.bndtools.templating.Template) TemplateEngine(org.bndtools.templating.TemplateEngine) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Requirement(org.osgi.resource.Requirement) Promise(org.osgi.util.promise.Promise) Repository(org.osgi.service.repository.Repository) Collection(java.util.Collection) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with Success

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

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

Promise (org.osgi.util.promise.Promise)4 Success (org.osgi.util.promise.Success)4 ArrayList (java.util.ArrayList)3 Deferred (org.osgi.util.promise.Deferred)3 IOException (java.io.IOException)2 List (java.util.List)2 Repository (org.osgi.service.repository.Repository)2 Failure (org.osgi.util.promise.Failure)2 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)1 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)1 BundleDescriptor (aQute.bnd.repository.maven.provider.IndexFile.BundleDescriptor)1 TaggedData (aQute.bnd.service.url.TaggedData)1 Archive (aQute.maven.api.Archive)1 WorkspaceR5Repository (bndtools.central.WorkspaceR5Repository)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1