Search in sources :

Example 1 with DownloadListener

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

the class ResourceRepoTest method testBasic.

public void testBasic() throws Exception {
    // Just basic check
    assertEquals(0, repoImpl.filter(null, null).size());
    SearchableRepository.ResourceDescriptor osgi = create("jar/osgi.jar");
    assertNull(repoImpl.getResource(osgi.id));
    // Add it
    repoImpl.add("x", osgi);
    // See if descriptor exists
    SearchableRepository.ResourceDescriptor t = repoImpl.getResourceDescriptor(osgi.id);
    assertNotNull(t);
    File ff = repoImpl.getResource(osgi.id);
    assertTrue(ff.isFile());
    //
    // Should also be in the list
    //
    List<? extends ResourceDescriptor> list = repoImpl.filter("x", null);
    assertNotNull(list);
    assertEquals(1, list.size());
    //
    // Adding it multiple times
    // is idempotent
    //
    repoImpl.add("x", osgi);
    assertEquals(1, list.size());
    repoImpl.add("y", osgi);
    assertEquals(1, list.size());
    //
    // Check we can delete the cache but this should
    // not delete the index
    //
    repoImpl.deleteCache(t.id);
    list = repoImpl.filter(null, null);
    assertNotNull(list);
    assertEquals(1, list.size());
    //
    // Check download listeners
    //
    final Semaphore s = new Semaphore(0);
    final AtomicBoolean success = new AtomicBoolean(false);
    repoImpl.getResource(t.id, new DownloadListener() {

        @Override
        public void success(File file) throws Exception {
            System.out.println("Success");
            success.set(true);
            s.release();
        }

        @Override
        public void failure(File file, String reason) throws Exception {
            System.out.println("Failure");
            success.set(false);
            s.release();
        }

        @Override
        public boolean progress(File file, int percentage) throws Exception {
            return true;
        }
    });
    s.acquire();
    assertTrue(success.get());
    repoImpl.delete(null, t.id);
    assertEquals(0, repoImpl.filter(null, null).size());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SearchableRepository(aQute.bnd.service.repository.SearchableRepository) DownloadListener(aQute.bnd.service.RepositoryPlugin.DownloadListener) ResourceDescriptor(aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor) Semaphore(java.util.concurrent.Semaphore) File(java.io.File) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 2 with DownloadListener

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

the class ResourceRepoTest method testMultipleDownloads.

public void testMultipleDownloads() throws Exception {
    final Semaphore s = new Semaphore(0);
    final AtomicInteger downloads = new AtomicInteger();
    ResourceDescriptor rd = create("jar/osgi.jar");
    repoImpl.add("x", rd);
    final Semaphore done = new Semaphore(0);
    DownloadListener l = new DownloadListener() {

        @Override
        public void success(File file) throws Exception {
            done.release();
        }

        @Override
        public void failure(File file, String reason) throws Exception {
            System.out.println("failure! " + file + " " + reason);
        }

        @Override
        public boolean progress(File file, int percentage) throws Exception {
            return false;
        }
    };
    repoImpl.addListener(new Listener() {

        @Override
        public void events(ResourceRepositoryEvent... events) throws Exception {
            for (ResourceRepositoryEvent event : events) {
                if (event.type == TYPE.START_DOWNLOAD) {
                    System.out.println("trying to acquire s");
                    s.acquire();
                    System.out.println("got it");
                    downloads.incrementAndGet();
                }
            }
        }
    });
    File f1 = repoImpl.getResource(rd.id, l);
    File f2 = repoImpl.getResource(rd.id, l);
    assertFalse(f1.isFile());
    assertFalse(f2.isFile());
    assertEquals(0, downloads.get());
    s.release();
    done.acquire(2);
    assertTrue(f1.isFile());
    assertTrue(f2.isFile());
    assertTrue(f1.equals(f2));
    assertEquals(1, downloads.get());
}
Also used : DownloadListener(aQute.bnd.service.RepositoryPlugin.DownloadListener) Listener(aQute.bnd.service.repository.ResourceRepository.Listener) ResourceRepositoryEvent(aQute.bnd.service.repository.ResourceRepository.ResourceRepositoryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DownloadListener(aQute.bnd.service.RepositoryPlugin.DownloadListener) Semaphore(java.util.concurrent.Semaphore) File(java.io.File) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ResourceDescriptor(aQute.bnd.service.repository.SearchableRepository.ResourceDescriptor)

Example 3 with DownloadListener

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

the class DownloadListenerPromise method call.

@Override
public Promise<Void> call(Promise<File> resolved) throws Exception {
    File file = resolved.getValue();
    if (file == null) {
        throw new FileNotFoundException("Download failed");
    }
    logger.debug("{}: success {}", this, file);
    if (linked != null) {
        IO.createSymbolicLinkOrCopy(linked, file);
    }
    for (DownloadListener dl : dls) {
        try {
            dl.success(file);
        } catch (Throwable e) {
            reporter.warning("%s: Success callback failed to %s: %s", this, dl, e);
        }
    }
    return null;
}
Also used : DownloadListener(aQute.bnd.service.RepositoryPlugin.DownloadListener) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Example 4 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 5 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