diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-12-21 12:19:33 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-12-21 12:19:33 +0000 |
commit | eb28861955f2823b4b2e92c4a59683d16d0b8dbb (patch) | |
tree | 8118f2fcbad24d9209b389dd9e718b0fb6f8433c /sonar-batch | |
parent | 8569b5c29e3577cd2137e9680ea79f4e5ee6db7c (diff) | |
download | sonarqube-eb28861955f2823b4b2e92c4a59683d16d0b8dbb.tar.gz sonarqube-eb28861955f2823b4b2e92c4a59683d16d0b8dbb.zip |
fix deletion of batch temp directories on MS Windows
Diffstat (limited to 'sonar-batch')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempDirectories.java | 28 |
1 files changed, 24 insertions, 4 deletions
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 9e820ed40b2..80ab6940d41 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 @@ -21,22 +21,33 @@ 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 java.io.File; +import java.io.FileFilter; import java.io.IOException; +import java.util.Arrays; import java.util.Map; public final 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("sonar-batch"); + this.rootDir = TempFileUtils.createTempDirectory(DIR_PREFIX); LoggerFactory.getLogger(getClass()).debug("Temporary directory: " + rootDir.getAbsolutePath()); } @@ -76,9 +87,18 @@ public final class TempDirectories { */ public void stop() { directoriesByKey.clear(); - LoggerFactory.getLogger(getClass()).debug("Delete temporary directory: " + rootDir.getAbsolutePath()); - // it probably does not work on MS Windows and Sun JVM. URLClassLoader locks JARs and resources. - FileUtils.deleteQuietly(rootDir); + LoggerFactory.getLogger(getClass()).debug("Delete temporary directories"); + + // 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. + rootDir.setLastModified(System.currentTimeMillis() - AGE_BEFORE_DELETION - 60 * 60 * 1000); // older than AGE_BEFORE_DELETION to be sure that the current dir is deleted on mac and linux. + + 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); + } } } |