diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-10-21 11:17:16 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-10-21 11:17:55 +0200 |
commit | 8842c50c19054ced2809a8ebdde854a83312a8d2 (patch) | |
tree | 6c40289430c8dc4ec8cf82a1c5369abdb7b92a41 | |
parent | eb321c1da9949532872a6a7c64be6d51e11affe4 (diff) | |
download | sonarqube-8842c50c19054ced2809a8ebdde854a83312a8d2.tar.gz sonarqube-8842c50c19054ced2809a8ebdde854a83312a8d2.zip |
SONAR-4748 Fix issues and regression for temp folders
8 files changed, 70 insertions, 35 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java index abfd8de5fc2..13bb325e89d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java @@ -26,6 +26,7 @@ import org.sonar.api.platform.ComponentContainer; import org.sonar.api.platform.PluginMetadata; import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.UriReader; +import org.sonar.api.utils.internal.TempFolderCleaner; import org.sonar.batch.components.PastMeasuresLoader; import org.sonar.batch.components.PastSnapshotFinder; import org.sonar.batch.components.PastSnapshotFinderByDate; @@ -88,6 +89,7 @@ public class BootstrapContainer extends ComponentContainer { ServerMetadata.class, org.sonar.batch.ServerMetadata.class, new TempFolderProvider(), + TempFolderCleaner.class, HttpDownloader.class, UriReader.class, new FileCacheProvider()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java index fc773552216..ed0ea506615 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java @@ -33,7 +33,7 @@ public class TempFolderProvider extends ProviderAdapter { public TempFolder provide(BootstrapSettings bootstrapSettings) { String workingDirPath = bootstrapSettings.property(CoreProperties.WORKING_DIRECTORY, CoreProperties.WORKING_DIRECTORY_DEFAULT_VALUE); File workingDir = new File(workingDirPath); - File tempDir = new File(workingDir, "tmp"); + File tempDir = new File(workingDir, ".sonartmp"); try { FileUtils.forceMkdir(tempDir); } catch (IOException e) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempFolderProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempFolderProviderTest.java index 8596dd33afd..adc744b8da8 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempFolderProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/TempFolderProviderTest.java @@ -26,7 +26,6 @@ import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; import org.sonar.api.utils.TempFolder; -import org.sonar.api.utils.internal.DefaultTempFolder; import java.io.File; @@ -43,14 +42,12 @@ public class TempFolderProviderTest { @Test public void createTempFolder() throws Exception { File workingDir = temp.newFolder(); - TempFolder tempFolder = new TempFolderProvider().provide(new BootstrapSettings( + TempFolderProvider tempFolderProvider = new TempFolderProvider(); + TempFolder tempFolder = tempFolderProvider.provide(new BootstrapSettings( new BootstrapProperties(ImmutableMap.of(CoreProperties.WORKING_DIRECTORY, workingDir.getAbsolutePath())))); tempFolder.newDir(); tempFolder.newFile(); - assertThat(new File(workingDir, "tmp")).exists(); - assertThat(new File(workingDir, "tmp").list()).hasSize(2); - - ((DefaultTempFolder) tempFolder).stop(); - assertThat(new File(workingDir, "tmp")).doesNotExist(); + assertThat(new File(workingDir, ".sonartmp")).exists(); + assertThat(new File(workingDir, ".sonartmp").list()).hasSize(2); } } 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 3a33f349575..a1862ede487 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 @@ -21,7 +21,6 @@ package org.sonar.api.utils.internal; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; -import org.picocontainer.Startable; import org.sonar.api.utils.TempFolder; import javax.annotation.Nullable; @@ -29,7 +28,7 @@ import javax.annotation.Nullable; import java.io.File; import java.io.IOException; -public class DefaultTempFolder implements TempFolder, Startable { +public class DefaultTempFolder implements TempFolder { /** Maximum loop count when creating temp directories. */ private static final int TEMP_DIR_ATTEMPTS = 10000; @@ -104,13 +103,7 @@ public class DefaultTempFolder implements TempFolder, Startable { + baseName + "0" + suffix + " to " + baseName + (TEMP_DIR_ATTEMPTS - 1) + suffix + ")"); } - @Override - public void start() { - // Nothing to do - } - - @Override - public void stop() { + public void clean() { FileUtils.deleteQuietly(tempDir); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/TempFolderCleaner.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/TempFolderCleaner.java new file mode 100644 index 00000000000..be28c981798 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/TempFolderCleaner.java @@ -0,0 +1,44 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 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.api.utils.internal; + +import org.picocontainer.Startable; +import org.sonar.api.ServerComponent; +import org.sonar.api.task.TaskComponent; +import org.sonar.api.utils.TempFolder; + +public class TempFolderCleaner implements TaskComponent, ServerComponent, Startable { + + private TempFolder defaultTempFolder; + + public TempFolderCleaner(TempFolder defaultTempFolder) { + this.defaultTempFolder = defaultTempFolder; + } + + @Override + public void start() { + // Nothing to do + } + + @Override + public void stop() { + ((DefaultTempFolder) defaultTempFolder).clean(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java index 93a795474ab..07f6d7545c6 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/DefaultTempFolderTest.java @@ -38,26 +38,26 @@ public class DefaultTempFolderTest { @Test public void createTempFolderAndFile() throws Exception { - File tempFolder = temp.newFolder(); - DefaultTempFolder tempUtils = new DefaultTempFolder(tempFolder); - File dir = tempUtils.newDir(); + File rootTempFolder = temp.newFolder(); + DefaultTempFolder tempFolder = new DefaultTempFolder(rootTempFolder); + File dir = tempFolder.newDir(); assertThat(dir).exists().isDirectory(); - File file = tempUtils.newFile(); + File file = tempFolder.newFile(); assertThat(file).exists().isFile(); - tempUtils.stop(); - assertThat(tempFolder).doesNotExist(); + new TempFolderCleaner(tempFolder).stop(); + assertThat(rootTempFolder).doesNotExist(); } @Test public void createTempFolderWithName() throws Exception { - File tempFolder = temp.newFolder(); - DefaultTempFolder tempUtils = new DefaultTempFolder(tempFolder); - File dir = tempUtils.newDir("sample"); + File rootTempFolder = temp.newFolder(); + DefaultTempFolder tempFolder = new DefaultTempFolder(rootTempFolder); + File dir = tempFolder.newDir("sample"); assertThat(dir).exists().isDirectory(); - assertThat(new File(tempFolder, "sample")).isEqualTo(dir); + assertThat(new File(rootTempFolder, "sample")).isEqualTo(dir); - tempUtils.stop(); - assertThat(tempFolder).doesNotExist(); + new TempFolderCleaner(tempFolder).stop(); + assertThat(rootTempFolder).doesNotExist(); } } diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 72c62fbc304..42797970a28 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -35,6 +35,7 @@ import org.sonar.api.rules.XMLRuleParser; import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.TimeProfiler; import org.sonar.api.utils.UriReader; +import org.sonar.api.utils.internal.TempFolderCleaner; import org.sonar.core.component.SnapshotPerspectives; import org.sonar.core.config.Logback; import org.sonar.core.i18n.GwtI18n; @@ -239,7 +240,8 @@ public final class Platform { rootContainer.addSingleton(PreviewDatabaseFactory.class); rootContainer.addSingleton(SemaphoreUpdater.class); rootContainer.addSingleton(SemaphoresImpl.class); - rootContainer.addSingleton(new TempFolderProvider()); + rootContainer.addPicoAdapter(new TempFolderProvider()); + rootContainer.addSingleton(TempFolderCleaner.class); rootContainer.startComponents(); } diff --git a/sonar-server/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java b/sonar-server/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java index 0d157d591ee..878b2f0d984 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java @@ -25,7 +25,6 @@ import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.platform.ServerFileSystem; import org.sonar.api.utils.TempFolder; -import org.sonar.api.utils.internal.DefaultTempFolder; import java.io.File; @@ -46,13 +45,11 @@ public class TempFolderProviderTest { ServerFileSystem fs = mock(ServerFileSystem.class); File serverTempFolder = temp.newFolder(); when(fs.getTempDir()).thenReturn(serverTempFolder); - TempFolder tempUtils = new TempFolderProvider().provide(fs); + TempFolderProvider tempFolderProvider = new TempFolderProvider(); + TempFolder tempUtils = tempFolderProvider.provide(fs); tempUtils.newDir(); tempUtils.newFile(); assertThat(new File(serverTempFolder, "tmp")).exists(); assertThat(new File(serverTempFolder, "tmp").list()).hasSize(2); - - ((DefaultTempFolder) tempUtils).stop(); - assertThat(new File(serverTempFolder, "tmp")).doesNotExist(); } } |