diff options
author | Matteo Mara <matteo.mara@sonarsource.com> | 2023-12-06 16:58:03 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-12-21 20:02:33 +0000 |
commit | 0f4fe6e6fb7d11f768c084bbe2ddb7ffd42d7c56 (patch) | |
tree | 456c4b2191c11e11f998428384007fb458d70fab /sonar-core | |
parent | b0af995a136c1ec1b233337f6ee4a15fcd4e0c95 (diff) | |
download | sonarqube-0f4fe6e6fb7d11f768c084bbe2ddb7ffd42d7c56.tar.gz sonarqube-0f4fe6e6fb7d11f768c084bbe2ddb7ffd42d7c56.zip |
SONAR-21195 Enhance scanner engine to download only required plugins
Diffstat (limited to 'sonar-core')
5 files changed, 84 insertions, 11 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/metric/ScannerMetrics.java b/sonar-core/src/main/java/org/sonar/core/metric/ScannerMetrics.java index f63a6acf3fd..50f0c8e0a4a 100644 --- a/sonar-core/src/main/java/org/sonar/core/metric/ScannerMetrics.java +++ b/sonar-core/src/main/java/org/sonar/core/metric/ScannerMetrics.java @@ -19,14 +19,11 @@ */ package org.sonar.core.metric; -import com.google.common.collect.ImmutableSet; -import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; -import javax.annotation.concurrent.Immutable; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.measures.Metric; import org.sonar.api.measures.Metrics; @@ -61,12 +58,11 @@ import static org.sonar.api.measures.CoreMetrics.TEST_FAILURES; * <p/> * Scanners should not send other metrics, and the Compute Engine should not allow other metrics. */ -@Immutable @ComputeEngineSide @ScannerSide public class ScannerMetrics { - private static final Set<Metric> ALLOWED_CORE_METRICS = ImmutableSet.of( + private static final Set<Metric> ALLOWED_CORE_METRICS = Set.of( GENERATED_LINES, NCLOC, NCLOC_DATA, @@ -95,7 +91,7 @@ public class ScannerMetrics { EXECUTABLE_LINES_DATA); - private final Set<Metric> metrics; + private Set<Metric> metrics; @Autowired(required = false) public ScannerMetrics() { @@ -103,8 +99,9 @@ public class ScannerMetrics { } @Autowired(required = false) - public ScannerMetrics(Metrics[] metricsRepositories) { - this.metrics = Stream.concat(getPluginMetrics(metricsRepositories), ALLOWED_CORE_METRICS.stream()).collect(Collectors.toSet()); + public ScannerMetrics(List<Metrics> metricsRepositories) { + this.metrics = ALLOWED_CORE_METRICS; + addPluginMetrics(metricsRepositories); } /** @@ -115,8 +112,15 @@ public class ScannerMetrics { return metrics; } - private static Stream<Metric> getPluginMetrics(Metrics[] metricsRepositories) { - return Arrays.stream(metricsRepositories) + /** + * Adds the given metrics to the set of allowed metrics + */ + public void addPluginMetrics(List<Metrics> metricsRepositories) { + this.metrics = Stream.concat(getPluginMetrics(metricsRepositories.stream()), this.metrics.stream()).collect(Collectors.toSet()); + } + + private static Stream<Metric> getPluginMetrics(Stream<Metrics> metricsStream) { + return metricsStream .map(Metrics::getMetrics) .filter(Objects::nonNull) .flatMap(List::stream); diff --git a/sonar-core/src/main/java/org/sonar/core/platform/ExtensionContainer.java b/sonar-core/src/main/java/org/sonar/core/platform/ExtensionContainer.java index 5a188c04b99..b344eb721eb 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/ExtensionContainer.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/ExtensionContainer.java @@ -19,6 +19,7 @@ */ package org.sonar.core.platform; +import java.util.List; import java.util.Set; import javax.annotation.CheckForNull; import javax.annotation.Nullable; @@ -36,6 +37,10 @@ public interface ExtensionContainer extends Container { Set<Class<?>> getWebApiV2ConfigurationClasses(); + <T> T getParentComponentByType(Class<T> type); + + <T> List<T> getParentComponentsByType(Class<T> type); + @CheckForNull ExtensionContainer getParent(); } diff --git a/sonar-core/src/main/java/org/sonar/core/platform/ListContainer.java b/sonar-core/src/main/java/org/sonar/core/platform/ListContainer.java index 50d314e7e7b..4e87c215136 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/ListContainer.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/ListContainer.java @@ -102,6 +102,16 @@ public class ListContainer implements ExtensionContainer { } @Override + public <T> T getParentComponentByType(Class<T> type) { + throw new UnsupportedOperationException(); + } + + @Override + public <T> List<T> getParentComponentsByType(Class<T> type) { + throw new UnsupportedOperationException(); + } + + @Override public ExtensionContainer getParent() { throw new UnsupportedOperationException(); } diff --git a/sonar-core/src/main/java/org/sonar/core/platform/SpringComponentContainer.java b/sonar-core/src/main/java/org/sonar/core/platform/SpringComponentContainer.java index 079bc8b19cc..6a7afd35c19 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/SpringComponentContainer.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/SpringComponentContainer.java @@ -60,6 +60,10 @@ public class SpringComponentContainer implements StartableContainer { this(parent, parent.propertyDefinitions, emptyList(), new LazyUnlessStartableStrategy()); } + protected SpringComponentContainer(SpringComponentContainer parent, List<?> externalExtensions) { + this(parent, parent.propertyDefinitions, externalExtensions, new LazyUnlessStartableStrategy()); + } + protected SpringComponentContainer(SpringComponentContainer parent, SpringInitStrategy initStrategy) { this(parent, parent.propertyDefinitions, emptyList(), initStrategy); } @@ -81,6 +85,15 @@ public class SpringComponentContainer implements StartableContainer { add(propertyDefs); } + //TODO: To be removed, added for moving on with the non matching LanguagesRepository beans + public void addIfMissing(Object object, Class<?> objectType) { + try { + getParentComponentByType(objectType); + } catch (IllegalStateException e) { + add(object); + } + } + /** * Beans need to have a unique name, otherwise they'll override each other. * The strategy is: @@ -120,6 +133,24 @@ public class SpringComponentContainer implements StartableContainer { return Set.copyOf(webConfigurationClasses); } + @Override + public <T> T getParentComponentByType(Class<T> type) { + if (parent == null) { + throw new IllegalStateException("No parent container"); + } else { + return parent.getComponentByType(type); + } + } + + @Override + public <T> List<T> getParentComponentsByType(Class<T> type) { + if (parent == null) { + throw new IllegalStateException("No parent container"); + } else { + return parent.getComponentsByType(type); + } + } + private <T> void registerInstance(T instance) { Supplier<T> supplier = () -> instance; Class<T> clazz = (Class<T>) instance.getClass(); diff --git a/sonar-core/src/test/java/org/sonar/core/metric/ScannerMetricsTest.java b/sonar-core/src/test/java/org/sonar/core/metric/ScannerMetricsTest.java index 5be329da26d..8186b726d53 100644 --- a/sonar-core/src/test/java/org/sonar/core/metric/ScannerMetricsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/metric/ScannerMetricsTest.java @@ -56,8 +56,23 @@ public class ScannerMetricsTest { assertThat(metrics).isEqualTo(okMetrics.getMetrics()); } + @Test + public void should_add_new_plugin_metrics() { + Metrics fakeMetrics = new FakeMetrics(); + Metrics fakeMetrics2 = new FakeMetrics2(); + + ScannerMetrics underTest = new ScannerMetrics(List.of(fakeMetrics)); + assertThat(underTest.getMetrics()).hasSize(24); + assertThat(underTest.getMetrics()).containsAll(fakeMetrics.getMetrics()); + + underTest.addPluginMetrics(List.of( fakeMetrics, fakeMetrics2 )); + assertThat(underTest.getMetrics()).hasSize(25); + assertThat(underTest.getMetrics()).containsAll(fakeMetrics.getMetrics()); + assertThat(underTest.getMetrics()).containsAll(fakeMetrics2.getMetrics()); + } + private static List<Metric> metrics(Metrics... metrics) { - return new ArrayList<>(new ScannerMetrics(metrics).getMetrics()); + return new ArrayList<>(new ScannerMetrics(Arrays.asList(metrics)).getMetrics()); } private static class FakeMetrics implements Metrics { @@ -68,4 +83,12 @@ public class ScannerMetricsTest { new Metric.Builder("key2", "name2", Metric.ValueType.FLOAT).create()); } } + + private static class FakeMetrics2 implements Metrics { + @Override + public List<Metric> getMetrics() { + return Arrays.asList( + new Metric.Builder("key3", "name1", Metric.ValueType.INT).create()); + } + } } |