@@ -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()); |
@@ -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) { |
@@ -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); | |||
} | |||
} |
@@ -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); | |||
} | |||
@@ -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(); | |||
} | |||
} |
@@ -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(); | |||
} | |||
} |
@@ -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(); | |||
} | |||
@@ -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(); | |||
} | |||
} |