aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2013-10-15 17:14:44 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2013-10-16 15:25:41 +0200
commit18a4c15b060d1b56f08826bf6006d08eaf2625ec (patch)
tree03cc1afb062054a3d3d74849bf18e7a6485ab05c /sonar-server
parent3f946fc11c49585f8cdf003c54f19c322a4aab29 (diff)
downloadsonarqube-18a4c15b060d1b56f08826bf6006d08eaf2625ec.tar.gz
sonarqube-18a4c15b060d1b56f08826bf6006d08eaf2625ec.zip
SONAR-4748 Introduce a new extension for managing temp files/folders.
Diffstat (limited to 'sonar-server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/platform/Platform.java2
-rw-r--r--sonar-server/src/main/java/org/sonar/server/util/ServerTempUtils.java41
-rw-r--r--sonar-server/src/test/java/org/sonar/server/util/ServerTempUtilsTest.java56
3 files changed, 99 insertions, 0 deletions
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 6c5f06364c7..b30b64b45bc 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
@@ -148,6 +148,7 @@ import org.sonar.server.ui.SecurityRealmFactory;
import org.sonar.server.ui.Views;
import org.sonar.server.user.DefaultUserService;
import org.sonar.server.user.NewUserNotifier;
+import org.sonar.server.util.ServerTempUtils;
import javax.servlet.ServletContext;
@@ -239,6 +240,7 @@ public final class Platform {
rootContainer.addSingleton(PreviewDatabaseFactory.class);
rootContainer.addSingleton(SemaphoreUpdater.class);
rootContainer.addSingleton(SemaphoresImpl.class);
+ rootContainer.addSingleton(ServerTempUtils.class);
rootContainer.startComponents();
}
diff --git a/sonar-server/src/main/java/org/sonar/server/util/ServerTempUtils.java b/sonar-server/src/main/java/org/sonar/server/util/ServerTempUtils.java
new file mode 100644
index 00000000000..bc43ca71481
--- /dev/null
+++ b/sonar-server/src/main/java/org/sonar/server/util/ServerTempUtils.java
@@ -0,0 +1,41 @@
+/*
+ * 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.server.util;
+
+import org.apache.commons.io.FileUtils;
+import org.sonar.api.platform.ServerFileSystem;
+import org.sonar.core.utils.AbstractTempUtils;
+
+import java.io.File;
+import java.io.IOException;
+
+public class ServerTempUtils extends AbstractTempUtils {
+
+ public ServerTempUtils(ServerFileSystem fs) {
+ File tempDir = new File(fs.getTempDir(), "tmp");
+ try {
+ FileUtils.forceMkdir(tempDir);
+ } catch (IOException e) {
+ throw new IllegalStateException("Unable to create root temp directory " + tempDir, e);
+ }
+ setTempDir(tempDir);
+ }
+
+}
diff --git a/sonar-server/src/test/java/org/sonar/server/util/ServerTempUtilsTest.java b/sonar-server/src/test/java/org/sonar/server/util/ServerTempUtilsTest.java
new file mode 100644
index 00000000000..7d057de0f7d
--- /dev/null
+++ b/sonar-server/src/test/java/org/sonar/server/util/ServerTempUtilsTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.server.util;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.platform.ServerFileSystem;
+
+import java.io.File;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class ServerTempUtilsTest {
+
+ @Rule
+ public ExpectedException throwable = ExpectedException.none();
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void createTempFolder() throws Exception {
+ ServerFileSystem fs = mock(ServerFileSystem.class);
+ File serverTempFolder = temp.newFolder();
+ when(fs.getTempDir()).thenReturn(serverTempFolder);
+ ServerTempUtils tempUtils = new ServerTempUtils(fs);
+ tempUtils.createTempDirectory();
+ tempUtils.createTempFile();
+ assertThat(new File(serverTempFolder, "tmp")).exists();
+ assertThat(new File(serverTempFolder, "tmp").list()).hasSize(2);
+
+ tempUtils.stop();
+ assertThat(new File(serverTempFolder, "tmp")).doesNotExist();
+ }
+}