Search in sources :

Example 6 with Resource

use of org.apache.ivy.plugins.repository.Resource in project ant-ivy by apache.

the class ResolverHelper method findAll.

public static ResolvedResource[] findAll(Repository rep, ModuleRevisionId mrid, String pattern, Artifact artifact) {
    // substitute all but revision
    String partiallyResolvedPattern = IvyPatternHelper.substitute(pattern, ModuleRevisionId.newInstance(mrid, IvyPatternHelper.getTokenString(IvyPatternHelper.REVISION_KEY)), artifact);
    Message.debug("\tlisting all in " + partiallyResolvedPattern);
    String[] revs = listTokenValues(rep, partiallyResolvedPattern, IvyPatternHelper.REVISION_KEY);
    if (revs != null) {
        Message.debug("\tfound revs: " + Arrays.asList(revs));
        List<ResolvedResource> ret = new ArrayList<>(revs.length);
        for (String rev : revs) {
            String rres = IvyPatternHelper.substituteToken(partiallyResolvedPattern, IvyPatternHelper.REVISION_KEY, rev);
            try {
                Resource res = rep.getResource(rres);
                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
                    ret.add(new ResolvedResource(res, rev));
                }
            } catch (IOException e) {
                Message.warn("impossible to get resource from name listed by repository: " + rres, e);
            }
        }
        if (revs.length != ret.size()) {
            Message.debug("\tfound resolved res: " + ret);
        }
        return ret.toArray(new ResolvedResource[ret.size()]);
    }
    if (!partiallyResolvedPattern.contains("[" + IvyPatternHelper.REVISION_KEY + "]")) {
        // the partially resolved pattern is completely resolved, check the resource
        try {
            Resource res = rep.getResource(partiallyResolvedPattern);
            if (res.exists()) {
                Message.debug("\tonly one resource found without real listing: " + "using and defining it as working@" + rep.getName() + " revision: " + res.getName());
                return new ResolvedResource[] { new ResolvedResource(res, "working@" + rep.getName()) };
            }
        } catch (IOException e) {
            Message.debug("\timpossible to get resource from name listed by repository: " + partiallyResolvedPattern, e);
        }
        Message.debug("\tno revision found");
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.ivy.plugins.repository.Resource) IOException(java.io.IOException)

Example 7 with Resource

use of org.apache.ivy.plugins.repository.Resource 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 8 with Resource

use of org.apache.ivy.plugins.repository.Resource 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 9 with Resource

use of org.apache.ivy.plugins.repository.Resource in project ant-ivy by apache.

the class DeliverEngine method deliver.

/**
 * Delivers a resolved ivy file based upon last resolve call status. If resolve report file
 * cannot be found in cache, then it throws an IllegalStateException (maybe resolve has not been
 * called before ?).
 *
 * @param mrid
 *            the module revision id of the module to deliver
 * @param revision
 *            the revision to which the module should be delivered
 * @param destIvyPattern
 *            the pattern to which the delivered ivy file should be written
 * @param options
 *            the options with which deliver should be done
 * @throws IOException if something goes wrong
 * @throws ParseException if something goes wrong
 */
