Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CacheRuleFinder.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2008-2011 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar 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. * Sonar 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
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.core.components;
  21. import com.google.common.collect.BiMap;
  22. import com.google.common.collect.HashBiMap;
  23. import com.google.common.collect.Maps;
  24. import org.sonar.api.rules.Rule;
  25. import org.sonar.api.rules.RuleQuery;
  26. import org.sonar.jpa.session.DatabaseSessionFactory;
  27. import java.util.Map;
  28. public final class CacheRuleFinder extends DefaultRuleFinder {
  29. private BiMap<Integer, Rule> rulesById = HashBiMap.create();
  30. private Map<String, Map<String, Rule>> rulesByKey = Maps.newHashMap();
  31. public CacheRuleFinder(DatabaseSessionFactory sessionFactory) {
  32. super(sessionFactory);
  33. }
  34. @Override
  35. public Rule findById(int ruleId) {
  36. Rule rule = rulesById.get(ruleId);
  37. if (rule == null) {
  38. rule = doFindById(ruleId);
  39. if (rule != null) {
  40. loadRepository(rule.getRepositoryKey());
  41. }
  42. }
  43. return rule;
  44. }
  45. @Override
  46. public Rule findByKey(String repositoryKey, String ruleKey) {
  47. Map<String, Rule> repository = loadRepository(repositoryKey);
  48. return repository.get(ruleKey);
  49. }
  50. private Map<String, Rule> loadRepository(String repositoryKey) {
  51. Map<String, Rule> repository = rulesByKey.get(repositoryKey);
  52. if (repository == null) {
  53. repository = Maps.newHashMap();
  54. rulesByKey.put(repositoryKey, repository);
  55. for (Rule rule : findAll(RuleQuery.create().withRepositoryKey(repositoryKey))) {
  56. repository.put(rule.getKey(), rule);
  57. rulesById.put(rule.getId(), rule);
  58. }
  59. }
  60. return repository;
  61. }
  62. }