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 org.apache.commons.configuration.Configuration;
023 import org.slf4j.Logger;
024 import org.slf4j.LoggerFactory;
025 import org.sonar.api.CoreProperties;
026 import org.sonar.api.batch.bootstrap.ProjectReactor;
027 import org.sonar.api.config.PropertyDefinitions;
028 import org.sonar.api.config.Settings;
029 import org.sonar.api.utils.MessageException;
030 import org.sonar.batch.bootstrap.AnalysisMode;
031 import org.sonar.batch.bootstrap.GlobalSettings;
032 import org.sonar.batch.protocol.input.ProjectReferentials;
033
034 import javax.annotation.Nullable;
035
036 public class ProjectSettings extends Settings {
037
038 private static final Logger LOG = LoggerFactory.getLogger(ProjectSettings.class);
039
040 private Configuration deprecatedConfiguration;
041
042 private final GlobalSettings globalSettings;
043 private final ProjectReferentials projectReferentials;
044 private final AnalysisMode mode;
045
046 public ProjectSettings(ProjectReactor reactor, GlobalSettings globalSettings, PropertyDefinitions propertyDefinitions,
047 ProjectReferentials projectReferentials, Configuration deprecatedConfiguration, AnalysisMode mode) {
048 super(propertyDefinitions);
049 this.mode = mode;
050 getEncryption().setPathToSecretKey(globalSettings.getString(CoreProperties.ENCRYPTION_SECRET_KEY_PATH));
051 this.globalSettings = globalSettings;
052 this.projectReferentials = projectReferentials;
053 this.deprecatedConfiguration = deprecatedConfiguration;
054 init(reactor);
055 }
056
057 private void init(ProjectReactor reactor) {
058 LOG.info("Load project settings");
059
060 addProperties(globalSettings.getProperties());
061
062 addProperties(projectReferentials.settings(reactor.getRoot().getKeyWithBranch()));
063
064 addProperties(reactor.getRoot().getProperties());
065 }
066
067 @Override
068 protected void doOnSetProperty(String key, @Nullable String value) {
069 deprecatedConfiguration.setProperty(key, value);
070 }
071
072 @Override
073 protected void doOnRemoveProperty(String key) {
074 deprecatedConfiguration.clearProperty(key);
075 }
076
077 @Override
078 protected void doOnClearProperties() {
079 deprecatedConfiguration.clear();
080 }
081
082 @Override
083 protected void doOnGetProperties(String key) {
084 if (mode.isPreview() && key.endsWith(".secured") && !key.contains(".license")) {
085 throw MessageException.of("Access to the secured property '" + key
086 + "' is not possible in preview mode. The SonarQube plugin which requires this property must be deactivated in preview mode.");
087 }
088 }
089 }