You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SonarCache.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Sonar Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner;
  21. import org.apache.commons.codec.digest.DigestUtils;
  22. import org.apache.commons.io.FileUtils;
  23. import org.apache.commons.io.IOUtils;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.IOException;
  27. /**
  28. * This class is responsible for managing Sonar batch file cache. You can put file into cache and
  29. * later try to retrieve them. MD5 is used to differentiate files (name is not secure as files may come
  30. * from different Sonar servers and have same name but be actually different, and same for SNAPSHOTs).
  31. * Default location of cache is
  32. * @author Julien HENRY
  33. *
  34. */
  35. public class SonarCache {
  36. private static final int TEMP_FILE_ATTEMPTS = 10000;
  37. private File cacheLocation;
  38. /**
  39. * Temporary directory where files should be stored before be inserted in the cache.
  40. * Having a temporary close to the final location (read on same FS) will assure
  41. * the move will be atomic.
  42. */
  43. private File tmpDir;
  44. private SonarCache(File cacheLocation) {
  45. this.cacheLocation = cacheLocation;
  46. tmpDir = new File(cacheLocation, "tmp");
  47. if (!cacheLocation.exists()) {
  48. Logs.debug("Creating cache directory: " + cacheLocation.getAbsolutePath());
  49. try {
  50. FileUtils.forceMkdir(cacheLocation);
  51. } catch (IOException e) {
  52. throw new RuntimeException("Unable to create cache directory " + cacheLocation.getAbsolutePath(), e);
  53. }
  54. }
  55. }
  56. public static class Builder {
  57. private File sonarUserHomeLocation;
  58. private File cacheLocation;
  59. public Builder(File sonarUserHomeLocation) {
  60. this.sonarUserHomeLocation = sonarUserHomeLocation;
  61. }
  62. public Builder setCacheLocation(File cacheLocation) {
  63. this.cacheLocation = cacheLocation;
  64. return this;
  65. }
  66. public SonarCache build() {
  67. if (cacheLocation == null) {
  68. return new SonarCache(new File(sonarUserHomeLocation, "cache"));
  69. }
  70. else {
  71. return new SonarCache(cacheLocation);
  72. }
  73. }
  74. }
  75. public static Builder create(File sonarUserHomeLocation) {
  76. if (sonarUserHomeLocation == null) {
  77. throw new RunnerException("Sonar user home directory should not be null");
  78. }
  79. return new Builder(sonarUserHomeLocation);
  80. }
  81. /**
  82. * Move the given file inside the cache. Return the MD5 of the cached file.
  83. * @param sourceFile
  84. * @throws IOException
  85. */
  86. public String cacheFile(File sourceFile, String filename) throws IOException {
  87. Logs.debug("Trying to cache file " + sourceFile.getAbsolutePath() + " with filename " + filename);
  88. File tmpFileName = null;
  89. try {
  90. if (!sourceFile.getParentFile().equals(getTmpDir())) {
  91. // Provided file is not close to the cache so we will move it first in a temporary file (could be non atomic)
  92. tmpFileName = getTemporaryFile();
  93. FileUtils.moveFile(sourceFile, tmpFileName);
  94. }
  95. else {
  96. tmpFileName = sourceFile;
  97. }
  98. // Now compute the md5 to find the final destination
  99. String md5;
  100. FileInputStream fis = null;
  101. try {
  102. fis = new FileInputStream(tmpFileName);
  103. md5 = DigestUtils.md5Hex(fis);
  104. } finally {
  105. IOUtils.closeQuietly(fis);
  106. }
  107. File finalDir = new File(cacheLocation, md5);
  108. File finalFileName = new File(finalDir, filename);
  109. // Try to create final destination folder
  110. FileUtils.forceMkdir(finalDir);
  111. // Now try to move the file from temporary folder to final location
  112. boolean rename = tmpFileName.renameTo(finalFileName);
  113. if (!rename) {
  114. // Check if the file was already in cache
  115. if (!finalFileName.exists()) {
  116. Logs.warn("Unable to rename " + tmpFileName.getAbsolutePath() + " to " + finalFileName.getAbsolutePath());
  117. Logs.warn("A copy/delete will be tempted but with no garantee of atomicity");
  118. FileUtils.moveFile(tmpFileName, finalFileName);
  119. }
  120. }
  121. Logs.debug("File cached at " + finalFileName.getAbsolutePath());
  122. return md5;
  123. } finally {
  124. FileUtils.deleteQuietly(tmpFileName);
  125. }
  126. }
  127. /**
  128. * Look for a file in the cache by its filename and md5 checksum. If the file is not
  129. * present then return null.
  130. */
  131. public File getFileFromCache(String filename, String md5) {
  132. File location = new File(new File(cacheLocation, md5), filename);
  133. Logs.debug("Looking for " + location.getAbsolutePath());
  134. if (location.exists()) {
  135. return location;
  136. }
  137. Logs.debug("No file found in the cache with name " + filename + " and checksum " + md5);
  138. return null;
  139. }
  140. /**
  141. * Return a temporary file that caller can use to store file content before
  142. * asking for caching it with {@link #cacheFile(File)}.
  143. * This is to avoid extra copy.
  144. * @return
  145. * @throws IOException
  146. */
  147. public File getTemporaryFile() throws IOException {
  148. return createTempFile(getTmpDir());
  149. }
  150. /**
  151. * Create a temporary file in the given directory.
  152. * @param baseDir
  153. * @return
  154. * @throws IOException
  155. */
  156. private static File createTempFile(File baseDir) throws IOException {
  157. String baseName = System.currentTimeMillis() + "-";
  158. for (int counter = 0; counter < TEMP_FILE_ATTEMPTS; counter++) {
  159. File tempFile = new File(baseDir, baseName + counter);
  160. if (tempFile.createNewFile()) {
  161. return tempFile;
  162. }
  163. }
  164. throw new IOException("Failed to create temporary file in " + baseDir.getAbsolutePath() + " within "
  165. + TEMP_FILE_ATTEMPTS + " attempts (tried "
  166. + baseName + "0 to " + baseName + (TEMP_FILE_ATTEMPTS - 1) + ')');
  167. }
  168. public File getTmpDir() {
  169. if (!tmpDir.exists()) {
  170. Logs.debug("Creating temporary cache directory: " + tmpDir.getAbsolutePath());
  171. try {
  172. FileUtils.forceMkdir(tmpDir);
  173. } catch (IOException e) {
  174. throw new RuntimeException("Unable to create temporary cache directory " + tmpDir.getAbsolutePath(), e);
  175. }
  176. }
  177. return tmpDir;
  178. }
  179. public File getCacheLocation() {
  180. return cacheLocation;
  181. }
  182. }