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.collect.Lists;
023    import org.apache.commons.lang.StringUtils;
024    import org.picocontainer.injectors.ProviderAdapter;
025    import org.sonar.api.CoreProperties;
026    import org.sonar.api.batch.rule.ActiveRules;
027    import org.sonar.api.batch.rule.internal.DefaultActiveRules;
028    import org.sonar.api.config.Settings;
029    import org.sonar.api.profiles.RulesProfile;
030    import org.sonar.api.rules.ActiveRule;
031    import org.sonar.api.rules.Rule;
032    import org.sonar.api.rules.RuleFinder;
033    import org.sonar.api.rules.RulePriority;
034    
035    import java.util.Collection;
036    import java.util.Map;
037    
038    /**
039     * Ensures backward-compatibility with extensions that use {@link org.sonar.api.profiles.RulesProfile}.
040     */
041    public class RulesProfileProvider extends ProviderAdapter {
042    
043      private RulesProfile singleton = null;
044    
045      public RulesProfile provide(ModuleQProfiles qProfiles, ActiveRules activeRules, RuleFinder ruleFinder, Settings settings) {
046        if (singleton == null) {
047          String lang = settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY);
048          if (StringUtils.isNotBlank(lang)) {
049            // Backward-compatibility with single-language modules
050            singleton = loadSingleLanguageProfile(qProfiles, activeRules, ruleFinder, lang);
051          } else {
052            singleton = loadProfiles(qProfiles, activeRules, ruleFinder);
053          }
054        }
055        return singleton;
056      }
057    
058      private RulesProfile loadSingleLanguageProfile(ModuleQProfiles qProfiles, ActiveRules activeRules,
059        RuleFinder ruleFinder, String language) {
060        QProfile qProfile = qProfiles.findByLanguage(language);
061        if (qProfile != null) {
062          return new RulesProfileWrapper(select(qProfile, activeRules, ruleFinder));
063        }
064        return new RulesProfileWrapper(Lists.<RulesProfile>newArrayList());
065      }
066    
067      private RulesProfile loadProfiles(ModuleQProfiles qProfiles, ActiveRules activeRules, RuleFinder ruleFinder) {
068        Collection<RulesProfile> dtos = Lists.newArrayList();
069        for (QProfile qProfile : qProfiles.findAll()) {
070          dtos.add(select(qProfile, activeRules, ruleFinder));
071        }
072        return new RulesProfileWrapper(dtos);
073      }
074    
075      private RulesProfile select(QProfile qProfile, ActiveRules activeRules, RuleFinder ruleFinder) {
076        RulesProfile deprecatedProfile = new RulesProfile();
077        // TODO deprecatedProfile.setVersion(qProfile.version());
078        deprecatedProfile.setName(qProfile.getName());
079        deprecatedProfile.setLanguage(qProfile.getLanguage());
080        for (org.sonar.api.batch.rule.ActiveRule activeRule : ((DefaultActiveRules) activeRules).findByLanguage(qProfile.getLanguage())) {
081          Rule rule = ruleFinder.findByKey(activeRule.ruleKey());
082          ActiveRule deprecatedActiveRule = deprecatedProfile.activateRule(rule, RulePriority.valueOf(activeRule.severity()));
083          for (Map.Entry<String, String> param : activeRule.params().entrySet()) {
084            deprecatedActiveRule.setParameter(param.getKey(), param.getValue());
085          }
086        }
087        return deprecatedProfile;
088      }
089    
090    }