Search in sources :

Example 1 with Function

use of org.osgi.util.function.Function 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 Function

use of org.osgi.util.function.Function in project bnd by bndtools.

the class MavenRepository method getPomPromise.

private Promise<POM> getPomPromise(final Revision revision) throws Exception {
    Deferred<POM> deferred;
    synchronized (poms) {
        Promise<POM> promise = poms.get(revision);
        if (promise != null) {
            return promise;
        }
        deferred = new Deferred<>();
        poms.put(revision, deferred.getPromise());
    }
    Archive pomArchive = revision.getPomArchive();
    deferred.resolveWith(get(pomArchive, false).map(new Function<File, POM>() {

        @Override
        public POM apply(File pomFile) {
            if (pomFile == null) {
                return null;
            }
            try (InputStream fin = IO.stream(pomFile)) {
                return getPom(fin);
            } catch (Exception e) {
                logger.error("Failed to parse pom {} from file {}", revision, pomFile, e);
                return null;
            }
        }
    }));
    return deferred.getPromise();
}
Also used : Function(org.osgi.util.function.Function) Archive(aQute.maven.api.Archive) InputStream(java.io.InputStream) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with Function

use of org.osgi.util.function.Function 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 Promises.resolved(Collections.<Template>emptyList());
    List<Promise<Template>> promises = new LinkedList<>();
    Parameters githubRepos = new GitRepoPreferences().getGithubRepos();
    for (Entry<String, Attrs> entry : githubRepos.entrySet()) {
        final String repo = GitRepoPreferences.removeDuplicateMarker(entry.getKey());
        final Attrs attribs = entry.getValue();
        try {
            final GitHub gitHub = new GitHub(cache, executor);
            promises.add(gitHub.loadRepoDetails(repo).map(new Function<GithubRepoDetailsDTO, Template>() {

                @Override
                public Template apply(GithubRepoDetailsDTO 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 new GitCloneTemplate(params);
                }
            }));
        } catch (Exception e) {
            reporter.exception(e, "Error loading template from Github repository %s", repo);
        }
    }
    return Promises.all(promises);
}
Also used : Parameters(aQute.bnd.header.Parameters) Attrs(aQute.bnd.header.Attrs) URI(java.net.URI) LinkedList(java.util.LinkedList) Template(org.bndtools.templating.Template) Promise(org.osgi.util.promise.Promise) Function(org.osgi.util.function.Function)

Aggregations

Function (org.osgi.util.function.Function)3 File (java.io.File)2 IOException (java.io.IOException)2 URI (java.net.URI)2 Promise (org.osgi.util.promise.Promise)2 Attrs (aQute.bnd.header.Attrs)1 Parameters (aQute.bnd.header.Parameters)1 ResourcesRepository (aQute.bnd.osgi.repository.ResourcesRepository)1 CapabilityBuilder (aQute.bnd.osgi.resource.CapabilityBuilder)1 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)1 ContentCapability (aQute.bnd.osgi.resource.ResourceUtils.ContentCapability)1 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)1 DownloadListenerPromise (aQute.bnd.util.repository.DownloadListenerPromise)1 Archive (aQute.maven.api.Archive)1 Artifact (aQute.p2.api.Artifact)1 P2Impl (aQute.p2.provider.P2Impl)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1