1 /* 2 * Copyright (C) 2005-2008 Jive Software. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.jivesoftware.openfire.roster; 18 19 import java.util.List; 20 import java.util.concurrent.CopyOnWriteArrayList; 21 22 import org.slf4j.Logger; 23 import org.slf4j.LoggerFactory; 24 25 /** 26 * Dispatches roster events. The following events are supported: 27 * <ul> 28 * <li><b>rosterLoaded</b> --> A roster has just been loaded.</li> 29 * <li><b>contactAdded</b> --> A contact has been added to a roster.</li> 30 * <li><b>contactUpdated</b> --> A contact has been updated of a roster.</li> 31 * <li><b>contactDeleted</b> --> A contact has been deleted from a roster.</li> 32 * </ul> 33 * Use {@link #addListener(RosterEventListener)} and {@link #removeListener(RosterEventListener)} 34 * to add or remove {@link RosterEventListener}. 35 * 36 * @author Gaston Dombiak 37 */ 38 public class RosterEventDispatcher { 39 private static final Logger Log = LoggerFactory.getLogger(RosterEventDispatcher.class); 40 41 private static List<RosterEventListener> listeners = 42 new CopyOnWriteArrayList<>(); 43 44 /** 45 * Registers a listener to receive events. 46 * 47 * @param listener the listener. 48 */ addListener(RosterEventListener listener)49 public static void addListener(RosterEventListener listener) { 50 if (listener == null) { 51 throw new NullPointerException(); 52 } 53 listeners.add(listener); 54 } 55 56 /** 57 * Unregisters a listener to receive events. 58 * 59 * @param listener the listener. 60 */ removeListener(RosterEventListener listener)61 public static void removeListener(RosterEventListener listener) { 62 listeners.remove(listener); 63 } 64 65 /** 66 * Notifies the listeners that a roster has just been loaded. 67 * 68 * @param roster the loaded roster. 69 */ rosterLoaded(Roster roster)70 public static void rosterLoaded(Roster roster) { 71 if (!listeners.isEmpty()) { 72 for (RosterEventListener listener : listeners) { 73 try { 74 listener.rosterLoaded(roster); 75 } catch (Exception e) { 76 Log.warn("An exception occurred while dispatching a 'rosterLoaded' event!", e); 77 } 78 } 79 } 80 } 81 82 /** 83 * Notifies listeners that a contact is about to be added to a roster. New contacts 84 * may be persisted to the database or not. Listeners may indicate that contact about 85 * to be persisted should not be persisted. Only one listener is needed to return 86 * {@code false} so that the contact is not persisted. 87 * 88 * @param roster the roster that was updated. 89 * @param item the new roster item. 90 * @param persistent true if the new contact is going to be saved to the database. 91 * @return false if the contact should not be persisted to the database. 92 */ addingContact(Roster roster, RosterItem item, boolean persistent)93 public static boolean addingContact(Roster roster, RosterItem item, boolean persistent) { 94 boolean answer = persistent; 95 if (!listeners.isEmpty()) { 96 for (RosterEventListener listener : listeners) { 97 try { 98 if (!listener.addingContact(roster, item, persistent)) { 99 answer = false; 100 } 101 } catch (Exception e) { 102 Log.warn("An exception occurred while dispatching a 'addingContact' event!", e); 103 } 104 } 105 } 106 return answer; 107 } 108 109 /** 110 * Notifies the listeners that a contact has been added to a roster. 111 * 112 * @param roster the roster that was updated. 113 * @param item the new roster item. 114 */ contactAdded(Roster roster, RosterItem item)115 public static void contactAdded(Roster roster, RosterItem item) { 116 if (!listeners.isEmpty()) { 117 for (RosterEventListener listener : listeners) { 118 try { 119 listener.contactAdded(roster, item); 120 } catch (Exception e) { 121 Log.warn("An exception occurred while dispatching a 'contactAdded' event!", e); 122 } 123 } 124 } 125 } 126 127 /** 128 * Notifies the listeners that a contact has been updated. 129 * 130 * @param roster the roster that was updated. 131 * @param item the updated roster item. 132 */ contactUpdated(Roster roster, RosterItem item)133 public static void contactUpdated(Roster roster, RosterItem item) { 134 if (!listeners.isEmpty()) { 135 for (RosterEventListener listener : listeners) { 136 try { 137 listener.contactUpdated(roster, item); 138 } catch (Exception e) { 139 Log.warn("An exception occurred while dispatching a 'contactUpdated' event!", e); 140 } 141 } 142 } 143 } 144 145 /** 146 * Notifies the listeners that a contact has been deleted from a roster. 147 * 148 * @param roster the roster that was updated. 149 * @param item the roster item that was deleted. 150 */ contactDeleted(Roster roster, RosterItem item)151 public static void contactDeleted(Roster roster, RosterItem item) { 152 if (!listeners.isEmpty()) { 153 for (RosterEventListener listener : listeners) { 154 try { 155 listener.contactDeleted(roster, item); 156 } catch (Exception e) { 157 Log.warn("An exception occurred while dispatching a 'contactDeleted' event!", e); 158 } 159 } 160 } 161 } 162 } 163