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.slf4j.Logger;
024 import org.slf4j.LoggerFactory;
025 import org.sonar.api.profiles.RulesProfile;
026 import org.sonar.api.rules.ActiveRule;
027 import org.sonar.api.rules.Rule;
028 import org.sonar.api.utils.SonarException;
029
030 import java.util.ArrayList;
031 import java.util.Collection;
032 import java.util.List;
033
034 /**
035 * This wrapper is used to try to preserve backward compatibility for plugins that used to
036 * depends on {@link org.sonar.api.profiles.RulesProfile}
037 *
038 * @since 4.2
039 */
040 public class RulesProfileWrapper extends RulesProfile {
041
042 private static final Logger LOG = LoggerFactory.getLogger(RulesProfileWrapper.class);
043 private static final String DEPRECATED_USAGE_MESSAGE = "Please update your plugin to support multi-language analysis";
044
045 private final Collection<RulesProfile> profiles;
046 private final RulesProfile singleLanguageProfile;
047
048 public RulesProfileWrapper(Collection<RulesProfile> profiles) {
049 this.profiles = profiles;
050 this.singleLanguageProfile = null;
051 }
052
053 public RulesProfileWrapper(RulesProfile profile) {
054 this.profiles = Lists.newArrayList(profile);
055 this.singleLanguageProfile = profile;
056 }
057
058 @Override
059 public Integer getId() {
060 return getSingleProfileOrFail().getId();
061 }
062
063 private RulesProfile getSingleProfileOrFail() {
064 if (singleLanguageProfile == null) {
065 throw new IllegalStateException(DEPRECATED_USAGE_MESSAGE);
066 }
067 return singleLanguageProfile;
068 }
069
070 @Override
071 public String getName() {
072 return singleLanguageProfile != null ? singleLanguageProfile.getName() : "SonarQube";
073 }
074
075 @Override
076 public String getLanguage() {
077 if (singleLanguageProfile == null) {
078 // Multi-languages module
079 // This is a hack for CommonChecksDecorator that call this method in its constructor
080 LOG.debug(DEPRECATED_USAGE_MESSAGE, new SonarException(DEPRECATED_USAGE_MESSAGE));
081 return "";
082 }
083 return singleLanguageProfile.getLanguage();
084 }
085
086 @Override
087 public List<ActiveRule> getActiveRules() {
088 List<ActiveRule> activeRules = new ArrayList<ActiveRule>();
089 for (RulesProfile profile : profiles) {
090 activeRules.addAll(profile.getActiveRules());
091 }
092 return activeRules;
093 }
094
095 @Override
096 public ActiveRule getActiveRule(String repositoryKey, String ruleKey) {
097 for (RulesProfile profile : profiles) {
098 ActiveRule activeRule = profile.getActiveRule(repositoryKey, ruleKey);
099 if (activeRule != null) {
100 return activeRule;
101 }
102 }
103 return null;
104 }
105
106 @Override
107 public List<ActiveRule> getActiveRulesByRepository(String repositoryKey) {
108 List<ActiveRule> activeRules = new ArrayList<ActiveRule>();
109 for (RulesProfile profile : profiles) {
110 activeRules.addAll(profile.getActiveRulesByRepository(repositoryKey));
111 }
112 return activeRules;
113 }
114
115 @Override
116 public List<ActiveRule> getActiveRules(boolean acceptDisabledRules) {
117 List<ActiveRule> activeRules = new ArrayList<ActiveRule>();
118 for (RulesProfile profile : profiles) {
119 activeRules.addAll(profile.getActiveRules(acceptDisabledRules));
120 }
121 return activeRules;
122 }
123
124 @Override
125 public ActiveRule getActiveRule(Rule rule) {
126 for (RulesProfile profile : profiles) {
127 ActiveRule activeRule = profile.getActiveRule(rule);
128 if (activeRule != null) {
129 return activeRule;
130 }
131 }
132 return null;
133 }
134
135 @Override
136 public Boolean getDefaultProfile() {
137 return getSingleProfileOrFail().getDefaultProfile();
138 }
139
140 }