Search in sources :

Example 1 with ModuleDescriptorParser

use of org.apache.ivy.plugins.parser.ModuleDescriptorParser in project ant-ivy by apache.

the class BasicResolver method cacheModuleDescriptor.

private void cacheModuleDescriptor(ModuleDescriptor systemMd, ModuleRevisionId systemMrid, ResolvedResource ivyRef, ResolvedModuleRevision rmr) {
    RepositoryCacheManager cacheManager = getRepositoryCacheManager();
    final ModuleDescriptorParser parser = systemMd.getParser();
    // the metadata artifact which was used to cache the original metadata file
    Artifact requestedMetadataArtifact = (ivyRef == null) ? systemMd.getMetadataArtifact() : parser.getMetadataArtifact(ModuleRevisionId.newInstance(systemMrid, systemMd.getRevision()), ivyRef.getResource());
    cacheManager.originalToCachedModuleDescriptor(this, ivyRef, requestedMetadataArtifact, rmr, new ModuleDescriptorWriter() {

        public void write(ResolvedResource originalMdResource, ModuleDescriptor md, File src, File dest) throws IOException, ParseException {
            if (originalMdResource == null) {
                // a basic ivy file is written containing default data
                XmlModuleDescriptorWriter.write(md, dest);
            } else {
                // copy and update ivy file from source to cache
                parser.toIvyFile(new FileInputStream(src), originalMdResource.getResource(), dest, md);
                long repLastModified = originalMdResource.getLastModified();
                if (repLastModified > 0) {
                    dest.setLastModified(repLastModified);
                }
            }
        }
    });
}
Also used : DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) RepositoryCacheManager(org.apache.ivy.core.cache.RepositoryCacheManager) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) MDResolvedResource(org.apache.ivy.plugins.resolver.util.MDResolvedResource) IOException(java.io.IOException) ParseException(java.text.ParseException) ModuleDescriptorWriter(org.apache.ivy.core.cache.ModuleDescriptorWriter) XmlModuleDescriptorWriter(org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter) File(java.io.File) ModuleDescriptorParser(org.apache.ivy.plugins.parser.ModuleDescriptorParser) Artifact(org.apache.ivy.core.module.descriptor.Artifact) FileInputStream(java.io.FileInputStream)

Example 2 with ModuleDescriptorParser

use of org.apache.ivy.plugins.parser.ModuleDescriptorParser 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 3 with ModuleDescriptorParser

use of org.apache.ivy.plugins.parser.ModuleDescriptorParser in project ant-ivy by apache.

the class DefaultRepositoryCacheManager method doFindModuleInCache.

