diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2018-05-25 11:23:29 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-06-12 20:20:59 +0200 |
commit | ded90fa8efaec6c497b066500e1d0f4dc531e4d5 (patch) | |
tree | 76d528783f01352144fc6f4ed65076ae758b63ad /sonar-scanner-engine/src | |
parent | 10f7f22611704b51dc9ebef153311fbd01aa7455 (diff) | |
download | sonarqube-ded90fa8efaec6c497b066500e1d0f4dc531e4d5.tar.gz sonarqube-ded90fa8efaec6c497b066500e1d0f4dc531e4d5.zip |
SONAR-10690 add Core Extension support in SonarQube
Diffstat (limited to 'sonar-scanner-engine/src')
8 files changed, 209 insertions, 28 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java index 1aea964a478..bc49833ec6f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalContainer.java @@ -32,6 +32,9 @@ import org.sonar.api.utils.UriReader; import org.sonar.api.utils.Version; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.core.extension.CoreExtensionRepositoryImpl; +import org.sonar.core.extension.CoreExtensionsInstaller; +import org.sonar.core.extension.CoreExtensionsLoader; import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.PluginClassloaderFactory; import org.sonar.core.platform.PluginInfo; @@ -39,6 +42,7 @@ import org.sonar.core.platform.PluginLoader; import org.sonar.core.platform.PluginRepository; import org.sonar.core.util.DefaultHttpDownloader; import org.sonar.core.util.UuidFactoryImpl; +import org.sonar.scanner.extension.ScannerCoreExtensionsInstaller; import org.sonar.scanner.platform.DefaultServer; import org.sonar.scanner.repository.DefaultMetricsRepositoryLoader; import org.sonar.scanner.repository.MetricsRepositoryLoader; @@ -99,6 +103,7 @@ public class GlobalContainer extends ComponentContainer { new MetricsRepositoryProvider(), UuidFactoryImpl.INSTANCE); addIfMissing(ScannerPluginInstaller.class, PluginInstaller.class); + add(CoreExtensionRepositoryImpl.class, CoreExtensionsLoader.class, ScannerCoreExtensionsInstaller.class); addIfMissing(DefaultSettingsLoader.class, SettingsLoader.class); addIfMissing(DefaultMetricsRepositoryLoader.class, MetricsRepositoryLoader.class); } @@ -106,6 +111,7 @@ public class GlobalContainer extends ComponentContainer { @Override protected void doAfterStart() { installPlugins(); + loadCoreExtensions(); } private void installPlugins() { @@ -116,6 +122,11 @@ public class GlobalContainer extends ComponentContainer { } } + private void loadCoreExtensions() { + CoreExtensionsLoader loader = getComponentByType(CoreExtensionsLoader.class); + loader.load(); + } + public void executeTask(Map<String, String> taskProperties, Object... components) { long startTime = System.currentTimeMillis(); new TaskContainer(this, taskProperties, components).execute(); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/extension/ScannerCoreExtensionsInstaller.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/extension/ScannerCoreExtensionsInstaller.java new file mode 100644 index 00000000000..e1b2f49aa8a --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/extension/ScannerCoreExtensionsInstaller.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.scanner.extension; + +import org.sonar.api.SonarRuntime; +import org.sonar.api.batch.ScannerSide; +import org.sonar.core.extension.CoreExtensionRepository; +import org.sonar.core.extension.CoreExtensionsInstaller; + +@ScannerSide +public class ScannerCoreExtensionsInstaller extends CoreExtensionsInstaller { + public ScannerCoreExtensionsInstaller(SonarRuntime sonarRuntime, CoreExtensionRepository coreExtensionRepository) { + super(sonarRuntime, coreExtensionRepository, ScannerSide.class); + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/extension/package-info.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/extension/package-info.java new file mode 100644 index 00000000000..eb9aa1ab845 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/extension/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.scanner.extension; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java index f00208dd652..af03f0ae932 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java @@ -21,17 +21,16 @@ package org.sonar.scanner.scan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.FileMetadata; import org.sonar.api.batch.rule.CheckFactory; import org.sonar.api.issue.NoSonarFilter; import org.sonar.api.resources.Project; import org.sonar.api.scan.filesystem.FileExclusions; +import org.sonar.core.extension.CoreExtensionsInstaller; import org.sonar.core.platform.ComponentContainer; import org.sonar.scanner.DefaultFileLinesContextFactory; import org.sonar.scanner.bootstrap.ExtensionInstaller; -import org.sonar.scanner.bootstrap.ExtensionUtils; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.ScannerExtensionDictionnary; import org.sonar.scanner.deprecated.DeprecatedSensorContext; @@ -73,6 +72,10 @@ import org.sonar.scanner.sensor.SensorOptimizer; import org.sonar.scanner.source.HighlightableBuilder; import org.sonar.scanner.source.SymbolizableBuilder; +import static org.sonar.api.batch.InstantiationStrategy.PER_PROJECT; +import static org.sonar.scanner.bootstrap.ExtensionUtils.isInstantiationStrategy; +import static org.sonar.scanner.bootstrap.ExtensionUtils.isScannerSide; + public class ModuleScanContainer extends ComponentContainer { private static final Logger LOG = LoggerFactory.getLogger(ModuleScanContainer.class); private final DefaultInputModule module; @@ -165,8 +168,10 @@ public class ModuleScanContainer extends ComponentContainer { } private void addExtensions() { - ExtensionInstaller installer = getComponentByType(ExtensionInstaller.class); - installer.install(this, e -> ExtensionUtils.isScannerSide(e) && ExtensionUtils.isInstantiationStrategy(e, InstantiationStrategy.PER_PROJECT)); + ExtensionInstaller pluginInstaller = getComponentByType(ExtensionInstaller.class); + pluginInstaller.install(this, e -> isScannerSide(e) && isInstantiationStrategy(e, PER_PROJECT)); + CoreExtensionsInstaller coreExtensionsInstaller = getComponentByType(CoreExtensionsInstaller.class); + coreExtensionsInstaller.install(this, t -> isInstantiationStrategy(t, PER_PROJECT)); } @Override diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index af9ea941f51..c0ab266b34f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -22,7 +22,6 @@ package org.sonar.scanner.scan; import com.google.common.annotations.VisibleForTesting; import javax.annotation.Nullable; import org.sonar.api.CoreProperties; -import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.InputModuleHierarchy; import org.sonar.api.batch.fs.internal.SensorStrategy; @@ -33,6 +32,7 @@ import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.core.config.ScannerProperties; +import org.sonar.core.extension.CoreExtensionsInstaller; import org.sonar.core.metric.ScannerMetrics; import org.sonar.core.platform.ComponentContainer; import org.sonar.scanner.ProjectAnalysisInfo; @@ -41,7 +41,6 @@ import org.sonar.scanner.analysis.AnalysisTempFolderProvider; import org.sonar.scanner.analysis.DefaultAnalysisMode; import org.sonar.scanner.bootstrap.ExtensionInstaller; import org.sonar.scanner.bootstrap.ExtensionMatcher; -import org.sonar.scanner.bootstrap.ExtensionUtils; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.MetricProvider; import org.sonar.scanner.cpd.CpdExecutor; @@ -102,6 +101,10 @@ import org.sonar.scanner.scan.measure.MeasureCache; import org.sonar.scanner.scm.ScmChangedFilesProvider; import org.sonar.scanner.storage.Storages; +import static org.sonar.api.batch.InstantiationStrategy.PER_BATCH; +import static org.sonar.scanner.bootstrap.ExtensionUtils.isInstantiationStrategy; +import static org.sonar.scanner.bootstrap.ExtensionUtils.isScannerSide; + public class ProjectScanContainer extends ComponentContainer { private static final Logger LOG = Loggers.get(ProjectScanContainer.class); @@ -238,7 +241,15 @@ public class ProjectScanContainer extends ComponentContainer { } private void addBatchExtensions() { - getComponentByType(ExtensionInstaller.class).install(this, new BatchExtensionFilter()); + getComponentByType(ExtensionInstaller.class) + .install(this, getBatchPluginExtensionsFilter()); + getComponentByType(CoreExtensionsInstaller.class) + .install(this, extension -> isInstantiationStrategy(extension, PER_BATCH)); + } + + @VisibleForTesting + static ExtensionMatcher getBatchPluginExtensionsFilter() { + return extension -> isScannerSide(extension) && isInstantiationStrategy(extension, PER_BATCH); } @Override @@ -301,12 +312,4 @@ public class ProjectScanContainer extends ComponentContainer { new ModuleScanContainer(this, module, analysisMode).execute(); } - static class BatchExtensionFilter implements ExtensionMatcher { - @Override - public boolean accept(Object extension) { - return ExtensionUtils.isScannerSide(extension) - && ExtensionUtils.isInstantiationStrategy(extension, InstantiationStrategy.PER_BATCH); - } - } - } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/TaskContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/TaskContainer.java index e863148e9e8..4d81fa18127 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/TaskContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/task/TaskContainer.java @@ -22,16 +22,18 @@ package org.sonar.scanner.task; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; -import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.task.Task; import org.sonar.api.task.TaskDefinition; import org.sonar.api.utils.MessageException; +import org.sonar.core.extension.CoreExtensionsInstaller; import org.sonar.core.platform.ComponentContainer; import org.sonar.scanner.bootstrap.ExtensionInstaller; -import org.sonar.scanner.bootstrap.ExtensionMatcher; -import org.sonar.scanner.bootstrap.ExtensionUtils; import org.sonar.scanner.bootstrap.GlobalProperties; +import static org.sonar.api.batch.InstantiationStrategy.PER_TASK; +import static org.sonar.scanner.bootstrap.ExtensionUtils.isInstantiationStrategy; +import static org.sonar.scanner.bootstrap.ExtensionUtils.isScannerSide; + public class TaskContainer extends ComponentContainer { private final Map<String, String> taskProperties; @@ -57,15 +59,10 @@ public class TaskContainer extends ComponentContainer { } private void addTaskExtensions() { - getComponentByType(ExtensionInstaller.class).install(this, new TaskExtensionFilter()); - } - - static class TaskExtensionFilter implements ExtensionMatcher { - @Override - public boolean accept(Object extension) { - return ExtensionUtils.isScannerSide(extension) - && ExtensionUtils.isInstantiationStrategy(extension, InstantiationStrategy.PER_TASK); - } + getComponentByType(ExtensionInstaller.class) + .install(this, extension -> isScannerSide(extension) && isInstantiationStrategy(extension, PER_TASK)); + getComponentByType(CoreExtensionsInstaller.class) + .install(this, t -> isInstantiationStrategy(t, PER_TASK)); } @Override diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/extension/ScannerCoreExtensionsInstallerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/extension/ScannerCoreExtensionsInstallerTest.java new file mode 100644 index 00000000000..a359ba30967 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/extension/ScannerCoreExtensionsInstallerTest.java @@ -0,0 +1,109 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.scanner.extension; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.stream.Stream; +import org.junit.Test; +import org.sonar.api.SonarRuntime; +import org.sonar.api.batch.ScannerSide; +import org.sonar.api.ce.ComputeEngineSide; +import org.sonar.api.server.ServerSide; +import org.sonar.core.extension.CoreExtension; +import org.sonar.core.extension.CoreExtensionRepository; +import org.sonar.core.platform.ComponentContainer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ScannerCoreExtensionsInstallerTest { + private SonarRuntime sonarRuntime = mock(SonarRuntime.class); + private CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class); + + private ScannerCoreExtensionsInstaller underTest = new ScannerCoreExtensionsInstaller(sonarRuntime, coreExtensionRepository); + + @Test + public void install_only_adds_ScannerSide_annotated_extension_to_container() { + when(coreExtensionRepository.loadedCoreExtensions()).thenReturn(Stream.of( + new CoreExtension() { + @Override + public String getName() { + return "foo"; + } + + @Override + public void load(Context context) { + context.addExtensions(CeClass.class, ScannerClass.class, WebServerClass.class, + NoAnnotationClass.class, OtherAnnotationClass.class, MultipleAnnotationClass.class); + } + })); + ComponentContainer container = new ComponentContainer(); + + underTest.install(container, t -> true); + + assertThat(container.getPicoContainer().getComponentAdapters()) + .hasSize(ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2); + assertThat(container.getComponentByType(ScannerClass.class)).isNotNull(); + assertThat(container.getComponentByType(MultipleAnnotationClass.class)).isNotNull(); + } + + @ComputeEngineSide + public static final class CeClass { + + } + + @ServerSide + public static final class WebServerClass { + + } + + @ScannerSide + public static final class ScannerClass { + + } + + @ServerSide + @ComputeEngineSide + @ScannerSide + public static final class MultipleAnnotationClass { + + } + + public static final class NoAnnotationClass { + + } + + @DarkSide + public static final class OtherAnnotationClass { + + } + + @Documented + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + public @interface DarkSide { + } + +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectScanContainerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectScanContainerTest.java index dd1a77c3d26..8ca3ebf301f 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectScanContainerTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectScanContainerTest.java @@ -24,6 +24,7 @@ import org.sonar.api.BatchExtension; import org.sonar.api.ServerExtension; import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.task.TaskExtension; +import org.sonar.scanner.bootstrap.ExtensionMatcher; import static org.assertj.core.api.Assertions.assertThat; @@ -31,7 +32,7 @@ public class ProjectScanContainerTest { @Test public void should_add_only_batch_extensions() { - ProjectScanContainer.BatchExtensionFilter filter = new ProjectScanContainer.BatchExtensionFilter(); + ExtensionMatcher filter = ProjectScanContainer.getBatchPluginExtensionsFilter(); assertThat(filter.accept(new MyBatchExtension())).isTrue(); assertThat(filter.accept(MyBatchExtension.class)).isTrue(); |