Search in sources :

Example 6 with ResolvedResource

use of org.apache.ivy.plugins.resolver.util.ResolvedResource in project ant-ivy by apache.

the class IBiblioResolver method findIvyFileRef.

@Override
public ResolvedResource findIvyFileRef(DependencyDescriptor dd, ResolveData data) {
    if (!isM2compatible() || !isUsepoms()) {
        return null;
    }
    ModuleRevisionId mrid = dd.getDependencyRevisionId();
    mrid = convertM2IdForResourceSearch(mrid);
    final String revision = dd.getDependencyRevisionId().getRevision();
    final MavenTimedSnapshotVersionMatcher.MavenSnapshotRevision snapshotRevision = MavenTimedSnapshotVersionMatcher.computeIfSnapshot(revision);
    if (snapshotRevision != null) {
        final ResolvedResource rres = findSnapshotDescriptor(dd, data, mrid, snapshotRevision);
        if (rres != null) {
            return rres;
        }
    }
    return findResourceUsingPatterns(mrid, getIvyPatterns(), DefaultArtifact.newPomArtifact(mrid, data.getDate()), getRMDParser(dd, data), data.getDate());
}
Also used : ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) MavenTimedSnapshotVersionMatcher(org.apache.ivy.plugins.version.MavenTimedSnapshotVersionMatcher)

Example 7 with ResolvedResource

use of org.apache.ivy.plugins.resolver.util.ResolvedResource in project ant-ivy by apache.

the class IBiblioResolver method listResources.

@Override
protected ResolvedResource[] listResources(Repository repository, ModuleRevisionId mrid, String pattern, Artifact artifact) {
    if (shouldUseMavenMetadata(pattern)) {
        List<String> revs = listRevisionsWithMavenMetadata(repository, mrid.getModuleId().getAttributes());
        if (revs != null) {
            Message.debug("\tfound revs: " + revs);
            List<ResolvedResource> rres = new ArrayList<>();
            for (String rev : revs) {
                ModuleRevisionId historicalMrid = ModuleRevisionId.newInstance(mrid, rev);
                String patternForRev = pattern;
                if (rev.endsWith("SNAPSHOT")) {
                    String snapshotVersion = findTimestampedSnapshotVersion(historicalMrid);
                    if (snapshotVersion != null) {
                        patternForRev = pattern.replaceFirst("\\-\\[revision\\]", "-" + snapshotVersion);
                    }
                }
                String resolvedPattern = IvyPatternHelper.substitute(patternForRev, historicalMrid, artifact);
                try {
                    Resource res = repository.getResource(resolvedPattern);
                    if (res != null) {
                        // we do not test if the resource actually exist here, it would cause
                        // a lot of checks which are not always necessary depending on the usage
                        // which is done of the returned ResolvedResource array
                        rres.add(new ResolvedResource(res, rev));
                    }
                } catch (IOException e) {
                    Message.warn("impossible to get resource from name listed by maven-metadata.xml:" + rres, e);
                }
            }
            return rres.toArray(new ResolvedResource[rres.size()]);
        } else {
            // use default listing capability
            return super.listResources(repository, mrid, pattern, artifact);
        }
    } else {
        return super.listResources(repository, mrid, pattern, artifact);
    }
}
Also used : ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) ArrayList(java.util.ArrayList) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) Resource(org.apache.ivy.plugins.repository.Resource) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) IOException(java.io.IOException)

Example 8 with ResolvedResource

use of org.apache.ivy.plugins.resolver.util.ResolvedResource in project ant-ivy by apache.

the class RepositoryResolver method findResourceUsingPattern.