private ResolvedModuleRevision doFindModuleInCache(ModuleRevisionId mrid, CacheMetadataOptions options, String expectedResolver) {
    if (!lockMetadataArtifact(mrid)) {
        Message.error("impossible to acquire lock for " + mrid);
        return null;
    }
    boolean unlock = true;
    try {
        if (settings.getVersionMatcher().isDynamic(mrid)) {
            String resolvedRevision = getResolvedRevision(expectedResolver, mrid, options);
            if (resolvedRevision == null) {
                return null;
            }
            Message.verbose("found resolved revision in cache: " + mrid + " => " + resolvedRevision);
            // we have found another module in the cache, make sure we unlock
            // the original module
            unlockMetadataArtifact(mrid);
            mrid = ModuleRevisionId.newInstance(mrid, resolvedRevision);
            // don't forget to request a lock on the new module!
            if (!lockMetadataArtifact(mrid)) {
                Message.error("impossible to acquire lock for " + mrid);
                // we couldn't lock the new module, so no need to unlock it
                unlock = false;
                return null;
            }
        }
        File ivyFile = getIvyFileInCache(mrid);
        if (ivyFile.exists()) {
            // found in cache !
            try {
                ModuleDescriptorParser parser = getModuleDescriptorParser(ivyFile);
                ModuleDescriptor depMD = getMdFromCache(parser, options, ivyFile);
                String resolverName = getSavedResolverName(depMD);
                String artResolverName = getSavedArtResolverName(depMD);
                DependencyResolver resolver = settings.getResolver(resolverName);
                if (resolver == null) {
                    Message.debug("\tresolver not found: " + resolverName + " => trying to use the one configured for " + mrid);
                    resolver = settings.getResolver(depMD.getResolvedModuleRevisionId());
                    if (resolver != null) {
                        Message.debug("\tconfigured resolver found for " + depMD.getResolvedModuleRevisionId() + ": " + resolver.getName() + ": saving this data");
                        saveResolver(depMD, resolver.getName());
                    }
                }
                DependencyResolver artResolver = settings.getResolver(artResolverName);
                if (artResolver == null) {
                    artResolver = resolver;
                }
                if (resolver != null) {
                    Message.debug("\tfound ivy file in cache for " + mrid + " (resolved by " + resolver.getName() + "): " + ivyFile);
                    if (expectedResolver == null || expectedResolver.equals(resolver.getName())) {
                        MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(depMD.getMetadataArtifact());
                        madr.setDownloadStatus(DownloadStatus.NO);
                        madr.setSearched(false);
                        madr.setLocalFile(ivyFile);
                        madr.setSize(ivyFile.length());
                        madr.setArtifactOrigin(getSavedArtifactOrigin(depMD.getMetadataArtifact()));
                        if (madr.getArtifactOrigin().isExists()) {
                            if (madr.getArtifactOrigin().isLocal() && madr.getArtifactOrigin().getArtifact().getUrl() != null) {
                                madr.setOriginalLocalFile(new File(madr.getArtifactOrigin().getArtifact().getUrl().toURI()));
                            } else {
                                // find locally cached file
                                madr.setOriginalLocalFile(getArchiveFileInCache(madr.getArtifactOrigin().getArtifact()));
                            }
                        }
                        return new ResolvedModuleRevision(resolver, artResolver, depMD, madr);
                    }
                    Message.debug("found module in cache but with a different resolver: " + "discarding: " + mrid + "; expected resolver=" + expectedResolver + "; resolver=" + resolver.getName());
                } else {
                    Message.debug("\tresolver not found: " + resolverName + " => cannot use cached ivy file for " + mrid);
                }
            } catch (Exception e) {
                // will try with resolver
                Message.debug("\tproblem while parsing cached ivy file for: " + mrid, e);
            }
        } else {
            Message.debug("\tno ivy file in cache for " + mrid + ": tried " + ivyFile);
        }
    } finally {
        if (unlock) {
            unlockMetadataArtifact(mrid);
        }
    }
    return null;
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File) ModuleDescriptorParser(org.apache.ivy.plugins.parser.ModuleDescriptorParser) XmlModuleDescriptorParser(org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParser) ParseException(java.text.ParseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver)

Example 4 with ModuleDescriptorParser

use of org.apache.ivy.plugins.parser.ModuleDescriptorParser in project ant-ivy by apache.

the class ResolveEngine method resolve.

/**
 * Resolve dependencies of a module described by an ivy file.
 *
 * @param ivySource URL
 * @param options ResolveOptions
 * @return ResolveReport
 * @throws ParseException if something goes wrong
 * @throws IOException if something goes wrong
 */
public ResolveReport resolve(URL ivySource, ResolveOptions options) throws ParseException, IOException {
    URLResource res = new URLResource(ivySource);
    ModuleDescriptorParser parser = ModuleDescriptorParserRegistry.getInstance().getParser(res);
    Message.verbose("using " + parser + " to parse " + ivySource);
    ModuleDescriptor md = parser.parseDescriptor(settings, ivySource, options.isValidate());
    String revision = options.getRevision();
    if (revision == null && md.getResolvedModuleRevisionId().getRevision() == null) {
        revision = Ivy.getWorkingRevision();
    }
    if (revision != null) {
        md.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(md.getModuleRevisionId(), revision));
    }
    return resolve(md, options);
}
Also used : URLResource(org.apache.ivy.plugins.repository.url.URLResource) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ModuleDescriptorParser(org.apache.ivy.plugins.parser.ModuleDescriptorParser)

Example 5 with ModuleDescriptorParser

use of org.apache.ivy.plugins.parser.ModuleDescriptorParser in project ant-ivy by apache.

the class DefaultRepositoryCacheManager method cacheModuleDescriptor.

