Search in sources :

Example 1 with DownloadBlocker

use of aQute.bnd.build.DownloadBlocker in project bnd by bndtools.

the class RepoCommand method _copy.

public void _copy(CopyOptions options) throws Exception {
    List<String> args = options._arguments();
    String srcName = args.remove(0);
    String dstName = args.remove(0);
    RepositoryPlugin source = workspace.getRepository(srcName);
    RepositoryPlugin dest = workspace.getRepository(dstName);
    if (source == null) {
        bnd.error("No such source repository: %s, available repos %s", srcName, workspace.getRepositories());
    }
    if (dest == null) {
        bnd.error("No such destination repository: %s, available repos %s", dstName, workspace.getRepositories());
    } else if (!dest.canWrite())
        bnd.error("Destination repository cannot write: %s", dest);
    if (!bnd.isOk() || source == null || dest == null) {
        return;
    }
    logger.debug("src = {} -> {}", srcName, source);
    logger.debug("dst = {} -> {}", dstName, dest);
    @SuppressWarnings("unused")
    class Spec {

        DownloadBlocker src;

        DownloadBlocker dst;

        String bsn;

        Version version;

        public byte[] digest;
    }
    List<Spec> sources = new ArrayList<Spec>();
    for (String bsn : source.list(null)) {
        for (Version version : source.versions(bsn)) {
            logger.debug("src: {} {}", bsn, version);
            Spec spec = new Spec();
            spec.bsn = bsn;
            spec.version = version;
            spec.src = new DownloadBlocker(bnd);
            File src = source.get(bsn, version, null, spec.src);
            if (src == null) {
                bnd.error("No such entry: %s-%s", bsn, version);
            } else {
                spec.dst = findMatchingVersion(dest, bsn, version);
                sources.add(spec);
            }
        }
    }
    for (Spec spec : sources) {
        String reason = spec.src.getReason();
        if (reason != null) {
            bnd.error("Failed to find %s because: %s", spec.src.getFile(), reason);
        }
        File src = spec.src.getFile();
        if (!src.isFile()) {
            bnd.error("Not a valid file %s", spec.src.getFile());
        }
        spec.digest = SHA1.digest(src).digest();
    }
    //
    // See if we can prune the list by diffing
    //
    ResourceRepository resources = null;
    if (dest instanceof ResourceRepository)
        resources = (ResourceRepository) dest;
    nextFile: for (Iterator<Spec> i = sources.iterator(); i.hasNext(); ) {
        Spec spec = i.next();
        if (resources != null) {
            ResourceDescriptor rd = resources.getResourceDescriptor(spec.digest);
            if (rd != null)
                // Already exists
                continue nextFile;
        }
    // TODO Diff
    }
    if (!bnd.isOk())
        return;
    for (Spec spec : sources) {
        File src = spec.src.getFile();
        if (!options.dry()) {
            try (InputStream fin = IO.stream(src)) {
                PutResult put = dest.put(fin, null);
                if (put.digest != null) {
                    if (!Arrays.equals(spec.digest, put.digest)) {
                        bnd.error("Digest error in upload %s", src);
                    }
                }
            } catch (Exception e) {
                bnd.exception(e, "Exception %s in upload %s", e, src);
            }
        }
    }
    for (String bsn : source.list(null)) {
        for (Version version : source.versions(bsn)) {
            System.out.println(bsn + ";version=" + version);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) DownloadBlocker(aQute.bnd.build.DownloadBlocker) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Version(aQute.bnd.version.Version) Iterator(java.util.Iterator) ResourceRepository(aQute.bnd.service.repository.ResourceRepository) File(java.io.File) PutResult(aQute.bnd.service.RepositoryPlugin.PutResult) ResourceDescriptor(aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)

Example 2 with DownloadBlocker

use of aQute.bnd.build.DownloadBlocker in project bnd by bndtools.

the class InfoRepositoryWrapper method init.

boolean init() {
    try {
        if (System.currentTimeMillis() < lastTime + 10000)
            return true;
    } finally {
        lastTime = System.currentTimeMillis();
    }
    Set<String> errors = new LinkedHashSet<String>();
    try {
        //
        // Get the current repo contents
        //
        Set<String> toBeDeleted = new HashSet<String>(persistent.keySet());
        Map<String, DownloadBlocker> blockers = new HashMap<String, DownloadBlocker>();
        for (InfoRepository repo : repos) {
            Map<String, ResourceDescriptor> map = collectKeys(repo);
            for (final Map.Entry<String, ResourceDescriptor> entry : map.entrySet()) {
                final String id = entry.getKey();
                toBeDeleted.remove(id);
                if (persistent.containsKey(id))
                    continue;
                final ResourceDescriptor rd = entry.getValue();
                DownloadBlocker blocker = new DownloadBlocker(null) {

                    //
                    // We steal the thread of the downloader to index
                    //
                    @Override
                    public void success(File file) throws Exception {
                        IndexResult index = null;
                        try {
                            index = repoIndexer.indexFile(file);
                            ResourceBuilder rb = new ResourceBuilder();
                            for (org.osgi.service.indexer.Capability capability : index.capabilities) {
                                CapReqBuilder cb = new CapReqBuilder(capability.getNamespace());
                                cb.addAttributes(capability.getAttributes());
                                cb.addDirectives(capability.getDirectives());
                                rb.addCapability(cb.buildSyntheticCapability());
                            }
                            for (org.osgi.service.indexer.Requirement requirement : index.requirements) {
                                CapReqBuilder cb = new CapReqBuilder(requirement.getNamespace());
                                cb.addAttributes(requirement.getAttributes());
                                cb.addDirectives(requirement.getDirectives());
                                rb.addRequirement(cb.buildSyntheticRequirement());
                            }
                            Resource resource = rb.build();
                            PersistentResource pr = new PersistentResource(resource);
                            persistent.put(id, pr);
                        } finally {
                            super.success(file);
                            if (index != null) {
                                index.resource.close();
                            }
                        }
                    }
                };
                blockers.put(entry.getKey(), blocker);
                repo.get(rd.bsn, rd.version, null, blocker);
            }
        }
        for (Entry<String, DownloadBlocker> entry : blockers.entrySet()) {
            String key = entry.getKey();
            DownloadBlocker blocker = entry.getValue();
            String reason = blocker.getReason();
            if (reason != null) {
                errors.add(key + ": " + reason);
            }
        }
        persistent.keySet().removeAll(toBeDeleted);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (!errors.isEmpty())
        throw new IllegalStateException("Cannot index " + repos + " due to " + errors);
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) IndexResult(org.osgi.service.indexer.ResourceIndexer.IndexResult) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) PersistentResource(aQute.bnd.osgi.resource.PersistentResource) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) PersistentResource(aQute.bnd.osgi.resource.PersistentResource) Resource(org.osgi.resource.Resource) DownloadBlocker(aQute.bnd.build.DownloadBlocker) InfoRepository(aQute.bnd.service.repository.InfoRepository) IOException(java.io.IOException) HashMap(java.util.HashMap) MultiMap(aQute.lib.collections.MultiMap) Map(java.util.Map) PersistentMap(aQute.lib.persistentmap.PersistentMap) File(java.io.File) ResourceDescriptor(aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)

Aggregations

DownloadBlocker (aQute.bnd.build.DownloadBlocker)2 ResourceDescriptor (aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)2 File (java.io.File)2 IOException (java.io.IOException)2 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)1 PersistentResource (aQute.bnd.osgi.resource.PersistentResource)1 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)1 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)1 PutResult (aQute.bnd.service.RepositoryPlugin.PutResult)1 InfoRepository (aQute.bnd.service.repository.InfoRepository)1 ResourceRepository (aQute.bnd.service.repository.ResourceRepository)1 Version (aQute.bnd.version.Version)1 MultiMap (aQute.lib.collections.MultiMap)1 PersistentMap (aQute.lib.persistentmap.PersistentMap)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1