public DownloadReport download(Artifact[] artifacts, DownloadOptions options) {
IvyContext.getContext().pushResolver(this);
try {
CacheManager cacheManager = options.getCacheManager();
EventManager eventManager = options.getEventManager();
boolean useOrigin = options.isUseOrigin();
clearArtifactAttempts();
DownloadReport dr = new DownloadReport();
for (int i = 0; i < artifacts.length; i++) {
final ArtifactDownloadReport adr = new ArtifactDownloadReport(artifacts[i]);
dr.addArtifactReport(adr);
if (eventManager != null) {
eventManager.fireIvyEvent(new NeedArtifactEvent(this, artifacts[i]));
}
ArtifactOrigin origin = cacheManager.getSavedArtifactOrigin(artifacts[i]);
// if we can use origin file, we just ask ivy for the file in cache, and it will return
// the original one if possible. If we are not in useOrigin mode, we use the getArchivePath
// method which always return a path in the actual cache
File archiveFile = cacheManager.getArchiveFileInCache(artifacts[i], origin, options.isUseOrigin());
if (archiveFile.exists()) {
Message.verbose("\t[NOT REQUIRED] "+artifacts[i]);
adr.setDownloadStatus(DownloadStatus.NO);
adr.setSize(archiveFile.length());
adr.setArtifactOrigin(origin);
} else {
Artifact artifact = fromSystem(artifacts[i]);
if (!artifact.equals(artifacts[i])) {
Message.verbose("\t"+getName()+"looking for artifact "+artifact+ " (is "+artifacts[i]+" in system namespace)");
}
long start = System.currentTimeMillis();
try {
ResolvedResource artifactRef = getArtifactRef(artifact, null);
if (artifactRef != null) {
origin = new ArtifactOrigin(artifactRef.getResource().isLocal(), artifactRef.getResource().getName());
if (useOrigin && artifactRef.getResource().isLocal()) {
Message.verbose("\t[NOT REQUIRED] "+artifacts[i]);
cacheManager.saveArtifactOrigin(artifacts[i], origin);
archiveFile = cacheManager.getArchiveFileInCache(artifacts[i], origin);
adr.setDownloadStatus(DownloadStatus.NO);
adr.setSize(archiveFile.length());
adr.setArtifactOrigin(origin);
} else {
// refresh archive file now that we better now its origin
archiveFile = cacheManager.getArchiveFileInCache(artifacts[i], origin, useOrigin);
if (ResourceHelper.equals(artifactRef.getResource(),
archiveFile)) {
Message.error("invalid configuration for resolver '"+getName()+"': pointing artifacts to ivy cache is forbidden !");
return null;
}
Message.info("downloading "+artifactRef.getResource()+" ...");
if (eventManager != null) {
eventManager.fireIvyEvent(new StartArtifactDownloadEvent(this, artifacts[i], origin));
}
File tmp = cacheManager.getArchiveFileInCache(
new DefaultArtifact(
artifacts[i].getModuleRevisionId(),
artifacts[i].getPublicationDate(),
artifacts[i].getName(),
artifacts[i].getType(),
artifacts[i].getExt()+".part",
artifacts[i].getExtraAttributes()),
origin, useOrigin);
// deal with artifact with url special case
if (artifactRef.getResource().getName().equals(String.valueOf(artifacts[i].getUrl()))) {
Message.verbose("\t"+getName()+": downloading "+artifactRef.getResource().getName());
Message.debug("\t\tto "+tmp);
if (tmp.getParentFile() != null) {
tmp.getParentFile().mkdirs();
}
_extartifactrep.get(artifactRef.getResource().getName(), tmp);
adr.setSize(tmp.length());
} else {
adr.setSize(getAndCheck(artifactRef.getResource(), tmp));
}
if (!tmp.renameTo(archiveFile)) {
Message.warn("\t[FAILED ] "+artifacts[i]+" impossible to move temp file to definitive one ("+(System.currentTimeMillis()-start)+"ms)");
adr.setDownloadStatus(DownloadStatus.FAILED);
} else {
cacheManager.saveArtifactOrigin(artifacts[i], origin);
Message.info("\t[SUCCESSFUL ] "+artifacts[i]+" ("+(System.currentTimeMillis()-start)+"ms)");
adr.setDownloadStatus(DownloadStatus.SUCCESSFUL);
adr.setArtifactOrigin(origin);
}
}
} else {
adr.setDownloadStatus(DownloadStatus.FAILED);
}
} catch (Exception ex) {
Message.warn("\t[FAILED ] "+artifacts[i]+" : "+ex.getMessage()+" ("+(System.currentTimeMillis()-start)+"ms)");
adr.setDownloadStatus(DownloadStatus.FAILED);
}
checkInterrupted();
}
if (eventManager != null) {
eventManager.fireIvyEvent(new EndArtifactDownloadEvent(this, artifacts[i], adr, archiveFile));
}
}
return dr;
} finally {
IvyContext.getContext().popResolver();