diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-11-06 23:55:19 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-11-06 23:55:19 +0100 |
commit | 07d2a0d30c6f11b42273659378f4ee63fcfc05e5 (patch) | |
tree | 6504aafd0f099c2127e8b87b76fd58b192da6bf5 /sonar-batch | |
parent | 8162e11b955940fbc620b78ff1551eccfc7005b2 (diff) | |
download | sonarqube-07d2a0d30c6f11b42273659378f4ee63fcfc05e5.tar.gz sonarqube-07d2a0d30c6f11b42273659378f4ee63fcfc05e5.zip |
SONAR-3400 Move all batch-side temporary files to the Sonar working directory
Diffstat (limited to 'sonar-batch')
5 files changed, 52 insertions, 85 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java index b281dd0db36..4651bcb0db8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java @@ -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"); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginDownloader.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginDownloader.java index e462afbc10b..bf3020f8bad 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginDownloader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/PluginDownloader.java @@ -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); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempDirectories.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempDirectories.java index d397e443c52..42ecaf0224f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempDirectories.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempDirectories.java @@ -19,86 +19,52 @@ */ 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); - } - } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java index 86133b672ab..55b4b936690 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java @@ -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"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempDirectoriesTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempDirectoriesTest.java index 7eb4d8b304f..57a0fd417d2 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempDirectoriesTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempDirectoriesTest.java @@ -19,9 +19,12 @@ */ 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"); |