diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-02-11 10:14:31 +0100 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-02-12 13:48:36 +0100 |
commit | 0008f4ed732f47528bddc1392f2afd74a978cd25 (patch) | |
tree | 4480f8218665986da0bc85f143f0196bbc7851c2 /sonar-application/src/main/java | |
parent | 54cdd41e77bf99b81134435499219eaef4862525 (diff) | |
download | sonarqube-0008f4ed732f47528bddc1392f2afd74a978cd25.tar.gz sonarqube-0008f4ed732f47528bddc1392f2afd74a978cd25.zip |
SONAR-7125 use nio to clean temp directory at startup
we used to use Commons IO which loads all files in memory, causing OOM in the App process because it is configured with only 16Mb of heap
Diffstat (limited to 'sonar-application/src/main/java')
-rw-r--r-- | sonar-application/src/main/java/org/sonar/application/AppFileSystem.java | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java b/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java index b71ba43faae..0f513928c98 100644 --- a/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java +++ b/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java @@ -21,12 +21,18 @@ package org.sonar.application; import java.io.File; import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.process.Props; import org.sonar.process.monitor.FileSystem; -import static org.apache.commons.io.FileUtils.cleanDirectory; +import static java.nio.file.Files.delete; +import static java.nio.file.Files.walkFileTree; import static org.apache.commons.io.FileUtils.forceMkdir; import static org.sonar.process.ProcessProperties.PATH_DATA; import static org.sonar.process.ProcessProperties.PATH_HOME; @@ -111,9 +117,36 @@ public class AppFileSystem implements FileSystem { private static void createOrCleanDirectory(Props props, String propKey) throws IOException { File dir = props.nonNullValueAsFile(propKey); - LOG.info("Cleaning and/or creating temp directory {}", dir.getAbsolutePath()); + LOG.info("Cleaning or creating temp directory {}", dir.getAbsolutePath()); if (!createDirectory(props, propKey)) { cleanDirectory(dir); } } + + private static void cleanDirectory(File dir) throws IOException { + Path path = Paths.get(dir.toURI()); + walkFileTree(path, new CleanRecursivelyFileVisitor(path)); + } + + private static class CleanRecursivelyFileVisitor extends SimpleFileVisitor<Path> { + private final Path path; + + public CleanRecursivelyFileVisitor(Path path) { + this.path = path; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + if (!dir.equals(path)) { + delete(dir); + } + return FileVisitResult.CONTINUE; + } + } } |