123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /*
- * SonarQube
- * Copyright (C) 2009-2020 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.ce.task.projectanalysis.issue;
-
- import com.google.common.collect.Multimap;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Optional;
- import java.util.Set;
- import java.util.function.Supplier;
- import javax.annotation.CheckForNull;
- import org.sonar.api.rule.RuleKey;
- import org.sonar.api.rule.RuleStatus;
- import org.sonar.api.rules.RuleType;
- import org.sonar.api.server.debt.DebtRemediationFunction;
- import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
- import org.sonar.core.util.stream.MoreCollectors;
- import org.sonar.db.DbClient;
- import org.sonar.db.DbSession;
- import org.sonar.db.rule.DeprecatedRuleKeyDto;
- import org.sonar.db.rule.RuleDto;
-
- import static com.google.common.base.Preconditions.checkArgument;
- import static java.util.Objects.requireNonNull;
-
- public class RuleRepositoryImpl implements RuleRepository {
-
- @CheckForNull
- private Map<RuleKey, Rule> rulesByKey;
- @CheckForNull
- private Map<String, Rule> rulesByUuid;
-
- private final AdHocRuleCreator creator;
- private final DbClient dbClient;
- private final AnalysisMetadataHolder analysisMetadataHolder;
-
- private Map<RuleKey, NewAdHocRule> adHocRulesPersist = new HashMap<>();
-
- public RuleRepositoryImpl(AdHocRuleCreator creator, DbClient dbClient, AnalysisMetadataHolder analysisMetadataHolder) {
- this.creator = creator;
- this.dbClient = dbClient;
- this.analysisMetadataHolder = analysisMetadataHolder;
- }
-
- public void addOrUpdateAddHocRuleIfNeeded(RuleKey ruleKey, Supplier<NewAdHocRule> ruleSupplier) {
- ensureInitialized();
-
- Rule existingRule = rulesByKey.get(ruleKey);
- if (existingRule == null || (existingRule.isAdHoc() && !adHocRulesPersist.containsKey(ruleKey))) {
- NewAdHocRule newAdHocRule = ruleSupplier.get();
- adHocRulesPersist.put(ruleKey, newAdHocRule);
- rulesByKey.put(ruleKey, new AdHocRuleWrapper(newAdHocRule));
- }
- }
-
- @Override
- public void saveOrUpdateAddHocRules(DbSession dbSession) {
- ensureInitialized();
-
- adHocRulesPersist.values().forEach(r -> persistAndIndex(dbSession, r));
- }
-
- private void persistAndIndex(DbSession dbSession, NewAdHocRule adHocRule) {
- Rule rule = new RuleImpl(creator.persistAndIndex(dbSession, adHocRule, analysisMetadataHolder.getOrganization().toDto()));
- rulesByUuid.put(rule.getUuid(), rule);
- rulesByKey.put(adHocRule.getKey(), rule);
- }
-
- @Override
- public Rule getByKey(RuleKey key) {
- verifyKeyArgument(key);
-
- ensureInitialized();
-
- Rule rule = rulesByKey.get(key);
- checkArgument(rule != null, "Can not find rule for key %s. This rule does not exist in DB", key);
- return rule;
- }
-
- @Override
- public Optional<Rule> findByKey(RuleKey key) {
- verifyKeyArgument(key);
-
- ensureInitialized();
-
- return Optional.ofNullable(rulesByKey.get(key));
- }
-
- @Override
- public Rule getByUuid(String uuid) {
- ensureInitialized();
-
- Rule rule = rulesByUuid.get(uuid);
- checkArgument(rule != null, "Can not find rule for uuid %s. This rule does not exist in DB", uuid);
- return rule;
- }
-
- @Override
- public Optional<Rule> findByUuid(String uuid) {
- ensureInitialized();
-
- return Optional.ofNullable(rulesByUuid.get(uuid));
- }
-
- private static void verifyKeyArgument(RuleKey key) {
- requireNonNull(key, "RuleKey can not be null");
- }
-
- private void ensureInitialized() {
- if (rulesByKey == null) {
- try (DbSession dbSession = dbClient.openSession(false)) {
- loadRulesFromDb(dbSession);
- }
- }
- }
-
- private void loadRulesFromDb(DbSession dbSession) {
- this.rulesByKey = new HashMap<>();
- this.rulesByUuid = new HashMap<>();
- String organizationUuid = analysisMetadataHolder.getOrganization().getUuid();
- Multimap<String, DeprecatedRuleKeyDto> deprecatedRuleKeysByRuleUuid = dbClient.ruleDao().selectAllDeprecatedRuleKeys(dbSession).stream()
- .collect(MoreCollectors.index(DeprecatedRuleKeyDto::getRuleUuid));
- for (RuleDto ruleDto : dbClient.ruleDao().selectAll(dbSession, organizationUuid)) {
- Rule rule = new RuleImpl(ruleDto);
- rulesByKey.put(ruleDto.getKey(), rule);
- rulesByUuid.put(ruleDto.getUuid(), rule);
- deprecatedRuleKeysByRuleUuid.get(ruleDto.getUuid()).forEach(t -> rulesByKey.put(RuleKey.of(t.getOldRepositoryKey(), t.getOldRuleKey()), rule));
- }
- }
-
- private static class AdHocRuleWrapper implements Rule {
- private final NewAdHocRule addHocRule;
-
- private AdHocRuleWrapper(NewAdHocRule addHocRule) {
- this.addHocRule = addHocRule;
- }
-
- public NewAdHocRule getDelegate() {
- return addHocRule;
- }
-
- @Override
- public String getUuid() {
- throw new UnsupportedOperationException("Rule is not persisted, can't know the uuid");
- }
-
- @Override
- public RuleKey getKey() {
- return addHocRule.getKey();
- }
-
- @Override
- public String getName() {
- return addHocRule.getName();
- }
-
- @Override
- @CheckForNull
- public String getLanguage() {
- return null;
- }
-
- @Override
- public RuleStatus getStatus() {
- return RuleStatus.defaultStatus();
- }
-
- @Override
- @CheckForNull
- public RuleType getType() {
- return null;
- }
-
- @Override
- public boolean isExternal() {
- return true;
- }
-
- @Override
- public boolean isAdHoc() {
- return true;
- }
-
- @Override
- public Set<String> getTags() {
- return Collections.emptySet();
- }
-
- @CheckForNull
- @Override
- public DebtRemediationFunction getRemediationFunction() {
- return null;
- }
-
- @CheckForNull
- @Override
- public String getPluginKey() {
- return null;
- }
- }
- }
|