3 * Copyright (C) 2009-2017 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.qualitygate.changeevent;
22 import com.google.common.collect.ImmutableList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.List;
26 import org.sonar.api.utils.log.Logger;
27 import org.sonar.api.utils.log.Loggers;
29 import static java.lang.String.format;
32 * Broadcast a given collection of {@link QGChangeEvent} for a specific trigger to all the registered
33 * {@link QGChangeEventListener} in Pico.
35 * This class ensures that an {@link Exception} occurring calling one of the {@link QGChangeEventListener} doesn't
36 * prevent from calling the others.
38 public class QGChangeEventListenersImpl implements QGChangeEventListeners {
39 private static final Logger LOG = Loggers.get(QGChangeEventListenersImpl.class);
41 private final QGChangeEventListener[] listeners;
44 * Used by Pico when there is no QGChangeEventListener instance in container.
46 public QGChangeEventListenersImpl() {
47 this.listeners = new QGChangeEventListener[0];
50 public QGChangeEventListenersImpl(QGChangeEventListener[] listeners) {
51 this.listeners = listeners;
55 public void broadcastOnIssueChange(QGChangeEventFactory.IssueChangeData issueChangeData, Collection<QGChangeEvent> changeEvents) {
56 if (listeners.length == 0 || issueChangeData.getComponents().isEmpty() || issueChangeData.getIssues().isEmpty() || changeEvents.isEmpty()) {
61 List<QGChangeEvent> immutableChangeEvents = ImmutableList.copyOf(changeEvents);
62 Arrays.stream(listeners)
63 .forEach(listener -> broadcastTo(Trigger.ISSUE_CHANGE, immutableChangeEvents, listener));
65 LOG.warn(format("Broadcasting to listeners failed for %s events", changeEvents.size()), e);
69 private static void broadcastTo(Trigger trigger, Collection<QGChangeEvent> changeEvents, QGChangeEventListener listener) {
71 LOG.trace("calling onChange() on listener {} for events {}...", listener.getClass().getName(), changeEvents);
72 listener.onChanges(trigger, changeEvents);
73 } catch (Exception e) {
74 LOG.warn(format("onChange() call failed on listener %s for events %s", listener.getClass().getName(), changeEvents), e);