From cf02e5d9daefd6ab52ef81ac03797412383d1536 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 25 Mar 2014 11:22:43 +0100 Subject: [PATCH] SONAR-4843 disable restart on Windows + Java 6 (UrlClassloader locks JAR files) --- .../server/platform/ws/RestartHandler.java | 19 ++++++++++-- .../platform/ws/RestartHandlerTest.java | 30 +++++++++++++++++-- .../server/platform/ws/SystemWsTest.java | 3 +- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ws/RestartHandler.java b/sonar-server/src/main/java/org/sonar/server/platform/ws/RestartHandler.java index d40d0651887..45e480ae2c0 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ws/RestartHandler.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ws/RestartHandler.java @@ -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; + } + } diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartHandlerTest.java index ca18bb20d2f..84590153905 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartHandlerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/ws/RestartHandlerTest.java @@ -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); diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemWsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemWsTest.java index 97800606bb3..dd7b6e0c712 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemWsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemWsTest.java @@ -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(); -- 2.39.5