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.

RuleRepositoryImpl.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.ce.task.projectanalysis.issue;
  21. import com.google.common.collect.Multimap;
  22. import java.util.Collections;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.Optional;
  26. import java.util.Set;
  27. import java.util.function.Supplier;
  28. import javax.annotation.CheckForNull;
  29. import org.sonar.api.rule.RuleKey;
  30. import org.sonar.api.rule.RuleStatus;
  31. import org.sonar.api.rules.RuleType;
  32. import org.sonar.api.server.debt.DebtRemediationFunction;
  33. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  34. import org.sonar.core.util.stream.MoreCollectors;
  35. import org.sonar.db.DbClient;
  36. import org.sonar.db.DbSession;
  37. import org.sonar.db.rule.DeprecatedRuleKeyDto;
  38. import org.sonar.db.rule.RuleDto;
  39. import static com.google.common.base.Preconditions.checkArgument;
  40. import static java.util.Objects.requireNonNull;
  41. public class RuleRepositoryImpl implements RuleRepository {
  42. @CheckForNull
  43. private Map<RuleKey, Rule> rulesByKey;
  44. @CheckForNull
  45. private Map<String, Rule> rulesByUuid;
  46. private final AdHocRuleCreator creator;
  47. private final DbClient dbClient;
  48. private final AnalysisMetadataHolder analysisMetadataHolder;
  49. private Map<RuleKey, NewAdHocRule> adHocRulesPersist = new HashMap<>();
  50. public RuleRepositoryImpl(AdHocRuleCreator creator, DbClient dbClient, AnalysisMetadataHolder analysisMetadataHolder) {
  51. this.creator = creator;
  52. this.dbClient = dbClient;
  53. this.analysisMetadataHolder = analysisMetadataHolder;
  54. }
  55. public void addOrUpdateAddHocRuleIfNeeded(RuleKey ruleKey, Supplier<NewAdHocRule> ruleSupplier) {
  56. ensureInitialized();
  57. Rule existingRule = rulesByKey.get(ruleKey);
  58. if (existingRule == null || (existingRule.isAdHoc() && !adHocRulesPersist.containsKey(ruleKey))) {
  59. NewAdHocRule newAdHocRule = ruleSupplier.get();
  60. adHocRulesPersist.put(ruleKey, newAdHocRule);
  61. rulesByKey.put(ruleKey, new AdHocRuleWrapper(newAdHocRule));
  62. }
  63. }
  64. @Override
  65. public void saveOrUpdateAddHocRules(DbSession dbSession) {
  66. ensureInitialized();
  67. adHocRulesPersist.values().forEach(r -> persistAndIndex(dbSession, r));
  68. }
  69. private void persistAndIndex(DbSession dbSession, NewAdHocRule adHocRule) {
  70. Rule rule = new RuleImpl(creator.persistAndIndex(dbSession, adHocRule, analysisMetadataHolder.getOrganization().toDto()));
  71. rulesByUuid.put(rule.getUuid(), rule);
  72. rulesByKey.put(adHocRule.getKey(), rule);
  73. }
  74. @Override
  75. public Rule getByKey(RuleKey key) {
  76. verifyKeyArgument(key);
  77. ensureInitialized();
  78. Rule rule = rulesByKey.get(key);
  79. checkArgument(rule != null, "Can not find rule for key %s. This rule does not exist in DB", key);
  80. return rule;
  81. }
  82. @Override
  83. public Optional<Rule> findByKey(RuleKey key) {
  84. verifyKeyArgument(key);
  85. ensureInitialized();
  86. return Optional.ofNullable(rulesByKey.get(key));
  87. }
  88. @Override
  89. public Rule getByUuid(String uuid) {
  90. ensureInitialized();
  91. Rule rule = rulesByUuid.get(uuid);
  92. checkArgument(rule != null, "Can not find rule for uuid %s. This rule does not exist in DB", uuid);
  93. return rule;
  94. }
  95. @Override
  96. public Optional<Rule> findByUuid(String uuid) {
  97. ensureInitialized();
  98. return Optional.ofNullable(rulesByUuid.get(uuid));
  99. }
  100. private static void verifyKeyArgument(RuleKey key) {
  101. requireNonNull(key, "RuleKey can not be null");
  102. }
  103. private void ensureInitialized() {
  104. if (rulesByKey == null) {
  105. try (DbSession dbSession = dbClient.openSession(false)) {
  106. loadRulesFromDb(dbSession);
  107. }
  108. }
  109. }
  110. private void loadRulesFromDb(DbSession dbSession) {
  111. this.rulesByKey = new HashMap<>();
  112. this.rulesByUuid = new HashMap<>();
  113. String organizationUuid = analysisMetadataHolder.getOrganization().getUuid();
  114. Multimap<String, DeprecatedRuleKeyDto> deprecatedRuleKeysByRuleUuid = dbClient.ruleDao().selectAllDeprecatedRuleKeys(dbSession).stream()
  115. .collect(MoreCollectors.index(DeprecatedRuleKeyDto::getRuleUuid));
  116. for (RuleDto ruleDto : dbClient.ruleDao().selectAll(dbSession, organizationUuid)) {
  117. Rule rule = new RuleImpl(ruleDto);
  118. rulesByKey.put(ruleDto.getKey(), rule);
  119. rulesByUuid.put(ruleDto.getUuid(), rule);
  120. deprecatedRuleKeysByRuleUuid.get(ruleDto.getUuid()).forEach(t -> rulesByKey.put(RuleKey.of(t.getOldRepositoryKey(), t.getOldRuleKey()), rule));
  121. }
  122. }
  123. private static class AdHocRuleWrapper implements Rule {
  124. private final NewAdHocRule addHocRule;
  125. private AdHocRuleWrapper(NewAdHocRule addHocRule) {
  126. this.addHocRule = addHocRule;
  127. }
  128. public NewAdHocRule getDelegate() {
  129. return addHocRule;
  130. }
  131. @Override
  132. public String getUuid() {
  133. throw new UnsupportedOperationException("Rule is not persisted, can't know the uuid");
  134. }
  135. @Override
  136. public RuleKey getKey() {
  137. return addHocRule.getKey();
  138. }
  139. @Override
  140. public String getName() {
  141. return addHocRule.getName();
  142. }
  143. @Override
  144. @CheckForNull
  145. public String getLanguage() {
  146. return null;
  147. }
  148. @Override
  149. public RuleStatus getStatus() {
  150. return RuleStatus.defaultStatus();
  151. }
  152. @Override
  153. @CheckForNull
  154. public RuleType getType() {
  155. return null;
  156. }
  157. @Override
  158. public boolean isExternal() {
  159. return true;
  160. }
  161. @Override
  162. public boolean isAdHoc() {
  163. return true;
  164. }
  165. @Override
  166. public Set<String> getTags() {
  167. return Collections.emptySet();
  168. }
  169. @CheckForNull
  170. @Override
  171. public DebtRemediationFunction getRemediationFunction() {
  172. return null;
  173. }
  174. @CheckForNull
  175. @Override
  176. public String getPluginKey() {
  177. return null;
  178. }
  179. }
  180. }