001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.batch.scan;
021
022 import com.google.common.collect.Lists;
023 import org.apache.commons.configuration.Configuration;
024 import org.slf4j.LoggerFactory;
025 import org.sonar.api.CoreProperties;
026 import org.sonar.api.batch.bootstrap.ProjectDefinition;
027 import org.sonar.api.config.Settings;
028 import org.sonar.api.utils.MessageException;
029 import org.sonar.batch.bootstrap.AnalysisMode;
030 import org.sonar.batch.bootstrap.GlobalSettings;
031 import org.sonar.batch.protocol.input.ProjectReferentials;
032
033 import javax.annotation.Nullable;
034
035 import java.util.List;
036
037 /**
038 * @since 2.12
039 */
040 public class ModuleSettings extends Settings {
041
042 private final Configuration deprecatedCommonsConf;
043 private final ProjectReferentials projectReferentials;
044 private AnalysisMode analysisMode;
045
046 public ModuleSettings(GlobalSettings batchSettings, ProjectDefinition project, Configuration deprecatedCommonsConf, ProjectReferentials projectReferentials,
047 AnalysisMode analysisMode) {
048 super(batchSettings.getDefinitions());
049 this.projectReferentials = projectReferentials;
050 this.analysisMode = analysisMode;
051 getEncryption().setPathToSecretKey(batchSettings.getString(CoreProperties.ENCRYPTION_SECRET_KEY_PATH));
052
053 LoggerFactory.getLogger(ModuleSettings.class).info("Load module settings");
054 this.deprecatedCommonsConf = deprecatedCommonsConf;
055 init(project, batchSettings);
056 }
057
058 private ModuleSettings init(ProjectDefinition project, GlobalSettings batchSettings) {
059 addProjectProperties(project, batchSettings);
060 addBuildProperties(project);
061 return this;
062 }
063
064 private void addProjectProperties(ProjectDefinition project, GlobalSettings batchSettings) {
065 addProperties(batchSettings.getProperties());
066 addProperties(projectReferentials.settings(project.getKeyWithBranch()));
067 }
068
069 private void addBuildProperties(ProjectDefinition project) {
070 List<ProjectDefinition> orderedProjects = getTopDownParentProjects(project);
071 for (ProjectDefinition p : orderedProjects) {
072 addProperties(p.getProperties());
073 }
074 }
075
076 /**
077 * From root to given project
078 */
079 static List<ProjectDefinition> getTopDownParentProjects(ProjectDefinition project) {
080 List<ProjectDefinition> result = Lists.newArrayList();
081 ProjectDefinition p = project;
082 while (p != null) {
083 result.add(0, p);
084 p = p.getParent();
085 }
086 return result;
087 }
088
089 @Override
090 protected void doOnSetProperty(String key, @Nullable String value) {
091 deprecatedCommonsConf.setProperty(key, value);
092 }
093
094 @Override
095 protected void doOnRemoveProperty(String key) {
096 deprecatedCommonsConf.clearProperty(key);
097 }
098
099 @Override
100 protected void doOnClearProperties() {
101 deprecatedCommonsConf.clear();
102 }
103
104 @Override
105 protected void doOnGetProperties(String key) {
106 if (analysisMode.isPreview() && key.endsWith(".secured") && !key.contains(".license")) {
107 throw MessageException.of("Access to the secured property '" + key
108 + "' is not possible in preview mode. The SonarQube plugin which requires this property must be deactivated in preview mode.");
109 }
110 }
111 }