All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jfrog.hudson.generic.DependenciesDownloaderImpl Maven / Gradle / Ivy

The newest version!
package org.jfrog.hudson.generic;

import hudson.FilePath;
import hudson.remoting.VirtualChannel;
import org.apache.commons.io.IOUtils;
import org.jfrog.build.api.Dependency;
import org.jfrog.build.api.dependency.DownloadableArtifact;
import org.jfrog.build.api.util.FileChecksumCalculator;
import org.jfrog.build.api.util.Log;
import org.jfrog.build.client.ArtifactoryDependenciesClient;
import org.jfrog.build.util.DependenciesDownloader;
import org.jfrog.build.util.DependenciesDownloaderHelper;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Primary implementation of dependencies downloader,
 * handles Jenkins slaves and re-use a client for HTTP communication.
 *
 * @author Shay Yaakov
 */
public class DependenciesDownloaderImpl implements DependenciesDownloader {

    private ArtifactoryDependenciesClient client;
    private FilePath workspace;
    private Log log;

    public DependenciesDownloaderImpl(ArtifactoryDependenciesClient client, FilePath workspace, Log log) {
        this.client = client;
        this.workspace = workspace;
        this.log = log;
    }

    public ArtifactoryDependenciesClient getClient() {
        return client;
    }

    public List download(Set downloadableArtifacts) throws IOException {
        DependenciesDownloaderHelper helper = new DependenciesDownloaderHelper(this, log);
        return helper.downloadDependencies(downloadableArtifacts);
    }

    public String getTargetDir(String targetDir, String relativeDir) {
        FilePath targetDirFile = new FilePath(workspace, targetDir).child(relativeDir);
        return targetDirFile.getRemote();
    }

    public Map saveDownloadedFile(InputStream is, String filePath) throws IOException {
        try {
            FilePath child = workspace.child(filePath);
            child.copyFrom(is);
            return child.act(new DownloadFileCallable(log));
        } catch (InterruptedException e) {
            log.warn("Caught interrupted exception: " + e.getLocalizedMessage());
        } finally {
            IOUtils.closeQuietly(is);
        }

        return null;
    }

    private static class DownloadFileCallable implements FilePath.FileCallable> {
        private Log log;

        public DownloadFileCallable(Log log) {
            this.log = log;
        }

        public Map invoke(File f, VirtualChannel channel) throws IOException {
            try {
                return FileChecksumCalculator.calculateChecksums(f, "md5", "sha1");
            } catch (NoSuchAlgorithmException e) {
                log.warn("Could not find checksum algorithm: " + e.getLocalizedMessage());
            }

            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy