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.project;
22 import java.util.Arrays;
24 import java.util.function.Consumer;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
29 import static com.google.common.base.Preconditions.checkNotNull;
31 public class ProjectLifeCycleListenersImpl implements ProjectLifeCycleListeners {
32 private static final Logger LOG = LoggerFactory.getLogger(ProjectLifeCycleListenersImpl.class);
34 private final ProjectLifeCycleListener[] listeners;
37 * Used by the container when there is no ProjectLifeCycleListener implementation in container.
39 @Autowired(required = false)
40 public ProjectLifeCycleListenersImpl() {
41 this.listeners = new ProjectLifeCycleListener[0];
45 * Used by the container when there is at least one ProjectLifeCycleListener implementation in container.
47 @Autowired(required = false)
48 public ProjectLifeCycleListenersImpl(ProjectLifeCycleListener[] listeners) {
49 this.listeners = listeners;
53 public void onProjectsDeleted(Set<DeletedProject> projects) {
54 checkNotNull(projects, "projects can't be null");
55 if (projects.isEmpty()) {
59 Arrays.stream(listeners)
60 .forEach(safelyCallListener(listener -> listener.onProjectsDeleted(projects)));
64 public void onProjectBranchesDeleted(Set<Project> projects) {
65 checkNotNull(projects, "projects can't be null");
66 if (projects.isEmpty()) {
70 Arrays.stream(listeners)
71 .forEach(safelyCallListener(listener -> listener.onProjectBranchesChanged(projects)));
75 public void onProjectsRekeyed(Set<RekeyedProject> rekeyedProjects) {
76 checkNotNull(rekeyedProjects, "rekeyedProjects can't be null");
77 if (rekeyedProjects.isEmpty()) {
81 Arrays.stream(listeners)
82 .forEach(safelyCallListener(listener -> listener.onProjectsRekeyed(rekeyedProjects)));
85 private static Consumer<ProjectLifeCycleListener> safelyCallListener(Consumer<ProjectLifeCycleListener> task) {
88 task.accept(listener);
89 } catch (Error | Exception e) {
90 LOG.error("Call on ProjectLifeCycleListener \"{}\" failed", listener.getClass(), e);