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;
}
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);
}
}
}
Aggregations