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