001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.search;
021
022 import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
023 import org.elasticsearch.common.unit.TimeValue;
024 import org.elasticsearch.node.internal.InternalNode;
025 import org.slf4j.LoggerFactory;
026 import org.sonar.process.MinimumViableSystem;
027 import org.sonar.process.Monitored;
028 import org.sonar.process.ProcessEntryPoint;
029 import org.sonar.process.ProcessLogging;
030 import org.sonar.process.Props;
031
032 public class SearchServer implements Monitored {
033
034 private final SearchSettings settings;
035 private InternalNode node;
036
037 public SearchServer(Props props) {
038 this.settings = new SearchSettings(props);
039 new MinimumViableSystem().check();
040 }
041
042 @Override
043 public void start() {
044 LoggerFactory.getLogger(SearchServer.class).info("Starting Elasticsearch[{}] on port {}", settings.clusterName(), settings.tcpPort());
045
046 node = new InternalNode(settings.build(), true);
047 node.start();
048 }
049
050 @Override
051 public boolean isReady() {
052 return node != null && node.client().admin().cluster().prepareHealth()
053 .setWaitForYellowStatus()
054 .setTimeout(TimeValue.timeValueSeconds(30L))
055 .get()
056 .getStatus() != ClusterHealthStatus.RED;
057 }
058
059 @Override
060 public void awaitStop() {
061 while (node != null && !node.isClosed()) {
062 try {
063 Thread.sleep(200L);
064 } catch (InterruptedException e) {
065 // Ignore
066 }
067 }
068 }
069
070 @Override
071 public void stop() {
072 if (node != null && !node.isClosed()) {
073 node.close();
074 }
075 }
076
077 public static void main(String... args) {
078 ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args);
079 new ProcessLogging().configure(entryPoint.getProps(), "/org/sonar/search/logback.xml");
080 SearchServer searchServer = new SearchServer(entryPoint.getProps());
081 entryPoint.launch(searchServer);
082 }
083 }