diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-02-04 23:05:00 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-02-05 14:24:44 +0100 |
commit | bea23239548d8f1abb7ecf5def540f069317f452 (patch) | |
tree | 815b9dc6af70942b6ceda4ac5a25daf451cbe60f /server | |
parent | 39cc96f13fff24c17bb9b1a37311cd37281f6e03 (diff) | |
download | sonarqube-bea23239548d8f1abb7ecf5def540f069317f452.tar.gz sonarqube-bea23239548d8f1abb7ecf5def540f069317f452.zip |
SONAR-6740 do not start Rails if Java container fails to start
Diffstat (limited to 'server')
5 files changed, 103 insertions, 5 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java b/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java index c1a3da27b84..61a1b676669 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java @@ -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() diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/PlatformServletContextListener.java b/server/sonar-server/src/main/java/org/sonar/server/platform/PlatformServletContextListener.java index 09a93201e90..83a6a75c188 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/PlatformServletContextListener.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/PlatformServletContextListener.java @@ -20,15 +20,16 @@ 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 index 00000000000..3f66d39afbe --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/RubyRailsContextListener.java @@ -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 index 00000000000..4e2cb8cb9db --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/RubyRailsContextListenerTest.java @@ -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()); + } +} 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 65f54d512ff..4bcacb4effd 100644 --- a/server/sonar-web/src/main/webapp/WEB-INF/web.xml +++ b/server/sonar-web/src/main/webapp/WEB-INF/web.xml @@ -104,7 +104,7 @@ <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> |