KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > event > UserEventDispatcher


1 /**
2  * $RCSfile: UserEventDispatcher.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2005/07/26 17:10:33 $
5  *
6  * Copyright (C) 2004-2005 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.event;
13
14 import org.jivesoftware.util.Log;
15 import org.jivesoftware.messenger.user.User;
16
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
20
21 /**
22  * Dispatches user events. Each event has a {@link EventType type}
23  * and optional parameters, as follows:<p>
24  *
25  * <table border="1">
26  * <tr><th>Event Type</th><th>Extra Params</th></tr>
27  * <tr><td>{@link EventType#user_created user_created}</td><td><i>None</i></td></tr>
28  * <tr><td>{@link EventType#user_deleting user_deleting}</td><td><i>None</i></td></tr>
29  * <tr valign="top"><td>{@link EventType#user_modified user_modified}</td><td>
30  * <table><tr><td><b>Reason</b></td><td><b>Key</b></td><td><b>Value</b></td></tr>
31  * <tr><td colspan="3">Name modified</td></tr>
32  * <tr><td>&nbsp;</td><td>type</td><td>nameModified</td></tr>
33  * <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Name before it was modified)</i></td></tr>
34  *
35  * <tr><td colspan="3"><hr></td></tr>
36  * <tr><td colspan="3">Email modified</td></tr>
37  * <tr><td>&nbsp;</td><td>type</td><td>emailModified</td></tr>
38  * <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Email before it was
39  * modified)</i></td></tr>
40  * <tr><td colspan="3"><hr></td></tr>
41  *
42  * <tr><td colspan="3">Password modified</td></tr>
43  * <tr><td>&nbsp;</td><td>type</td><td>passwordModified</td></tr>
44  * <tr><td colspan="3"><hr></td></tr>
45  *
46  * <tr><td colspan="3">Creation date modified</td></tr>
47  * <tr><td>&nbsp;</td><td>type</td><td>creationDateModified</td></tr>
48  * <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Creation date before it was
49  * modified)</i></td></tr>
50  * <tr><td colspan="3"><hr></td></tr>
51  *
52  * <tr><td colspan="3">Modification date modified</td></tr>
53  * <tr><td>&nbsp;</td><td>type</td><td>modificationDateModified</td></tr>
54  * <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Modification date before it was
55  * modified)</i></td></tr>
56  * <tr><td colspan="3"><hr></td></tr>
57  *
58  * <tr><td colspan="3">Property modified</td></tr>
59  * <tr><td>&nbsp;</td><td>type</td><td>propertyModified</td></tr>
60  * <tr><td>&nbsp;</td><td>propertyKey</td><td><i>(Name of the property)</i></td>
61  * </tr>
62  * <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Property value before it was
63  * modified)</i></td></tr>
64  * <tr><td colspan="3"><hr></td></tr>
65  * <tr><td colspan="3">Property added</td></tr>
66  * <tr><td>&nbsp;</td><td>type</td><td>propertyAdded</td></tr>
67  * <tr><td>&nbsp;</td><td>propertyKey</td><td><i>(Name of the new property)</i></td>
68  * </tr>
69  * <tr><td colspan="3"><hr></td></tr>
70  * <tr><td colspan="3">Property deleted</td></tr>
71  * <tr><td>&nbsp;</td><td>type</td><td>propertyDeleted</td></tr>
72  * <tr><td>&nbsp;</td><td>propertyKey</td><td><i>(Name of the property deleted)</i></td></tr></table></td></tr>
73  * </table>
74  *
75  * @author Matt Tucker
76  */

77 public class UserEventDispatcher {
78
79     private static List JavaDoc<UserEventListener> listeners =
80             new CopyOnWriteArrayList JavaDoc<UserEventListener>();
81
82     private UserEventDispatcher() {
83         // Not instantiable.
84
}
85
86     /**
87      * Registers a listener to receive events.
88      *
89      * @param listener the listener.
90      */

91     public static void addListener(UserEventListener listener) {
92         if (listener == null) {
93             throw new NullPointerException JavaDoc();
94         }
95         listeners.add(listener);
96     }
97
98     /**
99      * Unregisters a listener to receive events.
100      *
101      * @param listener the listener.
102      */

103     public static void removeListener(UserEventListener listener) {
104         listeners.remove(listener);
105     }
106
107     /**
108      * Dispatches an event to all listeners.
109      *
110      * @param user the user.
111      * @param eventType the event type.
112      * @param params event parameters.
113      */

114     public static void dispatchEvent(User user, EventType eventType, Map JavaDoc params) {
115         for (UserEventListener listener : listeners) {
116             try {
117                 switch (eventType) {
118                     case user_created: {
119                         listener.userCreated(user, params);
120                         break;
121                     }
122                     case user_deleting: {
123                         listener.userDeleting(user, params);
124                         break;
125                     }
126                     case user_modified: {
127                         listener.userModified(user, params);
128                         break;
129                     }
130                     default:
131                         break;
132                 }
133             }
134             catch (Exception JavaDoc e) {
135                 Log.error(e);
136             }
137         }
138     }
139
140     /**
141      * Represents valid event types.
142      */

143     public enum EventType {
144
145         /**
146          * A new user was created.
147          */

148         user_created,
149
150         /**
151          * A user is about to be deleted. Note that this event is fired before
152          * a user is actually deleted. This allows for referential cleanup
153          * before the user is gone.
154          */

155         user_deleting,
156
157         /**
158          * The name, email, password, or extended property of a user was changed.
159          */

160         user_modified,
161     }
162 }
Popular Tags