Search in sources :

Example 1 with EndRetrieveArtifactEvent

use of org.apache.ivy.core.event.retrieve.EndRetrieveArtifactEvent in project ant-ivy by apache.

the class RetrieveEngine method retrieve.

public RetrieveReport retrieve(ModuleRevisionId mrid, RetrieveOptions options) throws IOException {
    RetrieveReport report = new RetrieveReport();
    ModuleId moduleId = mrid.getModuleId();
    if (LogOptions.LOG_DEFAULT.equals(options.getLog())) {
        Message.info(":: retrieving :: " + moduleId + (options.isSync() ? " [sync]" : ""));
    } else {
        Message.verbose(":: retrieving :: " + moduleId + (options.isSync() ? " [sync]" : ""));
    }
    Message.verbose("\tcheckUpToDate=" + settings.isCheckUpToDate());
    long start = System.currentTimeMillis();
    String destFilePattern = IvyPatternHelper.substituteVariables(options.getDestArtifactPattern(), settings.getVariables());
    String destIvyPattern = IvyPatternHelper.substituteVariables(options.getDestIvyPattern(), settings.getVariables());
    String[] confs = getConfs(mrid, options);
    if (LogOptions.LOG_DEFAULT.equals(options.getLog())) {
        Message.info("\tconfs: " + Arrays.asList(confs));
    } else {
        Message.verbose("\tconfs: " + Arrays.asList(confs));
    }
    if (this.eventManager != null) {
        this.eventManager.fireIvyEvent(new StartRetrieveEvent(mrid, confs, options));
    }
    try {
        Map<ArtifactDownloadReport, Set<String>> artifactsToCopy = determineArtifactsToCopy(mrid, destFilePattern, options);
        File fileRetrieveRoot = settings.resolveFile(IvyPatternHelper.getTokenRoot(destFilePattern));
        report.setRetrieveRoot(fileRetrieveRoot);
        File ivyRetrieveRoot = destIvyPattern == null ? null : settings.resolveFile(IvyPatternHelper.getTokenRoot(destIvyPattern));
        Collection<File> targetArtifactsStructure = new HashSet<>();
        // Set(File) set of all paths which should be present at then end of retrieve (useful
        // for sync)
        // same for ivy files
        Collection<File> targetIvysStructure = new HashSet<>();
        // do retrieve
        long totalCopiedSize = 0;
        for (Map.Entry<ArtifactDownloadReport, Set<String>> artifactAndPaths : artifactsToCopy.entrySet()) {
            ArtifactDownloadReport artifact = artifactAndPaths.getKey();
            File archive = artifact.getLocalFile();
            if (artifact.getUnpackedLocalFile() != null) {
                archive = artifact.getUnpackedLocalFile();
            }
            if (archive == null) {
                Message.verbose("\tno local file available for " + artifact + ": skipping");
                continue;
            }
            Message.verbose("\tretrieving " + archive);
            for (String path : artifactAndPaths.getValue()) {
                IvyContext.getContext().checkInterrupted();
                File destFile = settings.resolveFile(path);
                if (!settings.isCheckUpToDate() || !upToDate(archive, destFile, options)) {
                    Message.verbose("\t\tto " + destFile);
                    if (this.eventManager != null) {
                        this.eventManager.fireIvyEvent(new StartRetrieveArtifactEvent(artifact, destFile));
                    }
                    if (options.isMakeSymlinks()) {
                        boolean symlinkCreated;
                        try {
                            symlinkCreated = FileUtil.symlink(archive, destFile, true);
                        } catch (IOException ioe) {
                            symlinkCreated = false;
                            // warn about the inability to create a symlink
                            Message.warn("symlink creation failed at path " + destFile, ioe);
                        }
                        if (!symlinkCreated) {
                            // since symlink creation failed, let's attempt to an actual copy instead
                            Message.info("Attempting a copy operation (since symlink creation failed) at path " + destFile);
                            FileUtil.copy(archive, destFile, null, true);
                        }
                    } else {
                        FileUtil.copy(archive, destFile, null, true);
                    }
                    if (this.eventManager != null) {
                        this.eventManager.fireIvyEvent(new EndRetrieveArtifactEvent(artifact, destFile));
                    }
                    totalCopiedSize += FileUtil.getFileLength(destFile);
                    report.addCopiedFile(destFile, artifact);
                } else {
                    Message.verbose("\t\tto " + destFile + " [NOT REQUIRED]");
                    report.addUpToDateFile(destFile, artifact);
                }
                if ("ivy".equals(artifact.getType())) {
                    targetIvysStructure.addAll(FileUtil.getPathFiles(ivyRetrieveRoot, destFile));
                } else {
                    Collection<File> files = FileUtil.listAll(destFile, Collections.<String>emptyList());
                    for (File file : files) {
                        targetArtifactsStructure.addAll(FileUtil.getPathFiles(fileRetrieveRoot, file));
                    }
                }
            }
        }
        if (options.isSync()) {
            Message.verbose("\tsyncing...");
            String[] ignorableFilenames = settings.getIgnorableFilenames();
            Collection<String> ignoreList = Arrays.asList(ignorableFilenames);
            Collection<File> existingArtifacts = FileUtil.listAll(fileRetrieveRoot, ignoreList);
            Collection<File> existingIvys = (ivyRetrieveRoot == null) ? null : FileUtil.listAll(ivyRetrieveRoot, ignoreList);
            if (fileRetrieveRoot.equals(ivyRetrieveRoot)) {
                targetArtifactsStructure.addAll(targetIvysStructure);
                existingArtifacts.addAll(existingIvys);
                sync(targetArtifactsStructure, existingArtifacts);
            } else {
                sync(targetArtifactsStructure, existingArtifacts);
                if (existingIvys != null) {
                    sync(targetIvysStructure, existingIvys);
                }
            }
        }
        long elapsedTime = System.currentTimeMillis() - start;
        String msg = "\t" + report.getNbrArtifactsCopied() + " artifacts copied" + (settings.isCheckUpToDate() ? (", " + report.getNbrArtifactsUpToDate() + " already retrieved") : "") + " (" + (totalCopiedSize / KILO) + "kB/" + elapsedTime + "ms)";
        if (LogOptions.LOG_DEFAULT.equals(options.getLog())) {
            Message.info(msg);
        } else {
            Message.verbose(msg);
        }
        Message.verbose("\tretrieve done (" + (elapsedTime) + "ms)");
        if (this.eventManager != null) {
            this.eventManager.fireIvyEvent(new EndRetrieveEvent(mrid, confs, elapsedTime, report.getNbrArtifactsCopied(), report.getNbrArtifactsUpToDate(), totalCopiedSize, options));
        }
        return report;
    } catch (Exception ex) {
        throw new RuntimeException("problem during retrieve of " + moduleId + ": " + ex, ex);
    }
}
Also used : EndRetrieveEvent(org.apache.ivy.core.event.retrieve.EndRetrieveEvent) HashSet(java.util.HashSet) Set(java.util.Set) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) IOException(java.io.IOException) ParseException(java.text.ParseException) IOException(java.io.IOException) ModuleId(org.apache.ivy.core.module.id.ModuleId) StartRetrieveArtifactEvent(org.apache.ivy.core.event.retrieve.StartRetrieveArtifactEvent) StartRetrieveEvent(org.apache.ivy.core.event.retrieve.StartRetrieveEvent) EndRetrieveArtifactEvent(org.apache.ivy.core.event.retrieve.EndRetrieveArtifactEvent) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with EndRetrieveArtifactEvent

