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/src/main | |
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/src/main')
3 files changed, 26 insertions, 60 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); - } - } } |