3 * Copyright (C) 2009-2023 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.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.sonar.server.notification.NotificationDispatcherMetadata;
30 import org.springframework.beans.factory.annotation.Autowired;
32 public class NotificationCenter {
34 private static final Logger LOG = LoggerFactory.getLogger(NotificationCenter.class);
36 private final NotificationDispatcherMetadata[] dispatchersMetadata;
37 private final NotificationChannel[] channels;
39 @Autowired(required = false)
40 public NotificationCenter(NotificationDispatcherMetadata[] metadata, NotificationChannel[] channels) {
41 this.dispatchersMetadata = metadata;
42 this.channels = channels;
46 * Default constructor when no channels.
48 @Autowired(required = false)
49 public NotificationCenter(NotificationDispatcherMetadata[] metadata) {
50 this(metadata, new NotificationChannel[0]);
51 LOG.warn("There is no notification channel - no notification will be delivered!");
55 * Default constructor when no dispatcher metadata.
57 @Autowired(required = false)
58 public NotificationCenter(NotificationChannel[] channels) {
59 this(new NotificationDispatcherMetadata[0], channels);
62 @Autowired(required = false)
63 public NotificationCenter() {
64 this(new NotificationDispatcherMetadata[0], new NotificationChannel[0]);
65 LOG.warn("There is no notification channel - no notification will be delivered!");
68 public List<NotificationChannel> getChannels() {
69 return Arrays.asList(channels);
73 * Returns all the available dispatchers which metadata matches the given property and its value.
75 * If "propertyValue" is null, the verification is done on the existence of such a property (whatever the value).
77 public List<String> getDispatcherKeysForProperty(String propertyKey, @Nullable String propertyValue) {
78 ImmutableList.Builder<String> keys = ImmutableList.builder();
79 for (NotificationDispatcherMetadata metadata : dispatchersMetadata) {
80 String dispatcherKey = metadata.getDispatcherKey();
81 String value = metadata.getProperty(propertyKey);
82 if (value != null && (propertyValue == null || value.equals(propertyValue))) {
83 keys.add(dispatcherKey);