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