diff options
23 files changed, 348 insertions, 199 deletions
@@ -14,6 +14,7 @@ <module>sonar-application</module> <module>sonar-batch</module> <module>sonar-batch-maven-compat</module> + <module>sonar-batch-protocol</module> <module>sonar-check-api</module> <module>sonar-colorizer</module> <module>sonar-core</module> @@ -530,6 +531,11 @@ </dependency> <dependency> <groupId>org.codehaus.sonar</groupId> + <artifactId>sonar-batch-protocol</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.codehaus.sonar</groupId> <artifactId>sonar-deprecated</artifactId> <version>${project.version}</version> </dependency> diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Metric.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Metric.java new file mode 100644 index 00000000000..58f6ea49119 --- /dev/null +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/Metric.java @@ -0,0 +1,41 @@ +/* + * 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.protocol.input; + +public class Metric { + + private final String key; + + private final String valueType; + + public Metric(String key, String valueType) { + this.key = key; + this.valueType = valueType; + } + + public String key() { + return key; + } + + public String valueType() { + return valueType; + } + +} diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java index 5e672051a10..bff1a434069 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java @@ -24,27 +24,21 @@ import com.google.gson.Gson; import java.io.Reader; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; -import java.util.Map; public class ProjectReferentials { private long timestamp; private Collection<Language> languages = new ArrayList<Language>(); - private Map<String, Map<String, String>> projectSettings = new HashMap<String, Map<String, String>>(); - - public Map<String, String> projectSettings(String projectOrSubProjectKey) { - return projectSettings.get(projectOrSubProjectKey); - } - - public void setProjectSettings(String projectOrSubProjectKey, Map<String, String> projectSettings) { - this.projectSettings.put(projectOrSubProjectKey, projectSettings); - } + private Collection<Metric> metrics = new ArrayList<Metric>(); public Collection<Language> languages() { return languages; } + public Collection<Metric> metrics() { + return metrics; + } + public long timestamp() { return timestamp; } diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java index 037517958b4..e4d5142f10d 100644 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java @@ -24,7 +24,6 @@ import org.junit.Test; import org.skyscreamer.jsonassert.JSONAssert; import java.io.StringReader; -import java.util.HashMap; import static org.fest.assertions.Assertions.assertThat; @@ -33,17 +32,19 @@ public class ProjectReferentialsTest { @Test public void testToJson() throws JSONException { ProjectReferentials ref = new ProjectReferentials(); - HashMap<String, String> projectSettings = new HashMap<String, String>(); - projectSettings.put("sonar.foo", "bar"); - ref.setProjectSettings("foo", projectSettings); + ref.metrics().add(new Metric("ncloc", "INT")); - JSONAssert.assertEquals("{languages: [], projectSettings: {foo: {'sonar.foo': 'bar'}}, timestamp: 0}", ref.toJson(), true); + System.out.println(ref.toJson()); + JSONAssert.assertEquals("{timestamp:0,languages:[],metrics:[{key:ncloc,valueType:INT}]}", ref.toJson(), true); } @Test public void testFromJson() throws JSONException { - ProjectReferentials ref = ProjectReferentials.fromJson(new StringReader("{languages: [], projectSettings: {foo: {'sonar.foo': 'bar'}}, timestamp: 1}")); + ProjectReferentials ref = ProjectReferentials.fromJson(new StringReader("{timestamp:1,languages:[],metrics:[{key:ncloc,valueType:INT}]}")); assertThat(ref.timestamp()).isEqualTo(1); + Metric metric = ref.metrics().iterator().next(); + assertThat(metric.key()).isEqualTo("ncloc"); + assertThat(metric.valueType()).isEqualTo("INT"); } } 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-core/src/main/java/org/sonar/core/metric/CacheMetricFinder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DeprecatedMetricFinder.java index cd8a1d2a8f9..c2a8a7e6399 100644 --- a/sonar-core/src/main/java/org/sonar/core/metric/CacheMetricFinder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/DeprecatedMetricFinder.java @@ -17,37 +17,32 @@ * 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; +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.jpa.session.DatabaseSessionFactory; +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 CacheMetricFinder extends DefaultMetricFinder { +public final class DeprecatedMetricFinder implements MetricFinder { private Map<String, Metric> metricsByKey = Maps.newLinkedHashMap(); - private Map<Integer, Metric> metricsById = Maps.newLinkedHashMap(); - public CacheMetricFinder(DatabaseSessionFactory sessionFactory) { - super(sessionFactory); - } - - public void start() { - Collection<Metric> metrics = doFindAll(); - for (Metric metric : metrics) { - metricsByKey.put(metric.getKey(), metric); - metricsById.put(metric.getId(), metric); + 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) { - return metricsById.get(metricId); + throw new UnsupportedOperationException("Metric id is not available on batch side"); } @Override 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(); } diff --git a/sonar-core/src/test/java/org/sonar/core/metric/CacheMetricFinderTest.java b/sonar-core/src/test/java/org/sonar/core/metric/CacheMetricFinderTest.java deleted file mode 100644 index 515c8e71835..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/metric/CacheMetricFinderTest.java +++ /dev/null @@ -1,66 +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 org.junit.Before; -import org.junit.Test; -import org.sonar.core.metric.CacheMetricFinder; -import org.sonar.jpa.test.AbstractDbUnitTestCase; - -import java.util.Arrays; - -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsNull.nullValue; -import static org.junit.Assert.assertThat; - - -public class CacheMetricFinderTest extends AbstractDbUnitTestCase { - - private CacheMetricFinder finder; - - @Before - public void initFinder() { - setupData("shared"); - finder = new CacheMetricFinder(getSessionFactory()); - finder.start(); - } - - @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-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java index 7b5a059b680..a5fa7ab0d93 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java @@ -165,6 +165,18 @@ public class ProjectDefinition { return properties.getProperty(CoreProperties.PROJECT_KEY_PROPERTY); } + /** + * @since 4.5 + */ + public String getKeyWithBranch() { + String branch = properties.getProperty(CoreProperties.PROJECT_BRANCH_PROPERTY); + String projectKey = getKey(); + if (StringUtils.isNotBlank(branch)) { + projectKey = String.format("%s:%s", projectKey, branch); + } + return projectKey; + } + public String getVersion() { return properties.getProperty(CoreProperties.PROJECT_VERSION_PROPERTY); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/measure/MetricFinder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/measure/MetricFinder.java new file mode 100644 index 00000000000..4f91ea5aae4 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/measure/MetricFinder.java @@ -0,0 +1,40 @@ +/* + * 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.api.batch.measure; + +import org.sonar.api.BatchComponent; + +import javax.annotation.CheckForNull; + +import java.util.Collection; +import java.util.List; + +/** + * @since 4.5 + */ +public interface MetricFinder extends BatchComponent { + + @CheckForNull + Metric findByKey(String key); + + Collection<Metric> findAll(List<String> metricKeys); + + Collection<Metric> findAll(); +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/MetricFinder.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/MetricFinder.java index c6f68aaedce..5d9aefd6013 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/MetricFinder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/MetricFinder.java @@ -29,7 +29,9 @@ import java.util.List; /** * @since 2.5 + * @deprecated since 4.5 use {@link org.sonar.api.batch.measure.MetricFinder} on batch side */ +@Deprecated public interface MetricFinder extends TaskComponent, ServerComponent { @CheckForNull |