3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.platformlevel;
22 import org.sonar.api.utils.log.Loggers;
23 import org.sonar.core.platform.EditionProvider;
24 import org.sonar.core.platform.PlatformEditionProvider;
25 import org.sonar.server.app.ProcessCommandWrapper;
26 import org.sonar.server.authentication.DefaultAdminCredentialsVerifierImpl;
27 import org.sonar.server.ce.queue.CeQueueCleaner;
28 import org.sonar.server.es.IndexerStartupTask;
29 import org.sonar.server.platform.ServerLifecycleNotifier;
30 import org.sonar.server.platform.web.RegisterServletFilters;
31 import org.sonar.server.plugins.DetectPluginChange;
32 import org.sonar.server.plugins.PluginConsentVerifier;
33 import org.sonar.server.qualitygate.ProjectsInWarningDaemon;
34 import org.sonar.server.qualitygate.RegisterQualityGates;
35 import org.sonar.server.qualityprofile.builtin.BuiltInQProfileInsertImpl;
36 import org.sonar.server.qualityprofile.builtin.BuiltInQProfileLoader;
37 import org.sonar.server.qualityprofile.builtin.BuiltInQProfileUpdateImpl;
38 import org.sonar.server.qualityprofile.builtin.BuiltInQualityProfilesUpdateListener;
39 import org.sonar.server.qualityprofile.RegisterQualityProfiles;
40 import org.sonar.server.rule.RegisterRules;
41 import org.sonar.server.rule.WebServerRuleFinder;
42 import org.sonar.server.startup.GeneratePluginIndex;
43 import org.sonar.server.startup.RegisterMetrics;
44 import org.sonar.server.startup.RegisterPermissionTemplates;
45 import org.sonar.server.startup.RegisterPlugins;
46 import org.sonar.server.startup.RenameDeprecatedPropertyKeys;
47 import org.sonar.server.startup.UpgradeSuggestionsCleaner;
48 import org.sonar.server.user.DoPrivileged;
49 import org.sonar.server.user.ThreadLocalUserSession;
51 public class PlatformLevelStartup extends PlatformLevel {
52 private AddIfStartupLeaderAndPluginsChanged addIfPluginsChanged;
54 public PlatformLevelStartup(PlatformLevel parent) {
55 super("startup tasks", parent);
59 protected void configureLevel() {
60 add(GeneratePluginIndex.class,
61 ServerLifecycleNotifier.class);
64 IndexerStartupTask.class);
65 addIfStartupLeaderAndPluginsChanged(
66 RegisterMetrics.class,
67 RegisterQualityGates.class,
69 BuiltInQProfileLoader.class);
71 BuiltInQualityProfilesUpdateListener.class,
72 BuiltInQProfileUpdateImpl.class);
73 addIfStartupLeaderAndPluginsChanged(
74 BuiltInQProfileInsertImpl.class,
75 RegisterQualityProfiles.class);
77 RegisterPermissionTemplates.class,
78 RenameDeprecatedPropertyKeys.class,
80 UpgradeSuggestionsCleaner.class,
81 PluginConsentVerifier.class);
82 add(RegisterPlugins.class,
83 // RegisterServletFilters makes the WebService engine of Level4 served by the MasterServletFilter, therefore it
84 // must be started after all the other startup tasks
85 RegisterServletFilters.class
90 * Add a component to container only if plugins have changed since last start.
92 * @throws IllegalStateException if called from PlatformLevel3 or below, plugin info is loaded yet
94 AddIfStartupLeaderAndPluginsChanged addIfStartupLeaderAndPluginsChanged(Object... objects) {
95 if (addIfPluginsChanged == null) {
96 this.addIfPluginsChanged = new AddIfStartupLeaderAndPluginsChanged(getWebServer().isStartupLeader() && anyPluginChanged());
98 addIfPluginsChanged.ifAdd(objects);
99 return addIfPluginsChanged;
102 private boolean anyPluginChanged() {
103 return parent.getOptional(DetectPluginChange.class)
104 .map(DetectPluginChange::anyPluginChanged)
105 .orElseThrow(() -> new IllegalStateException("DetectPluginChange not available in the container yet"));
108 public final class AddIfStartupLeaderAndPluginsChanged extends AddIf {
109 private AddIfStartupLeaderAndPluginsChanged(boolean condition) {
115 public PlatformLevel start() {
116 DoPrivileged.execute(new DoPrivileged.Task(parent.get(ThreadLocalUserSession.class)) {
118 protected void doPrivileged() {
119 PlatformLevelStartup.super.start();
120 getOptional(IndexerStartupTask.class).ifPresent(IndexerStartupTask::execute);
121 // Need to be executed after indexing as it executes an ES query
122 get(ProjectsInWarningDaemon.class).notifyStart();
123 get(ServerLifecycleNotifier.class).notifyStart();
124 get(ProcessCommandWrapper.class).notifyOperational();
125 get(WebServerRuleFinder.class).stopCaching();
126 Loggers.get(PlatformLevelStartup.class)
127 .info("Running {} Edition", get(PlatformEditionProvider.class).get().map(EditionProvider.Edition::getLabel).orElse(""));
128 get(DefaultAdminCredentialsVerifierImpl.class).runAtStart();