You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProjectConfigurationLoaderImpl.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.setting;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.util.function.Function;
  25. import java.util.stream.Collectors;
  26. import org.sonar.api.config.Configuration;
  27. import org.sonar.api.config.internal.Settings;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.component.ComponentDto;
  31. import org.sonar.db.property.PropertyDto;
  32. import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
  33. public class ProjectConfigurationLoaderImpl implements ProjectConfigurationLoader {
  34. private final Settings globalSettings;
  35. private final DbClient dbClient;
  36. public ProjectConfigurationLoaderImpl(Settings globalSettings, DbClient dbClient) {
  37. this.globalSettings = globalSettings;
  38. this.dbClient = dbClient;
  39. }
  40. @Override
  41. public Map<String, Configuration> loadProjectConfigurations(DbSession dbSession, Set<ComponentDto> projects) {
  42. Set<String> mainBranchDbKeys = projects.stream().map(ComponentDto::getKey).collect(Collectors.toSet());
  43. Map<String, ChildSettings> mainBranchSettingsByDbKey = loadMainBranchConfigurations(dbSession, mainBranchDbKeys);
  44. return projects.stream()
  45. .collect(uniqueIndex(ComponentDto::uuid, component -> {
  46. if (component.getDbKey().equals(component.getKey())) {
  47. return mainBranchSettingsByDbKey.get(component.getKey()).asConfiguration();
  48. }
  49. ChildSettings settings = new ChildSettings(mainBranchSettingsByDbKey.get(component.getKey()));
  50. dbClient.propertiesDao()
  51. .selectProjectProperties(dbSession, component.getDbKey())
  52. .forEach(property -> settings.setProperty(property.getKey(), property.getValue()));
  53. return settings.asConfiguration();
  54. }));
  55. }
  56. private Map<String, ChildSettings> loadMainBranchConfigurations(DbSession dbSession, Set<String> dbKeys) {
  57. return dbKeys.stream().collect(uniqueIndex(Function.identity(), dbKey -> {
  58. ChildSettings settings = new ChildSettings(globalSettings);
  59. List<PropertyDto> propertyDtos = dbClient.propertiesDao()
  60. .selectProjectProperties(dbSession, dbKey);
  61. propertyDtos
  62. .forEach(property -> settings.setProperty(property.getKey(), property.getValue()));
  63. return settings;
  64. }));
  65. }
  66. }