diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2022-02-01 15:16:25 -0600 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-02-22 20:02:46 +0000 |
commit | 60c1a4038e041a342dda9810e6fd761d66b01bdb (patch) | |
tree | 0e76b4252e4d7d257cf4ddcb6f081996bb1e03ab /server/sonar-server-common/src | |
parent | 9694d4113bf401b84e86e0223dbea8f5339388d8 (diff) | |
download | sonarqube-60c1a4038e041a342dda9810e6fd761d66b01bdb.tar.gz sonarqube-60c1a4038e041a342dda9810e6fd761d66b01bdb.zip |
SONAR-15994 Migrate Sonarqube IOC framework from Pico to Spring
Diffstat (limited to 'server/sonar-server-common/src')
39 files changed, 166 insertions, 299 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/async/AsyncExecutionMBeanImpl.java b/server/sonar-server-common/src/main/java/org/sonar/server/async/AsyncExecutionMBeanImpl.java index d0f6d8c6ce5..87c8f749fd2 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/async/AsyncExecutionMBeanImpl.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/async/AsyncExecutionMBeanImpl.java @@ -19,7 +19,7 @@ */ package org.sonar.server.async; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.process.Jmx; public class AsyncExecutionMBeanImpl implements AsyncExecutionMBean, Startable { diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java index b77f46ef4f0..cadee3af797 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexDefinition.java @@ -36,6 +36,8 @@ import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_ import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder; import static org.sonar.server.permission.index.IndexAuthorizationConstants.TYPE_AUTHORIZATION; +import javax.inject.Inject; + public class ComponentIndexDefinition implements IndexDefinition { public static final Index DESCRIPTOR = Index.withRelations("components"); @@ -58,6 +60,7 @@ public class ComponentIndexDefinition implements IndexDefinition { this.enableSource = enableSource; } + @Inject public ComponentIndexDefinition(Configuration config) { this(config, false); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/config/ConfigurationProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/config/ConfigurationProvider.java index babc5971a4e..f4a8c7cca58 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/config/ConfigurationProvider.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/config/ConfigurationProvider.java @@ -19,26 +19,22 @@ */ package org.sonar.server.config; -import java.util.Optional; -import java.util.function.UnaryOperator; +import static java.util.function.UnaryOperator.identity; import org.apache.commons.lang.ArrayUtils; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.config.Configuration; import org.sonar.api.config.PropertyDefinition; -import org.sonar.api.config.internal.Settings; - -import static java.util.function.UnaryOperator.identity; import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv; +import org.sonar.api.config.internal.Settings; +import org.springframework.context.annotation.Bean; -public class ConfigurationProvider extends ProviderAdapter { +import java.util.Optional; +import java.util.function.UnaryOperator; - private Configuration configuration; +public class ConfigurationProvider { + @Bean("Configuration") public Configuration provide(Settings settings) { - if (configuration == null) { - configuration = new ServerConfigurationAdapter(settings); - } - return configuration; + return new ServerConfigurationAdapter(settings); } private static class ServerConfigurationAdapter implements Configuration { diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientProvider.java index 16afc5b7e08..41f87cb5c97 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientProvider.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientProvider.java @@ -28,13 +28,13 @@ import java.util.List; import java.util.stream.Collectors; import org.apache.http.HttpHost; import org.elasticsearch.common.settings.Settings; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.config.Configuration; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.process.cluster.NodeType; +import org.springframework.context.annotation.Bean; import static org.sonar.process.ProcessProperties.Property.CLUSTER_ENABLED; import static org.sonar.process.ProcessProperties.Property.CLUSTER_NAME; @@ -47,38 +47,33 @@ import static org.sonar.process.cluster.NodeType.SEARCH; @ComputeEngineSide @ServerSide -public class EsClientProvider extends ProviderAdapter { - +public class EsClientProvider { private static final Logger LOGGER = Loggers.get(EsClientProvider.class); - private EsClient cache; - + @Bean("EsClient") public EsClient provide(Configuration config) { - if (cache == null) { - Settings.Builder esSettings = Settings.builder(); + Settings.Builder esSettings = Settings.builder(); - // mandatory property defined by bootstrap process - esSettings.put("cluster.name", config.get(CLUSTER_NAME.getKey()).get()); + // mandatory property defined by bootstrap process + esSettings.put("cluster.name", config.get(CLUSTER_NAME.getKey()).get()); - boolean clusterEnabled = config.getBoolean(CLUSTER_ENABLED.getKey()).orElse(false); - boolean searchNode = !clusterEnabled || SEARCH.equals(NodeType.parse(config.get(CLUSTER_NODE_TYPE.getKey()).orElse(null))); - List<HttpHost> httpHosts; - if (clusterEnabled && !searchNode) { - httpHosts = getHttpHosts(config); + boolean clusterEnabled = config.getBoolean(CLUSTER_ENABLED.getKey()).orElse(false); + boolean searchNode = !clusterEnabled || SEARCH.equals(NodeType.parse(config.get(CLUSTER_NODE_TYPE.getKey()).orElse(null))); + List<HttpHost> httpHosts; + if (clusterEnabled && !searchNode) { + httpHosts = getHttpHosts(config); - LOGGER.info("Connected to remote Elasticsearch: [{}]", displayedAddresses(httpHosts)); - } else { - // defaults provided in: - // * in org.sonar.process.ProcessProperties.Property.SEARCH_HOST - // * in org.sonar.process.ProcessProperties.Property.SEARCH_PORT - HostAndPort host = HostAndPort.fromParts(config.get(SEARCH_HOST.getKey()).get(), config.getInt(SEARCH_PORT.getKey()).get()); - httpHosts = Collections.singletonList(toHttpHost(host)); - LOGGER.info("Connected to local Elasticsearch: [{}]", displayedAddresses(httpHosts)); - } - - cache = new EsClient(config.get(CLUSTER_SEARCH_PASSWORD.getKey()).orElse(null), httpHosts.toArray(new HttpHost[0])); + LOGGER.info("Connected to remote Elasticsearch: [{}]", displayedAddresses(httpHosts)); + } else { + // defaults provided in: + // * in org.sonar.process.ProcessProperties.Property.SEARCH_HOST + // * in org.sonar.process.ProcessProperties.Property.SEARCH_PORT + HostAndPort host = HostAndPort.fromParts(config.get(SEARCH_HOST.getKey()).get(), config.getInt(SEARCH_PORT.getKey()).get()); + httpHosts = Collections.singletonList(toHttpHost(host)); + LOGGER.info("Connected to local Elasticsearch: [{}]", displayedAddresses(httpHosts)); } - return cache; + + return new EsClient(config.get(CLUSTER_SEARCH_PASSWORD.getKey()).orElse(null), httpHosts.toArray(new HttpHost[0])); } private static List<HttpHost> getHttpHosts(Configuration config) { diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientStopper.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientStopper.java deleted file mode 100644 index ef0957ceb4f..00000000000 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsClientStopper.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.server.es; - -import org.sonar.api.Startable; - -/** - * Workaround of a behaviour of picocontainer: components - * instantiated by {@link org.picocontainer.injectors.ProviderAdapter} - * can't have a lifecycle. The methods start() and stop() - * of {@link Startable} are not executed. - * The same behaviour exists for the {@link org.picocontainer.injectors.ProviderAdapter} - * itself. - * - * As {@link EsClientStopper} implements {@link Startable}, it can - * close {@link EsClient} when process shutdowns. - * - */ -public class EsClientStopper implements Startable { - - private final EsClient esClient; - - public EsClientStopper(EsClient esClient) { - this.esClient = esClient; - } - - @Override - public void start() { - // nothing to do - } - - @Override - public void stop() { - esClient.close(); - } -} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsModule.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsModule.java index 8d5762a7e88..f6697d6e85d 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsModule.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsModule.java @@ -25,6 +25,5 @@ public class EsModule extends Module { @Override protected void configureModule() { add(new EsClientProvider()); - add(EsClientStopper.class); } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBootstraper.java b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBootstraper.java index fe6600e00e2..6e3d37b922a 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBootstraper.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBootstraper.java @@ -25,7 +25,7 @@ import org.sonar.api.platform.ServerStartHandler; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import static java.lang.String.format; @@ -35,9 +35,9 @@ import static java.lang.String.format; public class CoreExtensionBootstraper implements ServerStartHandler { private static final Logger LOGGER = Loggers.get(CoreExtensionBootstraper.class); - private final ComponentContainer componentContainer; + private final SpringComponentContainer componentContainer; - public CoreExtensionBootstraper(ComponentContainer componentContainer) { + public CoreExtensionBootstraper(SpringComponentContainer componentContainer) { this.componentContainer = componentContainer; } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBridge.java b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBridge.java index f1f9e379fae..f68235dee48 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBridge.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionBridge.java @@ -19,7 +19,7 @@ */ package org.sonar.server.extension; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; /** * Interface implemented by the Extension point exposed by the Core Extensions that serves as the unique access @@ -32,11 +32,11 @@ public interface CoreExtensionBridge { /** * Bootstraps the plugin. * - * @param parent the parent ComponentContainer which provides Platform components for the Privileged plugin to use. + * @param parent the parent SpringComponentContainer which provides Platform components for the Privileged plugin to use. * * @throws IllegalStateException if called more than once */ - void startPlugin(ComponentContainer parent); + void startPlugin(SpringComponentContainer parent); /** * This method is called when Platform is shutting down. diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionStopper.java b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionStopper.java index 6ed17663943..d03548ac46d 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionStopper.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/extension/CoreExtensionStopper.java @@ -20,11 +20,11 @@ package org.sonar.server.extension; import java.util.List; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.Container; import static java.lang.String.format; @@ -35,9 +35,9 @@ import static java.lang.String.format; public class CoreExtensionStopper implements Startable { private static final Logger LOGGER = Loggers.get(CoreExtensionStopper.class); - private final ComponentContainer platformContainer; + private final Container platformContainer; - public CoreExtensionStopper(ComponentContainer platformContainer) { + public CoreExtensionStopper(Container platformContainer) { this.platformContainer = platformContainer; } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java b/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java index 34c211d318b..fed04f29c97 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java @@ -32,6 +32,8 @@ import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_ import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder; import static org.sonar.server.permission.index.IndexAuthorizationConstants.TYPE_AUTHORIZATION; +import javax.inject.Inject; + /** * Definition of ES index "issues", including settings and fields. */ @@ -110,6 +112,7 @@ public class IssueIndexDefinition implements IndexDefinition { private final Configuration config; private final boolean enableSource; + @Inject public IssueIndexDefinition(Configuration config) { this(config, false); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/issue/workflow/IssueWorkflow.java b/server/sonar-server-common/src/main/java/org/sonar/server/issue/workflow/IssueWorkflow.java index c39ebfee08c..f3b53317538 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/issue/workflow/IssueWorkflow.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/issue/workflow/IssueWorkflow.java @@ -20,7 +20,7 @@ package org.sonar.server.issue.workflow; import java.util.List; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.issue.DefaultTransitions; import org.sonar.api.issue.Issue; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerLogging.java b/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerLogging.java index 8e3c7fa4f46..196fb2253db 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerLogging.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/log/ServerLogging.java @@ -23,7 +23,8 @@ import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import com.google.common.annotations.VisibleForTesting; import java.io.File; -import org.picocontainer.Startable; +import javax.inject.Inject; +import org.sonar.api.Startable; import org.slf4j.LoggerFactory; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.config.Configuration; @@ -47,6 +48,7 @@ public class ServerLogging implements Startable { private final ServerProcessLogging serverProcessLogging; private final Database database; + @Inject public ServerLogging(Configuration config, ServerProcessLogging serverProcessLogging, Database database) { this(new LogbackHelper(), config, serverProcessLogging, database); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java b/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java index 5ade001ab94..a4d3534e391 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexDefinition.java @@ -34,6 +34,8 @@ import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_INTERVAL; import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder; +import javax.inject.Inject; + public class ProjectMeasuresIndexDefinition implements IndexDefinition { public static final Index DESCRIPTOR = Index.withRelations("projectmeasures"); @@ -71,6 +73,7 @@ public class ProjectMeasuresIndexDefinition implements IndexDefinition { this.enableSource = enableSource; } + @Inject public ProjectMeasuresIndexDefinition(Configuration config) { this(config, false); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java index aafa02f2c6f..c3bd24b8bc3 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java @@ -56,13 +56,10 @@ public class DefaultNotificationManager implements NotificationManager { private static final String UNABLE_TO_READ_NOTIFICATION = "Unable to read notification"; - private NotificationChannel[] notificationChannels; + private final NotificationChannel[] notificationChannels; private final DbClient dbClient; private boolean alreadyLoggedDeserializationIssue = false; - /** - * Default constructor used by Pico - */ public DefaultNotificationManager(NotificationChannel[] channels, DbClient dbClient) { this.notificationChannels = channels; this.dbClient = dbClient; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationManager.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationManager.java index f31572a35b2..925cc6bf436 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationManager.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationManager.java @@ -32,7 +32,7 @@ import static java.util.Objects.requireNonNull; /** * The notification manager receives notifications and is in charge of storing them so that they are processed by the notification service. * <p> - * Pico provides an instance of this class, and plugins just need to create notifications and pass them to this manager with + * The ioc container provides an instance of this class, and plugins just need to create notifications and pass them to this manager with * the {@link NotificationManager#scheduleForSending(Notification)} method. * </p> */ diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationService.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationService.java index 8092ef3ad54..710658ca998 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationService.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationService.java @@ -39,6 +39,7 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbClient; +import org.springframework.beans.factory.annotation.Autowired; import static com.google.common.base.Preconditions.checkArgument; @@ -52,6 +53,7 @@ public class NotificationService { private final List<NotificationHandler<? extends Notification>> handlers; private final DbClient dbClient; + @Autowired(required = false) public NotificationService(DbClient dbClient, NotificationDispatcher[] dispatchers, NotificationHandler<? extends Notification>[] handlers) { this.dbClient = dbClient; this.dispatchers = ImmutableList.copyOf(dispatchers); @@ -59,22 +61,25 @@ public class NotificationService { } /** - * Used by Pico when there are no handler nor dispatcher. + * Used by the ioc container when there are no handler nor dispatcher. */ + @Autowired(required = false) public NotificationService(DbClient dbClient) { this(dbClient, new NotificationDispatcher[0], new NotificationHandler[0]); } /** - * Used by Pico when there are no dispatcher. + * Used by the ioc container when there are no dispatcher. */ + @Autowired(required = false) public NotificationService(DbClient dbClient, NotificationHandler[] handlers) { this(dbClient, new NotificationDispatcher[0], handlers); } /** - * Used by Pico when there are no handler. + * Used by the ioc container when there are no handler. */ + @Autowired(required = false) public NotificationService(DbClient dbClient, NotificationDispatcher[] dispatchers) { this(dbClient, dispatchers, new NotificationHandler[0]); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerFileSystemImpl.java b/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerFileSystemImpl.java index 687d490057a..eed1210d9b7 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerFileSystemImpl.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerFileSystemImpl.java @@ -22,7 +22,7 @@ package org.sonar.server.platform; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.api.config.Configuration; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerLifecycleNotifier.java b/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerLifecycleNotifier.java index f71654e8b35..fa720a5239b 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerLifecycleNotifier.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/platform/ServerLifecycleNotifier.java @@ -19,44 +19,33 @@ */ package org.sonar.server.platform; -import org.picocontainer.Startable; -import org.sonar.api.utils.log.Loggers; +import javax.annotation.Nullable; +import org.sonar.api.Startable; import org.sonar.api.platform.Server; import org.sonar.api.platform.ServerStartHandler; import org.sonar.api.platform.ServerStopHandler; +import org.sonar.api.utils.log.Loggers; /** * @since 2.2 */ public class ServerLifecycleNotifier implements Startable { - private ServerStartHandler[] startHandlers; - private ServerStopHandler[] stopHandlers; - private Server server; + private final ServerStartHandler[] startHandlers; + private final ServerStopHandler[] stopHandlers; + private final Server server; - public ServerLifecycleNotifier(Server server, ServerStartHandler[] startHandlers, ServerStopHandler[] stopHandlers) { - this.startHandlers = startHandlers; - this.stopHandlers = stopHandlers; + public ServerLifecycleNotifier(Server server, @Nullable ServerStartHandler[] startHandlers, @Nullable ServerStopHandler[] stopHandlers) { + this.startHandlers = startHandlers != null ? startHandlers : new ServerStartHandler[0]; + this.stopHandlers = stopHandlers != null ? stopHandlers: new ServerStopHandler[0]; this.server = server; } - public ServerLifecycleNotifier(Server server, ServerStartHandler[] startHandlers) { - this(server, startHandlers, new ServerStopHandler[0]); - } - - public ServerLifecycleNotifier(Server server, ServerStopHandler[] stopHandlers) { - this(server, new ServerStartHandler[0], stopHandlers); - } - - public ServerLifecycleNotifier(Server server) { - this(server, new ServerStartHandler[0], new ServerStopHandler[0]); - } - @Override public void start() { /* IMPORTANT : we want to be sure that handlers are notified when all other services are started. - That's why the class Platform explicitely executes the method notifyStart(), instead of letting picocontainer + That's why the class Platform explicitely executes the method notifyStart(), instead of letting the ioc container choose the startup order. */ } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/platform/StartupMetadataProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/platform/StartupMetadataProvider.java index 88efc4d81f8..19e714c9a28 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/platform/StartupMetadataProvider.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/platform/StartupMetadataProvider.java @@ -19,7 +19,6 @@ */ package org.sonar.server.platform; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.CoreProperties; import org.sonar.api.SonarQubeSide; import org.sonar.api.SonarRuntime; @@ -30,6 +29,7 @@ import org.sonar.api.utils.System2; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.property.PropertyDto; +import org.springframework.context.annotation.Bean; import static com.google.common.base.Preconditions.checkState; import static org.apache.commons.lang.StringUtils.isBlank; @@ -37,19 +37,14 @@ import static org.sonar.api.CoreProperties.SERVER_STARTTIME; @ComputeEngineSide @ServerSide -public class StartupMetadataProvider extends ProviderAdapter { - - private StartupMetadata cache = null; - +public class StartupMetadataProvider { + @Bean("StartupMetadata") public StartupMetadata provide(System2 system, SonarRuntime runtime, WebServer webServer, DbClient dbClient) { - if (cache == null) { - if (runtime.getSonarQubeSide() == SonarQubeSide.SERVER && webServer.isStartupLeader()) { - cache = generate(system); - } else { - cache = load(dbClient); - } + if (runtime.getSonarQubeSide() == SonarQubeSide.SERVER && webServer.isStartupLeader()) { + return generate(system); + } else { + return load(dbClient); } - return cache; } /** diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/platform/TempFolderProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/platform/TempFolderProvider.java index af3c93c31dd..9c5d476b045 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/platform/TempFolderProvider.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/platform/TempFolderProvider.java @@ -22,24 +22,19 @@ package org.sonar.server.platform; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.impl.utils.DefaultTempFolder; import org.sonar.api.utils.TempFolder; +import org.springframework.context.annotation.Bean; -public class TempFolderProvider extends ProviderAdapter { - - private TempFolder tempFolder; - +public class TempFolderProvider { + @Bean("TempFolder") public TempFolder provide(ServerFileSystem fs) { - if (tempFolder == null) { - File tempDir = new File(fs.getTempDir(), "tmp"); - try { - FileUtils.forceMkdir(tempDir); - } catch (IOException e) { - throw new IllegalStateException("Unable to create temp directory " + tempDir, e); - } - tempFolder = new DefaultTempFolder(tempDir); + File tempDir = new File(fs.getTempDir(), "tmp"); + try { + FileUtils.forceMkdir(tempDir); + } catch (IOException e) { + throw new IllegalStateException("Unable to create temp directory " + tempDir, e); } - return tempFolder; + return new DefaultTempFolder(tempDir); } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/plugins/InstalledPluginReferentialFactory.java b/server/sonar-server-common/src/main/java/org/sonar/server/plugins/InstalledPluginReferentialFactory.java index 78530477e26..a93f2d80d73 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/plugins/InstalledPluginReferentialFactory.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/plugins/InstalledPluginReferentialFactory.java @@ -19,7 +19,7 @@ */ package org.sonar.server.plugins; -import org.picocontainer.Startable; +import org.sonar.api.Startable; import org.sonar.core.platform.PluginRepository; import org.sonar.updatecenter.common.PluginReferential; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java b/server/sonar-server-common/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java index 89b64ce6365..523d70fe1dc 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/plugins/ServerExtensionInstaller.java @@ -22,18 +22,19 @@ package org.sonar.server.plugins; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ListMultimap; -import java.lang.annotation.Annotation; -import java.util.Collection; -import java.util.Set; import org.sonar.api.Plugin; import org.sonar.api.SonarRuntime; import org.sonar.api.config.Configuration; import org.sonar.api.internal.PluginContextImpl; import org.sonar.api.utils.AnnotationUtils; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ExtensionContainer; import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginRepository; +import java.lang.annotation.Annotation; +import java.util.Collection; +import java.util.Set; + import static java.lang.String.format; import static java.util.Objects.requireNonNull; @@ -41,18 +42,21 @@ import static java.util.Objects.requireNonNull; * Loads the plugins server extensions and injects them to DI container */ public abstract class ServerExtensionInstaller { + private final Configuration configuration; private final SonarRuntime sonarRuntime; private final PluginRepository pluginRepository; private final Set<Class<? extends Annotation>> supportedAnnotationTypes; - protected ServerExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository, Collection<Class<? extends Annotation>> supportedAnnotationTypes) { + protected ServerExtensionInstaller(Configuration configuration, SonarRuntime sonarRuntime, PluginRepository pluginRepository, + Collection<Class<? extends Annotation>> supportedAnnotationTypes) { requireNonNull(supportedAnnotationTypes, "At least one supported annotation type must be specified"); + this.configuration = configuration; this.sonarRuntime = sonarRuntime; this.pluginRepository = pluginRepository; this.supportedAnnotationTypes = ImmutableSet.copyOf(supportedAnnotationTypes); } - public void installExtensions(ComponentContainer container) { + public void installExtensions(ExtensionContainer container) { ListMultimap<PluginInfo, Object> installedExtensionsByPlugin = ArrayListMultimap.create(); for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) { try { @@ -62,7 +66,7 @@ public abstract class ServerExtensionInstaller { Plugin.Context context = new PluginContextImpl.Builder() .setSonarRuntime(sonarRuntime) - .setBootConfiguration(container.getComponentByType(Configuration.class)) + .setBootConfiguration(configuration) .build(); plugin.define(context); for (Object extension : context.getExtensions()) { @@ -79,7 +83,7 @@ public abstract class ServerExtensionInstaller { } } - private Object installExtension(ComponentContainer container, PluginInfo pluginInfo, Object extension) { + private Object installExtension(ExtensionContainer container, PluginInfo pluginInfo, Object extension) { for (Class<? extends Annotation> supportedAnnotationType : supportedAnnotationTypes) { if (AnnotationUtils.getAnnotation(extension, supportedAnnotationType) != null) { container.addExtension(pluginInfo, extension); diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexDefinition.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexDefinition.java index b4f42b8fb57..23b0b2a2693 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexDefinition.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexDefinition.java @@ -37,6 +37,8 @@ import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ import static org.sonar.server.es.newindex.SettingsConfiguration.MANUAL_REFRESH_INTERVAL; import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder; +import javax.inject.Inject; + /** * Definition of ES index "rules", including settings and fields. */ @@ -82,6 +84,7 @@ public class RuleIndexDefinition implements IndexDefinition { private final Configuration config; private final boolean enableSource; + @Inject public RuleIndexDefinition(Configuration config) { this(config, false); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/setting/ThreadLocalSettings.java b/server/sonar-server-common/src/main/java/org/sonar/server/setting/ThreadLocalSettings.java index 530f318adf9..0bc77907753 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/setting/ThreadLocalSettings.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/setting/ThreadLocalSettings.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Properties; +import javax.inject.Inject; import org.apache.ibatis.exceptions.PersistenceException; import org.sonar.api.CoreProperties; import org.sonar.api.ce.ComputeEngineSide; @@ -60,6 +61,7 @@ public class ThreadLocalSettings extends Settings { private Map<String, String> getPropertiesDbFailureCache = Collections.emptyMap(); private SettingLoader settingLoader; + @Inject public ThreadLocalSettings(PropertyDefinitions definitions, Properties props) { this(definitions, props, new NopSettingLoader()); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/util/OkHttpClientProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/util/OkHttpClientProvider.java index ca822c8cb97..8c1a88e9484 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/util/OkHttpClientProvider.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/util/OkHttpClientProvider.java @@ -20,12 +20,12 @@ package org.sonar.server.util; import okhttp3.OkHttpClient; -import org.picocontainer.injectors.ProviderAdapter; import org.sonar.api.SonarRuntime; import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.config.Configuration; import org.sonar.api.server.ServerSide; import org.sonarqube.ws.client.OkHttpClientBuilder; +import org.springframework.context.annotation.Bean; import static java.lang.String.format; import static org.sonar.process.ProcessProperties.Property.HTTP_PROXY_PASSWORD; @@ -43,28 +43,24 @@ import static org.sonar.process.ProcessProperties.Property.HTTP_PROXY_USER; */ @ServerSide @ComputeEngineSide -public class OkHttpClientProvider extends ProviderAdapter { +public class OkHttpClientProvider { private static final int DEFAULT_CONNECT_TIMEOUT_IN_MS = 10_000; private static final int DEFAULT_READ_TIMEOUT_IN_MS = 10_000; - private okhttp3.OkHttpClient okHttpClient; - /** * @return a {@link OkHttpClient} singleton */ + @Bean("OkHttpClient") public OkHttpClient provide(Configuration config, SonarRuntime runtime) { - if (okHttpClient == null) { - OkHttpClientBuilder builder = new OkHttpClientBuilder(); - builder.setConnectTimeoutMs(DEFAULT_CONNECT_TIMEOUT_IN_MS); - builder.setReadTimeoutMs(DEFAULT_READ_TIMEOUT_IN_MS); - // no need to define proxy URL as system-wide proxy is used and properly - // configured by bootstrap process. - builder.setProxyLogin(config.get(HTTP_PROXY_USER.getKey()).orElse(null)); - builder.setProxyPassword(config.get(HTTP_PROXY_PASSWORD.getKey()).orElse(null)); - builder.setUserAgent(format("SonarQube/%s", runtime.getApiVersion().toString())); - okHttpClient = builder.build(); - } - return okHttpClient; + OkHttpClientBuilder builder = new OkHttpClientBuilder(); + builder.setConnectTimeoutMs(DEFAULT_CONNECT_TIMEOUT_IN_MS); + builder.setReadTimeoutMs(DEFAULT_READ_TIMEOUT_IN_MS); + // no need to define proxy URL as system-wide proxy is used and properly + // configured by bootstrap process. + builder.setProxyLogin(config.get(HTTP_PROXY_USER.getKey()).orElse(null)); + builder.setProxyPassword(config.get(HTTP_PROXY_PASSWORD.getKey()).orElse(null)); + builder.setUserAgent(format("SonarQube/%s", runtime.getApiVersion().toString())); + return builder.build(); } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableExecutorService.java b/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableExecutorService.java index a576ecaea83..07b97ad2c0d 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableExecutorService.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableExecutorService.java @@ -20,10 +20,10 @@ package org.sonar.server.util; import java.util.concurrent.ExecutorService; -import org.picocontainer.Startable; +import org.sonar.api.Startable; /** - * ExecutorService that exposes a {@code stop} method which can be invoked by Pico container to shutdown properly + * ExecutorService that exposes a {@code stop} method which can be invoked by the ioc container to shutdown properly * the service. */ public interface StoppableExecutorService extends ExecutorService, Startable { diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableScheduledExecutorService.java b/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableScheduledExecutorService.java index 4d16122024e..a6cf185477a 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableScheduledExecutorService.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/util/StoppableScheduledExecutorService.java @@ -22,7 +22,7 @@ package org.sonar.server.util; import java.util.concurrent.ScheduledExecutorService; /** - * ScheduledExecutorService that exposes a {@code stop} method which can be invoked by Pico container to shutdown + * ScheduledExecutorService that exposes a {@code stop} method which can be invoked by the ioc container to shutdown * properly the service. */ public interface StoppableScheduledExecutorService extends ScheduledExecutorService, StoppableExecutorService { diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientProviderTest.java index 959488fd09b..a4957a6b68b 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientProviderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientProviderTest.java @@ -42,8 +42,6 @@ import static org.sonar.process.ProcessProperties.Property.SEARCH_HOST; import static org.sonar.process.ProcessProperties.Property.SEARCH_PORT; public class EsClientProviderTest { - - @Rule public LogTester logTester = new LogTester(); @@ -74,9 +72,6 @@ public class EsClientProviderTest { assertThat(node.getHost().getPort()).isEqualTo(9000); assertThat(logTester.logs(LoggerLevel.INFO)).has(new Condition<>(s -> s.contains("Connected to local Elasticsearch: [http://" + localhostHostname + ":9000]"), "")); - - // keep in cache - assertThat(underTest.provide(settings.asConfig())).isSameAs(client); } @Test @@ -99,9 +94,6 @@ public class EsClientProviderTest { assertThat(logTester.logs(LoggerLevel.INFO)) .has(new Condition<>(s -> s.contains("Connected to remote Elasticsearch: [http://" + localhostHostname + ":8080, http://" + localhostHostname + ":8081]"), "")); - - // keep in cache - assertThat(underTest.provide(settings.asConfig())).isSameAs(client); } @Test @@ -147,8 +139,5 @@ public class EsClientProviderTest { assertThat(logTester.logs(LoggerLevel.INFO)) .has(new Condition<>(s -> s.contains("Connected to remote Elasticsearch: [http://" + localhostHostname + ":9001, http://" + localhostHostname + ":8081]"), "")); - - // keep in cache - assertThat(underTest.provide(settings.asConfig())).isSameAs(client); } } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientStopperTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientStopperTest.java deleted file mode 100644 index 6787d21af65..00000000000 --- a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsClientStopperTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.server.es; - -import org.junit.Test; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - -public class EsClientStopperTest { - - private EsClient client = mock(EsClient.class); - private EsClientStopper underTest = new EsClientStopper(client); - - @Test - public void stop_client() { - underTest.start(); - verifyNoMoreInteractions(client); - - underTest.stop(); - verify(client).close(); - } -} diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsModuleTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/es/EsModuleTest.java index c48c2d0bbbc..3c4a5ebe4a7 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsModuleTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/es/EsModuleTest.java @@ -20,17 +20,15 @@ package org.sonar.server.es; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class EsModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new EsModule().configure(container); - assertThat(container.size()).isEqualTo(2 + COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER); + assertThat(container.getAddedObjects()).hasSize(1); } - } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionBootstraperTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionBootstraperTest.java index a316b2f46ae..c000fe9ded9 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionBootstraperTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionBootstraperTest.java @@ -21,17 +21,16 @@ package org.sonar.server.extension; import org.junit.Test; import org.sonar.api.platform.Server; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; public class CoreExtensionBootstraperTest { - private ComponentContainer componentContainer = new ComponentContainer(); - private CoreExtensionBridge bridge = mock(CoreExtensionBridge.class); - - private CoreExtensionBootstraper underTest = new CoreExtensionBootstraper(componentContainer); + private final SpringComponentContainer componentContainer = new SpringComponentContainer(); + private final CoreExtensionBridge bridge = mock(CoreExtensionBridge.class); + private final CoreExtensionBootstraper underTest = new CoreExtensionBootstraper(componentContainer); @Test public void onServerStart_calls_startPlugin_if_Bridge_exists_in_container() { @@ -47,6 +46,7 @@ public class CoreExtensionBootstraperTest { @Test public void onServerStart_does_not_call_startPlugin_if_Bridge_does_not_exist_in_container() { + componentContainer.startComponents(); underTest.onServerStart(mock(Server.class)); verifyNoMoreInteractions(bridge); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionStopperTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionStopperTest.java index c93913c2078..995e03a9b74 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionStopperTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/extension/CoreExtensionStopperTest.java @@ -20,17 +20,16 @@ package org.sonar.server.extension; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.SpringComponentContainer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; public class CoreExtensionStopperTest { - private ComponentContainer componentContainer = new ComponentContainer(); - private CoreExtensionBridge bridge = mock(CoreExtensionBridge.class); - - private CoreExtensionStopper underTest = new CoreExtensionStopper(componentContainer); + private final SpringComponentContainer componentContainer = new SpringComponentContainer(); + private final CoreExtensionBridge bridge = mock(CoreExtensionBridge.class); + private final CoreExtensionStopper underTest = new CoreExtensionStopper(componentContainer); @Test public void stop_calls_stopPlugin_if_Bridge_exists_in_container() { @@ -46,6 +45,7 @@ public class CoreExtensionStopperTest { @Test public void stop_does_not_call_stopPlugin_if_Bridge_does_not_exist_in_container() { + componentContainer.startComponents(); underTest.stop(); verifyNoMoreInteractions(bridge); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/IssuesChangesNotificationModuleTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/IssuesChangesNotificationModuleTest.java index 9bfc49467bd..66e7e3912f1 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/IssuesChangesNotificationModuleTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/IssuesChangesNotificationModuleTest.java @@ -20,17 +20,16 @@ package org.sonar.server.issue.notification; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; public class IssuesChangesNotificationModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); new IssuesChangesNotificationModule().configure(container); - assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 7); + assertThat(container.getAddedObjects()).hasSize(7); } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/platform/ServerLifecycleNotifierTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/platform/ServerLifecycleNotifierTest.java index d6e54529be7..e09a0ba8824 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/platform/ServerLifecycleNotifierTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/platform/ServerLifecycleNotifierTest.java @@ -26,6 +26,7 @@ import org.sonar.api.platform.Server; import org.sonar.api.platform.ServerStartHandler; import org.sonar.api.platform.ServerStopHandler; +import static org.assertj.core.api.Assertions.assertThatNoException; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -52,7 +53,7 @@ public class ServerLifecycleNotifierTest { */ @Test public void doNotNotifyWithTheStartMethod() { - ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[]{start1, start2}, new ServerStopHandler[]{stop2}); + ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[] {start1, start2}, new ServerStopHandler[] {stop2}); notifier.start(); verify(start1, never()).onServerStart(server); @@ -62,7 +63,7 @@ public class ServerLifecycleNotifierTest { @Test public void notifyOnStart() { - ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[]{start1, start2}, new ServerStopHandler[]{stop2}); + ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[] {start1, start2}, new ServerStopHandler[] {stop2}); notifier.notifyStart(); verify(start1).onServerStart(server); @@ -70,10 +71,9 @@ public class ServerLifecycleNotifierTest { verify(stop1, never()).onServerStop(server); } - @Test public void notifyOnStop() { - ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[]{start1, start2}, new ServerStopHandler[]{stop1, stop2}); + ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, new ServerStartHandler[] {start1, start2}, new ServerStopHandler[] {stop1, stop2}); notifier.stop(); verify(start1, never()).onServerStart(server); @@ -81,6 +81,13 @@ public class ServerLifecycleNotifierTest { verify(stop1).onServerStop(server); verify(stop2).onServerStop(server); } + + @Test + public void null_handler_param_wont_lead_to_NPE() { + ServerLifecycleNotifier notifier = new ServerLifecycleNotifier(server, null, null); + assertThatNoException().isThrownBy(notifier::notifyStart); + assertThatNoException().isThrownBy(notifier::stop); + } } class FakeServer extends Server { diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/platform/StartupMetadataProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/platform/StartupMetadataProviderTest.java index 724e54efda2..1b1eb7f902a 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/platform/StartupMetadataProviderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/platform/StartupMetadataProviderTest.java @@ -34,21 +34,19 @@ import org.sonar.db.property.PropertyDto; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import static org.sonar.api.utils.DateUtils.formatDateTime; public class StartupMetadataProviderTest { - private static final long A_DATE = 1_500_000_000_000L; - @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - private StartupMetadataProvider underTest = new StartupMetadataProvider(); - private System2 system = mock(System2.class); - private WebServer webServer = mock(WebServer.class); + private final StartupMetadataProvider underTest = new StartupMetadataProvider(); + private final System2 system = mock(System2.class); + private final WebServer webServer = mock(WebServer.class); @Test public void generate_SERVER_STARTIME_but_do_not_persist_it_if_server_is_startup_leader() { @@ -60,10 +58,6 @@ public class StartupMetadataProviderTest { assertThat(metadata.getStartedAt()).isEqualTo(A_DATE); assertNotPersistedProperty(CoreProperties.SERVER_STARTTIME); - - // keep a cache - StartupMetadata secondMetadata = underTest.provide(system, runtime, webServer, dbTester.getDbClient()); - assertThat(secondMetadata).isSameAs(metadata); } @Test @@ -109,11 +103,7 @@ public class StartupMetadataProviderTest { // still in database assertPersistedProperty(CoreProperties.SERVER_STARTTIME, formatDateTime(A_DATE)); - // keep a cache - StartupMetadata secondMetadata = underTest.provide(system, runtime, webServer, dbTester.getDbClient()); - assertThat(secondMetadata).isSameAs(metadata); - - verifyZeroInteractions(system); + verifyNoInteractions(system); } private void assertPersistedProperty(String propertyKey, String expectedValue) { diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java index 7ea17aeca90..f8a82450dff 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/platform/TempFolderProviderTest.java @@ -31,12 +31,10 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class TempFolderProviderTest { - - @Rule public TemporaryFolder temp = new TemporaryFolder(); - TempFolderProvider underTest = new TempFolderProvider(); + private final TempFolderProvider underTest = new TempFolderProvider(); @Test public void existing_temp_dir() throws Exception { diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/plugins/ServerExtensionInstallerTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/plugins/ServerExtensionInstallerTest.java index c8b3e0db118..ce7004ea6cb 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/plugins/ServerExtensionInstallerTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/plugins/ServerExtensionInstallerTest.java @@ -27,10 +27,11 @@ import org.sonar.api.Plugin; import org.sonar.api.SonarEdition; import org.sonar.api.SonarQubeSide; import org.sonar.api.SonarRuntime; +import org.sonar.api.config.Configuration; import org.sonar.api.internal.SonarRuntimeImpl; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.Version; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import org.sonar.core.platform.PluginInfo; import org.sonar.core.platform.PluginRepository; @@ -42,7 +43,6 @@ import static org.mockito.Mockito.when; public class ServerExtensionInstallerTest { private SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.parse("8.0"), SonarQubeSide.SERVER, SonarEdition.COMMUNITY); private TestPluginRepository pluginRepository = new TestPluginRepository(); - private TestServerExtensionInstaller underTest = new TestServerExtensionInstaller(sonarRuntime, pluginRepository); @Test @@ -50,11 +50,11 @@ public class ServerExtensionInstallerTest { PluginInfo fooPluginInfo = newPlugin("foo", "Foo"); Plugin fooPlugin = mock(Plugin.class); pluginRepository.add(fooPluginInfo, fooPlugin); - ComponentContainer componentContainer = new ComponentContainer(); + ListContainer componentContainer = new ListContainer(); underTest.installExtensions(componentContainer); - assertThat(componentContainer.getPicoContainer().getComponents()).contains(fooPlugin); + assertThat(componentContainer.getAddedObjects()).contains(fooPlugin); } private static PluginInfo newPlugin(String key, String name) { @@ -108,7 +108,7 @@ public class ServerExtensionInstallerTest { private static class TestServerExtensionInstaller extends ServerExtensionInstaller { protected TestServerExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository) { - super(sonarRuntime, pluginRepository, singleton(ServerSide.class)); + super(mock(Configuration.class), sonarRuntime, pluginRepository, singleton(ServerSide.class)); } } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/util/OkHttpClientProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/util/OkHttpClientProviderTest.java index 729ac9654dc..01879d1053a 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/util/OkHttpClientProviderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/util/OkHttpClientProviderTest.java @@ -41,8 +41,8 @@ import static org.assertj.core.api.Assertions.assertThat; public class OkHttpClientProviderTest { - private MapSettings settings = new MapSettings(); - private SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("6.2"), SonarQubeSide.SERVER, SonarEdition.COMMUNITY); + private final MapSettings settings = new MapSettings(); + private final SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("6.2"), SonarQubeSide.SERVER, SonarEdition.COMMUNITY); private final OkHttpClientProvider underTest = new OkHttpClientProvider(); @Rule @@ -74,13 +74,6 @@ public class OkHttpClientProviderTest { assertThat(request.header("Proxy-Authorization")).isEqualTo("Basic " + Base64.getEncoder().encodeToString("the-login:the-password".getBytes())); } - @Test - public void get_returns_a_singleton() { - OkHttpClient client1 = underTest.provide(settings.asConfig(), runtime); - OkHttpClient client2 = underTest.provide(settings.asConfig(), runtime); - assertThat(client2).isNotNull().isSameAs(client1); - } - private RecordedRequest call(OkHttpClient client) throws IOException, InterruptedException { server.enqueue(new MockResponse().setBody("pong")); client.newCall(new Request.Builder().url(server.url("/ping")).build()).execute(); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookModuleTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookModuleTest.java index f53ad77b459..20bd6df587e 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookModuleTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookModuleTest.java @@ -20,7 +20,7 @@ package org.sonar.server.webhook; import org.junit.Test; -import org.sonar.core.platform.ComponentContainer; +import org.sonar.core.platform.ListContainer; import static org.assertj.core.api.Assertions.assertThat; @@ -30,10 +30,10 @@ public class WebhookModuleTest { @Test public void verify_count_of_added_components() { - ComponentContainer container = new ComponentContainer(); + ListContainer container = new ListContainer(); underTest.configure(container); - assertThat(container.size()).isNotZero(); + assertThat(container.getAddedObjects().size()).isNotZero(); } } |