3 * Copyright (C) 2009-2024 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.RegisterQualityGates;
34 import org.sonar.server.qualityprofile.RegisterQualityProfiles;
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.rule.AdvancedRuleDescriptionSectionsGenerator;
40 import org.sonar.server.rule.LegacyHotspotRuleDescriptionSectionsGenerator;
41 import org.sonar.server.rule.LegacyIssueRuleDescriptionSectionsGenerator;
42 import org.sonar.server.rule.RuleDescriptionSectionsGeneratorResolver;
43 import org.sonar.server.rule.WebServerRuleFinder;
44 import org.sonar.server.rule.registration.NewRuleCreator;
45 import org.sonar.server.rule.registration.QualityProfileChangesUpdater;
46 import org.sonar.server.rule.registration.RulesKeyVerifier;
47 import org.sonar.server.rule.registration.RulesRegistrant;
48 import org.sonar.server.rule.registration.StartupRuleUpdater;
49 import org.sonar.server.startup.RegisterMetrics;
50 import org.sonar.server.startup.RegisterPermissionTemplates;
51 import org.sonar.server.startup.RegisterPlugins;
52 import org.sonar.server.startup.RenameDeprecatedPropertyKeys;
53 import org.sonar.server.startup.UpgradeSuggestionsCleaner;
54 import org.sonar.server.user.DoPrivileged;
55 import org.sonar.server.user.ThreadLocalUserSession;
57 public class PlatformLevelStartup extends PlatformLevel {
58 private AddIfStartupLeaderAndPluginsChanged addIfPluginsChanged;
60 public PlatformLevelStartup(PlatformLevel parent) {
61 super("startup tasks", parent);
65 protected void configureLevel() {
66 add(ServerLifecycleNotifier.class);
69 IndexerStartupTask.class);
71 RuleDescriptionSectionsGeneratorResolver.class,
72 AdvancedRuleDescriptionSectionsGenerator.class,
73 LegacyHotspotRuleDescriptionSectionsGenerator.class,
74 LegacyIssueRuleDescriptionSectionsGenerator.class,
75 RulesRegistrant.class,
77 RulesKeyVerifier.class,
78 StartupRuleUpdater.class,
79 QualityProfileChangesUpdater.class);
80 addIfStartupLeaderAndPluginsChanged(
81 RegisterMetrics.class,
82 RegisterQualityGates.class,
83 BuiltInQProfileLoader.class);
85 BuiltInQualityProfilesUpdateListener.class,
86 BuiltInQProfileUpdateImpl.class);
87 addIfStartupLeaderAndPluginsChanged(
88 BuiltInQProfileInsertImpl.class,
89 RegisterQualityProfiles.class);
91 RegisterPermissionTemplates.class,
92 RenameDeprecatedPropertyKeys.class,
94 UpgradeSuggestionsCleaner.class,
95 PluginConsentVerifier.class);
96 add(RegisterPlugins.class,
97 // RegisterServletFilters makes the WebService engine of Level4 served by the MasterServletFilter, therefore it
98 // must be started after all the other startup tasks
99 RegisterServletFilters.class
104 * Add a component to container only if plugins have changed since last start.
106 * @throws IllegalStateException if called from PlatformLevel3 or below, plugin info is loaded yet
108 AddIfStartupLeaderAndPluginsChanged addIfStartupLeaderAndPluginsChanged(Object... objects) {
109 if (addIfPluginsChanged == null) {
110 this.addIfPluginsChanged = new AddIfStartupLeaderAndPluginsChanged(getWebServer().isStartupLeader() && anyPluginChanged());
112 addIfPluginsChanged.ifAdd(objects);
113 return addIfPluginsChanged;
116 private boolean anyPluginChanged() {
117 return parent.getOptional(DetectPluginChange.class)
118 .map(DetectPluginChange::anyPluginChanged)
119 .orElseThrow(() -> new IllegalStateException("DetectPluginChange not available in the container yet"));
122 public final class AddIfStartupLeaderAndPluginsChanged extends AddIf {
123 private AddIfStartupLeaderAndPluginsChanged(boolean condition) {
129 public PlatformLevel start() {
130 DoPrivileged.execute(new DoPrivileged.Task(parent.get(ThreadLocalUserSession.class)) {
132 protected void doPrivileged() {
133 PlatformLevelStartup.super.start();
134 getOptional(IndexerStartupTask.class).ifPresent(IndexerStartupTask::execute);
135 get(ServerLifecycleNotifier.class).notifyStart();
136 get(ProcessCommandWrapper.class).notifyOperational();
137 get(WebServerRuleFinder.class).stopCaching();
138 LoggerFactory.getLogger(PlatformLevelStartup.class)
139 .info("Running {} Edition", get(PlatformEditionProvider.class).get().map(EditionProvider.Edition::getLabel).orElse(""));
140 get(DefaultAdminCredentialsVerifierImpl.class).runAtStart();