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.setting;
22 import java.util.List;
25 import java.util.function.Function;
26 import java.util.stream.Collectors;
27 import org.sonar.api.config.Configuration;
28 import org.sonar.api.config.internal.Settings;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.property.PropertyDto;
34 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
36 public class ProjectConfigurationLoaderImpl implements ProjectConfigurationLoader {
37 private final Settings globalSettings;
38 private final DbClient dbClient;
40 public ProjectConfigurationLoaderImpl(Settings globalSettings, DbClient dbClient) {
41 this.globalSettings = globalSettings;
42 this.dbClient = dbClient;
46 public Map<String, Configuration> loadProjectConfigurations(DbSession dbSession, Set<ComponentDto> projects) {
47 Set<String> mainBranchDbKeys = projects.stream().map(ComponentDto::getKey).collect(Collectors.toSet());
48 Map<String, ChildSettings> mainBranchSettingsByDbKey = loadMainBranchConfigurations(dbSession, mainBranchDbKeys);
49 return projects.stream()
50 .collect(uniqueIndex(ComponentDto::uuid, component -> {
51 if (component.getKey().equals(component.getKey())) {
52 return mainBranchSettingsByDbKey.get(component.getKey()).asConfiguration();
55 ChildSettings settings = new ChildSettings(mainBranchSettingsByDbKey.get(component.getKey()));
56 dbClient.propertiesDao()
57 .selectProjectProperties(dbSession, component.getKey())
58 .forEach(property -> settings.setProperty(property.getKey(), property.getValue()));
59 return settings.asConfiguration();
63 private Map<String, ChildSettings> loadMainBranchConfigurations(DbSession dbSession, Set<String> dbKeys) {
64 return dbKeys.stream().collect(uniqueIndex(Function.identity(), dbKey -> {
65 ChildSettings settings = new ChildSettings(globalSettings);
66 List<PropertyDto> propertyDtos = dbClient.propertiesDao()
67 .selectProjectProperties(dbSession, dbKey);
69 .forEach(property -> settings.setProperty(property.getKey(), property.getValue()));