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 */
020 package org.sonar.batch.config;
021
022 import com.google.common.collect.Lists;
023 import org.apache.commons.configuration.Configuration;
024 import org.sonar.api.CoreProperties;
025 import org.sonar.api.batch.bootstrap.ProjectDefinition;
026 import org.sonar.api.config.PropertyDefinitions;
027 import org.sonar.api.config.Settings;
028 import org.sonar.api.database.configuration.Property;
029 import org.sonar.api.resources.Project;
030 import org.sonar.core.config.ConfigurationUtils;
031 import org.sonar.jpa.session.DatabaseSessionFactory;
032
033 import java.util.List;
034
035 /**
036 * @since 2.12
037 */
038 public class ProjectSettings extends Settings {
039
040 private Configuration deprecatedCommonsConf;
041 private ProjectDefinition projectDefinition;
042 private DatabaseSessionFactory dbFactory;
043
044 public ProjectSettings(PropertyDefinitions definitions, ProjectDefinition projectDefinition, DatabaseSessionFactory dbFactory, Project project) {
045 super(definitions);
046 this.deprecatedCommonsConf = project.getConfiguration(); // Configuration is not a parameter to be sure that the project conf is used, not the global one
047 this.projectDefinition = projectDefinition;
048 this.dbFactory = dbFactory;
049 load();
050 }
051
052 public ProjectSettings load() {
053 clear();
054
055 // hack to obtain "sonar.branch" before loading settings from database
056 loadBuildProperties();
057 addEnvironmentVariables();
058 addSystemProperties();
059 String branch = getString(CoreProperties.PROJECT_BRANCH_PROPERTY);
060 clear();
061
062 // order is important -> bottom-up. The last one overrides all the others.
063 loadDatabaseGlobalSettings();
064 loadDatabaseProjectSettings(projectDefinition, branch);
065 loadBuildProperties();
066 addEnvironmentVariables();
067 addSystemProperties();
068
069 updateDeprecatedCommonsConfiguration();
070
071 return this;
072 }
073
074 private void loadBuildProperties() {
075 List<ProjectDefinition> orderedProjects = getOrderedProjects(projectDefinition);
076 for (ProjectDefinition p : orderedProjects) {
077 addProperties(p.getProperties());
078 }
079 }
080
081 private void loadDatabaseProjectSettings(ProjectDefinition projectDef, String branch) {
082 if (projectDef.getParent() != null) {
083 loadDatabaseProjectSettings(projectDef.getParent(), branch);
084 }
085 List<Property> props = ConfigurationUtils.getProjectProperties(dbFactory, projectDef.getKey(), branch);
086 for (Property dbProperty : props) {
087 setProperty(dbProperty.getKey(), dbProperty.getValue());
088 }
089 }
090
091 private void loadDatabaseGlobalSettings() {
092 List<Property> props = ConfigurationUtils.getGeneralProperties(dbFactory);
093 for (Property dbProperty : props) {
094 setProperty(dbProperty.getKey(), dbProperty.getValue());
095 }
096 }
097
098 private void updateDeprecatedCommonsConfiguration() {
099 ConfigurationUtils.copyToCommonsConfiguration(properties, deprecatedCommonsConf);
100 }
101
102 /**
103 * From root to module
104 */
105 static List<ProjectDefinition> getOrderedProjects(ProjectDefinition project) {
106 List<ProjectDefinition> result = Lists.newArrayList();
107 ProjectDefinition pd = project;
108 while (pd != null) {
109 result.add(0, pd);
110 pd = pd.getParent();
111 }
112 return result;
113 }
114 }