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.

App.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.application;
  21. import org.apache.commons.io.FilenameUtils;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.sonar.process.MinimumViableSystem;
  24. import org.sonar.process.ProcessCommands;
  25. import org.sonar.process.ProcessLogging;
  26. import org.sonar.process.Props;
  27. import org.sonar.process.StopWatcher;
  28. import org.sonar.process.Stoppable;
  29. import org.sonar.process.monitor.JavaCommand;
  30. import org.sonar.process.monitor.Monitor;
  31. import java.io.File;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import java.util.Properties;
  35. /**
  36. * Entry-point of process that starts and monitors elasticsearch and web servers
  37. */
  38. public class App implements Stoppable {
  39. private final Monitor monitor;
  40. private StopWatcher stopWatcher = null;
  41. public App() {
  42. this(Monitor.create());
  43. }
  44. App(Monitor monitor) {
  45. this.monitor = monitor;
  46. }
  47. public void start(Props props) {
  48. if (props.valueAsBoolean("sonar.enableStopCommand", false)) {
  49. // stop application when file <temp>/app.stop is created
  50. File tempDir = props.nonNullValueAsFile("sonar.path.temp");
  51. ProcessCommands commands = new ProcessCommands(tempDir, "app");
  52. stopWatcher = new StopWatcher(commands, this);
  53. stopWatcher.start();
  54. }
  55. monitor.start(createCommands(props));
  56. monitor.awaitTermination();
  57. }
  58. List<JavaCommand> createCommands(Props props) {
  59. List<JavaCommand> commands = new ArrayList<JavaCommand>();
  60. File homeDir = props.nonNullValueAsFile("sonar.path.home");
  61. File tempDir = props.nonNullValueAsFile("sonar.path.temp");
  62. JavaCommand elasticsearch = new JavaCommand("search");
  63. elasticsearch
  64. .setWorkDir(homeDir)
  65. .addJavaOptions(props.nonNullValue(DefaultSettings.SEARCH_JAVA_OPTS))
  66. .addJavaOptions(props.nonNullValue(DefaultSettings.SEARCH_JAVA_ADDITIONAL_OPTS))
  67. .setTempDir(tempDir.getAbsoluteFile())
  68. .setClassName("org.sonar.search.SearchServer")
  69. .setArguments(props.rawProperties())
  70. .addClasspath("./lib/common/*")
  71. .addClasspath("./lib/search/*");
  72. commands.add(elasticsearch);
  73. // do not yet start SQ in cluster mode. See SONAR-5483 & SONAR-5391
  74. if (StringUtils.isEmpty(props.value(DefaultSettings.CLUSTER_MASTER))) {
  75. JavaCommand webServer = new JavaCommand("web")
  76. .setWorkDir(homeDir)
  77. .addJavaOptions(props.nonNullValue(DefaultSettings.WEB_JAVA_OPTS))
  78. .addJavaOptions(props.nonNullValue(DefaultSettings.WEB_JAVA_ADDITIONAL_OPTS))
  79. .setTempDir(tempDir.getAbsoluteFile())
  80. // required for logback tomcat valve
  81. .setEnvVariable("sonar.path.logs", props.nonNullValue("sonar.path.logs"))
  82. .setClassName("org.sonar.server.app.WebServer")
  83. .setArguments(props.rawProperties())
  84. .addClasspath("./lib/common/*")
  85. .addClasspath("./lib/server/*");
  86. String driverPath = props.value(JdbcSettings.PROPERTY_DRIVER_PATH);
  87. if (driverPath != null) {
  88. webServer.addClasspath(driverPath);
  89. }
  90. commands.add(webServer);
  91. }
  92. return commands;
  93. }
  94. static String starPath(File homeDir, String relativePath) {
  95. File dir = new File(homeDir, relativePath);
  96. return FilenameUtils.concat(dir.getAbsolutePath(), "*");
  97. }
  98. public static void main(String[] args) throws Exception {
  99. new MinimumViableSystem().check();
  100. CommandLineParser cli = new CommandLineParser();
  101. Properties rawProperties = cli.parseArguments(args);
  102. Props props = new PropsBuilder(rawProperties, new JdbcSettings()).build();
  103. ProcessLogging logging = new ProcessLogging();
  104. logging.configure(props, "/org/sonar/application/logback.xml");
  105. if (props.valueAsBoolean("sonar.log.console", false)) {
  106. logging.addConsoleAppender();
  107. }
  108. App app = new App();
  109. app.start(props);
  110. }
  111. StopWatcher getStopWatcher() {
  112. return stopWatcher;
  113. }
  114. @Override
  115. public void stopAsync() {
  116. monitor.stopAsync();
  117. }
  118. }