]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4843 disable restart on Windows + Java 6 (UrlClassloader locks JAR files)
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 25 Mar 2014 10:22:43 +0000 (11:22 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 25 Mar 2014 10:40:39 +0000 (11:40 +0100)
sonar-server/src/main/java/org/sonar/server/platform/ws/RestartHandler.java
sonar-server/src/test/java/org/sonar/server/platform/ws/RestartHandlerTest.java
sonar-server/src/test/java/org/sonar/server/platform/ws/SystemWsTest.java

index d40d0651887ae1f67e9b68324f2284c1b588378d..45e480ae2c08188032aaff90bb423c71f15f73ae 100644 (file)
@@ -26,6 +26,7 @@ import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.RequestHandler;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.System2;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.platform.Platform;
 
@@ -33,22 +34,25 @@ public class RestartHandler implements RequestHandler {
 
   private final Settings settings;
   private final Platform platform;
+  private final System2 system;
 
-  public RestartHandler(Settings settings, Platform platform) {
+  public RestartHandler(Settings settings, Platform platform, System2 system) {
     this.settings = settings;
     this.platform = platform;
+    this.system = system;
   }
 
   void define(WebService.NewController controller) {
     controller.createAction("restart")
-      .setDescription("Restart server. Available only on development mode (sonar.dev=true)")
+      .setDescription("Restart server. Available only on development mode (sonar.dev=true), except when using Java 6 " +
+        "on MS Windows.")
       .setPost(true)
       .setHandler(this);
   }
 
   @Override
   public void handle(Request request, Response response) {
-    if (settings.getBoolean("sonar.dev")) {
+    if (canRestart()) {
       Logger logger = LoggerFactory.getLogger(getClass());
       logger.info("Restart server");
       platform.restart();
@@ -59,4 +63,13 @@ public class RestartHandler implements RequestHandler {
       throw new ForbiddenException();
     }
   }
+
+  private boolean canRestart() {
+    boolean ok = settings.getBoolean("sonar.dev");
+    if (ok) {
+      ok = !system.isOsWindows() || system.isJavaAtLeast17();
+    }
+    return ok;
+  }
+
 }
index ca18bb20d2f200838f1b16eafc2da402dcea0192..84590153905cd89838cbcf7d570998f3035a49b9 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.platform.ws;
 import org.junit.Test;
 import org.sonar.api.config.Settings;
 import org.sonar.api.server.ws.WsTester;
+import org.sonar.api.utils.System2;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.platform.Platform;
 
@@ -29,15 +30,20 @@ import static org.fest.assertions.Fail.fail;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
 
 public class RestartHandlerTest {
 
+  System2 system = mock(System2.class);
+
   @Test
   public void restart_if_dev_mode() throws Exception {
     Platform platform = mock(Platform.class);
     Settings settings = new Settings();
     settings.setProperty("sonar.dev", true);
-    RestartHandler restartHandler = new RestartHandler(settings, platform);
+    when(system.isOsWindows()).thenReturn(false);
+
+    RestartHandler restartHandler = new RestartHandler(settings, platform, system);
     SystemWs ws = new SystemWs(restartHandler);
 
     WsTester tester = new WsTester(ws);
@@ -50,7 +56,27 @@ public class RestartHandlerTest {
   public void fail_if_production_mode() throws Exception {
     Platform platform = mock(Platform.class);
     Settings settings = new Settings();
-    RestartHandler restartHandler = new RestartHandler(settings, platform);
+    RestartHandler restartHandler = new RestartHandler(settings, platform, system);
+    SystemWs ws = new SystemWs(restartHandler);
+
+    WsTester tester = new WsTester(ws);
+    try {
+      tester.newRequest("restart").execute();
+      fail();
+    } catch (ForbiddenException e) {
+      verifyZeroInteractions(platform);
+    }
+  }
+
+  @Test
+  public void fail_if_windows_java_6() throws Exception {
+    Platform platform = mock(Platform.class);
+    Settings settings = new Settings();
+    settings.setProperty("sonar.dev", true);
+    when(system.isOsWindows()).thenReturn(true);
+    when(system.isJavaAtLeast17()).thenReturn(false);
+
+    RestartHandler restartHandler = new RestartHandler(settings, platform, system);
     SystemWs ws = new SystemWs(restartHandler);
 
     WsTester tester = new WsTester(ws);
index 97800606bb38d3788e7ccd4e9e13f63b6993d839..dd7b6e0c712001188871fc0a157779312ee9be8d 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.platform.ws;
 import org.junit.Test;
 import org.sonar.api.config.Settings;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.System2;
 import org.sonar.server.platform.Platform;
 
 import static org.fest.assertions.Assertions.assertThat;
@@ -33,7 +34,7 @@ public class SystemWsTest {
   public void define() throws Exception {
     Platform platform = mock(Platform.class);
     Settings settings = new Settings();
-    RestartHandler restartHandler = new RestartHandler(settings, platform);
+    RestartHandler restartHandler = new RestartHandler(settings, platform, mock(System2.class));
     SystemWs ws = new SystemWs(restartHandler);
     WebService.Context context = new WebService.Context();