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.rule;
021    
022    import com.google.common.annotations.VisibleForTesting;
023    import org.apache.commons.lang.StringUtils;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    import org.sonar.api.BatchComponent;
027    import org.sonar.api.batch.fs.FileSystem;
028    import org.sonar.api.config.Settings;
029    import org.sonar.api.utils.MessageException;
030    
031    public class QProfileVerifier implements BatchComponent {
032    
033      private static final Logger LOG = LoggerFactory.getLogger(QProfileVerifier.class);
034    
035      private final Settings settings;
036      private final FileSystem fs;
037      private final ModuleQProfiles profiles;
038    
039      public QProfileVerifier(Settings settings, FileSystem fs, ModuleQProfiles profiles) {
040        this.settings = settings;
041        this.fs = fs;
042        this.profiles = profiles;
043      }
044    
045      public void execute() {
046        execute(LOG);
047      }
048    
049      @VisibleForTesting
050      void execute(Logger logger) {
051        String defaultName = settings.getString(ModuleQProfiles.SONAR_PROFILE_PROP);
052        boolean defaultNameUsed = StringUtils.isBlank(defaultName);
053        for (String lang : fs.languages()) {
054          QProfile profile = profiles.findByLanguage(lang);
055          if (profile == null) {
056            logger.warn("No Quality profile found for language " + lang);
057          } else {
058            logger.info("Quality profile for {}: {}", lang, profile.getName());
059            if (StringUtils.isNotBlank(defaultName) && defaultName.equals(profile.getName())) {
060              defaultNameUsed = true;
061            }
062          }
063        }
064        if (!defaultNameUsed && !fs.languages().isEmpty()) {
065          throw MessageException.of("sonar.profile was set to '" + defaultName + "' but didn't match any profile for any language. Please check your configuration.");
066        }
067      }
068    }