aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2014-07-18 09:51:04 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2014-07-18 15:43:39 +0200
commit274e9cefeb16fab1d5e35088289338f12993b208 (patch)
tree33e9e5a364815bd748973a568caf60a9a66019b9 /sonar-batch
parent9d7b13c044e50067989658700ca307b7ce4804cd (diff)
downloadsonarqube-274e9cefeb16fab1d5e35088289338f12993b208.tar.gz
sonarqube-274e9cefeb16fab1d5e35088289338f12993b208.zip
SONAR-5417 Load metrics using project referential
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/pom.xml4
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java10
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java65
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java48
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsLoader.java27
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/referential/package-info.java23
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java8
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java8
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettings.java8
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/measure/DefaultMetricFinder.java65
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/measure/DeprecatedMetricFinder.java69
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContext.java5
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContextFactory.java5
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java5
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/lang/MeasureAnalyzer.java47
15 files changed, 295 insertions, 102 deletions
diff --git a/sonar-batch/pom.xml b/sonar-batch/pom.xml
index 2e56504cd13..1ba3a32d61f 100644
--- a/sonar-batch/pom.xml
+++ b/sonar-batch/pom.xml
@@ -30,6 +30,10 @@
</dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-batch-protocol</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-deprecated</artifactId>
</dependency>
<dependency>
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java
index e0f8e64c090..7f89a3b0eaf 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java
@@ -23,7 +23,6 @@ import org.apache.commons.configuration.PropertiesConfiguration;
import org.sonar.api.CoreProperties;
import org.sonar.api.Plugin;
import org.sonar.api.config.EmailSettings;
-import org.sonar.api.measures.MetricFinder;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.platform.PluginMetadata;
import org.sonar.api.rules.RuleFinder;
@@ -40,6 +39,8 @@ import org.sonar.batch.components.PastSnapshotFinderByPreviousAnalysis;
import org.sonar.batch.components.PastSnapshotFinderByPreviousVersion;
import org.sonar.batch.components.PastSnapshotFinderByVersion;
import org.sonar.batch.debt.DebtModelProvider;
+import org.sonar.batch.referential.DefaultProjectReferentialsLoader;
+import org.sonar.batch.referential.ProjectReferentialsLoader;
import org.sonar.batch.rule.RulesProvider;
import org.sonar.batch.rules.DefaultQProfileReferential;
import org.sonar.batch.rules.QProfilesReferential;
@@ -49,7 +50,6 @@ import org.sonar.core.cluster.NullQueue;
import org.sonar.core.config.Logback;
import org.sonar.core.i18n.DefaultI18n;
import org.sonar.core.i18n.RuleI18nManager;
-import org.sonar.core.metric.CacheMetricFinder;
import org.sonar.core.persistence.DaoUtils;
import org.sonar.core.persistence.DatabaseVersion;
import org.sonar.core.persistence.MyBatis;
@@ -110,6 +110,9 @@ public class BootstrapContainer extends ComponentContainer {
UriReader.class,
new FileCacheProvider(),
System2.INSTANCE);
+ if (getComponentByType(ProjectReferentialsLoader.class) == null) {
+ add(DefaultProjectReferentialsLoader.class);
+ }
if (getComponentByType(SettingsReferential.class) == null) {
add(DefaultSettingsReferential.class);
}
@@ -119,9 +122,6 @@ public class BootstrapContainer extends ComponentContainer {
if (getComponentByType(RuleFinder.class) == null) {
add(CacheRuleFinder.class);
}
- if (getComponentByType(MetricFinder.class) == null) {
- add(CacheMetricFinder.class);
- }
if (getComponentByType(QProfilesReferential.class) == null) {
add(DefaultQProfileReferential.class);
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java
index a141d3c83c4..13d43e7846b 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/mediumtest/AnalyzerMediumTester.java
@@ -21,7 +21,6 @@ package org.sonar.batch.mediumtest;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
-import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.io.IOUtils;
import org.sonar.api.SonarPlugin;
@@ -33,7 +32,6 @@ import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.rule.internal.RulesBuilder;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.MetricFinder;
import org.sonar.api.platform.PluginMetadata;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.Rule;
@@ -44,6 +42,8 @@ import org.sonar.batch.bootstrapper.Batch;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
import org.sonar.batch.languages.Language;
import org.sonar.batch.languages.LanguagesReferential;
+import org.sonar.batch.protocol.input.ProjectReferentials;
+import org.sonar.batch.referential.ProjectReferentialsLoader;
import org.sonar.batch.rule.QProfile;
import org.sonar.batch.rules.QProfilesReferential;
import org.sonar.batch.scan.filesystem.InputFileCache;
@@ -75,9 +75,9 @@ public class AnalyzerMediumTester {
}
public static class AnalyzerMediumTesterBuilder {
+ private final FakeProjectReferentialsLoader refProvider = new FakeProjectReferentialsLoader();
private final FakeSettingsReferential settingsReferential = new FakeSettingsReferential();
private final FackPluginsReferential pluginsReferential = new FackPluginsReferential();
- private final FakeMetricFinder metricFinder = new FakeMetricFinder();
private final FakeRuleFinder ruleFinder = new FakeRuleFinder();
private final FakeQProfileReferential qProfileReferential = new FakeQProfileReferential();
private final FakeLanguageReferential languageReferential = new FakeLanguageReferential();
@@ -108,7 +108,7 @@ public class AnalyzerMediumTester {
}
public AnalyzerMediumTesterBuilder registerMetric(Metric<?> metric) {
- metricFinder.add(metricId, metric);
+ refProvider.add(metric);
metricId++;
return this;
}
@@ -162,7 +162,7 @@ public class AnalyzerMediumTester {
new EnvironmentInformation("mediumTest", "1.0"),
builder.settingsReferential,
builder.pluginsReferential,
- builder.metricFinder,
+ builder.refProvider,
builder.ruleFinder,
builder.qProfileReferential,
builder.rulesBuilder.build(),
@@ -261,6 +261,21 @@ public class AnalyzerMediumTester {
}
+ private static class FakeProjectReferentialsLoader implements ProjectReferentialsLoader {
+
+ private ProjectReferentials ref = new ProjectReferentials();
+
+ @Override
+ public ProjectReferentials load(String projectKey) {
+ return ref;
+ }
+
+ public FakeProjectReferentialsLoader add(Metric metric) {
+ ref.metrics().add(new org.sonar.batch.protocol.input.Metric(metric.key(), metric.getType().name()));
+ return this;
+ }
+ }
+
private static class FakeSettingsReferential implements SettingsReferential {
private Map<String, String> globalSettings = new HashMap<String, String>();
@@ -313,46 +328,6 @@ public class AnalyzerMediumTester {
}
- private static class FakeMetricFinder implements MetricFinder {
-
- private Map<String, Metric> metricsByKey = Maps.newLinkedHashMap();
- private Map<Integer, Metric> metricsById = Maps.newLinkedHashMap();
-
- public FakeMetricFinder add(int id, Metric metric) {
- metricsByKey.put(metric.getKey(), metric);
- metricsById.put(id, metric);
- return this;
- }
-
- @Override
- public Metric findById(int metricId) {
- return metricsById.get(metricId);
- }
-
- @Override
- public Metric findByKey(String key) {
- return metricsByKey.get(key);
- }
-
- @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 metricsByKey.values();
- }
-
- }
-
private static class FakeRuleFinder implements RuleFinder {
private BiMap<Integer, Rule> rulesById = HashBiMap.create();
private Map<String, Map<String, Rule>> rulesByRepoKeyAndRuleKey = Maps.newHashMap();
diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
new file mode 100644
index 00000000000..9f6bb327594
--- /dev/null
+++ b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
@@ -0,0 +1,48 @@
+/*
+ * 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.batch.referential;
+
+import org.sonar.api.measures.Metric;
+import org.sonar.batch.protocol.input.ProjectReferentials;
+import org.sonar.jpa.session.DatabaseSessionFactory;
+
+import java.util.Collection;
+
+public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoader {
+
+ private static final String ENABLED = "enabled";
+ private DatabaseSessionFactory sessionFactory;
+
+ protected Collection<Metric> doFindAll() {
+ return sessionFactory.getSession().getResults(Metric.class, ENABLED, true);
+ }
+
+ public DefaultProjectReferentialsLoader(DatabaseSessionFactory sessionFactory) {
+ this.sessionFactory = sessionFactory;
+ }
+
+ public ProjectReferentials load(String projectKey) {
+ ProjectReferentials ref = new ProjectReferentials();
+ for (Metric m : sessionFactory.getSession().getResults(Metric.class, ENABLED, true)) {
+ ref.metrics().add(new org.sonar.batch.protocol.input.Metric(m.getKey(), m.getType().name()));
+ }
+ return ref;
+ }
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsLoader.java
new file mode 100644
index 00000000000..3dff3784414
--- /dev/null
+++ b/sonar-batch/src/main/java/org/sonar/batch/referential/ProjectReferentialsLoader.java
@@ -0,0 +1,27 @@
+/*
+ * 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.batch.referential;
+
+import org.sonar.batch.protocol.input.ProjectReferentials;
+
+public interface ProjectReferentialsLoader {
+
+ ProjectReferentials load(String projectKey);
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/referential/package-info.java
new file mode 100644
index 00000000000..1a50ff50cdf
--- /dev/null
+++ b/sonar-batch/src/main/java/org/sonar/batch/referential/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.batch.referential;
+
+import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java
index b1fc05905c5..31201d4de9d 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleSettings.java
@@ -21,7 +21,6 @@ package org.sonar.batch.scan;
import com.google.common.collect.Lists;
import org.apache.commons.configuration.Configuration;
-import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
@@ -63,13 +62,8 @@ public class ModuleSettings extends Settings {
}
private void addProjectProperties(ProjectDefinition project, GlobalSettings batchSettings) {
- String branch = batchSettings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY);
- String projectKey = project.getKey();
- if (StringUtils.isNotBlank(branch)) {
- projectKey = String.format("%s:%s", projectKey, branch);
- }
addProperties(batchSettings.getProperties());
- addProperties(settingsReferential.projectSettings(projectKey));
+ addProperties(settingsReferential.projectSettings(project.getKeyWithBranch()));
}
private void addBuildProperties(ProjectDefinition project) {
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
index 588fb02fb77..e7b22e32a1b 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
@@ -63,9 +63,12 @@ import org.sonar.batch.issue.ScanIssueStorage;
import org.sonar.batch.languages.DeprecatedLanguagesReferential;
import org.sonar.batch.phases.GraphPersister;
import org.sonar.batch.profiling.PhasesSumUpTimeProfiler;
+import org.sonar.batch.referential.ProjectReferentialsLoader;
import org.sonar.batch.scan.filesystem.InputFileCache;
import org.sonar.batch.scan.maven.FakeMavenPluginExecutor;
import org.sonar.batch.scan.maven.MavenPluginExecutor;
+import org.sonar.batch.scan.measure.DefaultMetricFinder;
+import org.sonar.batch.scan.measure.DeprecatedMetricFinder;
import org.sonar.batch.scan.measure.MeasureCache;
import org.sonar.batch.source.HighlightableBuilder;
import org.sonar.batch.source.SymbolizableBuilder;
@@ -120,6 +123,9 @@ public class ProjectScanContainer extends ComponentContainer {
}
add(reactor);
}
+ ProjectReferentialsLoader projectReferentialsLoader = getComponentByType(ProjectReferentialsLoader.class);
+ add(projectReferentialsLoader.load(reactor.getRoot().getKeyWithBranch()));
+
}
private void addBatchComponents() {
@@ -185,6 +191,8 @@ public class ProjectScanContainer extends ComponentContainer {
// Measures
MeasureCache.class,
+ DeprecatedMetricFinder.class,
+ DefaultMetricFinder.class,
ProjectSettings.class);
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettings.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettings.java
index 7ad8745ef79..bfff4d73aab 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettings.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectSettings.java
@@ -20,7 +20,6 @@
package org.sonar.batch.scan;
import org.apache.commons.configuration.Configuration;
-import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
@@ -60,12 +59,7 @@ public class ProjectSettings extends Settings {
addProperties(globalSettings.getProperties());
- String branch = reactor.getRoot().getProperties().getProperty(CoreProperties.PROJECT_BRANCH_PROPERTY);
- String projectKey = reactor.getRoot().getKey();
- if (StringUtils.isNotBlank(branch)) {
- projectKey = String.format("%s:%s", projectKey, branch);
- }
- addProperties(settingsReferential.projectSettings(projectKey));
+ addProperties(settingsReferential.projectSettings(reactor.getRoot().getKeyWithBranch()));
addProperties(reactor.getRoot().getProperties());
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DefaultMetricFinder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DefaultMetricFinder.java
new file mode 100644
index 00000000000..4881bf8a6cd
--- /dev/null
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DefaultMetricFinder.java
@@ -0,0 +1,65 @@
+/*
+ * 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.batch.scan.measure;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.sonar.api.batch.measure.Metric;
+import org.sonar.api.batch.measure.MetricFinder;
+import org.sonar.api.measures.Metric.ValueType;
+import org.sonar.batch.protocol.input.ProjectReferentials;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+public class DefaultMetricFinder implements MetricFinder {
+
+ private Map<String, Metric> metricsByKey = Maps.newLinkedHashMap();
+
+ public DefaultMetricFinder(ProjectReferentials projectReferentials) {
+ for (org.sonar.batch.protocol.input.Metric metric : projectReferentials.metrics()) {
+ metricsByKey.put(metric.key(), new org.sonar.api.measures.Metric.Builder(metric.key(), metric.key(), ValueType.valueOf(metric.valueType())).create());
+ }
+ }
+
+ @Override
+ public Metric findByKey(String key) {
+ return metricsByKey.get(key);
+ }
+
+ @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 metricsByKey.values();
+ }
+
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DeprecatedMetricFinder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DeprecatedMetricFinder.java
new file mode 100644
index 00000000000..c2a8a7e6399
--- /dev/null
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DeprecatedMetricFinder.java
@@ -0,0 +1,69 @@
+/*
+ * 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.batch.scan.measure;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.Metric.ValueType;
+import org.sonar.api.measures.MetricFinder;
+import org.sonar.batch.protocol.input.ProjectReferentials;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+public final class DeprecatedMetricFinder implements MetricFinder {
+
+ private Map<String, Metric> metricsByKey = Maps.newLinkedHashMap();
+
+ public DeprecatedMetricFinder(ProjectReferentials projectReferentials) {
+ for (org.sonar.batch.protocol.input.Metric metric : projectReferentials.metrics()) {
+ metricsByKey.put(metric.key(), new org.sonar.api.measures.Metric.Builder(metric.key(), metric.key(), ValueType.valueOf(metric.valueType())).create());
+ }
+ }
+
+ @Override
+ public Metric findById(int metricId) {
+ throw new UnsupportedOperationException("Metric id is not available on batch side");
+ }
+
+ @Override
+ public Metric findByKey(String key) {
+ return metricsByKey.get(key);
+ }
+
+ @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 metricsByKey.values();
+ }
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContext.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContext.java
index 729a4076d3d..e8cb04faf3b 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContext.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContext.java
@@ -26,9 +26,8 @@ import com.google.common.collect.Maps;
import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
import org.sonar.api.batch.analyzer.measure.internal.DefaultAnalyzerMeasureBuilder;
import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.measures.FileLinesContext;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.MetricFinder;
import org.sonar.api.utils.KeyValueFormat;
import org.sonar.api.utils.KeyValueFormat.Converter;
import org.sonar.core.component.ComponentKeys;
@@ -112,7 +111,7 @@ public class DefaultFileLinesContext implements FileLinesContext {
public void save() {
for (Map.Entry<String, Map<Integer, Object>> entry : map.entrySet()) {
String metricKey = entry.getKey();
- Metric metric = metricFinder.findByKey(metricKey);
+ org.sonar.api.batch.measure.Metric<String> metric = metricFinder.findByKey(metricKey);
if (metric == null) {
throw new IllegalStateException("Unable to find metric with key: " + metricKey);
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContextFactory.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContextFactory.java
index 9dd70c7f3e8..addc6e18e3c 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContextFactory.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultFileLinesContextFactory.java
@@ -22,9 +22,9 @@ package org.sonar.batch.scan2;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
-import org.sonar.api.measures.MetricFinder;
import org.sonar.api.resources.Resource;
import org.sonar.batch.scan.filesystem.InputFileCache;
@@ -35,7 +35,8 @@ public class DefaultFileLinesContextFactory implements FileLinesContextFactory {
private final ProjectDefinition def;
private InputFileCache fileCache;
- public DefaultFileLinesContextFactory(InputFileCache fileCache, FileSystem fs, MetricFinder metricFinder, AnalyzerMeasureCache measureCache, ProjectDefinition def) {
+ public DefaultFileLinesContextFactory(InputFileCache fileCache, FileSystem fs, MetricFinder metricFinder, AnalyzerMeasureCache measureCache,
+ ProjectDefinition def) {
this.fileCache = fileCache;
this.metricFinder = metricFinder;
this.measureCache = measureCache;
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java
index b306a0ba35f..097f835afd8 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan2/ProjectScanContainer.java
@@ -34,11 +34,13 @@ import org.sonar.batch.bootstrap.ExtensionMatcher;
import org.sonar.batch.bootstrap.ExtensionUtils;
import org.sonar.batch.index.Caches;
import org.sonar.batch.profiling.PhasesSumUpTimeProfiler;
+import org.sonar.batch.referential.ProjectReferentialsLoader;
import org.sonar.batch.scan.ProjectReactorBuilder;
import org.sonar.batch.scan.ProjectSettings;
import org.sonar.batch.scan.filesystem.InputFileCache;
import org.sonar.batch.scan.maven.FakeMavenPluginExecutor;
import org.sonar.batch.scan.maven.MavenPluginExecutor;
+import org.sonar.batch.scan.measure.DefaultMetricFinder;
public class ProjectScanContainer extends ComponentContainer {
public ProjectScanContainer(ComponentContainer taskContainer) {
@@ -74,6 +76,8 @@ public class ProjectScanContainer extends ComponentContainer {
throw new IllegalStateException(bootstrapper + " has returned null as ProjectReactor");
}
add(reactor);
+ ProjectReferentialsLoader projectReferentialsLoader = getComponentByType(ProjectReferentialsLoader.class);
+ add(projectReferentialsLoader.load(reactor.getRoot().getKeyWithBranch()));
}
private void addBatchComponents() {
@@ -82,6 +86,7 @@ public class ProjectScanContainer extends ComponentContainer {
Caches.class,
// Measures
+ DefaultMetricFinder.class,
AnalyzerMeasureCache.class,
// file system
diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/lang/MeasureAnalyzer.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/lang/MeasureAnalyzer.java
index 8da7690333f..92d00406c48 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/lang/MeasureAnalyzer.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/xoo/plugin/lang/MeasureAnalyzer.java
@@ -27,9 +27,8 @@ import org.sonar.api.batch.analyzer.AnalyzerDescriptor;
import org.sonar.api.batch.analyzer.measure.AnalyzerMeasure;
import org.sonar.api.batch.analyzer.measure.AnalyzerMeasureBuilder;
import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.MetricFinder;
import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo;
import org.sonar.batch.mediumtest.xoo.plugin.base.XooConstants;
@@ -82,40 +81,22 @@ public class MeasureAnalyzer implements Analyzer {
}
private AnalyzerMeasure<?> createMeasure(AnalyzerContext context, InputFile xooFile, String metricKey, String value) {
- Metric metric = metricFinder.findByKey(metricKey);
+ org.sonar.api.batch.measure.Metric<Serializable> metric = metricFinder.findByKey(metricKey);
AnalyzerMeasureBuilder<Serializable> builder = context.measureBuilder()
.forMetric(metric)
.onFile(xooFile);
- switch (metric.getType()) {
- case BOOL:
- builder.withValue(Boolean.parseBoolean(value));
- break;
- case INT:
- case MILLISEC:
- builder.withValue(Integer.valueOf(value));
- break;
- case FLOAT:
- case PERCENT:
- case RATING:
- builder.withValue(Double.valueOf(value));
- break;
- case STRING:
- case LEVEL:
- case DATA:
- case DISTRIB:
- builder.withValue(value);
- break;
- case WORK_DUR:
- builder.withValue(Long.valueOf(value));
- break;
- default:
- if (metric.isNumericType()) {
- builder.withValue(Double.valueOf(value));
- } else if (metric.isDataType()) {
- builder.withValue(value);
- } else {
- throw new UnsupportedOperationException("Unsupported type :" + metric.getType());
- }
+ if (Boolean.class.equals(metric.valueType())) {
+ builder.withValue(Boolean.parseBoolean(value));
+ } else if (Integer.class.equals(metric.valueType())) {
+ builder.withValue(Integer.valueOf(value));
+ } else if (Double.class.equals(metric.valueType())) {
+ builder.withValue(Double.valueOf(value));
+ } else if (String.class.equals(metric.valueType())) {
+ builder.withValue(value);
+ } else if (Long.class.equals(metric.valueType())) {
+ builder.withValue(Long.valueOf(value));
+ } else {
+ throw new UnsupportedOperationException("Unsupported type :" + metric.valueType());
}
return builder.build();
}