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;
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();
throw new ForbiddenException();
}
}
+
+ private boolean canRestart() {
+ boolean ok = settings.getBoolean("sonar.dev");
+ if (ok) {
+ ok = !system.isOsWindows() || system.isJavaAtLeast17();
+ }
+ return ok;
+ }
+
}
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;
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);
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);
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;
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();