Search in sources :

Example 51 with ArtifactDownloadReport

use of org.apache.ivy.core.report.ArtifactDownloadReport in project ant-ivy by apache.

the class Main method invoke.

@SuppressWarnings("resource")
private static void invoke(Ivy ivy, File cache, ModuleDescriptor md, String[] confs, List<File> fileList, String mainclass, String[] args) {
    List<URL> urls = new ArrayList<>();
    // Add option cp (extra classpath) urls
    if (fileList != null && fileList.size() > 0) {
        for (File file : fileList) {
            try {
                urls.add(file.toURI().toURL());
            } catch (MalformedURLException e) {
            // Should not happen, just ignore.
            }
        }
    }
    try {
        Collection<ArtifactDownloadReport> all = new LinkedHashSet<>();
        ResolutionCacheManager cacheMgr = ivy.getResolutionCacheManager();
        XmlReportParser parser = new XmlReportParser();
        for (String conf : confs) {
            String resolveId = ResolveOptions.getDefaultResolveId(md);
            File report = cacheMgr.getConfigurationResolveReportInCache(resolveId, conf);
            parser.parse(report);
            all.addAll(Arrays.asList(parser.getArtifactReports()));
        }
        for (ArtifactDownloadReport artifact : all) {
            if (artifact.getLocalFile() != null) {
                urls.add(artifact.getLocalFile().toURI().toURL());
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException("impossible to build ivy cache path: " + ex.getMessage(), ex);
    }
    URLClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), Main.class.getClassLoader().getParent());
    try {
        Class<?> c = classLoader.loadClass(mainclass);
        Method mainMethod = c.getMethod("main", String[].class);
        Thread.currentThread().setContextClassLoader(classLoader);
        mainMethod.invoke(null, new Object[] { (args == null ? new String[0] : args) });
    } catch (ClassNotFoundException cnfe) {
        throw new RuntimeException("Could not find class: " + mainclass, cnfe);
    } catch (SecurityException | NoSuchMethodException e) {
        throw new RuntimeException("Could not find main method: " + mainclass, e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("No permissions to invoke main method: " + mainclass, e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException("Unexpected exception invoking main method: " + mainclass, e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MalformedURLException(java.net.MalformedURLException) ResolutionCacheManager(org.apache.ivy.core.cache.ResolutionCacheManager) ArrayList(java.util.ArrayList) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) Method(java.lang.reflect.Method) URL(java.net.URL) ResolveProcessException(org.apache.ivy.core.resolve.ResolveProcessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParseException(org.apache.ivy.util.cli.ParseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XmlReportParser(org.apache.ivy.plugins.report.XmlReportParser) URLClassLoader(java.net.URLClassLoader) PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File)

Example 52 with ArtifactDownloadReport

use of org.apache.ivy.core.report.ArtifactDownloadReport in project ant-ivy by apache.

the class Main method outputCachePath.

private static void outputCachePath(Ivy ivy, File cache, ModuleDescriptor md, String[] confs, String outFile) {
    try {
        StringBuilder buf = new StringBuilder();
        Collection<ArtifactDownloadReport> all = new LinkedHashSet<>();
        ResolutionCacheManager cacheMgr = ivy.getResolutionCacheManager();
        XmlReportParser parser = new XmlReportParser();
        for (String conf : confs) {
            String resolveId = ResolveOptions.getDefaultResolveId(md);
            File report = cacheMgr.getConfigurationResolveReportInCache(resolveId, conf);
            parser.parse(report);
            all.addAll(Arrays.asList(parser.getArtifactReports()));
        }
        for (ArtifactDownloadReport artifact : all) {
            if (artifact.getLocalFile() != null) {
                buf.append(artifact.getLocalFile().getCanonicalPath());
                buf.append(File.pathSeparator);
            }
        }
        PrintWriter writer = new PrintWriter(new FileOutputStream(outFile));
        if (buf.length() > 0) {
            buf.setLength(buf.length() - File.pathSeparator.length());
            writer.println(buf);
        }
        writer.close();
        System.out.println("cachepath output to " + outFile);
    } catch (Exception ex) {
        throw new RuntimeException("impossible to build ivy cache path: " + ex.getMessage(), ex);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XmlReportParser(org.apache.ivy.plugins.report.XmlReportParser) ResolutionCacheManager(org.apache.ivy.core.cache.ResolutionCacheManager) FileOutputStream(java.io.FileOutputStream) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File) ResolveProcessException(org.apache.ivy.core.resolve.ResolveProcessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParseException(org.apache.ivy.util.cli.ParseException) PrintWriter(java.io.PrintWriter)

Example 53 with ArtifactDownloadReport

use of org.apache.ivy.core.report.ArtifactDownloadReport 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)

Example 54 with ArtifactDownloadReport

use of org.apache.ivy.core.report.ArtifactDownloadReport in project ant-ivy by apache.

the class FileSystemResolverTest method testFixedRevision.

@Test
public void testFixedRevision() throws Exception {
    FileSystemResolver resolver = new FileSystemResolver();
    resolver.setName("test");
    resolver.setSettings(settings);
    assertEquals("test", resolver.getName());
    resolver.addIvyPattern(IVY_PATTERN);
    resolver.addArtifactPattern(settings.getBaseDir() + "/test/repositories/1/[organisation]/[module]/[type]s/[artifact]-[revision].[type]");
    ModuleRevisionId mrid = ModuleRevisionId.newInstance("org1", "mod1.1", "1.0");
    ResolvedModuleRevision rmr = resolver.getDependency(new DefaultDependencyDescriptor(mrid, false), data);
    assertNotNull(rmr);
    assertEquals(mrid, rmr.getId());
    Date pubdate = new GregorianCalendar(2004, 10, 1, 11, 0, 0).getTime();
    assertEquals(pubdate, rmr.getPublicationDate());
    // test to ask to download
    DefaultArtifact artifact = new DefaultArtifact(mrid, pubdate, "mod1.1", "jar", "jar");
    DownloadReport report = resolver.download(new Artifact[] { artifact }, getDownloadOptions());
    assertNotNull(report);
    assertEquals(1, report.getArtifactsReports().length);
    ArtifactDownloadReport ar = report.getArtifactReport(artifact);
    assertNotNull(ar);
    assertEquals(artifact, ar.getArtifact());
    assertEquals(DownloadStatus.SUCCESSFUL, ar.getDownloadStatus());
    // test to ask to download again, should use cache
    report = resolver.download(new Artifact[] { artifact }, getDownloadOptions());
    assertNotNull(report);
    assertEquals(1, report.getArtifactsReports().length);
    ar = report.getArtifactReport(artifact);
    assertNotNull(ar);
    assertEquals(artifact, ar.getArtifact());
    assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
}
Also used : DownloadReport(org.apache.ivy.core.report.DownloadReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) GregorianCalendar(java.util.GregorianCalendar) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) Date(java.util.Date) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Test(org.junit.Test)

Example 55 with ArtifactDownloadReport

use of org.apache.ivy.core.report.ArtifactDownloadReport in project ant-ivy by apache.

the class FileSystemResolverTest method testDownloadWithUseOriginIsTrue.

@Test
public void testDownloadWithUseOriginIsTrue() throws Exception {
    FileSystemResolver resolver = new FileSystemResolver();
    resolver.setName("test");
    resolver.setSettings(settings);
    ((DefaultRepositoryCacheManager) resolver.getRepositoryCacheManager()).setUseOrigin(true);
    assertEquals("test", resolver.getName());
    resolver.addIvyPattern(IVY_PATTERN);
    resolver.addArtifactPattern(settings.getBaseDir() + "/test/repositories/1/" + "[organisation]/[module]/[type]s/[artifact]-[revision].[type]");
    ModuleRevisionId mrid = ModuleRevisionId.newInstance("org1", "mod1.1", "1.0");
    ResolvedModuleRevision rmr = resolver.getDependency(new DefaultDependencyDescriptor(mrid, false), data);
    assertNotNull(rmr);
    assertEquals(mrid, rmr.getId());
    Date pubdate = new GregorianCalendar(2004, 10, 1, 11, 0, 0).getTime();
    assertEquals(pubdate, rmr.getPublicationDate());
    // test to ask to download
    DefaultArtifact artifact = new DefaultArtifact(mrid, pubdate, "mod1.1", "jar", "jar");
    DownloadReport report = resolver.download(new Artifact[] { artifact }, getDownloadOptions());
    assertNotNull(report);
    assertEquals(1, report.getArtifactsReports().length);
    ArtifactDownloadReport ar = report.getArtifactReport(artifact);
    assertNotNull(ar);
    assertEquals(artifact, ar.getArtifact());
    assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
}
Also used : DownloadReport(org.apache.ivy.core.report.DownloadReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) GregorianCalendar(java.util.GregorianCalendar) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) DefaultRepositoryCacheManager(org.apache.ivy.core.cache.DefaultRepositoryCacheManager) Date(java.util.Date) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Test(org.junit.Test)

Aggregations

ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)64 File (java.io.File)33 Artifact (org.apache.ivy.core.module.descriptor.Artifact)29 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)26 Test (org.junit.Test)25 DownloadReport (org.apache.ivy.core.report.DownloadReport)23 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)18 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)18 DefaultArtifact (org.apache.ivy.core.module.descriptor.DefaultArtifact)17 ResolveReport (org.apache.ivy.core.report.ResolveReport)15 ConfigurationResolveReport (org.apache.ivy.core.report.ConfigurationResolveReport)11 IOException (java.io.IOException)10 DownloadOptions (org.apache.ivy.core.resolve.DownloadOptions)10 URL (java.net.URL)8 JarFile (java.util.jar.JarFile)8 Date (java.util.Date)7 MetadataArtifactDownloadReport (org.apache.ivy.core.report.MetadataArtifactDownloadReport)7 URLResource (org.apache.ivy.plugins.repository.url.URLResource)7 ArtifactOrigin (org.apache.ivy.core.cache.ArtifactOrigin)6 MalformedURLException (java.net.MalformedURLException)5