]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6740 do not start Rails if Java container fails to start
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 4 Feb 2016 22:05:00 +0000 (23:05 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 5 Feb 2016 13:24:44 +0000 (14:24 +0100)
server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java
server/sonar-server/src/main/java/org/sonar/server/platform/PlatformServletContextListener.java
server/sonar-server/src/main/java/org/sonar/server/platform/RubyRailsContextListener.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/platform/RubyRailsContextListenerTest.java [new file with mode: 0644]
server/sonar-web/src/main/webapp/WEB-INF/web.xml

index c1a3da27b842f21bf49bae534d3d14fa5ba7c7c2..61a1b676669db67ceed573c8e75486a3e86e229a 100644 (file)
@@ -29,7 +29,7 @@ public class WebServer implements Monitored {
 
   private final EmbeddedTomcat tomcat;
 
-  WebServer(Props props) throws Exception {
+  WebServer(Props props) {
     new MinimumViableSystem()
       .checkJavaVersion()
       .checkWritableTempDir()
index 09a93201e905a92cb1ee0c14b69bdea7b867b1d6..83a6a75c188c2058a0c5229e80a800b89e0589bb 100644 (file)
 package org.sonar.server.platform;
 
 import com.google.common.base.Throwables;
-
+import java.util.Enumeration;
+import java.util.Properties;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
-import java.util.Enumeration;
-import java.util.Properties;
 
 public final class PlatformServletContextListener implements ServletContextListener {
 
+  static final String STARTED_ATTRIBUTE = "sonarqube.started";
+
   @Override
   public void contextInitialized(ServletContextEvent event) {
     try {
@@ -41,6 +42,8 @@ public final class PlatformServletContextListener implements ServletContextListe
       }
       Platform.getInstance().init(props, servletContext);
       Platform.getInstance().doStart();
+      event.getServletContext().setAttribute(STARTED_ATTRIBUTE, Boolean.TRUE);
+
     } catch (Throwable t) {
       // Tomcat 7 "limitations":
       // - server does not stop if webapp fails at 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
new file mode 100644 (file)
index 0000000..3f66d39
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform;
+
+import javax.servlet.ServletContextEvent;
+import org.jruby.rack.rails.RailsServletContextListener;
+
+/**
+ * Overriding {@link RailsServletContextListener} allows to disable initialization of Ruby on Rails
+ * environment when Java components fail to start.
+ * See https://jira.sonarsource.com/browse/SONAR-6740
+ */
+public class RubyRailsContextListener extends RailsServletContextListener {
+
+  @Override
+  public void contextInitialized(ServletContextEvent event) {
+    if (event.getServletContext().getAttribute(PlatformServletContextListener.STARTED_ATTRIBUTE) != null) {
+      super.contextInitialized(event);
+    }
+  }
+}
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
new file mode 100644 (file)
index 0000000..4e2cb8c
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform;
+
+import javax.servlet.ServletContextEvent;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static java.lang.Boolean.TRUE;
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class RubyRailsContextListenerTest {
+
+  ServletContextEvent event = mock(ServletContextEvent.class, Mockito.RETURNS_DEEP_STUBS);
+  RubyRailsContextListener underTest = new RubyRailsContextListener();
+
+  @Test
+  public void do_not_initialize_rails_if_error_during_startup() {
+    when(event.getServletContext().getAttribute(PlatformServletContextListener.STARTED_ATTRIBUTE)).thenReturn(null);
+
+    underTest.contextInitialized(event);
+
+    verify(event.getServletContext(), never()).setAttribute(anyString(), anyObject());
+  }
+
+  @Test
+  public void initialize_rails_if_no_errors_during_startup() {
+    when(event.getServletContext().getAttribute(PlatformServletContextListener.STARTED_ATTRIBUTE)).thenReturn(TRUE);
+    underTest.contextInitialized(event);
+    // Ruby environment is started
+    // See RailsServletContextListener -> RackServletContextListener
+    verify(event.getServletContext()).setAttribute(eq("rack.factory"), anyObject());
+  }
+}
index 65f54d512ff45ebf8c908752e478185bc6bea8a9..4bcacb4effd44fb6fece1d0fbaf62f2b9fdb76bc 100644 (file)
     <listener-class>org.sonar.server.platform.PlatformServletContextListener</listener-class>
   </listener>
   <listener>
-    <listener-class>org.jruby.rack.rails.RailsServletContextListener</listener-class>
+    <listener-class>org.sonar.server.platform.RubyRailsContextListener</listener-class>
   </listener>
 
   <mime-mapping>