Search in sources :

Example 1 with AbstractResolver

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

the class SearchEngine method findModuleRevisionIds.

public Collection<ModuleRevisionId> findModuleRevisionIds(DependencyResolver resolver, ModuleRevisionId pattern, PatternMatcher matcher) {
    Collection<ModuleRevisionId> mrids = new ArrayList<>();
    String resolverName = resolver.getName();
    Message.verbose("looking for modules matching " + pattern + " using " + matcher.getName());
    Namespace fromNamespace = null;
    if (resolver instanceof AbstractResolver) {
        fromNamespace = resolver.getNamespace();
    }
    Collection<ModuleEntry> modules = new ArrayList<>();
    OrganisationEntry[] orgs = resolver.listOrganisations();
    if (orgs == null || orgs.length == 0) {
        // hack for resolvers which are not able to list organisation, we try to see if the
        // asked organisation is not an exact one:
        String org = pattern.getOrganisation();
        if (fromNamespace != null) {
            org = NameSpaceHelper.transform(pattern.getModuleId(), fromNamespace.getFromSystemTransformer()).getOrganisation();
        }
        modules.addAll(Arrays.asList(resolver.listModules(new OrganisationEntry(resolver, org))));
    } else {
        Matcher orgMatcher = matcher.getMatcher(pattern.getOrganisation());
        for (OrganisationEntry oe : orgs) {
            String org = oe.getOrganisation();
            String systemOrg = (fromNamespace == null) ? org : NameSpaceHelper.transformOrganisation(org, fromNamespace.getToSystemTransformer());
            if (orgMatcher.matches(systemOrg)) {
                modules.addAll(Arrays.asList(resolver.listModules(new OrganisationEntry(resolver, org))));
            }
        }
    }
    Message.debug("found " + modules.size() + " modules for " + pattern.getOrganisation() + " on " + resolverName);
    boolean foundModule = false;
    for (ModuleEntry mEntry : modules) {
        ModuleId foundMid = new ModuleId(mEntry.getOrganisation(), mEntry.getModule());
        ModuleId systemMid = foundMid;
        if (fromNamespace != null) {
            systemMid = NameSpaceHelper.transform(foundMid, fromNamespace.getToSystemTransformer());
        }
        if (MatcherHelper.matches(matcher, pattern.getModuleId(), systemMid)) {
            // The module corresponds to the searched module pattern
            foundModule = true;
            RevisionEntry[] rEntries = resolver.listRevisions(mEntry);
            Message.debug("found " + rEntries.length + " revisions for [" + mEntry.getOrganisation() + ", " + mEntry.getModule() + "] on " + resolverName);
            boolean foundRevision = false;
            for (RevisionEntry rEntry : rEntries) {
                ModuleRevisionId foundMrid = ModuleRevisionId.newInstance(mEntry.getOrganisation(), mEntry.getModule(), rEntry.getRevision());
                ModuleRevisionId systemMrid = foundMrid;
                if (fromNamespace != null) {
                    systemMrid = fromNamespace.getToSystemTransformer().transform(foundMrid);
                }
                if (MatcherHelper.matches(matcher, pattern, systemMrid)) {
                    // We have a matching module revision
                    foundRevision = true;
                    mrids.add(systemMrid);
                }
            }
            if (!foundRevision) {
                Message.debug("no revision found matching " + pattern + " in [" + mEntry.getOrganisation() + "," + mEntry.getModule() + "] using " + resolverName);
            }
        }
    }
    if (!foundModule) {
        Message.debug("no module found matching " + pattern + " using " + resolverName);
    }
    return mrids;
}
Also used : Matcher(org.apache.ivy.plugins.matcher.Matcher) PatternMatcher(org.apache.ivy.plugins.matcher.PatternMatcher) ArrayList(java.util.ArrayList) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) Namespace(org.apache.ivy.plugins.namespace.Namespace) ModuleId(org.apache.ivy.core.module.id.ModuleId) AbstractResolver(org.apache.ivy.plugins.resolver.AbstractResolver)

Example 2 with AbstractResolver

use of org.apache.ivy.plugins.resolver.AbstractResolver 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

ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)2 AbstractResolver (org.apache.ivy.plugins.resolver.AbstractResolver)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Artifact (org.apache.ivy.core.module.descriptor.Artifact)1 DefaultArtifact (org.apache.ivy.core.module.descriptor.DefaultArtifact)1 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)1 ModuleId (org.apache.ivy.core.module.id.ModuleId)1 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)1 MetadataArtifactDownloadReport (org.apache.ivy.core.report.MetadataArtifactDownloadReport)1 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)1 Matcher (org.apache.ivy.plugins.matcher.Matcher)1 PatternMatcher (org.apache.ivy.plugins.matcher.PatternMatcher)1 Namespace (org.apache.ivy.plugins.namespace.Namespace)1 ModuleDescriptorParser (org.apache.ivy.plugins.parser.ModuleDescriptorParser)1 ParserSettings (org.apache.ivy.plugins.parser.ParserSettings)1 XmlModuleDescriptorParser (org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParser)1 ArtifactResourceResolver (org.apache.ivy.plugins.repository.ArtifactResourceResolver)1