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.

TestAppState.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.application;
  21. import java.util.ArrayList;
  22. import java.util.EnumMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Optional;
  26. import java.util.concurrent.atomic.AtomicBoolean;
  27. import javax.annotation.Nonnull;
  28. import org.sonar.process.NetworkUtilsImpl;
  29. import org.sonar.process.ProcessId;
  30. public class TestAppState implements AppState {
  31. private final Map<ProcessId, Boolean> localProcesses = new EnumMap<>(ProcessId.class);
  32. private final Map<ProcessId, Boolean> remoteProcesses = new EnumMap<>(ProcessId.class);
  33. private final List<AppStateListener> listeners = new ArrayList<>();
  34. private final AtomicBoolean webLeaderLocked = new AtomicBoolean(false);
  35. @Override
  36. public void addListener(@Nonnull AppStateListener listener) {
  37. this.listeners.add(listener);
  38. }
  39. @Override
  40. public boolean isOperational(ProcessId processId, boolean local) {
  41. if (local) {
  42. return localProcesses.computeIfAbsent(processId, p -> false);
  43. }
  44. return remoteProcesses.computeIfAbsent(processId, p -> false);
  45. }
  46. @Override
  47. public void setOperational(ProcessId processId) {
  48. localProcesses.put(processId, true);
  49. remoteProcesses.put(processId, true);
  50. listeners.forEach(l -> l.onAppStateOperational(processId));
  51. }
  52. public void setRemoteOperational(ProcessId processId) {
  53. remoteProcesses.put(processId, true);
  54. listeners.forEach(l -> l.onAppStateOperational(processId));
  55. }
  56. @Override
  57. public boolean tryToLockWebLeader() {
  58. return webLeaderLocked.compareAndSet(false, true);
  59. }
  60. @Override
  61. public void reset() {
  62. webLeaderLocked.set(false);
  63. localProcesses.clear();
  64. }
  65. @Override
  66. public void registerSonarQubeVersion(String sonarqubeVersion) {
  67. // nothing to do
  68. }
  69. @Override
  70. public void registerClusterName(String clusterName) {
  71. // nothing to do
  72. }
  73. @Override
  74. public Optional<String> getLeaderHostName() {
  75. return Optional.of(NetworkUtilsImpl.INSTANCE.getHostname());
  76. }
  77. @Override
  78. public void close() {
  79. // nothing to do
  80. }
  81. }