aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-06-18 00:03:18 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-06-18 11:52:18 +0200
commit021bf45623b748e70f20d956e86d595191241786 (patch)
tree759c5eab8fdd928cf01c3f3495ded8d968922e07 /sonar-core
parentd370049974502061b852e73f25700c0d3bf218e6 (diff)
downloadsonarqube-021bf45623b748e70f20d956e86d595191241786.tar.gz
sonarqube-021bf45623b748e70f20d956e86d595191241786.zip
Refactor registration of metrics at server startup
Replace Hibernate by MyBatis
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/metric/DefaultMetricFinder.java70
-rw-r--r--sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java4
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java2
-rw-r--r--sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionDao.java3
-rw-r--r--sonar-core/src/main/java/org/sonar/jpa/dao/MeasuresDao.java95
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml14
-rw-r--r--sonar-core/src/test/java/org/sonar/core/metric/DefaultMetricFinderTest.java62
-rw-r--r--sonar-core/src/test/java/org/sonar/jpa/dao/MeasuresDaoTest.java73
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/metric/DefaultMetricFinderTest/shared.xml12
9 files changed, 19 insertions, 316 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/metric/DefaultMetricFinder.java b/sonar-core/src/main/java/org/sonar/core/metric/DefaultMetricFinder.java
deleted file mode 100644
index 087a6be91b8..00000000000
--- a/sonar-core/src/main/java/org/sonar/core/metric/DefaultMetricFinder.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.metric;
-
-import com.google.common.collect.Lists;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.MetricFinder;
-import org.sonar.jpa.session.DatabaseSessionFactory;
-
-import java.util.Collection;
-import java.util.List;
-
-public class DefaultMetricFinder implements MetricFinder {
-
- private static final String ENABLED = "enabled";
- private DatabaseSessionFactory sessionFactory;
-
- public DefaultMetricFinder(DatabaseSessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- @Override
- public Metric findById(int id) {
- return sessionFactory.getSession().getSingleResult(Metric.class, "id", id, ENABLED, true);
- }
-
- @Override
- public Metric findByKey(String key) {
- return sessionFactory.getSession().getSingleResult(Metric.class, "key", key, ENABLED, true);
- }
-
- @Override
- public Collection<Metric> findAll(List<String> metricKeys) {
- List<Metric> result = Lists.newLinkedList();
- for (String metricKey : metricKeys) {
- Metric metric = findByKey(metricKey);
- if (metric != null) {
- result.add(metric);
- }
- }
- return result;
- }
-
- @Override
- public Collection<Metric> findAll() {
- return doFindAll();
- }
-
- protected Collection<Metric> doFindAll() {
- return sessionFactory.getSession().getResults(Metric.class, ENABLED, true);
- }
-
-}
diff --git a/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java b/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java
index dd216603d05..280fb82f4e0 100644
--- a/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java
+++ b/sonar-core/src/main/java/org/sonar/core/metric/db/MetricMapper.java
@@ -40,7 +40,9 @@ public interface MetricMapper {
List<MetricDto> selectByKeys(@Param("keys") List<String> keys);
- void disable(@Param("ids") List<Integer> ids);
+ void disableByIds(@Param("ids") List<Integer> ids);
+
+ void disableByKey(@Param("key") String key);
int countEnabled(@Param("isCustom") @Nullable Boolean isCustom);
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java
index 03372a70717..17764552b20 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java
@@ -39,6 +39,7 @@ import org.sonar.core.permission.PermissionDao;
import org.sonar.core.permission.PermissionTemplateDao;
import org.sonar.core.properties.PropertiesDao;
import org.sonar.core.purge.PurgeDao;
+import org.sonar.core.qualitygate.db.QualityGateConditionDao;
import org.sonar.core.qualityprofile.db.ActiveRuleDao;
import org.sonar.core.qualityprofile.db.QualityProfileDao;
import org.sonar.core.resource.ResourceDao;
@@ -84,6 +85,7 @@ public final class DaoUtils {
PermissionDao.class,
PermissionTemplateDao.class,
PropertiesDao.class,
+ QualityGateConditionDao.class,
QualityProfileDao.class,
PurgeDao.class,
CharacteristicDao.class,
diff --git a/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionDao.java b/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionDao.java
index a634f15bf61..5c1eec7664f 100644
--- a/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/qualitygate/db/QualityGateConditionDao.java
@@ -20,6 +20,7 @@
package org.sonar.core.qualitygate.db;
import org.apache.ibatis.session.SqlSession;
+import org.sonar.core.persistence.DaoComponent;
import org.sonar.core.persistence.MyBatis;
import java.util.Collection;
@@ -28,7 +29,7 @@ import java.util.Date;
/**
* @since 4.3
*/
-public class QualityGateConditionDao {
+public class QualityGateConditionDao implements DaoComponent {
private final MyBatis myBatis;
diff --git a/sonar-core/src/main/java/org/sonar/jpa/dao/MeasuresDao.java b/sonar-core/src/main/java/org/sonar/jpa/dao/MeasuresDao.java
deleted file mode 100644
index 372674b94c4..00000000000
--- a/sonar-core/src/main/java/org/sonar/jpa/dao/MeasuresDao.java
+++ /dev/null
@@ -1,95 +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.jpa.dao;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import org.apache.commons.collections.CollectionUtils;
-import org.apache.commons.collections.Predicate;
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.measures.Metric;
-
-public class MeasuresDao {
-
- private final DatabaseSession session;
- private final Map<String, Metric> metricsByName = new HashMap<>();
-
- public MeasuresDao(DatabaseSession session) {
- this.session = session;
- }
-
- public Metric getMetric(String metricName) {
- return getMetricsByName().get(metricName);
- }
-
- public Collection<Metric> getMetrics() {
- return getMetricsByName().values();
- }
-
- public Collection<Metric> getEnabledMetrics() {
- return CollectionUtils.select(getMetricsByName().values(), new Predicate() {
- @Override
- public boolean evaluate(Object o) {
- return ((Metric) o).getEnabled();
- }
- });
- }
-
- public void disableAutomaticMetrics() {
- session.createQuery("update " + Metric.class.getSimpleName() + " m set m.enabled=false where m.userManaged=false").executeUpdate();
- session.commit();
- metricsByName.clear();
- }
-
- public void registerMetrics(Collection<Metric> metrics) {
- if (metrics != null) {
- for (Metric metric : metrics) {
- metric.setEnabled(Boolean.TRUE);
- persistMetricWithoutClear(metric);
- }
- session.commit();
- }
- metricsByName.clear();
- }
-
- private void persistMetricWithoutClear(Metric metric) {
- Metric dbMetric = getMetric(metric.getKey());
- if (dbMetric != null) {
- dbMetric.merge(metric);
- session.getEntityManager().merge(dbMetric);
-
- } else {
- session.getEntityManager().persist(new Metric().merge(metric));
- }
- }
-
- private Map<String, Metric> getMetricsByName() {
- if (metricsByName.isEmpty()) {
- List<Metric> metrics = session.getResults(Metric.class);
- for (Metric metric : metrics) {
- metricsByName.put(metric.getKey(), metric);
- }
- }
- return metricsByName;
- }
-
-}
diff --git a/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml b/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml
index 9278b7252a5..a91e4c49259 100644
--- a/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/metric/db/MetricMapper.xml
@@ -47,6 +47,7 @@
</where>
ORDER BY UPPER(m.short_name), m.short_name
</select>
+
<select id="countEnabled" resultType="Integer">
SELECT COUNT(*)
FROM metrics m
@@ -85,7 +86,10 @@
val_type=#{valueType, jdbcType=VARCHAR},
enabled=#{enabled, jdbcType=BOOLEAN},
domain=#{domain, jdbcType=VARCHAR},
- description=#{description, jdbcType=VARCHAR}
+ description=#{description, jdbcType=VARCHAR},
+ direction=#{direction, jdbcType=INTEGER},
+ hidden=#{hidden, jdbcType=BOOLEAN},
+ qualitative=#{qualitative, jdbcType=BOOLEAN}
where id=#{id}
</update>
@@ -95,7 +99,7 @@
where m.domain is not null and m.enabled=${_true}
</select>
- <update id="disable">
+ <update id="disableByIds">
update metrics
set enabled=${_false}
<where>
@@ -107,6 +111,12 @@
</where>
</update>
+ <update id="disableByKey" parameterType="string">
+ update metrics
+ set enabled=${_false}
+ where name=#{key}
+ </update>
+
<select id="selectByKeys" resultType="org.sonar.core.metric.db.MetricDto">
SELECT
<include refid="metricColumns"/>
diff --git a/sonar-core/src/test/java/org/sonar/core/metric/DefaultMetricFinderTest.java b/sonar-core/src/test/java/org/sonar/core/metric/DefaultMetricFinderTest.java
deleted file mode 100644
index 34a00af08de..00000000000
--- a/sonar-core/src/test/java/org/sonar/core/metric/DefaultMetricFinderTest.java
+++ /dev/null
@@ -1,62 +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.metric;
-
-import java.util.Arrays;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
-
-import static org.hamcrest.core.Is.is;
-import static org.hamcrest.core.IsNull.nullValue;
-import static org.junit.Assert.assertThat;
-
-public class DefaultMetricFinderTest extends AbstractDbUnitTestCase {
-
- private DefaultMetricFinder finder;
-
- @Before
- public void setUp() {
- setupData("shared");
- finder = new DefaultMetricFinder(getSessionFactory());
- }
-
- @Test
- public void shouldFindAll() {
- assertThat(finder.findAll().size(), is(2));
- }
-
- @Test
- public void shouldFindByKeys() {
- assertThat(finder.findAll(Arrays.<String> asList("ncloc", "foo", "coverage")).size(), is(2));
- }
-
- @Test
- public void shouldFindById() {
- assertThat(finder.findById(1).getKey(), is("ncloc"));
- assertThat(finder.findById(3), nullValue());
- }
-
- @Test
- public void shouldFindByKey() {
- assertThat(finder.findByKey("ncloc").getKey(), is("ncloc"));
- assertThat(finder.findByKey("disabled"), nullValue());
- }
-}
diff --git a/sonar-core/src/test/java/org/sonar/jpa/dao/MeasuresDaoTest.java b/sonar-core/src/test/java/org/sonar/jpa/dao/MeasuresDaoTest.java
deleted file mode 100644
index 3c9d299cd3b..00000000000
--- a/sonar-core/src/test/java/org/sonar/jpa/dao/MeasuresDaoTest.java
+++ /dev/null
@@ -1,73 +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.jpa.dao;
-
-import java.util.Arrays;
-import java.util.Collection;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.database.model.ResourceModel;
-import org.sonar.api.measures.Metric;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
-
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
-public class MeasuresDaoTest extends AbstractDbUnitTestCase {
-
- private MeasuresDao service;
- private ResourceModel project;
-
- @Before
- public void before() {
- service = new MeasuresDao(getSession());
- project = new ResourceModel(ResourceModel.SCOPE_PROJECT, "foo:bar", ResourceModel.QUALIFIER_PROJECT_TRUNK, null, "Foo");
- project.setName("project name");
- getSession().save(project);
- }
-
- @Test
- public void shouldRegisterMetrics() {
- Collection<Metric> newMetrics = createMetrics();
- service.registerMetrics(newMetrics);
-
- Collection<Metric> metrics = service.getEnabledMetrics();
- assertThat(metrics.size(), is(newMetrics.size()));
- }
-
- private Collection<Metric> createMetrics() {
- Metric m1 = new Metric("metric1");
- m1.setEnabled(false);
-
- Metric m2 = new Metric("metric2");
- m2.setEnabled(true);
-
- Metric m3 = new Metric("metric3");
- m3.setEnabled(false);
-
- Metric m4 = new Metric("metric4");
- m4.setEnabled(true);
-
- Metric m5 = new Metric("metric5");
- m5.setEnabled(true);
-
- return Arrays.asList(m1, m2, m3, m4, m5);
- }
-}
diff --git a/sonar-core/src/test/resources/org/sonar/core/metric/DefaultMetricFinderTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/metric/DefaultMetricFinderTest/shared.xml
deleted file mode 100644
index dd645d66ec1..00000000000
--- a/sonar-core/src/test/resources/org/sonar/core/metric/DefaultMetricFinderTest/shared.xml
+++ /dev/null
@@ -1,12 +0,0 @@
-<dataset>
-
- <metrics delete_historical_data="[null]" id="1" name="ncloc" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name=""
- enabled="true" worst_value="[null]" optimized_best_value="[null]" best_value="[null]" direction="0"
- hidden="false"/>
-
- <metrics delete_historical_data="[null]" id="2" name="coverage" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name=""
- enabled="true" worst_value="0" optimized_best_value="true" best_value="100" direction="1" hidden="false"/>
-
- <metrics delete_historical_data="[null]" id="3" name="disabled" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name=""
- enabled="false" worst_value="0" optimized_best_value="true" best_value="100" direction="1" hidden="false"/>
-</dataset> \ No newline at end of file