Search in sources :

Example 6 with DownloadListener

use of aQute.bnd.service.RepositoryPlugin.DownloadListener in project bnd by bndtools.

the class ResourceRepositoryImpl method getResource.

/**
	 * Get the file belonging to a Resource Descriptor
	 */
public File getResource(byte[] rd, final RepositoryPlugin.DownloadListener... blockers) throws Exception {
    final ResourceDescriptorImpl rds = getResourceDescriptor(rd);
    if (rds == null) {
        logger.debug("no such descriptor {}", Hex.toHexString(rd));
        return null;
    }
    //
    // Construct a path
    //
    final File path = IO.getFile(cache, Hex.toHexString(rds.id) + "/" + rds.bsn + "-" + rds.version + ".jar");
    if (path.isFile()) {
        //
        // Ok, it is there, just report
        //
        ok(blockers, path);
        return path;
    }
    synchronized (failures) {
        Long l = failures.get(rds.url);
        if (l != null && (System.currentTimeMillis() - l) < THRESHOLD) {
            logger.debug("descriptor {}, had earlier failure not retrying", Hex.toHexString(rd));
            return null;
        }
    }
    //
    if (blockers == null || blockers.length == 0) {
        logger.debug("descriptor {}, not found, immediate download", Hex.toHexString(rd));
        download(rds, path);
        return path;
    }
    //
    // We have blockers so we can download in the background.
    //
    logger.debug("descriptor {}, not found, background download", Hex.toHexString(rd));
    synchronized (queues) {
        List<DownloadListener> list = queues.get(path);
        boolean first = list == null || list.isEmpty();
        for (DownloadListener b : blockers) {
            queues.add(path, b);
        }
        if (!first) {
            // return, file is being downloaded by another and that
            // other will signal the download listener.
            logger.debug("someone else is downloading our file {}", queues.get(path));
            return path;
        }
    }
    limitDownloads.acquire();
    executor.execute(new Runnable() {

        public void run() {
            try {
                download(rds, path);
                synchronized (queues) {
                    ok(queues.get(path).toArray(EMPTY_LISTENER), path);
                }
            } catch (Exception e) {
                synchronized (queues) {
                    fail(e, queues.get(path).toArray(EMPTY_LISTENER), path);
                }
            } finally {
                synchronized (queues) {
                    queues.remove(path);
                }
                limitDownloads.release();
            }
        }
    });
    return path;
}
Also used : DownloadListener(aQute.bnd.service.RepositoryPlugin.DownloadListener) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with DownloadListener

use of aQute.bnd.service.RepositoryPlugin.DownloadListener in project bnd by bndtools.

the class DownloadListenerPromise method fail.

@Override
public void fail(Promise<?> resolved) throws Exception {
    Throwable failure = resolved.getFailure();
    logger.debug("{}: failure", this, failure);
    String reason = Exceptions.toString(failure);
    for (DownloadListener dl : dls) {
        try {
            dl.failure(null, reason);
        } catch (Throwable e) {
            reporter.warning("%s: Fail callback failed to %s: %s", this, dl, e);
        }
    }
}
Also used : DownloadListener(aQute.bnd.service.RepositoryPlugin.DownloadListener)

Aggregations

DownloadListener (aQute.bnd.service.RepositoryPlugin.DownloadListener)7 File (java.io.File)6 ResourceDescriptor (aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)2 Version (aQute.bnd.version.Version)2 FileNotFoundException (java.io.FileNotFoundException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Semaphore (java.util.concurrent.Semaphore)2 PutResult (aQute.bnd.service.RepositoryPlugin.PutResult)1 Listener (aQute.bnd.service.repository.ResourceRepository.Listener)1 ResourceRepositoryEvent (aQute.bnd.service.repository.ResourceRepository.ResourceRepositoryEvent)1 SearchableRepository (aQute.bnd.service.repository.SearchableRepository)1 FileRepo (aQute.lib.deployer.FileRepo)1 IO.getFile (aQute.lib.io.IO.getFile)1 SHA1 (aQute.libg.cryptography.SHA1)1 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1