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