aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-06-17 20:54:48 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-06-17 20:54:48 +0200
commitf6a15f7112fae437d662ea1f14cfb031186a597d (patch)
treeb30bf3f63050097cd66a97441d93467d585f190d /sonar-core
parentaedda9b21f020b3f5bb4d89a85673ab14909afc7 (diff)
downloadsonarqube-f6a15f7112fae437d662ea1f14cfb031186a597d.tar.gz
sonarqube-f6a15f7112fae437d662ea1f14cfb031186a597d.zip
SONAR-6588 Remove RuleFinder from batch bootstrap phase
That allows to drop CacheRuleFinder and the Hibernate entities Rule/RuleParam
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java144
-rw-r--r--sonar-core/src/main/resources/META-INF/persistence.xml2
-rw-r--r--sonar-core/src/test/java/org/sonar/core/rule/CacheRuleFinderTest.java70
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/rule/CacheRuleFinderTest/shared.xml18
4 files changed, 0 insertions, 234 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java b/sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java
deleted file mode 100644
index a027c98aa40..00000000000
--- a/sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.core.rule;
-
-import com.google.common.collect.BiMap;
-import com.google.common.collect.HashBiMap;
-import com.google.common.collect.Maps;
-import org.apache.commons.lang.StringUtils;
-import org.hibernate.Hibernate;
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.rules.RuleQuery;
-import org.sonar.jpa.session.DatabaseSessionFactory;
-
-import javax.annotation.CheckForNull;
-import javax.persistence.Query;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @deprecated since 4.5
- */
-@Deprecated
-public final class CacheRuleFinder implements RuleFinder {
-
- private BiMap<Integer, Rule> rulesById = HashBiMap.create();
- private Map<String, Map<String, Rule>> rulesByKey = Maps.newHashMap();
-
- private DatabaseSessionFactory sessionFactory;
-
- public CacheRuleFinder(DatabaseSessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- @Override
- public Rule findById(int ruleId) {
- Rule rule = rulesById.get(ruleId);
- if (rule == null) {
- rule = doFindById(ruleId);
- if (rule != null) {
- loadRepository(rule.getRepositoryKey());
- }
- }
- return rule;
- }
-
- @Override
- public Rule findByKey(String repositoryKey, String ruleKey) {
- Map<String, Rule> repository = loadRepository(repositoryKey);
- return repository.get(ruleKey);
- }
-
- private Map<String, Rule> loadRepository(String repositoryKey) {
- Map<String, Rule> repository = rulesByKey.get(repositoryKey);
- if (repository == null) {
- repository = Maps.newHashMap();
- rulesByKey.put(repositoryKey, repository);
-
- for (Rule rule : findAll(RuleQuery.create().withRepositoryKey(repositoryKey))) {
- hibernateHack(rule);
- repository.put(rule.getKey(), rule);
- rulesById.put(rule.getId(), rule);
- }
- }
- return repository;
- }
-
- private void hibernateHack(Rule rule) {
- Hibernate.initialize(rule.getParams());
- }
-
- private Rule doFindById(int ruleId) {
- DatabaseSession session = sessionFactory.getSession();
- return session.getSingleResult(
- session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.id=:id and r.status<>:status")
- .setParameter("id", ruleId)
- .setParameter("status", Rule.STATUS_REMOVED
- ),
- null);
- }
-
- @Override
- @CheckForNull
- public Rule findByKey(RuleKey key) {
- return findByKey(key.repository(), key.rule());
- }
-
- @Override
- public final Rule find(RuleQuery query) {
- DatabaseSession session = sessionFactory.getSession();
- return session.getSingleResult(createHqlQuery(session, query), null);
- }
-
- @Override
- public final Collection<Rule> findAll(RuleQuery query) {
- DatabaseSession session = sessionFactory.getSession();
- return createHqlQuery(session, query).getResultList();
- }
-
- private Query createHqlQuery(DatabaseSession session, RuleQuery query) {
- StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" where status<>:status ");
- Map<String, Object> params = new HashMap<>();
- params.put("status", Rule.STATUS_REMOVED);
- if (StringUtils.isNotBlank(query.getRepositoryKey())) {
- hql.append("AND pluginName=:repositoryKey ");
- params.put("repositoryKey", query.getRepositoryKey());
- }
- if (StringUtils.isNotBlank(query.getKey())) {
- hql.append("AND key=:key ");
- params.put("key", query.getKey());
- }
- if (StringUtils.isNotBlank(query.getConfigKey())) {
- hql.append("AND configKey=:configKey ");
- params.put("configKey", query.getConfigKey());
- }
-
- Query hqlQuery = session.createQuery(hql.toString());
- for (Map.Entry<String, Object> entry : params.entrySet()) {
- hqlQuery.setParameter(entry.getKey(), entry.getValue());
- }
- return hqlQuery;
- }
-}
diff --git a/sonar-core/src/main/resources/META-INF/persistence.xml b/sonar-core/src/main/resources/META-INF/persistence.xml
index 543cc5e66c4..24ad54095ca 100644
--- a/sonar-core/src/main/resources/META-INF/persistence.xml
+++ b/sonar-core/src/main/resources/META-INF/persistence.xml
@@ -9,8 +9,6 @@
<class>org.sonar.api.database.model.Snapshot</class>
<class>org.sonar.api.measures.Metric</class>
<class>org.sonar.api.database.model.ResourceModel</class>
- <class>org.sonar.api.rules.Rule</class>
- <class>org.sonar.api.rules.RuleParam</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
diff --git a/sonar-core/src/test/java/org/sonar/core/rule/CacheRuleFinderTest.java b/sonar-core/src/test/java/org/sonar/core/rule/CacheRuleFinderTest.java
deleted file mode 100644
index d2282901d1c..00000000000
--- a/sonar-core/src/test/java/org/sonar/core/rule/CacheRuleFinderTest.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.core.rule;
-
-import org.junit.Test;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.core.rule.CacheRuleFinder;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
-
-import static org.hamcrest.Matchers.nullValue;
-import static org.hamcrest.core.Is.is;
-import static org.hamcrest.core.IsNull.notNullValue;
-import static org.junit.Assert.assertThat;
-
-public class CacheRuleFinderTest extends AbstractDbUnitTestCase {
-
- @Test
- public void shouldCacheFindById() {
- setupData("shared");
- RuleFinder finder = new CacheRuleFinder(getSessionFactory());
- assertThat(finder.findById(3).getConfigKey(), is("Checker/Treewalker/AnnotationUseStyleCheck"));
-
- deleteRules();
-
- assertThat(finder.findById(3), notNullValue());
- }
-
- @Test
- public void shouldNotFailIfUnknownRuleId() {
- setupData("shared");
- RuleFinder finder = new CacheRuleFinder(getSessionFactory());
- assertThat(finder.findById(33456816), nullValue());
- }
-
- @Test
- public void shouldCacheFindByKey() {
- setupData("shared");
- RuleFinder finder = new CacheRuleFinder(getSessionFactory());
- Rule rule = finder.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck");
- assertThat(rule.getConfigKey(), is("Checker/Treewalker/AnnotationUseStyleCheck"));
-
- deleteRules();
-
- rule = finder.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck");
- assertThat(rule, notNullValue());
- }
-
- private void deleteRules() {
- getSession().createQuery("delete " + Rule.class.getSimpleName()).executeUpdate();
- getSession().commit();
- }
-}
diff --git a/sonar-core/src/test/resources/org/sonar/core/rule/CacheRuleFinderTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/rule/CacheRuleFinderTest/shared.xml
deleted file mode 100644
index f01f4cf5fc0..00000000000
--- a/sonar-core/src/test/resources/org/sonar/core/rule/CacheRuleFinderTest/shared.xml
+++ /dev/null
@@ -1,18 +0,0 @@
-<dataset>
- <!-- CHECKSTYLE -->
-
- <rules tags="[null]" system_tags="[null]" id="1" name="Check Header" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
- plugin_config_key="Checker/Treewalker/HeaderCheck" plugin_name="checkstyle" description="[null]" priority="4" status="READY" is_template="[false]" template_id="[null]"/>
-
- <!-- disabled rule -->
- <rules tags="[null]" system_tags="[null]" id="2" name="Disabled checked" plugin_rule_key="DisabledCheck"
- plugin_config_key="Checker/Treewalker/DisabledCheck" plugin_name="checkstyle" description="[null]" priority="4" status="REMOVED" is_template="[false]" template_id="[null]" />
-
- <rules tags="[null]" system_tags="[null]" id="3" name="Check Annotation" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck"
- plugin_config_key="Checker/Treewalker/AnnotationUseStyleCheck" plugin_name="checkstyle" description="[null]" priority="4" status="READY" is_template="[false]" template_id="[null]" />
-
-
- <!-- PMD -->
- <rules tags="[null]" system_tags="[null]" id="4" name="Call Super First" plugin_rule_key="CallSuperFirst"
- plugin_config_key="rulesets/android.xml/CallSuperFirst" plugin_name="pmd" description="[null]" priority="2" status="READY" is_template="[false]" template_id="[null]" />
-</dataset>