]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3400 Move all batch-side temporary files to the Sonar working directory
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 6 Nov 2012 22:55:19 +0000 (23:55 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 6 Nov 2012 22:55:19 +0000 (23:55 +0100)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginDownloader.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempDirectories.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempDirectoriesTest.java

index b281dd0db36ac43e28360ba16b7adfbb55b9e151..4651bcb0db84baadb5a53f9ef5da056be40bf98f 100644 (file)
@@ -62,7 +62,7 @@ public class DryRunDatabase implements BatchComponent {
   public void start() {
     if (settings.getBoolean("sonar.dryRun")) {
       LOG.info("Dry run");
-      File databaseFile = tempDirectories.getFile("dry_run", "db.h2.db");
+      File databaseFile = tempDirectories.getFile("", "dryrun.h2.db");
       downloadDatabase(reactor.getRoot().getKey(), databaseFile);
 
       String databasePath = StringUtils.removeEnd(databaseFile.getAbsolutePath(), ".h2.db");
index e462afbc10bb1aec3ce716ba8d084e66a518a656..bf3020f8bad826355a439249cef62d3e81aeccf8 100644 (file)
@@ -36,17 +36,17 @@ public class PluginDownloader implements BatchComponent {
 
   private static final Logger LOG = LoggerFactory.getLogger(PluginDownloader.class);
 
-  private TempDirectories workingDirectories;
+  private TempDirectories tempDirs;
   private ServerClient server;
 
-  public PluginDownloader(TempDirectories workingDirectories, ServerClient server) {
-    this.workingDirectories = workingDirectories;
+  public PluginDownloader(TempDirectories tempDirs, ServerClient server) {
+    this.tempDirs = tempDirs;
     this.server = server;
   }
 
   public List<File> downloadPlugin(RemotePlugin remote) {
     try {
-      File targetDir = workingDirectories.getDir("plugins/" + remote.getKey());
+      File targetDir = tempDirs.getDir("plugins/" + remote.getKey());
       FileUtils.forceMkdir(targetDir);
       LOG.debug("Downloading plugin " + remote.getKey() + " into " + targetDir);
 
index d397e443c52afb568248b4f0f053076cb3a2ca9e..42ecaf0224f4ae6653a24cb23d65bd25bc6c9755 100644 (file)
  */
 package org.sonar.batch.bootstrap;
 
-import com.google.common.collect.Maps;
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.filefilter.AgeFileFilter;
-import org.apache.commons.io.filefilter.AndFileFilter;
-import org.apache.commons.io.filefilter.DirectoryFileFilter;
-import org.apache.commons.io.filefilter.PrefixFileFilter;
 import org.apache.commons.lang.StringUtils;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.utils.SonarException;
-import org.sonar.api.utils.TempFileUtils;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
 
 import java.io.File;
-import java.io.FileFilter;
 import java.io.IOException;
-import java.util.Arrays;
-import java.util.Map;
 
 public class TempDirectories {
 
-  public static final String DIR_PREFIX = "sonar-batch";
-
-  // this timeout must be greater than the longest analysis
-  public static final int AGE_BEFORE_DELETION = 24 * 60 * 60 * 1000;
-
   private File rootDir;
-  private Map<String, File> directoriesByKey = Maps.newHashMap();
 
-  public TempDirectories() throws IOException {
-    this.rootDir = TempFileUtils.createTempDirectory(DIR_PREFIX);
-    LoggerFactory.getLogger(getClass()).debug("Temporary directory: " + rootDir.getAbsolutePath());
+  public TempDirectories(ProjectReactor reactor) throws IOException {
+    this.rootDir = new File(reactor.getRoot().getWorkDir(), "_tmp");
+    if (rootDir.exists()) {
+      FileUtils.deleteDirectory(rootDir);
+    }
+    FileUtils.forceMkdir(rootDir);
   }
 
   public File getRoot() {
     return rootDir;
   }
 
+  public void stop() {
+    FileUtils.deleteQuietly(rootDir);
+  }
+
   /**
    * Get or create a working directory
    */
-  public File getDir(String key) {
-    if (StringUtils.isBlank(key)) {
+  public File getDir(String dirname) {
+    if (StringUtils.isBlank(dirname)) {
       return rootDir;
     }
 
-    File dir = directoriesByKey.get(key);
-    if (dir == null) {
-      dir = new File(rootDir, key);
-      try {
-        FileUtils.forceMkdir(dir);
-        directoriesByKey.put(key, dir);
-
-      } catch (IOException e) {
-        throw new SonarException("Can not create the temp directory: " + dir, e);
-      }
+    File dir = new File(rootDir, dirname);
+    try {
+      FileUtils.forceMkdir(dir);
+      return dir;
+    } catch (IOException e) {
+      throw new IllegalStateException("Fail to create temp directory: " + dir, e);
     }
-    return dir;
   }
 
-  public File getFile(String directoryKey, String filename) {
-    File dir = getDir(directoryKey);
+  public File getFile(String dirname, String filename) {
+    File dir = getDir(dirname);
     return new File(dir, filename);
   }
-
-  /**
-   * This method is executed by picocontainer during shutdown.
-   */
-  public void stop() {
-    directoriesByKey.clear();
-
-    // Deleting temp directory does not work on MS Windows and Sun JVM because URLClassLoader locks JARs and resources.
-    // The workaround is that sonar deletes orphans itself.
-
-    // older than AGE_BEFORE_DELETION to be sure that the current dir is deleted on mac and linux.
-    rootDir.setLastModified(System.currentTimeMillis() - AGE_BEFORE_DELETION - 60 * 60 * 1000);
-
-    File[] directoriesToDelete = rootDir.getParentFile().listFiles((FileFilter) new AndFileFilter(Arrays.asList(
-        DirectoryFileFilter.DIRECTORY, new PrefixFileFilter(DIR_PREFIX), new AgeFileFilter(System.currentTimeMillis() - AGE_BEFORE_DELETION))));
-    for (File dir : directoriesToDelete) {
-      LoggerFactory.getLogger(getClass()).debug("Delete temporary directory: " + dir);
-      FileUtils.deleteQuietly(dir);
-    }
-  }
 }
index 86133b672abe78f9b67df3f22a31a8e87b96fa83..55b4b9366904bbd9293b292093986f67a67779a7 100644 (file)
@@ -47,12 +47,15 @@ public class DryRunDatabaseTest {
   ServerClient server = mock(ServerClient.class);
   TempDirectories tempDirectories = mock(TempDirectories.class);
   ProjectReactor projectReactor = new ProjectReactor(ProjectDefinition.create().setKey("group:project"));
+  File databaseFile;
 
   @Rule
   public ExpectedException thrown = ExpectedException.none();
 
   @Before
   public void setUp() {
+    databaseFile = new File("/tmp/dryrun.h2.db");
+    when(tempDirectories.getFile("", "dryrun.h2.db")).thenReturn(databaseFile);
     settings.setProperty("sonar.dryRun", true);
     dryRunDatabase = new DryRunDatabase(settings, server, tempDirectories, projectReactor, mock(ProjectReactorReady.class));
   }
@@ -67,9 +70,6 @@ public class DryRunDatabaseTest {
 
   @Test
   public void should_download_database() {
-    File databaseFile = new File("/tmp/dry_run/db.h2.db");
-    when(tempDirectories.getFile("dry_run", "db.h2.db")).thenReturn(databaseFile);
-
     dryRunDatabase.start();
 
     verify(server).download("/batch_bootstrap/db?project=group:project", databaseFile);
@@ -77,21 +77,18 @@ public class DryRunDatabaseTest {
 
   @Test
   public void should_replace_database_settings() {
-    when(tempDirectories.getFile("dry_run", "db.h2.db")).thenReturn(new File("/tmp/dry_run/db.h2.db"));
-
     dryRunDatabase.start();
 
     assertThat(settings.getString(DatabaseProperties.PROP_DIALECT)).isEqualTo("h2");
     assertThat(settings.getString(DatabaseProperties.PROP_DRIVER)).isEqualTo("org.h2.Driver");
     assertThat(settings.getString(DatabaseProperties.PROP_USER)).isEqualTo("sonar");
     assertThat(settings.getString(DatabaseProperties.PROP_PASSWORD)).isEqualTo("sonar");
-    assertThat(settings.getString(DatabaseProperties.PROP_URL)).isEqualTo("jdbc:h2:/tmp/dry_run/db");
+    assertThat(settings.getString(DatabaseProperties.PROP_URL)).isEqualTo("jdbc:h2:/tmp/dryrun");
   }
 
   @Test
   public void should_fail_on_unknown_project() {
-    when(tempDirectories.getFile("dry_run", "db.h2.db")).thenReturn(new File("/tmp/dry_run/db.h2.db"));
-    doThrow(new SonarException(new FileNotFoundException())).when(server).download("/batch_bootstrap/db?project=group:project", new File("/tmp/dry_run/db.h2.db"));
+    doThrow(new SonarException(new FileNotFoundException())).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile);
 
     thrown.expect(SonarException.class);
     thrown.expectMessage("Project [group:project] doesn't exist on server");
@@ -101,8 +98,7 @@ public class DryRunDatabaseTest {
 
   @Test
   public void should_fail_on_invalid_role() {
-    when(tempDirectories.getFile("dry_run", "db.h2.db")).thenReturn(new File("/tmp/dry_run/db.h2.db"));
-    doThrow(new SonarException(new IOException("HTTP 401"))).when(server).download("/batch_bootstrap/db?project=group:project", new File("/tmp/dry_run/db.h2.db"));
+    doThrow(new SonarException(new IOException("HTTP 401"))).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile);
 
     thrown.expect(SonarException.class);
     thrown.expectMessage("You don't have access rights to project [group:project]");
@@ -112,8 +108,7 @@ public class DryRunDatabaseTest {
 
   @Test
   public void should_fail() {
-    when(tempDirectories.getFile("dry_run", "db.h2.db")).thenReturn(new File("/tmp/dry_run/db.h2.db"));
-    doThrow(new SonarException("BUG")).when(server).download("/batch_bootstrap/db?project=group:project", new File("/tmp/dry_run/db.h2.db"));
+    doThrow(new SonarException("BUG")).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile);
 
     thrown.expect(SonarException.class);
     thrown.expectMessage("BUG");
index 7eb4d8b304fb4f1ecb801b7fc53294edf9a0660b..57a0fd417d290b6c0a663c3664d99283fe832a29 100644 (file)
  */
 package org.sonar.batch.bootstrap;
 
-import org.junit.After;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
 
 import java.io.File;
 import java.io.IOException;
@@ -32,28 +35,31 @@ public class TempDirectoriesTest {
 
   private TempDirectories tempDirectories;
 
+  @Rule
+  public TemporaryFolder folder = new TemporaryFolder();
+
   @Before
   public void before() throws IOException {
-    tempDirectories = new TempDirectories();
-  }
-
-  @After
-  public void after() {
-    if (tempDirectories != null) {
-      tempDirectories.stop();
-    }
+    ProjectDefinition project = ProjectDefinition.create().setKey("foo").setWorkDir(folder.newFolder());
+    ProjectReactor reactor = new ProjectReactor(project);
+    tempDirectories = new TempDirectories(reactor);
   }
 
   @Test
-  public void shouldCreateRoot() {
+  public void should_create_root_temp_dir() {
     assertThat(tempDirectories.getRoot()).isNotNull();
     assertThat(tempDirectories.getRoot()).exists();
     assertThat(tempDirectories.getRoot()).isDirectory();
+    assertThat(tempDirectories.getRoot().getName()).isEqualTo("_tmp");
+  }
+
+  @Test
+  public void should_accept_empty_dirname() {
     assertThat(tempDirectories.getDir("")).isEqualTo(tempDirectories.getRoot());
   }
 
   @Test
-  public void shouldCreateDirectory() {
+  public void should_create_sub_directory() {
     File findbugsDir = tempDirectories.getDir("findbugs");
     assertThat(findbugsDir).isNotNull();
     assertThat(findbugsDir).exists();
@@ -62,7 +68,7 @@ public class TempDirectoriesTest {
   }
 
   @Test
-  public void shouldStopAndDeleteDirectory() {
+  public void should_delete_temp_dir_on_shutdown() {
     File root = tempDirectories.getRoot();
     File findbugsDir = tempDirectories.getDir("findbugs");
     assertThat(findbugsDir).exists();
@@ -74,7 +80,7 @@ public class TempDirectoriesTest {
   }
 
   @Test
-  public void shouldCreateDirectoryWhenGettingFile() {
+  public void should_create_parent_directory() {
     File file = tempDirectories.getFile("findbugs", "bcel.jar");
     assertThat(file).isNotNull();
     assertThat(file.getParentFile().getName()).isEqualTo("findbugs");