diff options
author | Godin <mandrikov@gmail.com> | 2010-09-24 16:17:56 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-09-24 16:17:56 +0000 |
commit | a35d6e4e1779800cbc2960e6547de29ed861ae34 (patch) | |
tree | ff56980577cf0f7d2d0e70c0565814bf20c464a2 /sonar-server | |
parent | 51c728457cb0ea1763625d3600bddc2c3d7f049b (diff) | |
download | sonarqube-a35d6e4e1779800cbc2960e6547de29ed861ae34.tar.gz sonarqube-a35d6e4e1779800cbc2960e6547de29ed861ae34.zip |
SONAR-1634: Provide meaningful error message, when plugin tries to define metric, which already defined in other plugin or in core
Diffstat (limited to 'sonar-server')
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/startup/RegisterMetrics.java | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterMetrics.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterMetrics.java index 414fa24fe16..4b66047e7d2 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterMetrics.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterMetrics.java @@ -20,27 +20,36 @@ package org.sonar.server.startup;
import org.sonar.api.database.DatabaseSession;
-import org.sonar.jpa.dao.MeasuresDao;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metrics;
+import org.sonar.api.platform.PluginRepository;
import org.sonar.api.profiles.Alert;
import org.sonar.api.utils.Logs;
import org.sonar.api.utils.TimeProfiler;
+import org.sonar.jpa.dao.MeasuresDao;
+import org.sonar.server.platform.ServerStartException;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+
import javax.persistence.Query;
public class RegisterMetrics {
+ private PluginRepository pluginRepository;
private MeasuresDao measuresDao;
private Metrics[] metricsRepositories = null;
private DatabaseSession session;
- public RegisterMetrics(DatabaseSession session, MeasuresDao measuresDao, Metrics[] metricsRepositories) {
+ public RegisterMetrics(DatabaseSession session, MeasuresDao measuresDao, PluginRepository pluginRepository, Metrics[] metricsRepositories) {
this.session = session;
this.measuresDao = measuresDao;
+ this.pluginRepository = pluginRepository;
this.metricsRepositories = metricsRepositories;
}
@@ -51,10 +60,14 @@ public class RegisterMetrics { public void start() {
TimeProfiler profiler = new TimeProfiler().start("Load metrics");
measuresDao.disableAutomaticMetrics();
- List<Metric> metricsToRegister = new ArrayList<Metric>();
+
+ ArrayList<Metric> metricsToRegister = Lists.newArrayList();
metricsToRegister.addAll(CoreMetrics.getMetrics());
+
+ HashMap<String, Metrics> metricsByRepository = Maps.newHashMap();
if (metricsRepositories != null) {
for (Metrics metrics : metricsRepositories) {
+ checkMetrics(metricsByRepository, metrics);
metricsToRegister.addAll(metrics.getMetrics());
}
}
@@ -63,6 +76,22 @@ public class RegisterMetrics { profiler.stop();
}
+ private void checkMetrics(HashMap<String, Metrics> metricsByRepository, Metrics metrics) {
+ for (Metric metric : metrics.getMetrics()) {
+ String metricKey = metric.getKey();
+ if (CoreMetrics.metrics.contains(metric)) {
+ throw new ServerStartException("Found plugin, which contains metric '" + metricKey + "' from core: " + pluginRepository.getPluginKeyForExtension(metrics));
+ }
+ Metrics anotherRepository = metricsByRepository.get(metricKey);
+ if (anotherRepository != null) {
+ String firstPlugin = pluginRepository.getPluginKeyForExtension(anotherRepository);
+ String secondPlugin = pluginRepository.getPluginKeyForExtension(metrics);
+ throw new ServerStartException("Found two plugins with the same metric '" + metricKey + "': " + firstPlugin + " and " + secondPlugin);
+ }
+ metricsByRepository.put(metricKey, metrics);
+ }
+ }
+
protected void cleanAlerts() {
Logs.INFO.info("cleaning alert thresholds...");
Query query = session.createQuery("delete from " + Alert.class.getSimpleName() + " a where NOT EXISTS(FROM Metric m WHERE m=a.metric))");
|