You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EmbeddedTomcat.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.app;
  21. import com.google.common.base.Throwables;
  22. import java.io.File;
  23. import org.apache.catalina.LifecycleException;
  24. import org.apache.catalina.core.StandardContext;
  25. import org.apache.catalina.startup.Tomcat;
  26. import org.sonar.api.utils.log.Loggers;
  27. import org.sonar.process.Props;
  28. import static org.sonar.core.util.FileUtils.deleteQuietly;
  29. import static org.sonar.process.ProcessProperties.Property.PATH_TEMP;
  30. class EmbeddedTomcat {
  31. private final Props props;
  32. private Tomcat tomcat = null;
  33. private volatile StandardContext webappContext;
  34. EmbeddedTomcat(Props props) {
  35. this.props = props;
  36. }
  37. void start() {
  38. // '%2F' (slash /) and '%5C' (backslash \) are permitted as path delimiters in URLs
  39. System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
  40. System.setProperty("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE", "true");
  41. tomcat = new Tomcat();
  42. // Initialize directories
  43. String basedir = tomcatBasedir().getAbsolutePath();
  44. tomcat.setBaseDir(basedir);
  45. tomcat.getHost().setAppBase(basedir);
  46. tomcat.getHost().setAutoDeploy(false);
  47. tomcat.getHost().setCreateDirs(false);
  48. tomcat.getHost().setDeployOnStartup(true);
  49. new TomcatAccessLog().configure(tomcat, props);
  50. TomcatConnectors.configure(tomcat, props);
  51. webappContext = new TomcatContexts().configure(tomcat, props);
  52. try {
  53. tomcat.start();
  54. new TomcatStartupLogs(Loggers.get(getClass())).log(tomcat);
  55. } catch (LifecycleException e) {
  56. Loggers.get(EmbeddedTomcat.class).error("Fail to start web server", e);
  57. Throwables.propagate(e);
  58. }
  59. }
  60. Status getStatus() {
  61. if (webappContext == null) {
  62. return Status.DOWN;
  63. }
  64. switch (webappContext.getState()) {
  65. case NEW:
  66. case INITIALIZING:
  67. case INITIALIZED:
  68. case STARTING_PREP:
  69. case STARTING:
  70. return Status.DOWN;
  71. case STARTED:
  72. return Status.UP;
  73. default:
  74. // problem, stopped or failed
  75. return Status.FAILED;
  76. }
  77. }
  78. public enum Status {
  79. DOWN, UP, FAILED
  80. }
  81. private File tomcatBasedir() {
  82. return new File(props.value(PATH_TEMP.getKey()), "tc");
  83. }
  84. void terminate() {
  85. if (tomcat.getServer().getState().isAvailable()) {
  86. try {
  87. tomcat.stop();
  88. tomcat.destroy();
  89. } catch (Exception e) {
  90. Loggers.get(EmbeddedTomcat.class).error("Fail to stop web server", e);
  91. }
  92. }
  93. deleteQuietly(tomcatBasedir());
  94. }
  95. void awaitTermination() {
  96. tomcat.getServer().await();
  97. }
  98. }