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.notification.ws;
22 import com.google.common.collect.ImmutableList;
23 import java.util.Arrays;
24 import java.util.List;
25 import javax.annotation.Nullable;
26 import org.sonar.api.notifications.NotificationChannel;
27 import org.sonar.api.utils.log.Logger;
28 import org.sonar.api.utils.log.Loggers;
29 import org.sonar.server.notification.NotificationDispatcherMetadata;
31 public class NotificationCenter {
33 private static final Logger LOG = Loggers.get(NotificationCenter.class);
35 private final NotificationDispatcherMetadata[] dispatchersMetadata;
36 private final NotificationChannel[] channels;
38 public NotificationCenter(NotificationDispatcherMetadata[] metadata, NotificationChannel[] channels) {
39 this.dispatchersMetadata = metadata;
40 this.channels = channels;
44 * Default constructor when no channels.
46 public NotificationCenter(NotificationDispatcherMetadata[] metadata) {
47 this(metadata, new NotificationChannel[0]);
48 LOG.warn("There is no notification channel - no notification will be delivered!");
52 * Default constructor when no dispatcher metadata.
54 public NotificationCenter(NotificationChannel[] channels) {
55 this(new NotificationDispatcherMetadata[0], channels);
58 public NotificationCenter() {
59 this(new NotificationDispatcherMetadata[0], new NotificationChannel[0]);
60 LOG.warn("There is no notification channel - no notification will be delivered!");
63 public List<NotificationChannel> getChannels() {
64 return Arrays.asList(channels);
68 * Returns all the available dispatchers which metadata matches the given property and its value.
70 * If "propertyValue" is null, the verification is done on the existence of such a property (whatever the value).
72 public List<String> getDispatcherKeysForProperty(String propertyKey, @Nullable String propertyValue) {
73 ImmutableList.Builder<String> keys = ImmutableList.builder();
74 for (NotificationDispatcherMetadata metadata : dispatchersMetadata) {
75 String dispatcherKey = metadata.getDispatcherKey();
76 String value = metadata.getProperty(propertyKey);
77 if (value != null && (propertyValue == null || value.equals(propertyValue))) {
78 keys.add(dispatcherKey);