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.server.platform;
021
022 import org.apache.commons.configuration.Configuration;
023 import org.sonar.api.CoreProperties;
024 import org.sonar.api.config.PropertyDefinitions;
025 import org.sonar.api.config.Settings;
026 import org.sonar.api.database.configuration.Property;
027 import org.sonar.core.config.ConfigurationUtils;
028 import org.sonar.jpa.session.DatabaseSessionFactory;
029
030 import javax.servlet.ServletContext;
031 import java.io.File;
032 import java.util.List;
033 import java.util.Properties;
034
035 /**
036 * Load settings in the following order (the last override the first) :
037 * <ol>
038 * <li>general settings persisted in database</li>
039 * <li>file $SONAR_HOME/conf/sonar.properties</li>
040 * <li>environment variables</li>
041 * <li>system properties</li>
042 * </ol>
043 *
044 * @since 2.12
045 */
046 public class ServerSettings extends Settings {
047
048 public static final String DEPLOY_DIR = "sonar.web.deployDir";
049
050 private DatabaseSessionFactory sessionFactory;
051 private Configuration deprecatedConfiguration;
052 private File deployDir;
053
054 public ServerSettings(PropertyDefinitions definitions, Configuration deprecatedConfiguration, ServletContext servletContext) {
055 super(definitions);
056 this.deprecatedConfiguration = deprecatedConfiguration;
057 this.deployDir = getDeployDir(servletContext);
058 load();
059 }
060
061 ServerSettings(PropertyDefinitions definitions, Configuration deprecatedConfiguration, File deployDir, File sonarHome) {
062 super(definitions);
063 this.deprecatedConfiguration = deprecatedConfiguration;
064 this.deployDir = deployDir;
065 load(sonarHome);
066 }
067
068 public ServerSettings setSessionFactory(DatabaseSessionFactory sessionFactory) {
069 this.sessionFactory = sessionFactory;
070 return this;
071 }
072
073 public ServerSettings load() {
074 return load(SonarHome.getHome());
075 }
076
077 ServerSettings load(File sonarHome) {
078 clear();
079 setProperty(CoreProperties.SONAR_HOME, sonarHome.getAbsolutePath());
080 setProperty(DEPLOY_DIR, deployDir.getAbsolutePath());
081
082 // order is important : the last override the first
083 loadDatabaseSettings();
084 loadPropertiesFile(sonarHome);
085 addEnvironmentVariables();
086 addSystemProperties();
087
088 // update deprecated configuration
089 ConfigurationUtils.copyToCommonsConfiguration(properties, deprecatedConfiguration);
090
091 return this;
092 }
093
094 private void loadDatabaseSettings() {
095 if (sessionFactory != null) {
096 List<Property> properties = ConfigurationUtils.getGeneralProperties(sessionFactory);
097 for (Property property : properties) {
098 setProperty(property.getKey(), property.getValue());
099 }
100 }
101 }
102
103 private void loadPropertiesFile(File sonarHome) {
104 File propertiesFile = new File(sonarHome, "conf/sonar.properties");
105 if (!propertiesFile.isFile() || !propertiesFile.exists()) {
106 throw new IllegalStateException("Properties file does not exist: " + propertiesFile);
107 }
108
109 try {
110 Properties p = ConfigurationUtils.openProperties(propertiesFile);
111 p = ConfigurationUtils.interpolateEnvVariables(p);
112 addProperties(p);
113 } catch (Exception e) {
114 throw new IllegalStateException("Fail to load configuration file: " + propertiesFile, e);
115 }
116 }
117
118 static File getDeployDir(ServletContext servletContext) {
119 String dirname = servletContext.getRealPath("/deploy/");
120 if (dirname == null) {
121 throw new IllegalArgumentException("Web app directory not found : /deploy/");
122 }
123 File dir = new File(dirname);
124 if (!dir.exists()) {
125 throw new IllegalArgumentException("Web app directory does not exist: " + dir);
126 }
127 return dir;
128 }
129 }