Java Code Examples for org.apache.ivy.core.report.ArtifactDownloadReport#setDownloadStatus()

The following examples show how to use org.apache.ivy.core.report.ArtifactDownloadReport#setDownloadStatus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultRepositoryCacheManager.java    From ant-ivy with Apache License 2.0 6 votes vote down vote up
private void unpackArtifact(Artifact artifact, ArtifactDownloadReport adr,
        CacheDownloadOptions options) {
    Artifact unpacked = packagingManager.getUnpackedArtifact(artifact);
    if (unpacked == null) {
        // nothing to unpack
        return;
    }

    File archiveFile = getArchiveFileInCache(unpacked, null, false);
    if (archiveFile.exists() && !options.isForce()) {
        adr.setUnpackedLocalFile(archiveFile);
        adr.setUnpackedArtifact(unpacked);
    } else {
        Message.info("\tUnpacking " + artifact.getId());
        try {
            final Artifact unpackedArtifact = packagingManager.unpackArtifact(artifact, adr.getLocalFile(), archiveFile);
            adr.setUnpackedLocalFile(archiveFile);
            adr.setUnpackedArtifact(unpackedArtifact);
        } catch (Exception e) {
            Message.debug(e);
            adr.setDownloadStatus(DownloadStatus.FAILED);
            adr.setDownloadDetails("The packed artifact " + artifact.getId()
                    + " could not be unpacked (" + e.getMessage() + ")");
        }
    }
}
 
Example 2
Source File: CacheResolver.java    From ant-ivy with Apache License 2.0 6 votes vote down vote up
@Override
public DownloadReport download(Artifact[] artifacts, DownloadOptions options) {
    ensureConfigured();
    clearArtifactAttempts();
    DownloadReport dr = new DownloadReport();
    for (Artifact artifact : artifacts) {
        final ArtifactDownloadReport adr = new ArtifactDownloadReport(artifact);
        dr.addArtifactReport(adr);
        ResolvedResource artifactRef = getArtifactRef(artifact, null);
        if (artifactRef != null) {
            Message.verbose("\t[NOT REQUIRED] " + artifact);
            ArtifactOrigin origin = new ArtifactOrigin(artifact, true, artifactRef
                    .getResource().getName());
            File archiveFile = ((FileResource) artifactRef.getResource()).getFile();
            adr.setDownloadStatus(DownloadStatus.NO);
            adr.setSize(archiveFile.length());
            adr.setArtifactOrigin(origin);
            adr.setLocalFile(archiveFile);
        } else {
            adr.setDownloadStatus(DownloadStatus.FAILED);
        }
    }
    return dr;
}
 
Example 3
Source File: NoOpRepositoryCacheManager.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ArtifactDownloadReport download(Artifact artifact, ArtifactResourceResolver resourceResolver, ResourceDownloader resourceDownloader, CacheDownloadOptions options) {
    ArtifactDownloadReport report = new ArtifactDownloadReport(null);
    report.setDownloadStatus(DownloadStatus.NO);
    return report;
}
 
Example 4
Source File: NoOpRepositoryCacheManager.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ArtifactDownloadReport download(Artifact artifact, ArtifactResourceResolver resourceResolver, ResourceDownloader resourceDownloader, CacheDownloadOptions options) {
    ArtifactDownloadReport report = new ArtifactDownloadReport(null);
    report.setDownloadStatus(DownloadStatus.NO);
    return report;
}
 
Example 5
Source File: DefaultRepositoryCacheManager.java    From ant-ivy with Apache License 2.0 4 votes vote down vote up
public ArtifactDownloadReport download(Artifact artifact,
        ArtifactResourceResolver resourceResolver, ResourceDownloader resourceDownloader,
        CacheDownloadOptions options) {
    final ArtifactDownloadReport adr = new ArtifactDownloadReport(artifact);
    boolean useOrigin = isUseOrigin();

    // TODO: see if we could lock on the artifact to download only, instead of the module
    // metadata artifact. We'd need to store artifact origin and is local in artifact specific
    // file to do so, or lock the metadata artifact only to update artifact origin, which would
    // mean acquiring nested locks, which can be a dangerous thing
    ModuleRevisionId mrid = artifact.getModuleRevisionId();
    if (!lockMetadataArtifact(mrid)) {
        adr.setDownloadStatus(DownloadStatus.FAILED);
        adr.setDownloadDetails("impossible to get lock for " + mrid);
        return adr;
    }
    try {
        DownloadListener listener = options.getListener();
        if (listener != null) {
            listener.needArtifact(this, artifact);
        }
        ArtifactOrigin origin = getSavedArtifactOrigin(artifact);
        // 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 = getArchiveFileInCache(artifact, origin, useOrigin);

        if (archiveFile.exists() && !options.isForce()) {
            adr.setDownloadStatus(DownloadStatus.NO);
            adr.setSize(archiveFile.length());
            adr.setArtifactOrigin(origin);
            adr.setLocalFile(archiveFile);
        } else {
            long start = System.currentTimeMillis();
            try {
                ResolvedResource artifactRef = resourceResolver.resolve(artifact);
                if (artifactRef != null) {
                    Resource artifactRes = artifactRef.getResource();
                    origin = new ArtifactOrigin(artifact, artifactRes.isLocal(),
                            artifactRes.getName());
                    if (useOrigin && artifactRes.isLocal()) {
                        if (artifactRes instanceof LocalizableResource) {
                            origin.setLocation(((LocalizableResource) artifactRes).getFile()
                                    .getAbsolutePath());
                        }
                        saveArtifactOrigin(artifact, origin);
                        archiveFile = getArchiveFileInCache(artifact, origin);
                        adr.setDownloadStatus(DownloadStatus.NO);
                        adr.setSize(archiveFile.length());
                        adr.setArtifactOrigin(origin);
                        adr.setLocalFile(archiveFile);
                    } else {
                        // refresh archive file now that we better now its origin
                        archiveFile = getArchiveFileInCache(artifact, origin, useOrigin);
                        if (ResourceHelper.equals(artifactRes, archiveFile)) {
                            throw new IllegalStateException("invalid settings for '"
                                    + resourceResolver
                                    + "': pointing repository to ivy cache is forbidden !");
                        }
                        if (listener != null) {
                            listener.startArtifactDownload(this, artifactRef, artifact, origin);
                        }

                        resourceDownloader.download(artifact, artifactRes, archiveFile);
                        adr.setSize(archiveFile.length());
                        saveArtifactOrigin(artifact, origin);
                        adr.setDownloadTimeMillis(System.currentTimeMillis() - start);
                        adr.setDownloadStatus(DownloadStatus.SUCCESSFUL);
                        adr.setArtifactOrigin(origin);
                        adr.setLocalFile(archiveFile);
                    }
                } else {
                    adr.setDownloadStatus(DownloadStatus.FAILED);
                    adr.setDownloadDetails(ArtifactDownloadReport.MISSING_ARTIFACT);
                    adr.setDownloadTimeMillis(System.currentTimeMillis() - start);
                }
            } catch (Exception ex) {
                Message.debug(ex);
                adr.setDownloadStatus(DownloadStatus.FAILED);
                adr.setDownloadDetails(ex.getMessage());
                adr.setDownloadTimeMillis(System.currentTimeMillis() - start);
            }
        }
        if (adr.getDownloadStatus() != DownloadStatus.FAILED) {
            unpackArtifact(artifact, adr, options);
        }
        if (listener != null) {
            listener.endArtifactDownload(this, artifact, adr, archiveFile);
        }
        return adr;
    } finally {
        unlockMetadataArtifact(mrid);
    }
}
 
