You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BuiltInQProfileDefinitionsBridge.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.qualityprofile;
  21. import com.google.common.collect.ImmutableList;
  22. import java.util.List;
  23. import org.sonar.api.profiles.ProfileDefinition;
  24. import org.sonar.api.profiles.RulesProfile;
  25. import org.sonar.api.rules.ActiveRuleParam;
  26. import org.sonar.api.rules.RulePriority;
  27. import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
  28. import org.sonar.api.utils.ValidationMessages;
  29. import org.sonar.api.utils.log.Logger;
  30. import org.sonar.api.utils.log.Loggers;
  31. import org.sonar.api.utils.log.Profiler;
  32. import static java.lang.String.format;
  33. /**
  34. * Bridge between deprecated {@link ProfileDefinition} API and new {@link BuiltInQualityProfilesDefinition}
  35. */
  36. public class BuiltInQProfileDefinitionsBridge implements BuiltInQualityProfilesDefinition {
  37. private static final Logger LOGGER = Loggers.get(BuiltInQProfileDefinitionsBridge.class);
  38. private final List<ProfileDefinition> definitions;
  39. /**
  40. * Requires for pico container when no {@link ProfileDefinition} is defined at all
  41. */
  42. public BuiltInQProfileDefinitionsBridge() {
  43. this(new ProfileDefinition[0]);
  44. }
  45. public BuiltInQProfileDefinitionsBridge(ProfileDefinition... definitions) {
  46. this.definitions = ImmutableList.copyOf(definitions);
  47. }
  48. @Override
  49. public void define(Context context) {
  50. Profiler profiler = Profiler.create(Loggers.get(getClass()));
  51. for (ProfileDefinition definition : definitions) {
  52. profiler.start();
  53. ValidationMessages validation = ValidationMessages.create();
  54. RulesProfile profile = definition.createProfile(validation);
  55. validation.log(LOGGER);
  56. if (profile == null) {
  57. profiler.stopDebug(format("Loaded definition %s that return no profile", definition));
  58. } else {
  59. if (!validation.hasErrors()) {
  60. define(context, profile);
  61. }
  62. profiler.stopDebug(format("Loaded deprecated profile definition %s for language %s", profile.getName(), profile.getLanguage()));
  63. }
  64. }
  65. }
  66. private static void define(Context context, RulesProfile profile) {
  67. NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(profile.getName(), profile.getLanguage())
  68. .setDefault(profile.getDefaultProfile());
  69. for (org.sonar.api.rules.ActiveRule ar : profile.getActiveRules()) {
  70. NewBuiltInActiveRule newActiveRule = newQp.activateRule(ar.getRepositoryKey(), ar.getRuleKey());
  71. RulePriority overriddenSeverity = ar.getOverriddenSeverity();
  72. if (overriddenSeverity != null) {
  73. newActiveRule.overrideSeverity(overriddenSeverity.name());
  74. }
  75. for (ActiveRuleParam param : ar.getActiveRuleParams()) {
  76. newActiveRule.overrideParam(param.getKey(), param.getValue());
  77. }
  78. }
  79. newQp.done();
  80. }
  81. }