use of org.apache.ivy.core.event.retrieve.EndRetrieveArtifactEvent in project ant-ivy by apache.

the class RetrieveTest method testEvent.

@Test
public void testEvent() throws Exception {
    ResolveReport report = ivy.resolve(new File("test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml").toURI().toURL(), getResolveOptions(new String[] { "*" }));
    final List<IvyEvent> events = new ArrayList<>();
    ivy.getEventManager().addIvyListener(new IvyListener() {

        public void progress(IvyEvent event) {
            events.add(event);
        }
    });
    ModuleDescriptor md = report.getModuleDescriptor();
    String pattern = "build/test/retrieve/[module]/[conf]/[artifact]-[revision].[ext]";
    ivy.retrieve(md.getModuleRevisionId(), getRetrieveOptions().setDestArtifactPattern(pattern));
    assertEquals(4, events.size());
    assertTrue(events.get(0) instanceof StartRetrieveEvent);
    assertTrue(events.get(1) instanceof StartRetrieveArtifactEvent);
    assertTrue(events.get(2) instanceof EndRetrieveArtifactEvent);
    assertTrue(events.get(3) instanceof EndRetrieveEvent);
    EndRetrieveEvent ev = (EndRetrieveEvent) events.get(3);
    assertEquals(1, ev.getNbCopied());
    assertEquals(0, ev.getNbUpToDate());
    events.clear();
    ivy.retrieve(md.getModuleRevisionId(), getRetrieveOptions().setDestArtifactPattern(pattern));
    assertEquals(2, events.size());
    assertTrue(events.get(0) instanceof StartRetrieveEvent);
    assertTrue(events.get(1) instanceof EndRetrieveEvent);
    ev = (EndRetrieveEvent) events.get(1);
    assertEquals(0, ev.getNbCopied());
    assertEquals(1, ev.getNbUpToDate());
}
Also used : IvyListener(org.apache.ivy.core.event.IvyListener) EndRetrieveEvent(org.apache.ivy.core.event.retrieve.EndRetrieveEvent) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ResolveReport(org.apache.ivy.core.report.ResolveReport) ArrayList(java.util.ArrayList) StartRetrieveArtifactEvent(org.apache.ivy.core.event.retrieve.StartRetrieveArtifactEvent) StartRetrieveEvent(org.apache.ivy.core.event.retrieve.StartRetrieveEvent) EndRetrieveArtifactEvent(org.apache.ivy.core.event.retrieve.EndRetrieveArtifactEvent) File(java.io.File) IvyEvent(org.apache.ivy.core.event.IvyEvent) Test(org.junit.Test)

Aggregations

File (java.io.File)2 EndRetrieveArtifactEvent (org.apache.ivy.core.event.retrieve.EndRetrieveArtifactEvent)2 EndRetrieveEvent (org.apache.ivy.core.event.retrieve.EndRetrieveEvent)2 StartRetrieveArtifactEvent (org.apache.ivy.core.event.retrieve.StartRetrieveArtifactEvent)2 StartRetrieveEvent (org.apache.ivy.core.event.retrieve.StartRetrieveEvent)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 IvyEvent (org.apache.ivy.core.event.IvyEvent)1 IvyListener (org.apache.ivy.core.event.IvyListener)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 ResolveReport (org.apache.ivy.core.report.ResolveReport)1 Test (org.junit.Test)1