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.batch.config;
021
022 import com.google.common.collect.Lists;
023 import org.apache.commons.configuration.Configuration;
024 import org.sonar.api.batch.bootstrap.ProjectDefinition;
025 import org.sonar.api.config.PropertyDefinitions;
026 import org.sonar.api.config.Settings;
027 import org.sonar.api.database.configuration.Property;
028 import org.sonar.api.resources.Project;
029 import org.sonar.core.config.ConfigurationUtils;
030 import org.sonar.jpa.session.DatabaseSessionFactory;
031
032 import java.util.Iterator;
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 // order is important -> bottom-up. The last one overrides all the others.
056 loadDatabaseGlobalSettings();
057 loadDatabaseProjectSettings(projectDefinition);
058 loadBuildProperties();
059 addEnvironmentVariables();
060 addSystemProperties();
061
062 updateDeprecatedCommonsConfiguration();
063
064 return this;
065 }
066
067 private void loadBuildProperties() {
068 List<ProjectDefinition> orderedProjects = getOrderedProjects(projectDefinition);
069 for (ProjectDefinition p : orderedProjects) {
070 addProperties(p.getProperties());
071 }
072 }
073
074 private void loadDatabaseProjectSettings(ProjectDefinition projectDef) {
075 if (projectDef.getParent() != null) {
076 loadDatabaseProjectSettings(projectDef.getParent());
077 }
078 List<Property> props = ConfigurationUtils.getProjectProperties(dbFactory, projectDef.getKey());
079 for (Property dbProperty : props) {
080 setProperty(dbProperty.getKey(), dbProperty.getValue());
081 }
082 }
083
084 private void loadDatabaseGlobalSettings() {
085 List<Property> props = ConfigurationUtils.getGeneralProperties(dbFactory);
086 for (Property dbProperty : props) {
087 setProperty(dbProperty.getKey(), dbProperty.getValue());
088 }
089 }
090
091 private void updateDeprecatedCommonsConfiguration() {
092 ConfigurationUtils.copyToCommonsConfiguration(properties, deprecatedCommonsConf);
093 }
094
095 /**
096 * From root to module
097 */
098 static List<ProjectDefinition> getOrderedProjects(ProjectDefinition project) {
099 List<ProjectDefinition> result = Lists.newArrayList();
100 ProjectDefinition pd = project;
101 while (pd != null) {
102 result.add(0, pd);
103 pd = pd.getParent();
104 }
105 return result;
106 }
107 }