Search in sources :

Example 1 with Promise

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

the class P2Indexer method readRepository.

private Repository readRepository() throws Exception {
    P2Impl p2 = new P2Impl(client, this.url, Processor.getExecutor());
    List<Artifact> artifacts = p2.getArtifacts();
    List<Promise<Resource>> fetched = new ArrayList<>(artifacts.size());
    Set<URI> visitedURIs = new HashSet<>(artifacts.size());
    Set<ArtifactID> visitedArtifacts = new HashSet<>(artifacts.size());
    Map<ArtifactID, Resource> knownResources = new HashMap<>();
    if (getBridge() != null) {
        for (Capability capability : getBridge().getRepository().findProviders(singleton(MD5_REQUIREMENT)).get(MD5_REQUIREMENT)) {
            Resource resource = capability.getResource();
            IdentityCapability identity = ResourceUtils.getIdentityCapability(resource);
            ArtifactID artifact = new ArtifactID(identity.osgi_identity(), identity.version(), (String) capability.getAttributes().get(MD5_ATTRIBUTE));
            knownResources.put(artifact, resource);
        }
    }
    for (final Artifact a : artifacts) {
        if (!visitedURIs.add(a.uri))
            continue;
        if (a.md5 != null) {
            ArtifactID id = new ArtifactID(a.id, toVersion(a.version), a.md5);
            if (!visitedArtifacts.add(id))
                continue;
            if (knownResources.containsKey(id)) {
                fetched.add(Promises.resolved(knownResources.get(id)));
                continue;
            }
        }
        Promise<Resource> promise = client.build().useCache(MAX_STALE).async(a.uri.toURL()).map(new Function<File, Resource>() {

            @Override
            public Resource apply(File file) {
                try {
                    ResourceBuilder rb = new ResourceBuilder();
                    rb.addFile(file, a.uri);
                    if (a.md5 != null)
                        rb.addCapability(new CapabilityBuilder(P2_CAPABILITY_NAMESPACE).addAttribute(MD5_ATTRIBUTE, a.md5));
                    return rb.build();
                } catch (Exception e) {
                    logger.debug("{}: Failed to create resource for %s from {}", name, a, file, e);
                    return RECOVERY;
                }
            }
        }).recover(new Function<Promise<?>, Resource>() {

            @Override
            public Resource apply(Promise<?> failed) {
                try {
                    logger.debug("{}: Failed to create resource for {}", name, a, failed.getFailure());
                } catch (InterruptedException e) {
                // impossible
                }
                return RECOVERY;
            }
        });
        fetched.add(promise);
    }
    Promise<List<Resource>> all = Promises.all(fetched);
    return all.map(new Function<List<Resource>, ResourcesRepository>() {

        @Override
        public ResourcesRepository apply(List<Resource> resources) {
            ResourcesRepository rr = new ResourcesRepository();
            for (Resource resource : resources) {
                if (resource != RECOVERY) {
                    rr.add(resource);
                }
            }
            return rr;
        }
    }).getValue();
}
Also used : P2Impl(aQute.p2.provider.P2Impl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CapabilityBuilder(aQute.bnd.osgi.resource.CapabilityBuilder) URI(java.net.URI) Function(org.osgi.util.function.Function) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) Capability(org.osgi.resource.Capability) ContentCapability(aQute.bnd.osgi.resource.ResourceUtils.ContentCapability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Resource(org.osgi.resource.Resource) Artifact(aQute.p2.api.Artifact) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) IOException(java.io.IOException) DownloadListenerPromise(aQute.bnd.util.repository.DownloadListenerPromise) Promise(org.osgi.util.promise.Promise) File(java.io.File) ResourcesRepository(aQute.bnd.osgi.repository.ResourcesRepository)

Example 2 with Promise

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

the class FileSetRepository method readFiles.

private Promise<BridgeRepository> readFiles() {
    List<Promise<Resource>> promises = new ArrayList<>(getFiles().size());
    for (File file : getFiles()) {
        promises.add(parse(Promises.resolved(file)));
    }
    Promise<List<Resource>> all = Promises.all(promises);
    return all.map(new Function<List<Resource>, BridgeRepository>() {

        @Override
        public BridgeRepository apply(List<Resource> resources) {
            try {
                ResourcesRepository rr = new ResourcesRepository();
                for (Resource r : resources) {
                    if (r != null) {
                        logger.debug("{}: adding resource {}", getName(), r);
                        rr.add(r);
                    }
                }
                return new BridgeRepository(rr);
            } catch (Exception e) {
                throw Exceptions.duck(e);
            }
        }
    });
}
Also used : ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) BridgeRepository(aQute.bnd.osgi.repository.BridgeRepository) DownloadListenerPromise(aQute.bnd.util.repository.DownloadListenerPromise) Promise(org.osgi.util.promise.Promise) ArrayList(java.util.ArrayList) List(java.util.List) JarFile(java.util.jar.JarFile) File(java.io.File) ResourcesRepository(aQute.bnd.osgi.repository.ResourcesRepository)

Example 3 with Promise

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

