diff options
Diffstat (limited to 'sonar-core')
10 files changed, 117 insertions, 38 deletions
diff --git a/sonar-core/build.gradle b/sonar-core/build.gradle index 3b658b278c9..3141d504391 100644 --- a/sonar-core/build.gradle +++ b/sonar-core/build.gradle @@ -16,10 +16,12 @@ dependencies { compile 'commons-codec:commons-codec' compile 'commons-io:commons-io' compile 'commons-lang:commons-lang' + compile 'javax.inject:javax.inject' compile 'org.codehaus.sonar:sonar-classloader' compile 'org.picocontainer:picocontainer' compile 'org.slf4j:slf4j-api' compile 'org.sonarsource.update-center:sonar-update-center-common' + compile 'org.springframework:spring-context' compile project(path: ':sonar-plugin-api', configuration: 'shadow') compile project(':sonar-plugin-api-impl') diff --git a/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java b/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java index f921ac2abc6..bae7811e160 100644 --- a/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java +++ b/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsInstaller.java @@ -30,7 +30,7 @@ import org.sonar.api.config.internal.MapSettings; import org.sonar.api.utils.AnnotationUtils; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ExtensionContainer; import static java.util.Objects.requireNonNull; @@ -41,8 +41,7 @@ public abstract class CoreExtensionsInstaller { private final CoreExtensionRepository coreExtensionRepository; private final Class<? extends Annotation> supportedAnnotationType; - protected CoreExtensionsInstaller(SonarRuntime sonarRuntime, CoreExtensionRepository coreExtensionRepository, - Class<? extends Annotation> supportedAnnotationType) { + protected CoreExtensionsInstaller(SonarRuntime sonarRuntime, CoreExtensionRepository coreExtensionRepository, Class<? extends Annotation> supportedAnnotationType) { this.sonarRuntime = sonarRuntime; this.coreExtensionRepository = coreExtensionRepository; this.supportedAnnotationType = supportedAnnotationType; @@ -63,12 +62,12 @@ public abstract class CoreExtensionsInstaller { * @param additionalSideFilter applied on top of filtering on {@link #supportedAnnotationType} to decide whether * extension should be added to container as an object or only as a PropertyDefinition. */ - public void install(ComponentContainer container, Predicate<Object> extensionFilter, Predicate<Object> additionalSideFilter) { + public void install(ExtensionContainer container, Predicate<Object> extensionFilter, Predicate<Object> additionalSideFilter) { coreExtensionRepository.loadedCoreExtensions() .forEach(coreExtension -> install(container, extensionFilter, additionalSideFilter, coreExtension)); } - private void install(ComponentContainer container, Predicate<Object> extensionFilter, Predicate<Object> additionalSideFilter, CoreExtension coreExtension) { + private void install(ExtensionContainer container, Predicate<Object> extensionFilter, Predicate<Object> additionalSideFilter, CoreExtension coreExtension) { String coreExtensionName = coreExtension.getName(); try { addDeclaredExtensions(container, extensionFilter, additionalSideFilter, coreExtension); @@ -80,33 +79,23 @@ public abstract class CoreExtensionsInstaller { } } - private void addDeclaredExtensions(ComponentContainer container, Predicate<Object> extensionFilter, + private void addDeclaredExtensions(ExtensionContainer container, Predicate<Object> extensionFilter, Predicate<Object> additionalSideFilter, CoreExtension coreExtension) { ContextImpl context = new ContextImpl(container, extensionFilter, additionalSideFilter, coreExtension.getName()); coreExtension.load(context); } - private <T> boolean addSupportedExtension(ComponentContainer container, Predicate<Object> additionalSideFilter, - String extensionCategory, T component) { - if (hasSupportedAnnotation(component) && additionalSideFilter.test(component)) { - container.addExtension(extensionCategory, component); - return true; - } - return false; - } - private <T> boolean hasSupportedAnnotation(T component) { return AnnotationUtils.getAnnotation(component, supportedAnnotationType) != null; } private class ContextImpl implements CoreExtension.Context { - private final ComponentContainer container; + private final ExtensionContainer container; private final Predicate<Object> extensionFilter; private final Predicate<Object> additionalSideFilter; private final String extensionCategory; - public ContextImpl(ComponentContainer container, Predicate<Object> extensionFilter, - Predicate<Object> additionalSideFilter, String extensionCategory) { + public ContextImpl(ExtensionContainer container, Predicate<Object> extensionFilter, Predicate<Object> additionalSideFilter, String extensionCategory) { this.container = container; this.extensionFilter = extensionFilter; this.additionalSideFilter = additionalSideFilter; @@ -150,5 +139,14 @@ public abstract class CoreExtensionsInstaller { components.forEach(this::addExtension); return this; } + + private <T> boolean addSupportedExtension(ExtensionContainer container, Predicate<Object> additionalSideFilter, + String extensionCategory, T component) { + if (hasSupportedAnnotation(component) && additionalSideFilter.test(component)) { + container.addExtension(extensionCategory, component); + return true; + } + return false; + } } } diff --git a/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsLoader.java b/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsLoader.java index 0109f30d357..3791f996640 100644 --- a/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsLoader.java +++ b/sonar-core/src/main/java/org/sonar/core/extension/CoreExtensionsLoader.java @@ -22,6 +22,7 @@ package org.sonar.core.extension; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; +import javax.inject.Inject; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.core.util.stream.MoreCollectors; @@ -37,6 +38,7 @@ public class CoreExtensionsLoader { private final CoreExtensionRepository coreExtensionRepository; private final ServiceLoaderWrapper serviceLoaderWrapper; + @Inject public CoreExtensionsLoader(CoreExtensionRepository coreExtensionRepository) { this(coreExtensionRepository, new ServiceLoaderWrapper()); } 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 4ca313de25c..4057b9ad1d5 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 @@ -30,6 +30,7 @@ import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.measures.Metric; import org.sonar.api.measures.Metrics; import org.sonar.api.scanner.ScannerSide; +import org.springframework.beans.factory.annotation.Autowired; import static org.sonar.api.measures.CoreMetrics.CLASSES; import static org.sonar.api.measures.CoreMetrics.COGNITIVE_COMPLEXITY; @@ -96,10 +97,12 @@ public class ScannerMetrics { private final Set<Metric> metrics; + @Autowired(required = false) public ScannerMetrics() { this.metrics = ALLOWED_CORE_METRICS; } + @Autowired(required = false) public ScannerMetrics(Metrics[] metricsRepositories) { this.metrics = Stream.concat(getPluginMetrics(metricsRepositories), ALLOWED_CORE_METRICS.stream()).collect(toSet()); } diff --git a/sonar-core/src/main/java/org/sonar/core/platform/ComponentContainer.java b/sonar-core/src/main/java/org/sonar/core/platform/ComponentContainer.java index 269ac851746..b8d7c56b310 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/ComponentContainer.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/ComponentContainer.java @@ -49,7 +49,7 @@ import static java.util.Optional.ofNullable; @ScannerSide @ServerSide @ComputeEngineSide -public class ComponentContainer implements ContainerPopulator.Container { +public class ComponentContainer implements ExtensionContainer { public static final int COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER = 2; private static final class ExtendedDefaultPicoContainer extends DefaultPicoContainer { @@ -235,6 +235,7 @@ public class ComponentContainer implements ContainerPopulator.Container { return this; } + @Override public ComponentContainer addExtension(@Nullable PluginInfo pluginInfo, Object extension) { Object key = componentKeys.of(extension); try { @@ -246,6 +247,7 @@ public class ComponentContainer implements ContainerPopulator.Container { return this; } + @Override public ComponentContainer addExtension(@Nullable String defaultCategory, Object extension) { Object key = componentKeys.of(extension); try { @@ -264,12 +266,16 @@ public class ComponentContainer implements ContainerPopulator.Container { return getName(extension.getClass()); } - public void declareExtension(@Nullable PluginInfo pluginInfo, Object extension) { + @Override + public ComponentContainer declareExtension(@Nullable PluginInfo pluginInfo, Object extension) { declareExtension(pluginInfo != null ? pluginInfo.getName() : "", extension); + return this; } - public void declareExtension(@Nullable String defaultCategory, Object extension) { + @Override + public ComponentContainer declareExtension(@Nullable String defaultCategory, Object extension) { propertyDefinitions.addComponent(extension, ofNullable(defaultCategory).orElse("")); + return this; } public ComponentContainer addPicoAdapter(ComponentAdapter<?> adapter) { diff --git a/sonar-core/src/main/java/org/sonar/core/platform/ComponentKeys.java b/sonar-core/src/main/java/org/sonar/core/platform/ComponentKeys.java index 7cb156b91bd..81a1b822b9d 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/ComponentKeys.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/ComponentKeys.java @@ -26,19 +26,27 @@ import org.sonar.core.util.Uuids; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; -class ComponentKeys { - +public class ComponentKeys { + private static final Logger LOG = Loggers.get(ComponentKeys.class); private static final Pattern IDENTITY_HASH_PATTERN = Pattern.compile(".+@[a-f0-9]+"); private final Set<Class> objectsWithoutToString = new HashSet<>(); Object of(Object component) { - return of(component, Loggers.get(ComponentKeys.class)); + return of(component, LOG); } Object of(Object component, Logger log) { if (component instanceof Class) { return component; } + return ofInstance(component, log); + } + + public String ofInstance(Object component) { + return ofInstance(component, LOG); + } + + String ofInstance(Object component, Logger log) { String key = component.toString(); if (IDENTITY_HASH_PATTERN.matcher(key).matches()) { if (!objectsWithoutToString.add(component.getClass())) { @@ -46,6 +54,6 @@ class ComponentKeys { } key += Uuids.create(); } - return new StringBuilder().append(component.getClass().getCanonicalName()).append("-").append(key).toString(); + return component.getClass().getCanonicalName() + "-" + key; } } diff --git a/sonar-core/src/main/java/org/sonar/core/platform/Container.java b/sonar-core/src/main/java/org/sonar/core/platform/Container.java new file mode 100644 index 00000000000..e7a9963623d --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/platform/Container.java @@ -0,0 +1,34 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 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.core.platform; + +import java.util.List; + +public interface Container { + Container add(Object... objects); + + Container addSingletons(Iterable<?> components); + + <T> T getComponentByType(Class<T> type); + + <T> List<T> getComponentsByType(Class<T> type); + + Container getParent(); +} diff --git a/sonar-core/src/main/java/org/sonar/core/platform/ContainerPopulator.java b/sonar-core/src/main/java/org/sonar/core/platform/ContainerPopulator.java index c582cfead3f..f5a6fe46be5 100644 --- a/sonar-core/src/main/java/org/sonar/core/platform/ContainerPopulator.java +++ b/sonar-core/src/main/java/org/sonar/core/platform/ContainerPopulator.java @@ -19,19 +19,7 @@ */ package org.sonar.core.platform; -import java.util.List; - @FunctionalInterface -public interface ContainerPopulator<T extends ContainerPopulator.Container> { +public interface ContainerPopulator<T extends Container> { void populateContainer(T container); - - interface Container { - Container add(Object... objects); - - Container addSingletons(Iterable<?> components); - - <T> T getComponentByType(Class<T> type); - - <T> List<T> getComponentsByType(Class<T> type); - } } 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 new file mode 100644 index 00000000000..0bf945711c5 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/platform/ExtensionContainer.java @@ -0,0 +1,36 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 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.core.platform; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +public interface ExtensionContainer extends Container { + ExtensionContainer addExtension(@Nullable PluginInfo pluginInfo, Object extension); + + ExtensionContainer addExtension(@Nullable String defaultCategory, Object extension); + + ExtensionContainer declareExtension(@Nullable PluginInfo pluginInfo, Object extension); + + ExtensionContainer declareExtension(@Nullable String defaultCategory, Object extension); + + @CheckForNull + ExtensionContainer getParent(); +} diff --git a/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java b/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java index 9fea2f6f74b..93f12376c92 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java +++ b/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java @@ -39,6 +39,7 @@ import java.util.List; import java.util.Optional; import java.util.zip.GZIPInputStream; import javax.annotation.Nullable; +import javax.inject.Inject; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; import org.sonar.api.CoreProperties; @@ -62,6 +63,7 @@ public class DefaultHttpDownloader extends HttpDownloader { private final Integer readTimeout; private final Integer connectTimeout; + @Inject public DefaultHttpDownloader(Server server, Configuration config) { this(server, config, null); } |