001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.application;
021
022import org.apache.commons.io.FileUtils;
023
024import java.io.File;
025import java.io.IOException;
026import java.net.URISyntaxException;
027import java.util.Properties;
028
029public final class StartServer {
030  private static final String DEFAULT_WEB_HOST = "0.0.0.0";
031  private static final int DEFAULT_WEB_PORT = 9000;
032  private static final String DEFAULT_WEB_CONTEXT = "/";
033
034  private StartServer() {
035  }
036
037  public static void main(String[] args) throws Exception {
038    canCreateTemporaryFiles();
039    configureHome();
040
041    Properties configuration = getConfiguration();
042    String host = configuration.getProperty("sonar.web.host", DEFAULT_WEB_HOST);
043    int port = Integer.parseInt(configuration.getProperty("sonar.web.port", "" + DEFAULT_WEB_PORT));
044    String context = configuration.getProperty("sonar.web.context", DEFAULT_WEB_CONTEXT);
045    JettyEmbedder jetty = new JettyEmbedder(host, port, context, StartServer.class.getResource("/jetty.xml"));
046    configureRequestLogs(jetty, configuration);
047
048    jetty.start();
049    Thread.currentThread().join();
050  }
051
052  /**
053   * This check is required in order to provide more meaningful message than JRuby - see SONAR-2715
054   */
055  private static void canCreateTemporaryFiles() {
056    File file = null;
057    try {
058      file = File.createTempFile("sonar-check", "tmp");
059    } catch (IOException e) {
060      throw new IllegalStateException("Unable to create file in temporary directory, please check existence of it and permissions: " + FileUtils.getTempDirectoryPath(), e);
061    } finally {
062      FileUtils.deleteQuietly(file);
063    }
064  }
065
066  private static void configureRequestLogs(JettyEmbedder jetty, Properties configuration) {
067    String filenamePattern = configuration.getProperty("sonar.web.jettyRequestLogs");
068    if (filenamePattern != null) {
069      jetty.configureRequestLogs(filenamePattern);
070    }
071  }
072
073  private static Properties getConfiguration() throws IOException {
074    Properties properties = new Properties();
075    properties.load(StartServer.class.getResourceAsStream("/conf/sonar.properties"));
076    return properties;
077  }
078
079  private static void configureHome() throws URISyntaxException {
080    File confFile = new File(StartServer.class.getResource("/conf/sonar.properties").toURI());
081    System.setProperty("SONAR_HOME" /* see constant org.sonar.server.platform.SonarHome.PROPERTY */,
082      confFile.getParentFile().getParentFile().getAbsolutePath());
083  }
084}