From: Fabrice Bellingard Date: Wed, 21 Mar 2012 09:43:28 +0000 (+0100) Subject: SONAR-2977 Stop Sonar if more than 1 default profile for a language X-Git-Tag: 3.0~149 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=06a9f756793c6403b4bf67d5b61e86a7b57e404e;p=sonarqube.git SONAR-2977 Stop Sonar if more than 1 default profile for a language --- diff --git a/sonar-server/src/main/java/org/sonar/server/startup/ActivateDefaultProfiles.java b/sonar-server/src/main/java/org/sonar/server/startup/ActivateDefaultProfiles.java index f597ee3214b..29d76befbdf 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/ActivateDefaultProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/ActivateDefaultProfiles.java @@ -70,6 +70,10 @@ public final class ActivateDefaultProfiles { profileToActivate = profiles.get(0); } } + activateProfileIfNeeded(session, profileToActivate, oneProfileIsActivated); + } + + private void activateProfileIfNeeded(DatabaseSession session, RulesProfile profileToActivate, boolean oneProfileIsActivated) { if (!oneProfileIsActivated && profileToActivate != null) { profileToActivate.setDefaultProfile(true); session.saveWithoutFlush(profileToActivate); diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java index b9a803b49d8..d8d504a0f2c 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterProvidedProfiles.java @@ -19,8 +19,12 @@ */ package org.sonar.server.startup; -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; @@ -28,16 +32,19 @@ import org.slf4j.LoggerFactory; import org.sonar.api.database.DatabaseSession; import org.sonar.api.profiles.ProfileDefinition; import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.*; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleFinder; +import org.sonar.api.rules.RuleParam; +import org.sonar.api.rules.RuleQuery; +import org.sonar.api.utils.SonarException; import org.sonar.api.utils.TimeProfiler; import org.sonar.api.utils.ValidationMessages; import org.sonar.jpa.session.DatabaseSessionFactory; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Set; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; public final class RegisterProvidedProfiles { @@ -72,19 +79,34 @@ public final class RegisterProvidedProfiles { profiler.stop(); } - List createProfiles() { + private List createProfiles() { List result = Lists.newArrayList(); + Map defaultProfilesByLanguage = Maps.newHashMap(); for (ProfileDefinition definition : definitions) { ValidationMessages validation = ValidationMessages.create(); RulesProfile profile = definition.createProfile(validation); validation.log(LOGGER); if (profile != null && !validation.hasErrors()) { result.add(profile); + checkIfNoMoreThanOneDefaultProfile(defaultProfilesByLanguage, profile); } } return result; } + private void checkIfNoMoreThanOneDefaultProfile(Map defaultProfilesByLanguage, RulesProfile profile) { + if (profile.getDefaultProfile()) { + RulesProfile defaultProfileForLanguage = defaultProfilesByLanguage.get(profile.getLanguage()); + if (defaultProfileForLanguage == null) { + defaultProfilesByLanguage.put(profile.getLanguage(), profile); + } else { + throw new SonarException("Language " + profile.getLanguage() + " can't have 2 default provided profiles: " + + profile.getName() + " and " + + defaultProfileForLanguage.getName()); + } + } + } + private void cleanProvidedProfiles(List profiles, DatabaseSession session) { TimeProfiler profiler = new TimeProfiler().start("Clean provided profiles"); List existingProfiles = session.getResults(RulesProfile.class, "provided", true);