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.

PersistentSettings.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.platform;
  21. import javax.annotation.CheckForNull;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.config.internal.Settings;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.property.PropertyDto;
  27. import org.sonar.server.setting.SettingsChangeNotifier;
  28. public class PersistentSettings {
  29. private final Settings delegate;
  30. private final DbClient dbClient;
  31. private final SettingsChangeNotifier changeNotifier;
  32. public PersistentSettings(Settings delegate, DbClient dbClient, SettingsChangeNotifier changeNotifier) {
  33. this.delegate = delegate;
  34. this.dbClient = dbClient;
  35. this.changeNotifier = changeNotifier;
  36. }
  37. @CheckForNull
  38. public String getString(String key) {
  39. return delegate.getString(key);
  40. }
  41. /**
  42. * Insert property into database if value is not {@code null}, else delete property from
  43. * database. Session is not committed but {@link org.sonar.api.config.GlobalPropertyChangeHandler}
  44. * are executed.
  45. */
  46. public PersistentSettings saveProperty(DbSession dbSession, String key, @Nullable String value) {
  47. savePropertyImpl(dbSession, key, value);
  48. changeNotifier.onGlobalPropertyChange(key, value);
  49. return this;
  50. }
  51. /**
  52. * Same as {@link #saveProperty(DbSession, String, String)} but a new database session is
  53. * opened and committed.
  54. */
  55. public PersistentSettings saveProperty(String key, @Nullable String value) {
  56. try (DbSession dbSession = dbClient.openSession(false)) {
  57. savePropertyImpl(dbSession, key, value);
  58. dbSession.commit();
  59. changeNotifier.onGlobalPropertyChange(key, value);
  60. return this;
  61. }
  62. }
  63. private void savePropertyImpl(DbSession dbSession, String key, @Nullable String value) {
  64. if (value == null) {
  65. dbClient.propertiesDao().deleteGlobalProperty(key, dbSession);
  66. } else {
  67. dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey(key).setValue(value));
  68. }
  69. // refresh the cache of settings
  70. delegate.setProperty(key, value);
  71. }
  72. public Settings getSettings() {
  73. return delegate;
  74. }
  75. }