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.List;
033    
034    /**
035     * @since 2.12
036     */
037    public class ProjectSettings extends Settings {
038    
039      private Configuration deprecatedCommonsConf;
040      private ProjectDefinition projectDefinition;
041      private DatabaseSessionFactory dbFactory;
042    
043      public ProjectSettings(PropertyDefinitions definitions, ProjectDefinition projectDefinition, DatabaseSessionFactory dbFactory, Project project) {
044        super(definitions);
045        this.deprecatedCommonsConf = project.getConfiguration(); // Configuration is not a parameter to be sure that the project conf is used, not the global one
046        this.projectDefinition = projectDefinition;
047        this.dbFactory = dbFactory;
048        load();
049      }
050    
051      public ProjectSettings load() {
052        clear();
053    
054        // order is important -> bottom-up. The last one overrides all the others.
055        loadDatabaseGlobalSettings();
056        loadDatabaseProjectSettings(projectDefinition);
057        loadBuildProperties();
058        addEnvironmentVariables();
059        addSystemProperties();
060    
061        updateDeprecatedCommonsConfiguration();
062    
063        return this;
064      }
065    
066      private void loadBuildProperties() {
067        List<ProjectDefinition> orderedProjects = getOrderedProjects(projectDefinition);
068        for (ProjectDefinition p : orderedProjects) {
069          addProperties(p.getProperties());
070        }
071      }
072    
073      private void loadDatabaseProjectSettings(ProjectDefinition projectDef) {
074        if (projectDef.getParent() != null) {
075          loadDatabaseProjectSettings(projectDef.getParent());
076        }
077        List<Property> props = ConfigurationUtils.getProjectProperties(dbFactory, projectDef.getKey());
078        for (Property dbProperty : props) {
079          setProperty(dbProperty.getKey(), dbProperty.getValue());
080        }
081      }
082    
083      private void loadDatabaseGlobalSettings() {
084        List<Property> props = ConfigurationUtils.getGeneralProperties(dbFactory);
085        for (Property dbProperty : props) {
086          setProperty(dbProperty.getKey(), dbProperty.getValue());
087        }
088      }
089    
090      private void updateDeprecatedCommonsConfiguration() {
091        ConfigurationUtils.copyToCommonsConfiguration(properties, deprecatedCommonsConf);
092      }
093    
094      /**
095       * From root to module
096       */
097      static List<ProjectDefinition> getOrderedProjects(ProjectDefinition project) {
098        List<ProjectDefinition> result = Lists.newArrayList();
099        ProjectDefinition pd = project;
100        while (pd != null) {
101          result.add(0, pd);
102          pd = pd.getParent();
103        }
104        return result;
105      }
106    }