From: Simon Brandhof Date: Thu, 24 Mar 2016 09:42:38 +0000 (+0100) Subject: Correctly stop server when an error is raised during startup X-Git-Tag: 5.5-M11~14 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=80d64d20e5fe3980b0057030a35368fc83ba0984;p=sonarqube.git Correctly stop server when an error is raised during startup --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/RubyRailsContextListener.java b/server/sonar-server/src/main/java/org/sonar/server/platform/RubyRailsContextListener.java index 3f66d39afbe..3f8fb0cd0e9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/RubyRailsContextListener.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/RubyRailsContextListener.java @@ -19,8 +19,11 @@ */ package org.sonar.server.platform; +import com.google.common.base.Throwables; import javax.servlet.ServletContextEvent; +import org.jruby.rack.RackApplicationFactory; import org.jruby.rack.rails.RailsServletContextListener; +import org.jruby.rack.servlet.ServletRackContext; /** * Overriding {@link RailsServletContextListener} allows to disable initialization of Ruby on Rails @@ -35,4 +38,14 @@ public class RubyRailsContextListener extends RailsServletContextListener { super.contextInitialized(event); } } + + // Always stop server when an error is raised during startup. + // By default Rack only logs an error (see org.jruby.rack.RackServletContextListener#handleInitializationException()). + // Rack propagates exceptions if the properties jruby.rack.exception or jruby.rack.error are set to true. + // Unfortunately we didn't succeed in defining these properties, so the method is overridden here. + // Initial need: SONAR-6171 + @Override + protected void handleInitializationException(Exception e, RackApplicationFactory factory, ServletRackContext rackContext) { + throw Throwables.propagate(e); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/RubyRailsContextListenerTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/RubyRailsContextListenerTest.java index 4e2cb8cb9db..976d7c73aac 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/RubyRailsContextListenerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/RubyRailsContextListenerTest.java @@ -19,8 +19,13 @@ */ package org.sonar.server.platform; +import java.io.IOException; import javax.servlet.ServletContextEvent; +import org.jruby.rack.RackApplicationFactory; +import org.jruby.rack.servlet.ServletRackContext; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mockito; import static java.lang.Boolean.TRUE; @@ -34,6 +39,9 @@ import static org.mockito.Mockito.when; public class RubyRailsContextListenerTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + ServletContextEvent event = mock(ServletContextEvent.class, Mockito.RETURNS_DEEP_STUBS); RubyRailsContextListener underTest = new RubyRailsContextListener(); @@ -54,4 +62,11 @@ public class RubyRailsContextListenerTest { // See RailsServletContextListener -> RackServletContextListener verify(event.getServletContext()).setAttribute(eq("rack.factory"), anyObject()); } + + @Test + public void always_propagates_initialization_errors() { + expectedException.expect(RuntimeException.class); + + underTest.handleInitializationException(new IOException(), mock(RackApplicationFactory.class), mock(ServletRackContext.class)); + } } diff --git a/server/sonar-web/src/main/webapp/WEB-INF/web.xml b/server/sonar-web/src/main/webapp/WEB-INF/web.xml index 5810000c72a..4bcacb4effd 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/web.xml +++ b/server/sonar-web/src/main/webapp/WEB-INF/web.xml @@ -30,14 +30,6 @@ jruby.rack.logging slf4j - - jruby.rack.error - true - - - jruby.rack.exception - true - ServletFilters