org.sonar.api.rules.RulesManager Maven / Gradle / Ivy
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2009 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.api.rules;
import org.apache.commons.collections.CollectionUtils;
import org.sonar.api.BatchExtension;
import org.sonar.api.Plugin;
import org.sonar.api.Plugins;
import org.sonar.api.database.daos.DaoFacade;
import org.sonar.api.database.daos.RulesDao;
import org.sonar.api.resources.Language;
import java.util.*;
/**
* A class to manage and access rules defined in Sonar.
* UGLY CLASS - WILL BE COMPLETELY REFACTORED IN SONAR 2.2
*
*/
public class RulesManager implements BatchExtension {
private final Set languages;
private final RulesRepository[] repositories;
private final Map>> rulesByLanguage;
private final Map> pluginsByLanguage;
private final Map> rulesByPluginAndKey = new HashMap>();
private final RulesDao rulesDao;
private final Plugins plugins;
/**
* Creates a RuleManager
* @param plugins the plugins dictionnary
* @param repositories the repositories of rules
* @param dao the dao object
*/
public RulesManager(Plugins plugins, RulesRepository[] repositories, DaoFacade dao) {
this.plugins = plugins;
this.rulesDao = dao.getRulesDao();
languages = new HashSet();
rulesByLanguage = new HashMap>>();
pluginsByLanguage = new HashMap>();
this.repositories = repositories;
for (RulesRepository repository : repositories) {
languages.add(repository.getLanguage());
List> list = rulesByLanguage.get(repository.getLanguage());
if (list == null) {
list = new ArrayList>();
rulesByLanguage.put(repository.getLanguage(), list);
}
list.add(repository);
List languagePlugins = pluginsByLanguage.get(repository.getLanguage());
if (languagePlugins == null) {
languagePlugins = new ArrayList();
pluginsByLanguage.put(repository.getLanguage(), languagePlugins);
}
languagePlugins.add(plugins.getPluginByExtension(repository));
}
}
/**
* Constructor for tests only
*
* @param dao the dao
*/
protected RulesManager(DaoFacade dao, Plugins plugins) {
this.rulesDao = dao.getRulesDao();
this.plugins = plugins;
languages = new HashSet();
rulesByLanguage = new HashMap>>();
pluginsByLanguage = new HashMap>();
repositories = null;
}
/**
* Returns the list of languages for which there is a rule repository
*
* @return a Set of languages
*/
public Set getLanguages() {
return languages;
}
/**
* Gets the list of Rules Repositories available for a language
*
* @param language the language
* @return the list of rules repositories
*/
public List> getRulesRepositories(Language language) {
List> rulesRepositories = rulesByLanguage.get(language);
if (CollectionUtils.isNotEmpty(rulesRepositories)) {
return rulesRepositories;
}
return Collections.emptyList();
}
/**
* Gets the complete list of Rules Repositories in the Sonar instance
*
* @return the list of rules repositories
*/
public List> getRulesRepositories() {
return Arrays.asList(repositories);
}
/**
* Gets the list of rules plugins for a given language
* @param language the language
* @return the list of plugins
*/
public List getPlugins(Language language) {
List result = pluginsByLanguage.get(language);
if (!CollectionUtils.isEmpty(result)) {
return result;
}
return Collections.emptyList();
}
/**
* Gets count of rules by categories defined for a given language
*
* @param language the language
* @return a Map with the category as key and the count as value
*/
public Map countRulesByCategory(Language language) {
return countRulesByCategory(language, rulesDao);
}
protected Map countRulesByCategory(Language language, RulesDao rulesDao) {
Map countByCategory = new HashMap();
List result = getPlugins(language);
if (!CollectionUtils.isEmpty(result)) {
List keys = getPluginKeys(getPlugins(language));
for (RulesCategory rulesCategory : rulesDao.getCategories()) {
Long rulesCount = rulesDao.countRules(keys, rulesCategory.getName());
countByCategory.put(rulesCategory.getName(), rulesCount);
}
}
return countByCategory;
}
private List getPluginKeys(List plugins) {
ArrayList keys = new ArrayList();
for (Plugin plugin : plugins) {
keys.add(plugin.getKey());
}
return keys;
}
/**
* Get the list of rules plugin that implement a mechanism of export for a given language
*
* @param language the language
* @return the list of plugins
*/
public List getExportablePlugins(Language language) {
List targets = new ArrayList();
List> rulesRepositories = getRulesRepositories(language);
for (RulesRepository repository : rulesRepositories) {
if (repository instanceof ConfigurationExportable) {
targets.add(plugins.getPluginByExtension(repository));
}
}
return targets;
}
/**
* Get the list of rules plugin that implement a mechanism of import for a given language
*
* @param language the language
* @return the list of plugins
*/
public List getImportablePlugins(Language language) {
List targets = new ArrayList();
for (RulesRepository repository : getRulesRepositories(language)) {
if (repository instanceof ConfigurationImportable) {
targets.add(plugins.getPluginByExtension(repository));
}
}
return targets;
}
/**
* Gets a list of rules indexed by their key for a given plugin
* @param pluginKey the plugin key
* @return a Map with the rule key and the rule
*/
public Map getPluginRulesIndexedByKey(String pluginKey) {
Map rulesByKey = rulesByPluginAndKey.get(pluginKey);
if (rulesByKey == null) {
rulesByKey = new HashMap();
List rules = rulesDao.getRulesByPlugin(pluginKey);
if (rules != null) {
for (Rule rule : rules) {
rulesByKey.put(rule.getKey(), rule);
}
}
rulesByPluginAndKey.put(pluginKey, rulesByKey);
}
return rulesByKey;
}
/**
* Gets a collection of rules belonging to a plugin
*
* @param pluginKey the plugin key
* @return the collection of rules
*/
public Collection getPluginRules(String pluginKey) {
Map rulesByKey = getPluginRulesIndexedByKey(pluginKey);
return rulesByKey.values();
}
/**
* Gets a rule belonging to a defined plugin based on its key
*
* @param pluginKey the plugin key
* @param ruleKey the rule key
* @return the rule
*/
public Rule getPluginRule(String pluginKey, String ruleKey) {
Map rulesByKey = getPluginRulesIndexedByKey(pluginKey);
return rulesByKey.get(ruleKey);
}
}