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.

RulesDefinitionContext.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.api.impl.server;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import javax.annotation.CheckForNull;
  26. import javax.annotation.Nullable;
  27. import org.sonar.api.rule.RuleKey;
  28. import org.sonar.api.server.rule.RulesDefinition;
  29. import org.sonar.api.server.rule.internal.DefaultNewRepository;
  30. import org.sonar.api.server.rule.internal.DefaultRepository;
  31. import static java.util.Collections.unmodifiableList;
  32. import static org.sonar.api.utils.Preconditions.checkState;
  33. public class RulesDefinitionContext extends RulesDefinition.Context {
  34. private final Map<String, RulesDefinition.Repository> repositoriesByKey = new HashMap<>();
  35. private String currentPluginKey = null;
  36. @Override
  37. public RulesDefinition.NewRepository createRepository(String key, String language) {
  38. return new DefaultNewRepository(this, key, language, false);
  39. }
  40. @Override
  41. public RulesDefinition.NewRepository createExternalRepository(String engineId, String language) {
  42. return new DefaultNewRepository(this, RuleKey.EXTERNAL_RULE_REPO_PREFIX + engineId, language, true);
  43. }
  44. @Override
  45. @CheckForNull
  46. public RulesDefinition.Repository repository(String key) {
  47. return repositoriesByKey.get(key);
  48. }
  49. @Override
  50. public List<RulesDefinition.Repository> repositories() {
  51. return unmodifiableList(new ArrayList<>(repositoriesByKey.values()));
  52. }
  53. public void registerRepository(DefaultNewRepository newRepository) {
  54. RulesDefinition.Repository existing = repositoriesByKey.get(newRepository.key());
  55. if (existing != null) {
  56. String existingLanguage = existing.language();
  57. checkState(existingLanguage.equals(newRepository.language()),
  58. "The rule repository '%s' must not be defined for two different languages: %s and %s",
  59. newRepository.key(), existingLanguage, newRepository.language());
  60. }
  61. repositoriesByKey.put(newRepository.key(), new DefaultRepository(newRepository, existing));
  62. }
  63. public String currentPluginKey() {
  64. return currentPluginKey;
  65. }
  66. @Override
  67. public void setCurrentPluginKey(@Nullable String pluginKey) {
  68. this.currentPluginKey = pluginKey;
  69. }
  70. }