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;
private ArtifactoryDependenciesClient client;
private FilePath workspace;
private Log log;
this.client = client;
this.workspace = workspace;
this.log = log;
}
public ArtifactoryDependenciesClient
getClient() {
return client;
}
public List<Dependency>
download(Set<DownloadableArtifact> 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<String, String>
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 Log log;
this.log = log;
}
public Map<String, String>
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;
}
}
}