public void deliver(ModuleRevisionId mrid, String revision, String destIvyPattern, DeliverOptions options) throws IOException, ParseException {
    Message.info(":: delivering :: " + mrid + " :: " + revision + " :: " + options.getStatus() + " :: " + options.getPubdate());
    Message.verbose("\toptions = " + options);
    long start = System.currentTimeMillis();
    destIvyPattern = settings.substitute(destIvyPattern);
    // 1) find the resolved module descriptor in cache
    ModuleDescriptor md = getCache().getResolvedModuleDescriptor(mrid);
    md.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(md.getModuleRevisionId(), options.getPubBranch() == null ? mrid.getBranch() : options.getPubBranch(), revision));
    md.setResolvedPublicationDate(options.getPubdate());
    // 2) parse resolvedRevisions From properties file
    // Map (ModuleId -> String revision)
    Map<ModuleRevisionId, String> resolvedRevisions = new HashMap<>();
    // Map (ModuleId -> String branch)
    Map<ModuleRevisionId, String> resolvedBranches = new HashMap<>();
    // Map (ModuleId -> String status)
    Map<ModuleRevisionId, String> dependenciesStatus = new HashMap<>();
    File ivyProperties = getCache().getResolvedIvyPropertiesInCache(mrid);
    if (!ivyProperties.exists()) {
        throw new IllegalStateException("ivy properties not found in cache for " + mrid + "; please resolve dependencies before delivering!");
    }
    Properties props = new Properties();
    FileInputStream in = new FileInputStream(ivyProperties);
    props.load(in);
    in.close();
    for (Object o : props.keySet()) {
        String depMridStr = (String) o;
        String[] parts = props.getProperty(depMridStr).split(" ");
        ModuleRevisionId decodedMrid = ModuleRevisionId.decode(depMridStr);
        if (options.isResolveDynamicRevisions()) {
            resolvedRevisions.put(decodedMrid, parts[0]);
            if (parts.length >= 4) {
                if (parts[3] != null && !"null".equals(parts[3])) {
                    resolvedBranches.put(decodedMrid, parts[3]);
                }
            }
        }
        dependenciesStatus.put(decodedMrid, parts[1]);
        if (options.isReplaceForcedRevisions()) {
            if (parts.length <= 2) {
                // so it is possible that this part doesn't exist.
                throw new IllegalStateException("ivy properties file generated by an older" + " version of Ivy which doesn't support replacing forced revisions!");
            }
            resolvedRevisions.put(decodedMrid, parts[2]);
        }
    }
    // 3) use pdrResolver to resolve dependencies info
    Map<ModuleRevisionId, String> resolvedDependencies = new HashMap<>();
    // Map (ModuleRevisionId -> String revision)
    for (DependencyDescriptor dependency : md.getDependencies()) {
        String rev = resolvedRevisions.get(dependency.getDependencyRevisionId());
        if (rev == null) {
            rev = dependency.getDependencyRevisionId().getRevision();
        }
        String bra = resolvedBranches.get(dependency.getDependencyRevisionId());
        if (bra == null || "null".equals(bra)) {
            bra = dependency.getDependencyRevisionId().getBranch();
        }
        String depStatus = dependenciesStatus.get(dependency.getDependencyRevisionId());
        ModuleRevisionId mrid2 = (bra == null) ? ModuleRevisionId.newInstance(dependency.getDependencyRevisionId(), rev) : ModuleRevisionId.newInstance(dependency.getDependencyRevisionId(), bra, rev);
        resolvedDependencies.put(dependency.getDependencyRevisionId(), options.getPdrResolver().resolve(md, options.getStatus(), mrid2, depStatus));
    }
    // 4) copy the source resolved ivy to the destination specified,
    // updating status, revision and dependency revisions obtained by
    // PublishingDependencyRevisionResolver
    File publishedIvy = settings.resolveFile(IvyPatternHelper.substitute(destIvyPattern, md.getResolvedModuleRevisionId()));
    Message.info("\tdelivering ivy file to " + publishedIvy);
    String[] confs = ConfigurationUtils.replaceWildcards(options.getConfs(), md);
    Set<String> confsToRemove = new HashSet<>(Arrays.asList(md.getConfigurationsNames()));
    confsToRemove.removeAll(Arrays.asList(confs));
    try {
        UpdateOptions opts = new UpdateOptions().setSettings(settings).setResolvedRevisions(resolvedDependencies).setStatus(options.getStatus()).setRevision(revision).setBranch(options.getPubBranch()).setPubdate(options.getPubdate()).setGenerateRevConstraint(options.isGenerateRevConstraint()).setMerge(options.isMerge()).setMergedDescriptor(md).setConfsToExclude(confsToRemove.toArray(new String[confsToRemove.size()]));
        if (!resolvedBranches.isEmpty()) {
            opts = opts.setResolvedBranches(resolvedBranches);
        }
        Resource res = md.getResource();
        XmlModuleDescriptorUpdater.update(res.openStream(), res, publishedIvy, opts);
    } catch (SAXException ex) {
        throw new RuntimeException("bad ivy file in cache for " + mrid, ex);
    }
    Message.verbose("\tdeliver done (" + (System.currentTimeMillis() - start) + "ms)");
}
Also used : HashMap(java.util.HashMap) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) Resource(org.apache.ivy.plugins.repository.Resource) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) UpdateOptions(org.apache.ivy.plugins.parser.xml.UpdateOptions) SAXException(org.xml.sax.SAXException) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) File(java.io.File) HashSet(java.util.HashSet)

