aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-11-29 17:00:54 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-11-29 17:00:54 +0000
commit6ce6cc598fdcd6e3d6eb662b47668b0c6b898f3d (patch)
tree0770d083d359e3e644ff64bf06dc8b9abc3ad04b /sonar-batch
parent0c4893c83a6873b224a50db86311082fd5af555f (diff)
downloadsonarqube-6ce6cc598fdcd6e3d6eb662b47668b0c6b898f3d.tar.gz
sonarqube-6ce6cc598fdcd6e3d6eb662b47668b0c6b898f3d.zip
SONAR-249 improve core components to load rules and metrics
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/Batch.java5
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/DefaultMetricFinder.java66
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java25
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java4
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java2
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java16
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java29
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java3
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java3
9 files changed, 51 insertions, 102 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/Batch.java b/sonar-batch/src/main/java/org/sonar/batch/Batch.java
index 303d4d099b5..d6c5c6a8bd6 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/Batch.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/Batch.java
@@ -30,6 +30,8 @@ import org.sonar.api.utils.HttpDownloader;
import org.sonar.api.utils.IocContainer;
import org.sonar.api.utils.ServerHttpClient;
import org.sonar.batch.index.*;
+import org.sonar.core.components.CacheMetricFinder;
+import org.sonar.core.components.CacheRuleFinder;
import org.sonar.core.plugin.JpaPluginDao;
import org.sonar.jpa.dao.MeasuresDao;
import org.sonar.jpa.session.DatabaseSessionProvider;
@@ -71,7 +73,6 @@ public class Batch {
batchContainer.as(Characteristics.CACHE).addComponent(ServerMetadata.class);
batchContainer.as(Characteristics.CACHE).addComponent(ProjectTree.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultResourceCreationLock.class);
- batchContainer.as(Characteristics.CACHE).addComponent(DefaultMetricFinder.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultIndex.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultPersistenceManager.class);
batchContainer.as(Characteristics.CACHE).addComponent(DependencyPersister.class);
@@ -87,6 +88,8 @@ public class Batch {
batchContainer.as(Characteristics.CACHE).addComponent(ServerHttpClient.class);
batchContainer.as(Characteristics.CACHE).addComponent(HttpDownloader.class);
batchContainer.as(Characteristics.CACHE).addComponent(MeasuresDao.class);
+ batchContainer.as(Characteristics.CACHE).addComponent(CacheRuleFinder.class);
+ batchContainer.as(Characteristics.CACHE).addComponent(CacheMetricFinder.class);
batchContainer.start();
ProjectTree projectTree = batchContainer.getComponent(ProjectTree.class);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultMetricFinder.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultMetricFinder.java
deleted file mode 100644
index b0c7f01c9c5..00000000000
--- a/sonar-batch/src/main/java/org/sonar/batch/DefaultMetricFinder.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2009 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.batch;
-
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.MetricFinder;
-
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-
-public final class DefaultMetricFinder implements MetricFinder {
-
- private DatabaseSession session;
- private Map<String, Metric> metrics = Maps.newHashMap();
-
- public DefaultMetricFinder(DatabaseSession session) {
- this.session = session;
- }
-
- public void start() {
- List<Metric> list = session.getResults(Metric.class, "enabled", true);
- for (Metric metric : list) {
- metrics.put(metric.getKey(), metric);
- }
- }
-
- public Metric find(String key) {
- return metrics.get(key);
- }
-
- public Collection<Metric> findAll(List<String> metricKeys) {
- List<Metric> result = Lists.newLinkedList();
- for (String metricKey : metricKeys) {
- Metric metric = find(metricKey);
- if (metric != null) {
- result.add(metric);
- }
- }
- return result;
- }
-
- public Collection<Metric> findAll() {
- return metrics.values();
- }
-}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java
index 86fe3f38b58..8aedf065a5e 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java
@@ -31,10 +31,9 @@ import org.sonar.api.measures.Metric;
import org.sonar.api.measures.MetricFinder;
import org.sonar.api.resources.Resource;
import org.sonar.batch.index.DefaultIndex;
-import org.sonar.jpa.dao.MeasuresDao;
-import java.util.*;
import javax.persistence.Query;
+import java.util.*;
public class DefaultTimeMachine implements TimeMachine {
@@ -56,7 +55,7 @@ public class DefaultTimeMachine implements TimeMachine {
for (Object[] object : objects) {
MeasureModel model = (MeasureModel) object[0];
- Measure measure = model.toMeasure(metricById.get(model.getMetricId()));
+ Measure measure = toMeasure(model, metricById.get(model.getMetricId()));
measure.setDate((Date) object[1]);
result.add(measure);
}
@@ -93,7 +92,7 @@ public class DefaultTimeMachine implements TimeMachine {
params.put("resourceId", resource.getId());
params.put("status", Snapshot.STATUS_PROCESSED);
- sb.append(" AND m.rule IS NULL AND m.rulePriority IS NULL AND m.rulesCategoryId IS NULL ");
+ sb.append(" AND m.ruleId IS NULL AND m.rulePriority IS NULL AND m.rulesCategoryId IS NULL ");
if (!metricIds.isEmpty()) {
sb.append(" AND m.metricId IN (:metricIds) ");
params.put("metricIds", metricIds);
@@ -136,4 +135,22 @@ public class DefaultTimeMachine implements TimeMachine {
}
return result;
}
+
+ static Measure toMeasure(MeasureModel model, Metric metric) {
+ // NOTE: measures on rule are not supported
+ Measure measure = new Measure(metric);
+ measure.setId(model.getId());
+ measure.setDescription(model.getDescription());
+ measure.setValue(model.getValue());
+ measure.setData(model.getData(metric));
+ measure.setAlertStatus(model.getAlertStatus());
+ measure.setAlertText(model.getAlertText());
+ measure.setTendency(model.getTendency());
+ measure.setDiffValue1(model.getDiffValue1());
+ measure.setDiffValue2(model.getDiffValue2());
+ measure.setDiffValue3(model.getDiffValue3());
+ measure.setUrl(model.getUrl());
+ measure.setCharacteristic(model.getCharacteristic());
+ return measure;
+ }
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java b/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java
index d6843c900dd..104f7eb0660 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java
@@ -38,8 +38,7 @@ import org.sonar.api.utils.SonarException;
import org.sonar.batch.index.DefaultIndex;
import org.sonar.batch.index.DefaultResourcePersister;
import org.sonar.batch.phases.Phases;
-import org.sonar.core.qualitymodel.DefaultModelFinder;
-import org.sonar.core.rule.DefaultRuleFinder;
+import org.sonar.core.components.DefaultModelFinder;
import org.sonar.jpa.dao.*;
import java.util.List;
@@ -93,7 +92,6 @@ public class ProjectBatch {
batchContainer.as(Characteristics.CACHE).addComponent(ViolationFilters.class);
batchContainer.as(Characteristics.CACHE).addComponent(ResourceFilters.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultModelFinder.class);
- batchContainer.as(Characteristics.CACHE).addComponent(DefaultRuleFinder.class);
batchContainer.addAdapter(new ProfileProvider());
batchContainer.addAdapter(new CheckProfileProvider());
loadCoreComponents(batchContainer);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
index 55dbfa90c64..fca1ee8f04b 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
@@ -238,7 +238,7 @@ public final class DefaultIndex extends SonarIndex {
public Measure addMeasure(Resource resource, Measure measure) {
Bucket bucket = getOrAddBucket(resource);
if (!bucket.isExcluded()) {
- Metric metric = metricFinder.find(measure.getMetricKey());
+ Metric metric = metricFinder.findByKey(measure.getMetricKey());
if (metric == null) {
throw new SonarException("Unknown metric: " + measure.getMetricKey());
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java
index c4e34052b4e..4b7c47b21fc 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java
@@ -32,6 +32,9 @@ import org.sonar.api.measures.RuleMeasure;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.ResourceUtils;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.api.utils.SonarException;
import java.util.Collection;
import java.util.Map;
@@ -42,10 +45,12 @@ public final class MeasurePersister {
private SetMultimap<Resource, Measure> unsavedMeasuresByResource = LinkedHashMultimap.create();
private DatabaseSession session;
private ResourcePersister resourcePersister;
+ private RuleFinder ruleFinder;
- public MeasurePersister(DatabaseSession session, ResourcePersister resourcePersister) {
+ public MeasurePersister(DatabaseSession session, ResourcePersister resourcePersister, RuleFinder ruleFinder) {
this.session = session;
this.resourcePersister = resourcePersister;
+ this.ruleFinder = ruleFinder;
}
public void setDelayedMode(boolean delayedMode) {
@@ -136,7 +141,14 @@ public final class MeasurePersister {
RuleMeasure ruleMeasure = (RuleMeasure) measure;
merge.setRulesCategoryId(ruleMeasure.getRuleCategory());
merge.setRulePriority(ruleMeasure.getRulePriority());
- merge.setRule(ruleMeasure.getRule());
+ if (ruleMeasure.getRule()!=null) {
+ Rule ruleWithId = ruleFinder.findByKey(ruleMeasure.getRule().getRepositoryKey(), ruleMeasure.getRule().getKey());
+ if (ruleWithId!=null) {
+ merge.setRuleId(ruleWithId.getId());
+ } else {
+ throw new SonarException("Can not save a measure with unknown rule " + ruleMeasure);
+ }
+ }
}
return merge;
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java
index f5d2e9661c8..934aaadb7bd 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java
@@ -19,29 +19,28 @@
*/
package org.sonar.batch.index;
-import com.google.common.collect.Maps;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.RuleFailureModel;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.Violation;
-import org.sonar.api.utils.SonarException;
import java.util.Collections;
import java.util.List;
-import java.util.Map;
public final class ViolationPersister {
private DatabaseSession session;
private ResourcePersister resourcePersister;
- private Map<Rule, Integer> ruleIds = Maps.newHashMap();
+ private RuleFinder ruleFinder;
- public ViolationPersister(DatabaseSession session, ResourcePersister resourcePersister) {
+ public ViolationPersister(DatabaseSession session, ResourcePersister resourcePersister, RuleFinder ruleFinder) {
this.session = session;
this.resourcePersister = resourcePersister;
+ this.ruleFinder = ruleFinder;
}
public List<RuleFailureModel> getPreviousViolations(Resource resource) {
@@ -76,28 +75,12 @@ public final class ViolationPersister {
}
private RuleFailureModel mergeModel(Violation violation, RuleFailureModel merge) {
- merge.setRuleId(getRuleId(violation.getRule()));
+ Rule rule = ruleFinder.findByKey(violation.getRule().getRepositoryKey(), violation.getRule().getKey());
+ merge.setRuleId(rule.getId());
merge.setPriority(violation.getPriority());
merge.setLine(violation.getLineId());
merge.setMessage(violation.getMessage());
merge.setCost(violation.getCost());
return merge;
}
-
- private Integer getRuleId(Rule rule) {
- Integer ruleId = ruleIds.get(rule);
- if (ruleId == null) {
- ruleId = rule.getId();
- if (ruleId == null) {
- Rule persistedRule = session.getSingleResult(Rule.class,
- "pluginName", rule.getRepositoryKey(), "key", rule.getKey(), "enabled", true);
- if (persistedRule == null) {
- throw new SonarException("Rule not found: " + rule);
- }
- ruleId = persistedRule.getId();
- }
- ruleIds.put(rule, ruleId);
- }
- return ruleId;
- }
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java
index 8d847fe0c73..5c556b8a53a 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java
@@ -31,6 +31,7 @@ import org.sonar.api.measures.PersistenceMode;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.JavaPackage;
import org.sonar.api.resources.Project;
+import org.sonar.core.components.DefaultRuleFinder;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import java.util.List;
@@ -63,7 +64,7 @@ public class MeasurePersisterTest extends AbstractDbUnitTestCase {
when(resourcePersister.saveResource((Project) anyObject(), eq(aPackage))).thenReturn(packageSnapshot);
when(resourcePersister.getSnapshot(project)).thenReturn(projectSnapshot);
when(resourcePersister.getSnapshot(aPackage)).thenReturn(packageSnapshot);
- measurePersister = new MeasurePersister(getSession(), resourcePersister);
+ measurePersister = new MeasurePersister(getSession(), resourcePersister, new DefaultRuleFinder(getSessionFactory()));
}
@Test
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java
index 0c6b898813d..f4b3164345f 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java
@@ -28,6 +28,7 @@ import org.sonar.api.resources.Project;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RulePriority;
import org.sonar.api.rules.Violation;
+import org.sonar.core.components.DefaultRuleFinder;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import java.util.List;
@@ -55,7 +56,7 @@ public class ViolationPersisterTest extends AbstractDbUnitTestCase {
when(resourcePersister.saveResource((Project) anyObject(), eq(javaFile))).thenReturn(snapshot);
when(resourcePersister.getPreviousLastSnapshot(snapshot)).thenReturn(snapshot);
when(resourcePersister.getSnapshot(javaFile)).thenReturn(snapshot);
- violationPersister = new ViolationPersister(getSession(), resourcePersister);
+ violationPersister = new ViolationPersister(getSession(), resourcePersister, new DefaultRuleFinder(getSessionFactory()));
}
@Test