From d7a4e565629a7c096239813aaa44245c819993a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 20 Jul 2015 15:21:24 +0200 Subject: [PATCH] SONAR-6700 fix undeleted computation working dir and cache files --- .../computation/ComputationContainer.java | 3 + .../ComputationTempFolderProvider.java | 94 +++++++++++++++++++ .../api/utils/internal/DefaultTempFolder.java | 4 +- 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/computation/ComputationTempFolderProvider.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationContainer.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationContainer.java index 21e9e7c9cb2..2e152ca992c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationContainer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationContainer.java @@ -59,6 +59,9 @@ public class ComputationContainer { */ static List componentClasses() { return Arrays.asList( + // temp directory + new ComputationTempFolderProvider(), + ComputationService.class, ComputationSteps.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationTempFolderProvider.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationTempFolderProvider.java new file mode 100644 index 00000000000..114af56d7df --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ComputationTempFolderProvider.java @@ -0,0 +1,94 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.computation; + +import java.io.File; +import java.io.IOException; +import javax.annotation.CheckForNull; +import org.apache.commons.io.FileUtils; +import org.picocontainer.ComponentLifecycle; +import org.picocontainer.PicoContainer; +import org.picocontainer.injectors.ProviderAdapter; +import org.sonar.api.platform.ServerFileSystem; +import org.sonar.api.utils.TempFolder; +import org.sonar.api.utils.internal.DefaultTempFolder; + +/** + * Provides a TempFolder instance pointing a directory dedicated to the processing of a specific item. + * This directory will be deleted at the end of the processing. + * This directory is located in the "ce" directory of the temp directory of the SonarQube instance. + */ +public class ComputationTempFolderProvider extends ProviderAdapter implements ComponentLifecycle { + private boolean started = false; + @CheckForNull + private AutoDeletedTempFolder tempFolder; + + public TempFolder provide(ServerFileSystem fs) { + if (this.tempFolder == null) { + File tempDir = new File(fs.getTempDir(), "ce"); + try { + FileUtils.forceMkdir(tempDir); + } catch (IOException e) { + throw new IllegalStateException("Unable to create computation temp directory " + tempDir, e); + } + File computationDir = new DefaultTempFolder(tempDir).newDir(); + this.tempFolder = new AutoDeletedTempFolder(computationDir); + } + return this.tempFolder; + } + + @Override + public void start(PicoContainer container) { + this.started = true; + } + + @Override + public void stop(PicoContainer container) { + if (tempFolder != null) { + tempFolder.stop(); + } + } + + @Override + public void dispose(PicoContainer container) { + // nothing to do + } + + @Override + public boolean componentHasLifecycle() { + return true; + } + + @Override + public boolean isStarted() { + return started; + } + + private static class AutoDeletedTempFolder extends DefaultTempFolder { + + public AutoDeletedTempFolder(File tempDir) { + super(tempDir); + } + + public void stop() { + FileUtils.deleteQuietly(tempDir); + } + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java index c96ecc3e488..5a20de88adb 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java @@ -34,7 +34,7 @@ public class DefaultTempFolder implements TempFolder { /** Maximum loop count when creating temp directories. */ private static final int TEMP_DIR_ATTEMPTS = 10000; - private final File tempDir; + protected final File tempDir; public DefaultTempFolder(File tempDir) { this.tempDir = tempDir; @@ -53,7 +53,7 @@ public class DefaultTempFolder implements TempFolder { for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) { File tempDir = new File(baseDir, baseName + counter); - if (tempDir.mkdir()) { + if (tempDir.mkdirs()) { return tempDir; } } -- 2.39.5