3 * Copyright (C) 2009-2023 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.slf4j.LoggerFactory;
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.AdvancedRuleDescriptionSectionsGenerator;
41 import org.sonar.server.rule.LegacyHotspotRuleDescriptionSectionsGenerator;
42 import org.sonar.server.rule.LegacyIssueRuleDescriptionSectionsGenerator;
43 import org.sonar.server.rule.RegisterRules;
44 import org.sonar.server.rule.RuleDescriptionSectionsGeneratorResolver;
45 import org.sonar.server.rule.WebServerRuleFinder;
46 import org.sonar.server.startup.GeneratePluginIndex;
47 import org.sonar.server.startup.RegisterMetrics;
48 import org.sonar.server.startup.RegisterPermissionTemplates;
49 import org.sonar.server.startup.RegisterPlugins;
50 import org.sonar.server.startup.RenameDeprecatedPropertyKeys;
51 import org.sonar.server.startup.UpgradeSuggestionsCleaner;
52 import org.sonar.server.user.DoPrivileged;
53 import org.sonar.server.user.ThreadLocalUserSession;
55 public class PlatformLevelStartup extends PlatformLevel {
56 private AddIfStartupLeaderAndPluginsChanged addIfPluginsChanged;
58 public PlatformLevelStartup(PlatformLevel parent) {
59 super("startup tasks", parent);
63 protected void configureLevel() {
64 add(GeneratePluginIndex.class,
65 ServerLifecycleNotifier.class);
68 IndexerStartupTask.class);
69 addIfStartupLeaderAndPluginsChanged(
70 RegisterMetrics.class,
71 RegisterQualityGates.class,
72 RuleDescriptionSectionsGeneratorResolver.class,
73 AdvancedRuleDescriptionSectionsGenerator.class,
74 LegacyHotspotRuleDescriptionSectionsGenerator.class,
75 LegacyIssueRuleDescriptionSectionsGenerator.class,
77 BuiltInQProfileLoader.class);
79 BuiltInQualityProfilesUpdateListener.class,
80 BuiltInQProfileUpdateImpl.class);
81 addIfStartupLeaderAndPluginsChanged(
82 BuiltInQProfileInsertImpl.class,
83 RegisterQualityProfiles.class);
85 RegisterPermissionTemplates.class,
86 RenameDeprecatedPropertyKeys.class,
88 UpgradeSuggestionsCleaner.class,
89 PluginConsentVerifier.class);
90 add(RegisterPlugins.class,
91 // RegisterServletFilters makes the WebService engine of Level4 served by the MasterServletFilter, therefore it
92 // must be started after all the other startup tasks
93 RegisterServletFilters.class
98 * Add a component to container only if plugins have changed since last start.
100 * @throws IllegalStateException if called from PlatformLevel3 or below, plugin info is loaded yet
102 AddIfStartupLeaderAndPluginsChanged addIfStartupLeaderAndPluginsChanged(Object... objects) {
103 if (addIfPluginsChanged == null) {
104 this.addIfPluginsChanged = new AddIfStartupLeaderAndPluginsChanged(getWebServer().isStartupLeader() && anyPluginChanged());
106 addIfPluginsChanged.ifAdd(objects);
107 return addIfPluginsChanged;
110 private boolean anyPluginChanged() {
111 return parent.getOptional(DetectPluginChange.class)
112 .map(DetectPluginChange::anyPluginChanged)
113 .orElseThrow(() -> new IllegalStateException("DetectPluginChange not available in the container yet"));
116 public final class AddIfStartupLeaderAndPluginsChanged extends AddIf {
117 private AddIfStartupLeaderAndPluginsChanged(boolean condition) {
123 public PlatformLevel start() {
124 DoPrivileged.execute(new DoPrivileged.Task(parent.get(ThreadLocalUserSession.class)) {
126 protected void doPrivileged() {
127 PlatformLevelStartup.super.start();
128 getOptional(IndexerStartupTask.class).ifPresent(IndexerStartupTask::execute);
129 // Need to be executed after indexing as it executes an ES query
130 get(ProjectsInWarningDaemon.class).notifyStart();
131 get(ServerLifecycleNotifier.class).notifyStart();
132 get(ProcessCommandWrapper.class).notifyOperational();
133 get(WebServerRuleFinder.class).stopCaching();
134 LoggerFactory.getLogger(PlatformLevelStartup.class)
135 .info("Running {} Edition", get(PlatformEditionProvider.class).get().map(EditionProvider.Edition::getLabel).orElse(""));
136 get(DefaultAdminCredentialsVerifierImpl.class).runAtStart();