diff options
Diffstat (limited to 'sonar-batch/src/main/java/org/sonar/batch/rule')
7 files changed, 49 insertions, 85 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java index 95762afd79d..a7aea325d48 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java @@ -28,6 +28,8 @@ import org.sonar.api.batch.rule.internal.NewActiveRule; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RuleParam; +import org.sonar.batch.api.rules.QProfile; +import org.sonar.batch.rules.QProfileWithId; import org.sonar.core.qualityprofile.db.ActiveRuleDao; import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; @@ -49,13 +51,14 @@ public class ActiveRulesProvider extends ProviderAdapter { private ActiveRules load(ModuleQProfiles qProfiles, ActiveRuleDao dao, RuleFinder ruleFinder) { ActiveRulesBuilder builder = new ActiveRulesBuilder(); - for (ModuleQProfiles.QProfile qProfile : qProfiles.findAll()) { + for (QProfile qProfile : qProfiles.findAll()) { + QProfileWithId qProfileWithId = (QProfileWithId) qProfile; ListMultimap<Integer, ActiveRuleParamDto> paramDtosByActiveRuleId = ArrayListMultimap.create(); - for (ActiveRuleParamDto dto : dao.selectParamsByProfileId(qProfile.id())) { + for (ActiveRuleParamDto dto : dao.selectParamsByProfileId(qProfileWithId.id())) { paramDtosByActiveRuleId.put(dto.getActiveRuleId(), dto); } - for (ActiveRuleDto activeDto : dao.selectByProfileId(qProfile.id())) { + for (ActiveRuleDto activeDto : dao.selectByProfileId(qProfileWithId.id())) { Rule rule = ruleFinder.findById(activeDto.getRulId()); if (rule != null) { NewActiveRule newActiveRule = builder.activate(rule.ruleKey()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java b/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java index fb5027d923b..674fbb59a56 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/ModuleQProfiles.java @@ -23,11 +23,11 @@ import com.google.common.collect.ImmutableMap; import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchComponent; import org.sonar.api.config.Settings; -import org.sonar.api.resources.Language; -import org.sonar.api.resources.Languages; import org.sonar.api.utils.MessageException; -import org.sonar.core.qualityprofile.db.QualityProfileDao; -import org.sonar.core.qualityprofile.db.QualityProfileDto; +import org.sonar.batch.api.languages.Language; +import org.sonar.batch.api.rules.QProfile; +import org.sonar.batch.languages.LanguagesReferential; +import org.sonar.batch.rules.QProfilesReferential; import javax.annotation.CheckForNull; @@ -41,55 +41,19 @@ public class ModuleQProfiles implements BatchComponent { public static final String SONAR_PROFILE_PROP = "sonar.profile"; - public static class QProfile { - private final String name, language; - private final Integer version; - private final int id; - - public QProfile(QualityProfileDto dto) { - this.id = dto.getId(); - this.name = dto.getName(); - this.language = dto.getLanguage(); - this.version = dto.getVersion(); - } - - QProfile(int id, String name, String language, Integer version) { - this.id = id; - this.name = name; - this.language = language; - this.version = version; - } - - public int id() { - return id; - } - - public String name() { - return name; - } - - public String language() { - return language; - } - - public Integer version() { - return version; - } - } - private final Map<String, QProfile> byLanguage; - public ModuleQProfiles(Settings settings, Languages languages, QualityProfileDao dao) { + public ModuleQProfiles(Settings settings, LanguagesReferential languages, QProfilesReferential qProfileRef) { ImmutableMap.Builder<String, QProfile> builder = ImmutableMap.builder(); String defaultName = settings.getString(SONAR_PROFILE_PROP); for (Language language : languages.all()) { QProfile profile = null; if (StringUtils.isNotBlank(defaultName)) { - profile = loadDefaultQProfile(dao, defaultName, language.getKey()); + profile = loadDefaultQProfile(qProfileRef, defaultName, language.key()); } if (profile == null) { - profile = loadQProfile(dao, settings, language.getKey()); + profile = loadQProfile(qProfileRef, settings, language.key()); } if (profile != null) { builder.put(profile.language(), profile); @@ -99,25 +63,21 @@ public class ModuleQProfiles implements BatchComponent { } @CheckForNull - private QProfile loadQProfile(QualityProfileDao dao, Settings settings, String language) { + private QProfile loadQProfile(QProfilesReferential qProfileRef, Settings settings, String language) { String profileName = settings.getString("sonar.profile." + language); if (profileName != null) { - QualityProfileDto dto = dao.selectByNameAndLanguage(profileName, language); + QProfile dto = qProfileRef.get(language, profileName); if (dto == null) { throw MessageException.of(String.format("Quality profile not found : '%s' on language '%s'", profileName, language)); } - return new QProfile(dto); + return dto; } return null; } @CheckForNull - private QProfile loadDefaultQProfile(QualityProfileDao dao, String profileName, String language) { - QualityProfileDto dto = dao.selectByNameAndLanguage(profileName, language); - if (dto != null) { - return new QProfile(dto); - } - return null; + private QProfile loadDefaultQProfile(QProfilesReferential qProfileRef, String profileName, String language) { + return qProfileRef.get(language, profileName); } public Collection<QProfile> findAll() { diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java index ee67fcb31cd..e84dc342c17 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileEventsDecorator.java @@ -34,8 +34,7 @@ import org.sonar.api.resources.Languages; import org.sonar.api.resources.Project; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Resource; -import org.sonar.api.resources.ResourceUtils; -import org.sonar.batch.rule.ModuleQProfiles.QProfile; +import org.sonar.batch.rules.QProfileWithId; import org.sonar.core.qualityprofile.db.QualityProfileDao; import org.sonar.core.qualityprofile.db.QualityProfileDto; @@ -101,14 +100,14 @@ public class QProfileEventsDecorator implements Decorator { } else { pastProfileVersion = pastProfileVersionMeasure.getIntValue(); } - pastProfiles = UsedQProfiles.fromProfiles(new ModuleQProfiles.QProfile(pastProfileId, pastProfileName, pastProfileLanguage, pastProfileVersion)); + pastProfiles = UsedQProfiles.fromProfiles(new QProfileWithId(pastProfileId, pastProfileName, pastProfileLanguage, pastProfileVersion)); } // Now create appropriate events - Map<Integer, QProfile> pastProfilesById = Maps.newHashMap(pastProfiles.profilesById()); - for (QProfile profile : currentProfiles.profilesById().values()) { + Map<Integer, QProfileWithId> pastProfilesById = Maps.newHashMap(pastProfiles.profilesById()); + for (QProfileWithId profile : currentProfiles.profilesById().values()) { if (pastProfilesById.containsKey(profile.id())) { - QProfile pastProfile = pastProfilesById.get(profile.id()); + QProfileWithId pastProfile = pastProfilesById.get(profile.id()); if (pastProfile.version() < profile.version()) { // New version of the same QP usedProfile(context, profile); @@ -118,25 +117,25 @@ public class QProfileEventsDecorator implements Decorator { usedProfile(context, profile); } } - for (QProfile profile : pastProfilesById.values()) { + for (QProfileWithId profile : pastProfilesById.values()) { // Following profiles are no more used stopUsedProfile(context, profile); } } - private void stopUsedProfile(DecoratorContext context, QProfile profile) { + private void stopUsedProfile(DecoratorContext context, QProfileWithId profile) { Language language = languages.get(profile.language()); String languageName = language != null ? language.getName() : profile.language(); context.createEvent("Stop using " + format(profile) + " (" + languageName + ")", format(profile) + " no more used for " + languageName, Event.CATEGORY_PROFILE, null); } - private void usedProfile(DecoratorContext context, QProfile profile) { + private void usedProfile(DecoratorContext context, QProfileWithId profile) { Language language = languages.get(profile.language()); String languageName = language != null ? language.getName() : profile.language(); context.createEvent("Use " + format(profile) + " (" + languageName + ")", format(profile) + " used for " + languageName, Event.CATEGORY_PROFILE, null); } - private String format(QProfile profile) { + private String format(QProfileWithId profile) { return profile.name() + " version " + profile.version(); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java index 0773a3c6472..620f769d8b7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileSensor.java @@ -26,6 +26,7 @@ import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.resources.Project; +import org.sonar.batch.rules.QProfileWithId; import org.sonar.core.qualityprofile.db.QualityProfileDao; import java.util.List; @@ -51,9 +52,9 @@ public class QProfileSensor implements Sensor { } public void analyse(Project project, SensorContext context) { - List<ModuleQProfiles.QProfile> profiles = Lists.newArrayList(); + List<QProfileWithId> profiles = Lists.newArrayList(); for (String language : fs.languages()) { - ModuleQProfiles.QProfile qProfile = moduleQProfiles.findByLanguage(language); + QProfileWithId qProfile = (QProfileWithId) moduleQProfiles.findByLanguage(language); if (qProfile != null) { dao.updateUsedColumn(qProfile.id(), true); profiles.add(qProfile); @@ -65,7 +66,7 @@ public class QProfileSensor implements Sensor { // For backward compatibility if (profiles.size() == 1) { - ModuleQProfiles.QProfile qProfile = profiles.get(0); + QProfileWithId qProfile = profiles.get(0); Measure measure = new Measure(CoreMetrics.PROFILE, qProfile.name()).setValue((double) qProfile.id()); Measure measureVersion = new Measure(CoreMetrics.PROFILE_VERSION, qProfile.version().doubleValue()); context.saveMeasure(measure); diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileVerifier.java b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileVerifier.java index ac8e51eb10e..10e110b6ee9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileVerifier.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/QProfileVerifier.java @@ -27,7 +27,7 @@ import org.sonar.api.BatchComponent; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.config.Settings; import org.sonar.api.utils.MessageException; -import org.sonar.batch.rule.ModuleQProfiles.QProfile; +import org.sonar.batch.api.rules.QProfile; public class QProfileVerifier implements BatchComponent { diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java index 2b778be21dc..2c5ec75a235 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/RulesProfileProvider.java @@ -30,6 +30,7 @@ import org.sonar.api.rules.ActiveRule; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RulePriority; +import org.sonar.batch.api.rules.QProfile; import java.util.Collection; import java.util.Map; @@ -55,8 +56,8 @@ public class RulesProfileProvider extends ProviderAdapter { } private RulesProfile loadSingleLanguageProfile(ModuleQProfiles qProfiles, ActiveRules activeRules, - RuleFinder ruleFinder, String language) { - ModuleQProfiles.QProfile qProfile = qProfiles.findByLanguage(language); + RuleFinder ruleFinder, String language) { + QProfile qProfile = qProfiles.findByLanguage(language); if (qProfile != null) { return new RulesProfileWrapper(select(qProfile, activeRules, ruleFinder)); } @@ -65,13 +66,13 @@ public class RulesProfileProvider extends ProviderAdapter { private RulesProfile loadProfiles(ModuleQProfiles qProfiles, ActiveRules activeRules, RuleFinder ruleFinder) { Collection<RulesProfile> dtos = Lists.newArrayList(); - for (ModuleQProfiles.QProfile qProfile : qProfiles.findAll()) { + for (QProfile qProfile : qProfiles.findAll()) { dtos.add(select(qProfile, activeRules, ruleFinder)); } return new RulesProfileWrapper(dtos); } - private RulesProfile select(ModuleQProfiles.QProfile qProfile, ActiveRules activeRules, RuleFinder ruleFinder) { + private RulesProfile select(QProfile qProfile, ActiveRules activeRules, RuleFinder ruleFinder) { RulesProfile deprecatedProfile = new RulesProfile(); deprecatedProfile.setVersion(qProfile.version()); deprecatedProfile.setName(qProfile.name()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/UsedQProfiles.java b/sonar-batch/src/main/java/org/sonar/batch/rule/UsedQProfiles.java index 1b1115ed18b..5973a74c593 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/UsedQProfiles.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/UsedQProfiles.java @@ -26,7 +26,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.sonar.api.utils.text.JsonWriter; -import org.sonar.batch.rule.ModuleQProfiles.QProfile; +import org.sonar.batch.rules.QProfileWithId; import javax.annotation.concurrent.Immutable; @@ -37,14 +37,14 @@ import java.util.Map; @Immutable public class UsedQProfiles { - private Map<Integer, ModuleQProfiles.QProfile> profilesById = Maps.newLinkedHashMap(); + private Map<Integer, QProfileWithId> profilesById = Maps.newLinkedHashMap(); private UsedQProfiles() { } - public static final UsedQProfiles fromProfiles(Iterable<QProfile> profiles) { + public static final UsedQProfiles fromProfiles(Iterable<QProfileWithId> profiles) { UsedQProfiles result = new UsedQProfiles(); - for (QProfile qProfile : profiles) { + for (QProfileWithId qProfile : profiles) { result.add(qProfile); } return result; @@ -54,7 +54,7 @@ public class UsedQProfiles { return new UsedQProfiles(); } - public static final UsedQProfiles fromProfiles(QProfile... profiles) { + public static final UsedQProfiles fromProfiles(QProfileWithId... profiles) { return fromProfiles(Arrays.asList(profiles)); } @@ -63,7 +63,7 @@ public class UsedQProfiles { JsonArray root = new JsonParser().parse(json).getAsJsonArray(); for (JsonElement elt : root) { JsonObject profile = elt.getAsJsonObject(); - result.add(new QProfile(profile.get("id").getAsInt(), profile.get("name").getAsString(), profile.get("language").getAsString(), profile.get("version").getAsInt())); + result.add(new QProfileWithId(profile.get("id").getAsInt(), profile.get("name").getAsString(), profile.get("language").getAsString(), profile.get("version").getAsInt())); } return result; } @@ -72,7 +72,7 @@ public class UsedQProfiles { StringWriter json = new StringWriter(); JsonWriter writer = JsonWriter.of(json); writer.beginArray(); - for (ModuleQProfiles.QProfile qProfile : profilesById.values()) { + for (QProfileWithId qProfile : profilesById.values()) { writer.beginObject() .prop("id", qProfile.id()) .prop("name", qProfile.name()) @@ -89,8 +89,8 @@ public class UsedQProfiles { return empty().mergeInPlace(this).mergeInPlace(other); } - private void add(ModuleQProfiles.QProfile profile) { - QProfile alreadyAdded = profilesById.get(profile.id()); + private void add(QProfileWithId profile) { + QProfileWithId alreadyAdded = profilesById.get(profile.id()); if (alreadyAdded == null // Keep only latest version || profile.version() > alreadyAdded.version()) { @@ -98,8 +98,8 @@ public class UsedQProfiles { } } - private UsedQProfiles addAll(Iterable<QProfile> profiles) { - for (QProfile profile : profiles) { + private UsedQProfiles addAll(Iterable<QProfileWithId> profiles) { + for (QProfileWithId profile : profiles) { this.add(profile); } return this; @@ -110,7 +110,7 @@ public class UsedQProfiles { return this; } - public Map<Integer, ModuleQProfiles.QProfile> profilesById() { + public Map<Integer, QProfileWithId> profilesById() { return ImmutableMap.copyOf(profilesById); } |