]> source.dussan.org Git - sonarqube.git/commitdiff
Correctly stop server when an error is raised during startup
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 24 Mar 2016 09:42:38 +0000 (10:42 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 24 Mar 2016 20:14:17 +0000 (21:14 +0100)
server/sonar-server/src/main/java/org/sonar/server/platform/RubyRailsContextListener.java
server/sonar-server/src/test/java/org/sonar/server/platform/RubyRailsContextListenerTest.java
server/sonar-web/src/main/webapp/WEB-INF/web.xml

index 3f66d39afbec95889fa40e5993d9ce520005e51c..3f8fb0cd0e9b69877293e00f8d2df34bc97c75dd 100644 (file)
  */
 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);
+  }
 }
index 4e2cb8cb9db17d7a7aa961ba0b337fe73e6e7ff4..976d7c73aac3998acb3df15104c02c13c626fd14 100644 (file)
  */
 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));
+  }
 }
index 5810000c72a962d21545e79a270066e48087247e..4bcacb4effd44fb6fece1d0fbaf62f2b9fdb76bc 100644 (file)
     <param-name>jruby.rack.logging</param-name>
     <param-value>slf4j</param-value>
   </context-param>
-  <context-param>
-    <param-name>jruby.rack.error</param-name>
-    <param-value>true</param-value>
-  </context-param>
-  <context-param>
-    <param-name>jruby.rack.exception</param-name>
-    <param-value>true</param-value>
-  </context-param>
 
   <filter>
     <filter-name>ServletFilters</filter-name>