001: /**
002: * $RCSfile$
003: * $Revision: 1705 $
004: * $Date: 2005-07-26 14:10:33 -0300 (Tue, 26 Jul 2005) $
005: *
006: * Copyright (C) 2004-2005 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.openfire.event;
011:
012: import org.jivesoftware.util.Log;
013: import org.jivesoftware.openfire.group.Group;
014:
015: import java.util.List;
016: import java.util.Map;
017: import java.util.concurrent.CopyOnWriteArrayList;
018:
019: /**
020: * Dispatches group events. Each event has a {@link EventType type}
021: * and optional parameters, as follows:<p>
022: *
023: * <table border="1">
024: * <tr><th>Event Type</th><th>Extra Params</th></tr>
025: * <tr><td>{@link EventType#group_created group_created}</td><td><i>None</i></td></tr>
026: * <tr><td>{@link EventType#group_deleting group_deleting}</td><td><i>None</i></td></tr>
027: * <tr><td>{@link EventType#member_added member_added}</td><td>A param named <tt>member</tt> with a String username as a
028: * payload</td></tr>
029: * <tr><td>{@link EventType#member_removed member_removed}</td><td>A param named <tt>member</tt> with a String username as a
030: * payload</td></tr>
031: * <tr><td>{@link EventType#admin_added admin_added}</td><td>A param named <tt>admin</tt> with a String username
032: * as a payload</td></tr>
033: * <tr><td>{@link EventType#admin_removed admin_removed}</td><td>A param named <tt>admin</tt> with a String username
034: * as a payload</td></tr>
035: * <tr valign="top"><td>{@link EventType#group_modified group_modified}</td><td>
036: * <table><tr><td><b>Reason</b></td><td><b>Key</b></td><td><b>Value</b></td></tr>
037: * <tr><td colspan="3">Name modified</td></tr>
038: * <tr><td> </td><td>type</td><td>nameModified</td></tr>
039: * <tr><td> </td><td>originalValue</td><td><i>(Name before it was modified)</i>
040: * </td></tr>
041: * <tr><td colspan="3"><hr></td></tr>
042: * <tr><td colspan="3">Description modified</td></tr>
043: * <tr><td> </td><td>type</td><td>descriptionModified</td></tr>
044: * <tr><td> </td><td>originalValue</td><td><i>(Description before it was
045: * modified)</i></td></tr>
046: * <tr><td colspan="3"><hr></td></tr>
047: * <tr><td colspan="3">Property modified</td></tr>
048: * <tr><td> </td><td>type</td><td>propertyModified</td></tr>
049: * <tr><td> </td><td>propertyKey</td><td><i>(Name of the property)</i></td>
050: * </tr>
051: * <tr><td> </td><td>originalValue</td><td><i>(Property value before it was
052: * modified)</i></td></tr>
053: * <tr><td colspan="3"><hr></td></tr>
054: * <tr><td colspan="3">Property added</td></tr>
055: * <tr><td> </td><td>type</td><td>propertyAdded</td></tr>
056: * <tr><td> </td><td>propertyKey</td><td><i>(Name of the new property)</i></td>
057: * </tr>
058: * <tr><td colspan="3"><hr></td></tr>
059: * <tr><td colspan="3">Property deleted</td></tr>
060: * <tr><td> </td><td>type</td><td>propertyDeleted</td></tr>
061: * <tr><td> </td><td>propertyKey</td><td><i>(Name of the property deleted)</i></td></tr></table></td></tr>
062: * </table>
063: *
064: * @author Matt Tucker
065: */
066: public class GroupEventDispatcher {
067:
068: private static List<GroupEventListener> listeners = new CopyOnWriteArrayList<GroupEventListener>();
069:
070: private GroupEventDispatcher() {
071: // Not instantiable.
072: }
073:
074: /**
075: * Registers a listener to receive events.
076: *
077: * @param listener the listener.
078: */
079: public static void addListener(GroupEventListener listener) {
080: if (listener == null) {
081: throw new NullPointerException();
082: }
083: listeners.add(listener);
084: }
085:
086: /**
087: * Unregisters a listener to receive events.
088: *
089: * @param listener the listener.
090: */
091: public static void removeListener(GroupEventListener listener) {
092: listeners.remove(listener);
093: }
094:
095: /**
096: * Dispatches an event to all listeners.
097: *
098: * @param group the group.
099: * @param eventType the event type.
100: * @param params event parameters.
101: */
102: public static void dispatchEvent(Group group, EventType eventType,
103: Map params) {
104: for (GroupEventListener listener : listeners) {
105: try {
106: switch (eventType) {
107: case group_created: {
108: listener.groupCreated(group, params);
109: break;
110: }
111: case group_deleting: {
112: listener.groupDeleting(group, params);
113: break;
114: }
115: case member_added: {
116: listener.memberAdded(group, params);
117: break;
118: }
119: case member_removed: {
120: listener.memberRemoved(group, params);
121: break;
122: }
123: case admin_added: {
124: listener.adminAdded(group, params);
125: break;
126: }
127: case admin_removed: {
128: listener.adminRemoved(group, params);
129: break;
130: }
131: case group_modified: {
132: listener.groupModified(group, params);
133: break;
134: }
135: default:
136: break;
137: }
138: } catch (Exception e) {
139: Log.error(e);
140: }
141: }
142: }
143:
144: /**
145: * Represents valid event types.
146: */
147: public enum EventType {
148:
149: /**
150: * A new group was created.
151: */
152: group_created,
153:
154: /**
155: * A group is about to be deleted. Note that this event is fired before
156: * a group is actually deleted. This allows for referential cleanup
157: * before the group is gone.
158: */
159: group_deleting,
160:
161: /**
162: * The name, description, or extended property of a group was changed.
163: */
164: group_modified,
165:
166: /**
167: * A member was added to a group.
168: */
169: member_added,
170:
171: /**
172: * A member was removed from a group.
173: */
174: member_removed,
175:
176: /**
177: * An administrator was added to a group.
178: */
179: admin_added,
180:
181: /**
182: * An administrator was removed from a group.
183: */
184: admin_removed;
185: }
186: }
|