aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-application/src/main/java/org/sonar/application/App.java
blob: 76ac6a16605455370909c4c7053f0bf3b71b7669 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2014 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube 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.
 *
 * SonarQube 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.application;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.process.JmxUtils;
import org.sonar.process.MinimumViableSystem;
import org.sonar.process.Monitor;
import org.sonar.process.ProcessLogging;
import org.sonar.process.ProcessMXBean;
import org.sonar.process.ProcessUtils;
import org.sonar.process.ProcessWrapper;
import org.sonar.process.Props;
import org.sonar.search.SearchServer;

import java.io.File;
import java.util.Properties;

/**
 * Entry-point of process that starts and monitors elasticsearch and web servers
 */
public class App implements ProcessMXBean {

  private Monitor monitor = new Monitor();
  private ProcessWrapper elasticsearch;
  private ProcessWrapper server;
  private boolean success = false;

  public App() {
    JmxUtils.registerMBean(this, "SonarQube");
    ProcessUtils.addSelfShutdownHook(this);
  }

  public void start(Props props) throws InterruptedException {
    try {
      Logger logger = LoggerFactory.getLogger(getClass());

      monitor.start();

      File homeDir = props.nonNullValueAsFile("sonar.path.home");
      File tempDir = props.nonNullValueAsFile("sonar.path.temp");
      elasticsearch = new ProcessWrapper(JmxUtils.SEARCH_SERVER_NAME);
      elasticsearch
        .setWorkDir(homeDir)
        .setJmxPort(props.valueAsInt(DefaultSettings.SEARCH_JMX_PORT))
        .addJavaOpts(props.value(DefaultSettings.SEARCH_JAVA_OPTS))
        .setTempDirectory(tempDir.getAbsoluteFile())
        .setClassName("org.sonar.search.SearchServer")
        .addProperties(props.rawProperties())
        .addClasspath("./lib/common/*")
        .addClasspath("./lib/search/*");
      if (elasticsearch.execute()) {
        monitor.monitor(elasticsearch);
        if (elasticsearch.waitForReady()) {
          logger.info("search server is up");

          // do not yet start SQ in cluster mode. See SONAR-5483 & SONAR-5391
          if (StringUtils.isEmpty(props.value(DefaultSettings.CLUSTER_MASTER))) {
            server = new ProcessWrapper(JmxUtils.WEB_SERVER_NAME)
              .setWorkDir(homeDir)
              .setJmxPort(props.valueAsInt(DefaultSettings.WEB_JMX_PORT))
              .addJavaOpts(props.nonNullValue(DefaultSettings.WEB_JAVA_OPTS))
              .setTempDirectory(tempDir.getAbsoluteFile())
              // required for logback tomcat valve
              .setLogDir(props.nonNullValueAsFile("sonar.path.logs"))
              .setClassName("org.sonar.server.app.WebServer")
              .addProperties(props.rawProperties())
              .addClasspath("./lib/common/*")
              .addClasspath("./lib/server/*");
            String driverPath = props.value(JdbcSettings.PROPERTY_DRIVER_PATH);
            if (driverPath != null) {
              server.addClasspath(driverPath);
            }
            if (server.execute()) {
              monitor.monitor(server);
              if (server.waitForReady()) {
                success = true;
                logger.info("web server is up");
              }
            }
          } else {
            success = true;
          }
        }
      }
    } finally {
      monitor.join();
      terminate();
    }
  }

  static String starPath(File homeDir, String relativePath) {
    File dir = new File(homeDir, relativePath);
    return FilenameUtils.concat(dir.getAbsolutePath(), "*");
  }

  @Override
  public boolean isReady() {
    return monitor.isAlive();
  }

  @Override
  public long ping() {
    return System.currentTimeMillis();
  }

  @Override
  public void terminate() {
    if (monitor != null && monitor.isAlive()) {
      monitor.terminate();
      monitor = null;
    }
    if (server != null) {
      server.terminate();
      server = null;
    }
    if (elasticsearch != null) {
      elasticsearch.terminate();
      elasticsearch = null;
    }
  }

  private boolean isSuccess() {
    return success;
  }

  public static void main(String[] args) {
    new MinimumViableSystem().check();
    CommandLineParser cli = new CommandLineParser();
    Properties rawProperties = cli.parseArguments(args);
    Props props;

    try {
      props = new PropsBuilder(rawProperties, new JdbcSettings()).build();
      new ProcessLogging().configure(props, "/org/sonar/application/logback.xml");
    } catch (Exception e) {
      throw new IllegalStateException(e);
    }

    App app = new App();
    ProcessUtils.addSelfShutdownHook(app);
    try {
      // start and wait for shutdown command
      if (props.contains(SearchServer.ES_CLUSTER_INET)) {
        LoggerFactory.getLogger(App.class).info("SonarQube slave configured to join SonarQube master : {}", props.value(SearchServer.ES_CLUSTER_INET));
    }
      app.start(props);
    } catch (InterruptedException e) {
      LoggerFactory.getLogger(App.class).info("interrupted");
    } finally {
      LoggerFactory.getLogger(App.class).info("stopped");
      System.exit(app.isSuccess() ? 0 : 1);
    }
  }
}