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