private final EmbeddedTomcat tomcat;
- WebServer(Props props) throws Exception {
+ WebServer(Props props) {
new MinimumViableSystem()
.checkJavaVersion()
.checkWritableTempDir()
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 {
}
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
--- /dev/null
+/*
+ * 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);
+ }
+ }
+}
--- /dev/null
+/*
+ * 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());
+ }
+}
<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>