3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.web;
22 import java.util.Enumeration;
23 import java.util.Properties;
24 import javax.servlet.ServletContext;
25 import javax.servlet.ServletContextEvent;
26 import javax.servlet.ServletContextListener;
27 import org.sonar.api.utils.log.Loggers;
28 import org.sonar.server.platform.PlatformImpl;
30 public final class PlatformServletContextListener implements ServletContextListener {
31 static final String STARTED_ATTRIBUTE = "sonarqube.started";
34 public void contextInitialized(ServletContextEvent event) {
36 Properties props = new Properties();
37 ServletContext servletContext = event.getServletContext();
38 Enumeration<String> paramKeys = servletContext.getInitParameterNames();
39 while (paramKeys.hasMoreElements()) {
40 String key = paramKeys.nextElement();
41 props.put(key, servletContext.getInitParameter(key));
43 PlatformImpl.getInstance().init(props, servletContext);
44 PlatformImpl.getInstance().doStart();
45 event.getServletContext().setAttribute(STARTED_ATTRIBUTE, Boolean.TRUE);
46 } catch (org.sonar.api.utils.MessageException | org.sonar.process.MessageException e) {
47 Loggers.get(PlatformImpl.class).error("Web server startup failed: " + e.getMessage());
49 } catch (Throwable t) {
50 Loggers.get(PlatformImpl.class).error("Web server startup failed", t);
52 throw new AbortTomcatStartException();
56 private void stopQuietly() {
58 PlatformImpl.getInstance().doStop();
59 } catch (Exception e) {
60 // ignored, but an error during startup generally prevents pico to be correctly stopped
65 public void contextDestroyed(ServletContextEvent event) {
66 PlatformImpl.getInstance().doStop();