/src/main/java/org/jfrog/bamboo/util/generic/DependenciesDownloaderImpl.java

https://github.com/JFrogDev/bamboo-artifactory-plugin · Java · 137 lines · 110 code · 21 blank · 6 comment · 15 complexity · fed118808663a356df1224c88801eb14 MD5 · raw file

  1. package org.jfrog.bamboo.util.generic;
  2. import com.atlassian.core.util.FileUtils;
  3. import com.google.common.base.Predicate;
  4. import com.google.common.collect.Iterables;
  5. import org.apache.commons.io.FilenameUtils;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.jfrog.build.api.Dependency;
  8. import org.jfrog.build.api.dependency.DownloadableArtifact;
  9. import org.jfrog.build.api.util.FileChecksumCalculator;
  10. import org.jfrog.build.api.util.Log;
  11. import org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryDependenciesClient;
  12. import org.jfrog.build.extractor.clientConfiguration.util.DependenciesDownloader;
  13. import org.jfrog.build.extractor.clientConfiguration.util.DependenciesDownloaderHelper;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.security.NoSuchAlgorithmException;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;
  21. /**
  22. * @author Lior Hasson
  23. */
  24. public class DependenciesDownloaderImpl implements DependenciesDownloader {
  25. private ArtifactoryDependenciesClient client;
  26. private Log log;
  27. private File workingDirectory;
  28. private boolean flatDownload = false;
  29. public DependenciesDownloaderImpl(ArtifactoryDependenciesClient client, File workingDirectory, Log log) {
  30. this.client = client;
  31. this.workingDirectory = workingDirectory;
  32. this.log = log;
  33. }
  34. @Override
  35. public ArtifactoryDependenciesClient getClient() {
  36. return client;
  37. }
  38. @Override
  39. public List<Dependency> download(Set<DownloadableArtifact> downloadableArtifacts) throws IOException {
  40. DependenciesDownloaderHelper helper = new DependenciesDownloaderHelper(this, log);
  41. return helper.downloadDependencies(downloadableArtifacts);
  42. }
  43. @Override
  44. public String getTargetDir(String targetDir, String relativeDir) throws IOException {
  45. return FilenameUtils.concat(workingDirectory.getPath(), FilenameUtils.concat(targetDir, relativeDir));
  46. }
  47. @Override
  48. public Map<String, String> saveDownloadedFile(InputStream is, String filePath) throws IOException {
  49. try {
  50. File newFile = new File(filePath);
  51. FileUtils.copyFile(is, newFile, true);
  52. return FileChecksumCalculator.calculateChecksums(newFile, "md5", "sha1");
  53. } catch (Exception e) {
  54. log.warn("Caught exception while saving dependency file" + e.getLocalizedMessage());
  55. } finally {
  56. if (is != null) {
  57. is.close();
  58. }
  59. }
  60. return null;
  61. }
  62. @Override
  63. public boolean isFileExistsLocally(String filePath, String md5, String sha1) throws IOException {
  64. File localFile = new File(filePath);
  65. if (!localFile.exists()) {
  66. return false;
  67. }
  68. // If it's a folder return true since we don't care about it, not going to download a folder anyway
  69. if (localFile.isDirectory()) {
  70. return true;
  71. }
  72. try {
  73. Map<String, String> checksumsMap = FileChecksumCalculator.calculateChecksums(localFile, "md5", "sha1");
  74. return checksumsMap != null &&
  75. StringUtils.isNotBlank(md5) && StringUtils.equals(md5, checksumsMap.get("md5")) &&
  76. StringUtils.isNotBlank(sha1) && StringUtils.equals(sha1, checksumsMap.get("sha1"));
  77. } catch (NoSuchAlgorithmException e) {
  78. log.warn("Could not find checksum algorithm: " + e.getLocalizedMessage());
  79. }
  80. return false;
  81. }
  82. @Override
  83. public void removeUnusedArtifactsFromLocal(Set<String> allResolvesFiles, Set<String> forDeletionFiles) throws IOException {
  84. try {
  85. for (String resolvedFile : forDeletionFiles) {
  86. File resolvedFileParent = org.apache.commons.io.FileUtils.getFile(resolvedFile).getParentFile();
  87. File[] fileSiblings = resolvedFileParent.listFiles();
  88. if (!(fileSiblings == null || fileSiblings.length == 0)) {
  89. for (File sibling : fileSiblings) {
  90. if (!isResolvedOrParentOfResolvedFile(allResolvesFiles, sibling.getPath())) {
  91. log.info("Deleted unresolved file '" + sibling.getPath() + "'");
  92. sibling.delete();
  93. }
  94. }
  95. }
  96. }
  97. } catch (Exception e) {
  98. log.warn("Caught interrupted exception: " + e.getLocalizedMessage());
  99. }
  100. }
  101. private boolean isResolvedOrParentOfResolvedFile(Set<String> resolvedFiles, final String path) {
  102. return Iterables.any(resolvedFiles, new Predicate<String>() {
  103. public boolean apply(String filePath) {
  104. return StringUtils.equals(filePath, path) || StringUtils.startsWith(filePath, path);
  105. }
  106. });
  107. }
  108. //TODO - as part of buildInfo V-2.6.x we need to do some changes to support the json spec
  109. public boolean getFlatDownload() {
  110. return this.flatDownload;
  111. }
  112. //TODO - as part of buildInfo V-2.6.x we need to do some changes to support the json spec
  113. public void setFlatDownload(boolean flat) {
  114. this.flatDownload = flat;
  115. }
  116. }