Example 10 with Resource

use of org.apache.ivy.plugins.repository.Resource in project ant-ivy by apache.

the class ResolveTest method testUseCacheOnly.

@Test
public void testUseCacheOnly() throws Exception {
    ResolveOptions option = getResolveOptions(new String[] { "*" }).setValidate(false);
    URL url = new File("test/repositories/1/usecacheonly/mod1/ivys/ivy-1.0.xml").toURI().toURL();
    // normal resolve, the file goes in the cache
    ResolveReport report = ivy.resolve(url, option);
    assertFalse(report.hasError());
    option.setUseCacheOnly(true);
    // use cache only, hit the cache
    report = ivy.resolve(url, option);
    assertFalse(report.hasError());
    CacheCleaner.deleteDir(cache);
    createCache();
    // no more in the cache, missed
    report = ivy.resolve(url, option);
    assertTrue(report.hasError());
    option.setUseCacheOnly(false);
    // try with use origin: should fail as the cache is empty
    ivy.getSettings().setDefaultUseOrigin(true);
    option.setUseCacheOnly(true);
    report = ivy.resolve(url, option);
    assertTrue(report.hasError());
    // populate the cache
    option.setUseCacheOnly(false);
    report = ivy.resolve(url, option);
    assertFalse(report.hasError());
    // use origin should now work
    option.setUseCacheOnly(true);
    report = ivy.resolve(url, option);
    assertFalse(report.hasError());
    // ensure that we hit only the cache and never try to hit in the repository
    FileSystemResolver resolver = (FileSystemResolver) ivy.getSettings().getResolver("1");
    resolver.setRepository(new AbstractRepository() {

        public List<String> list(String parent) {
            throw new UnsupportedOperationException();
        }

        public Resource getResource(String source) {
            throw new UnsupportedOperationException();
        }

        public void get(String source, File destination) {
            throw new UnsupportedOperationException();
        }
    });
    report = ivy.resolve(url, option);
    assertFalse(report.hasError());
}
Also used : ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ResolveReport(org.apache.ivy.core.report.ResolveReport) AbstractRepository(org.apache.ivy.plugins.repository.AbstractRepository) Resource(org.apache.ivy.plugins.repository.Resource) List(java.util.List) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) FileSystemResolver(org.apache.ivy.plugins.resolver.FileSystemResolver) Test(org.junit.Test)

Aggregations

Resource (org.apache.ivy.plugins.repository.Resource)20 ResolvedResource (org.apache.ivy.plugins.resolver.util.ResolvedResource)10 File (java.io.File)9 IOException (java.io.IOException)8 URL (java.net.URL)7 URLResource (org.apache.ivy.plugins.repository.url.URLResource)7 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)5 ArrayList (java.util.ArrayList)4 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)4 FileResource (org.apache.ivy.plugins.repository.file.FileResource)4 MDResolvedResource (org.apache.ivy.plugins.resolver.util.MDResolvedResource)4 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 ParseException (java.text.ParseException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 CacheResourceOptions (org.apache.ivy.core.cache.CacheResourceOptions)2