Example 6
Source File: DefaultRepositoryCacheManager.java    From ant-ivy with Apache License 2.0 4 votes vote down vote up
public ArtifactDownloadReport downloadRepositoryResource(final Resource resource, String name,
        String type, String extension, CacheResourceOptions options, Repository repository) {

    String hash = computeResourceNameHash(resource);
    ModuleRevisionId mrid = ModuleRevisionId.newInstance("_repository_metadata_", hash,
        Ivy.getWorkingRevision());
    Artifact artifact = new DefaultArtifact(mrid, null, name, type, extension);
    final ArtifactDownloadReport adr = new ArtifactDownloadReport(artifact);
    boolean useOrigin = isUseOrigin();

    try {
        DownloadListener listener = options.getListener();
        if (listener != null) {
            listener.needArtifact(this, artifact);
        }
        ArtifactOrigin savedOrigin = getSavedArtifactOrigin(artifact);
        File archiveFile = getArchiveFileInCache(artifact, savedOrigin, useOrigin);

        ArtifactOrigin origin = new ArtifactOrigin(artifact, resource.isLocal(),
                resource.getName());

        if (!options.isForce()
        // if the local file has been checked to be up to date enough recently, don't download
                && checkCacheUptodate(archiveFile, resource, savedOrigin, origin,
                    options.getTtl())) {
            if (archiveFile.exists()) {
                saveArtifactOrigin(artifact, origin);
                adr.setDownloadStatus(DownloadStatus.NO);
                adr.setSize(archiveFile.length());
                adr.setArtifactOrigin(savedOrigin);
                adr.setLocalFile(archiveFile);
            } else {
                // we trust the cache to says that the resource doesn't exist
                adr.setDownloadStatus(DownloadStatus.FAILED);
                adr.setDownloadDetails("Remote resource is known to not exist");
            }
        } else {
            long start = System.currentTimeMillis();
            origin.setLastChecked(start);
            try {
                ResolvedResource artifactRef = new ResolvedResource(resource,
                        Ivy.getWorkingRevision());
                if (useOrigin && resource.isLocal()) {
                    saveArtifactOrigin(artifact, origin);
                    archiveFile = getArchiveFileInCache(artifact, origin);
                    adr.setDownloadStatus(DownloadStatus.NO);
                    adr.setSize(archiveFile.length());
                    adr.setArtifactOrigin(origin);
                    adr.setLocalFile(archiveFile);
                } else {
                    if (listener != null) {
                        listener.startArtifactDownload(this, artifactRef, artifact, origin);
                    }

                    // actual download
                    if (archiveFile.exists()) {
                        archiveFile.delete();
                    }
                    File part = new File(archiveFile.getAbsolutePath() + ".part");
                    repository.get(resource.getName(), part);
                    if (!part.renameTo(archiveFile)) {
                        throw new IOException(
                                "impossible to move part file to definitive one: " + part
                                        + " -> " + archiveFile);
                    }

                    adr.setSize(archiveFile.length());
                    saveArtifactOrigin(artifact, origin);
                    adr.setDownloadTimeMillis(System.currentTimeMillis() - start);
                    adr.setDownloadStatus(DownloadStatus.SUCCESSFUL);
                    adr.setArtifactOrigin(origin);
                    adr.setLocalFile(archiveFile);
                }
            } catch (Exception ex) {
                Message.debug(ex);
                origin.setExist(false);
                saveArtifactOrigin(artifact, origin);
                adr.setDownloadStatus(DownloadStatus.FAILED);
                adr.setDownloadDetails(ex.getMessage());
                adr.setDownloadTimeMillis(System.currentTimeMillis() - start);
            }
        }
        if (listener != null) {
            listener.endArtifactDownload(this, artifact, adr, archiveFile);
        }
        return adr;
    } finally {
        unlockMetadataArtifact(mrid);
    }
}