@Override
protected ResolvedResource findResourceUsingPattern(ModuleRevisionId mrid, String pattern, Artifact artifact, ResourceMDParser rmdparser, Date date) {
    String name = getName();
    VersionMatcher versionMatcher = getSettings().getVersionMatcher();
    try {
        if (!versionMatcher.isDynamic(mrid) || isAlwaysCheckExactRevision()) {
            String resourceName = IvyPatternHelper.substitute(pattern, mrid, artifact);
            Message.debug("\t trying " + resourceName);
            logAttempt(resourceName);
            Resource res = repository.getResource(resourceName);
            boolean reachable = res.exists();
            if (reachable) {
                String revision;
                if (pattern.contains(IvyPatternHelper.REVISION_KEY)) {
                    revision = mrid.getRevision();
                } else {
                    if ("ivy".equals(artifact.getType()) || "pom".equals(artifact.getType())) {
                        // we can't determine the revision from the pattern, get it
                        // from the module descriptor itself
                        File temp = File.createTempFile("ivy", artifact.getExt());
                        temp.deleteOnExit();
                        repository.get(res.getName(), temp);
                        ModuleDescriptorParser parser = ModuleDescriptorParserRegistry.getInstance().getParser(res);
                        ModuleDescriptor md = parser.parseDescriptor(getParserSettings(), temp.toURI().toURL(), res, false);
                        revision = md.getRevision();
                        if (isNullOrEmpty(revision)) {
                            revision = "working@" + name;
                        }
                    } else {
                        revision = "working@" + name;
                    }
                }
                return new ResolvedResource(res, revision);
            } else if (versionMatcher.isDynamic(mrid)) {
                return findDynamicResourceUsingPattern(rmdparser, mrid, pattern, artifact, date);
            } else {
                Message.debug("\t" + name + ": resource not reachable for " + mrid + ": res=" + res);
                return null;
            }
        } else {
            return findDynamicResourceUsingPattern(rmdparser, mrid, pattern, artifact, date);
        }
    } catch (IOException | ParseException ex) {
        throw new RuntimeException(name + ": unable to get resource for " + mrid + ": res=" + IvyPatternHelper.substitute(pattern, mrid, artifact) + ": " + ex, ex);
    }
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) VersionMatcher(org.apache.ivy.plugins.version.VersionMatcher) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) Resource(org.apache.ivy.plugins.repository.Resource) IOException(java.io.IOException) ParseException(java.text.ParseException) File(java.io.File) ModuleDescriptorParser(org.apache.ivy.plugins.parser.ModuleDescriptorParser)

Example 9 with ResolvedResource

use of org.apache.ivy.plugins.resolver.util.ResolvedResource in project ant-ivy by apache.

the class DefaultRepositoryCacheManager method download.

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);
    }
}
Also used : ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) LocalizableResource(org.apache.ivy.plugins.repository.LocalizableResource) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) LocalizableResource(org.apache.ivy.plugins.repository.LocalizableResource) Resource(org.apache.ivy.plugins.repository.Resource) MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File) ParseException(java.text.ParseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 10 with ResolvedResource

use of org.apache.ivy.plugins.resolver.util.ResolvedResource in project ant-ivy by apache.

the class DefaultRepositoryCacheManager method downloadRepositoryResource.

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);
    }
}
Also used : ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) IOException(java.io.IOException) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) ParseException(java.text.ParseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact)

Aggregations

ResolvedResource (org.apache.ivy.plugins.resolver.util.ResolvedResource)24 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)12 MDResolvedResource (org.apache.ivy.plugins.resolver.util.MDResolvedResource)10 File (java.io.File)9 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)8 IOException (java.io.IOException)7 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)6 Resource (org.apache.ivy.plugins.repository.Resource)6 ParseException (java.text.ParseException)5 ArrayList (java.util.ArrayList)5 Artifact (org.apache.ivy.core.module.descriptor.Artifact)5 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)4 MetadataArtifactDownloadReport (org.apache.ivy.core.report.MetadataArtifactDownloadReport)4 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)4 ArtifactOrigin (org.apache.ivy.core.cache.ArtifactOrigin)3 DefaultArtifact (org.apache.ivy.core.module.descriptor.DefaultArtifact)3 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)3 IvyNode (org.apache.ivy.core.resolve.IvyNode)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2