public ResolvedModuleRevision cacheModuleDescriptor(DependencyResolver resolver, final ResolvedResource mdRef, DependencyDescriptor dd, Artifact moduleArtifact, ResourceDownloader downloader, CacheMetadataOptions options) throws ParseException {
    Date cachedPublicationDate = null;
    ArtifactDownloadReport report;
    ModuleRevisionId mrid = moduleArtifact.getModuleRevisionId();
    if (!lockMetadataArtifact(mrid)) {
        Message.error("impossible to acquire lock for " + mrid);
        return null;
    }
    BackupResourceDownloader backupDownloader = new BackupResourceDownloader(downloader);
    try {
        if (!moduleArtifact.isMetadata()) {
            // just make sure the old artifacts are deleted...
            if (isChanging(dd, mrid, options)) {
                long repoLastModified = mdRef.getLastModified();
                Artifact transformedArtifact = NameSpaceHelper.transform(moduleArtifact, options.getNamespace().getToSystemTransformer());
                ArtifactOrigin origin = getSavedArtifactOrigin(transformedArtifact);
                File artFile = getArchiveFileInCache(transformedArtifact, origin, false);
                if (artFile.exists() && repoLastModified > artFile.lastModified()) {
                    // artifacts have changed, they should be downloaded again
                    Message.verbose(mrid + " has changed: deleting old artifacts");
                    Message.debug("deleting " + artFile);
                    if (!artFile.delete()) {
                        Message.error("Couldn't delete outdated artifact from cache: " + artFile);
                        return null;
                    }
                    removeSavedArtifactOrigin(transformedArtifact);
                }
            }
            return null;
        }
        // now let's see if we can find it in cache and if it is up to date
        ResolvedModuleRevision rmr = doFindModuleInCache(mrid, options, null);
        if (rmr != null) {
            if (rmr.getDescriptor().isDefault() && rmr.getResolver() != resolver) {
                Message.verbose("\t" + getName() + ": found revision in cache: " + mrid + " (resolved by " + rmr.getResolver().getName() + "): but it's a default one, maybe we can find a better one");
            } else {
                if (!isCheckmodified(dd, mrid, options) && !isChanging(dd, mrid, options)) {
                    Message.verbose("\t" + getName() + ": revision in cache: " + mrid);
                    rmr.getReport().setSearched(true);
                    return rmr;
                }
                long repLastModified = mdRef.getLastModified();
                long cacheLastModified = rmr.getDescriptor().getLastModified();
                if (!rmr.getDescriptor().isDefault() && repLastModified <= cacheLastModified) {
                    Message.verbose("\t" + getName() + ": revision in cache (not updated): " + mrid);
                    rmr.getReport().setSearched(true);
                    return rmr;
                }
                Message.verbose("\t" + getName() + ": revision in cache is not up to date: " + mrid);
                if (isChanging(dd, mrid, options)) {
                    // ivy file has been updated, we should see if it has a new publication
                    // date to see if a new download is required (in case the dependency is
                    // a changing one)
                    cachedPublicationDate = rmr.getDescriptor().getResolvedPublicationDate();
                }
            }
        }
        Artifact originalMetadataArtifact = getOriginalMetadataArtifact(moduleArtifact);
        // now download module descriptor and parse it
        report = download(originalMetadataArtifact, new ArtifactResourceResolver() {

            public ResolvedResource resolve(Artifact artifact) {
                return mdRef;
            }
        }, backupDownloader, new CacheDownloadOptions().setListener(options.getListener()).setForce(true));
        Message.verbose("\t" + report);
        if (report.getDownloadStatus() == DownloadStatus.FAILED) {
            Message.warn("problem while downloading module descriptor: " + mdRef.getResource() + ": " + report.getDownloadDetails() + " (" + report.getDownloadTimeMillis() + "ms)");
            return null;
        }
        try {
            ModuleDescriptorParser parser = ModuleDescriptorParserRegistry.getInstance().getParser(mdRef.getResource());
            ParserSettings parserSettings = settings;
            if (resolver instanceof AbstractResolver) {
                parserSettings = ((AbstractResolver) resolver).getParserSettings();
            }
            ModuleDescriptor md = getStaledMd(parser, options, report.getLocalFile(), parserSettings);
            if (md == null) {
                throw new IllegalStateException("module descriptor parser returned a null module descriptor, which is not allowed. parser=" + parser + "; parser class=" + parser.getClass().getName() + "; module descriptor resource=" + mdRef.getResource());
            }
            Message.debug("\t" + getName() + ": parsed downloaded md file for " + mrid + "; parsed=" + md.getModuleRevisionId());
            // check if we should delete old artifacts
            boolean deleteOldArtifacts = false;
            if (cachedPublicationDate != null && !cachedPublicationDate.equals(md.getResolvedPublicationDate())) {
                // artifacts have changed, they should be downloaded again
                Message.verbose(mrid + " has changed: deleting old artifacts");
                deleteOldArtifacts = true;
            }
            if (deleteOldArtifacts) {
                for (String conf : md.getConfigurationsNames()) {
                    for (Artifact art : md.getArtifacts(conf)) {
                        Artifact transformedArtifact = NameSpaceHelper.transform(art, options.getNamespace().getToSystemTransformer());
                        ArtifactOrigin origin = getSavedArtifactOrigin(transformedArtifact);
                        File artFile = getArchiveFileInCache(transformedArtifact, origin, false);
                        if (artFile.exists()) {
                            Message.debug("deleting " + artFile);
                            if (!artFile.delete()) {
                                // Old artifacts couldn't get deleted!
                                // Restore the original ivy file so the next time we
                                // resolve the old artifacts are deleted again
                                backupDownloader.restore();
                                Message.error("Couldn't delete outdated artifact from cache: " + artFile);
                                return null;
                            }
                        }
                        removeSavedArtifactOrigin(transformedArtifact);
                    }
                }
            } else if (isChanging(dd, mrid, options)) {
                Message.verbose(mrid + " is changing, but has not changed: will trust cached artifacts if any");
            }
            MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(md.getMetadataArtifact());
            madr.setSearched(true);
            madr.setDownloadStatus(report.getDownloadStatus());
            madr.setDownloadDetails(report.getDownloadDetails());
            madr.setArtifactOrigin(report.getArtifactOrigin());
            madr.setDownloadTimeMillis(report.getDownloadTimeMillis());
            madr.setOriginalLocalFile(report.getLocalFile());
            madr.setSize(report.getSize());
            Artifact transformedMetadataArtifact = NameSpaceHelper.transform(md.getMetadataArtifact(), options.getNamespace().getToSystemTransformer());
            saveArtifactOrigin(transformedMetadataArtifact, report.getArtifactOrigin());
            return new ResolvedModuleRevision(resolver, resolver, md, madr);
        } catch (IOException ex) {
            Message.warn("io problem while parsing ivy file: " + mdRef.getResource(), ex);
            return null;
        }
    } finally {
        unlockMetadataArtifact(mrid);
        backupDownloader.cleanUp();
    }
}
Also used : MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) IOException(java.io.IOException) ArtifactResourceResolver(org.apache.ivy.plugins.repository.ArtifactResourceResolver) Date(java.util.Date) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) ModuleDescriptorParser(org.apache.ivy.plugins.parser.ModuleDescriptorParser) XmlModuleDescriptorParser(org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParser) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File) ParserSettings(org.apache.ivy.plugins.parser.ParserSettings) AbstractResolver(org.apache.ivy.plugins.resolver.AbstractResolver)

Aggregations

ModuleDescriptorParser (org.apache.ivy.plugins.parser.ModuleDescriptorParser)6 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)5 File (java.io.File)4 IOException (java.io.IOException)4 ParseException (java.text.ParseException)3 Artifact (org.apache.ivy.core.module.descriptor.Artifact)3 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)2 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)2 MetadataArtifactDownloadReport (org.apache.ivy.core.report.MetadataArtifactDownloadReport)2 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)2 XmlModuleDescriptorParser (org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParser)2 ResolvedResource (org.apache.ivy.plugins.resolver.util.ResolvedResource)2 PropertiesFile (org.apache.ivy.util.PropertiesFile)2 FileInputStream (java.io.FileInputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Date (java.util.Date)1 ModuleDescriptorWriter (org.apache.ivy.core.cache.ModuleDescriptorWriter)1 RepositoryCacheManager (org.apache.ivy.core.cache.RepositoryCacheManager)1