use of org.osgi.util.promise.Promise 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);
    // Map List<Repository> to Promise<List<Template>>
    Promise<List<Template>> promise = repos.stream().map(repo -> promiseFactory.submit(() -> {
        Map<Requirement, Collection<Capability>> providerMap = repo.findProviders(Collections.singleton(requirement));
        return providerMap.get(requirement).stream().map(cap -> {
            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)
                    return new CapabilityBasedTemplate(cap, locator, engine);
                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());
            }
            return null;
        }).filter(Objects::nonNull).collect(toList());
    })).collect(toPromise(promiseFactory)).map(ll -> ll.stream().flatMap(List::stream).collect(toList()));
    return promise;
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) ResourceUtils(aQute.bnd.osgi.resource.ResourceUtils) PromiseCollectors.toPromise(aQute.lib.promise.PromiseCollectors.toPromise) Constants(org.osgi.framework.Constants) IdentityNamespace(org.osgi.framework.namespace.IdentityNamespace) BndPreferences(bndtools.preferences.BndPreferences) HashMap(java.util.HashMap) Template(org.bndtools.templating.Template) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Capability(org.osgi.resource.Capability) CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) TemplateLoader(org.bndtools.templating.TemplateLoader) Component(org.osgi.service.component.annotations.Component) OSGiRepository(aQute.bnd.repository.osgi.OSGiRepository) Workspace(aQute.bnd.build.Workspace) Map(java.util.Map) PromiseFactory(org.osgi.util.promise.PromiseFactory) Activate(org.osgi.service.component.annotations.Activate) Repository(org.osgi.service.repository.Repository) ExecutorService(java.util.concurrent.ExecutorService) HttpClient(aQute.bnd.http.HttpClient) Processor(aQute.bnd.osgi.Processor) Reporter(aQute.service.reporter.Reporter) Deactivate(org.osgi.service.component.annotations.Deactivate) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Central(bndtools.central.Central) Collection(java.util.Collection) TemplateEngine(org.bndtools.templating.TemplateEngine) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Namespace(org.osgi.resource.Namespace) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) Executors(java.util.concurrent.Executors) Promise(org.osgi.util.promise.Promise) Objects(java.util.Objects) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ReferencePolicyOption(org.osgi.service.component.annotations.ReferencePolicyOption) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) Strings(aQute.lib.strings.Strings) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) ArrayList(java.util.ArrayList) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Requirement(org.osgi.resource.Requirement) TemplateEngine(org.bndtools.templating.TemplateEngine) OSGiRepository(aQute.bnd.repository.osgi.OSGiRepository) Repository(org.osgi.service.repository.Repository) Objects(java.util.Objects) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with Promise

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

the class GitHubWorkspaceTemplateLoader method findTemplates.

@Override
public Promise<List<Template>> findTemplates(String type, Reporter reporter) {
    if (!TEMPLATE_TYPE.equals(type)) {
        return promiseFactory.resolved(Collections.emptyList());
    }
    GitHub gitHub = new GitHub(cache, promiseFactory);
    Parameters githubRepos = new GitRepoPreferences().getGithubRepos();
    return githubRepos.entrySet().stream().map(entry -> {
        String repo = GitRepoPreferences.removeDuplicateMarker(entry.getKey());
        Attrs attribs = entry.getValue();
        return gitHub.loadRepoDetails(repo).map(detailsDTO -> {
            if (detailsDTO.clone_url == null)
                throw new IllegalArgumentException("Missing clone URL");
            // Generate icon URI from the owner avatar. The s=16 parameter
            // is added to select a 16x16 icon.
            URI avatarUri = null;
            if (detailsDTO.owner.avatar_url != null)
                avatarUri = URI.create(detailsDTO.owner.avatar_url + "&s=16");
            String name = attribs.get("name");
            if (name == null)
                name = repo;
            String branch = attribs.get("branch");
            final GitCloneTemplateParams params = new GitCloneTemplateParams();
            params.cloneUrl = detailsDTO.clone_url;
            if (branch != null)
                params.branch = branch;
            else
                params.branch = "origin/" + detailsDTO.default_branch;
            params.name = name;
            params.category = "GitHub";
            params.iconUri = avatarUri;
            if (detailsDTO.html_url != null) {
                params.helpUri = createHelpUri(repo, detailsDTO.html_url);
            }
            return (Template) new GitCloneTemplate(params);
        });
    }).collect(toPromise(promiseFactory));
}
Also used : Parameters(aQute.bnd.header.Parameters) PromiseCollectors.toPromise(aQute.lib.promise.PromiseCollectors.toPromise) Constants(org.osgi.framework.Constants) Reporter(aQute.service.reporter.Reporter) Deactivate(org.osgi.service.component.annotations.Deactivate) Base64(aQute.lib.base64.Base64) Template(org.bndtools.templating.Template) Executors(java.util.concurrent.Executors) Promise(org.osgi.util.promise.Promise) Objects(java.util.Objects) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) TemplateLoader(org.bndtools.templating.TemplateLoader) List(java.util.List) Component(org.osgi.service.component.annotations.Component) Attrs(aQute.bnd.header.Attrs) PromiseFactory(org.osgi.util.promise.PromiseFactory) Activate(org.osgi.service.component.annotations.Activate) ReferencePolicyOption(org.osgi.service.component.annotations.ReferencePolicyOption) URI(java.net.URI) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) ExecutorService(java.util.concurrent.ExecutorService) Parameters(aQute.bnd.header.Parameters) Attrs(aQute.bnd.header.Attrs) URI(java.net.URI)

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