KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > MessageEventManager


1 /**
2  * $RCSfile$
3  * $Revision: 2407 $
4  * $Date: 2004-11-02 20:37:00 -0300 (Tue, 02 Nov 2004) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smackx;
22
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.*;
26
27 import org.jivesoftware.smack.*;
28 import org.jivesoftware.smack.filter.*;
29 import org.jivesoftware.smack.packet.*;
30 import org.jivesoftware.smackx.packet.*;
31
32 /**
33  * Manages message events requests and notifications. A MessageEventManager provides a high
34  * level access to request for notifications and send event notifications. It also provides
35  * an easy way to hook up custom logic when requests or notifications are received.
36  *
37  * @author Gaston Dombiak
38  */

39 public class MessageEventManager {
40
41     private List messageEventNotificationListeners = new ArrayList();
42     private List messageEventRequestListeners = new ArrayList();
43
44     private XMPPConnection con;
45
46     private PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:event");
47     private PacketListener packetListener;
48
49     /**
50      * Creates a new message event manager.
51      *
52      * @param con an XMPPConnection.
53      */

54     public MessageEventManager(XMPPConnection con) {
55         this.con = con;
56         init();
57     }
58
59     /**
60      * Adds event notification requests to a message. For each event type that
61      * the user wishes event notifications from the message recepient for, <tt>true</tt>
62      * should be passed in to this method.
63      *
64      * @param message the message to add the requested notifications.
65      * @param offline specifies if the offline event is requested.
66      * @param delivered specifies if the delivered event is requested.
67      * @param displayed specifies if the displayed event is requested.
68      * @param composing specifies if the composing event is requested.
69      */

70     public static void addNotificationsRequests(Message message, boolean offline,
71             boolean delivered, boolean displayed, boolean composing)
72     {
73         // Create a MessageEvent Package and add it to the message
74
MessageEvent messageEvent = new MessageEvent();
75         messageEvent.setOffline(offline);
76         messageEvent.setDelivered(delivered);
77         messageEvent.setDisplayed(displayed);
78         messageEvent.setComposing(composing);
79         message.addExtension(messageEvent);
80     }
81
82     /**
83      * Adds a message event request listener. The listener will be fired anytime a request for
84      * event notification is received.
85      *
86      * @param messageEventRequestListener a message event request listener.
87      */

88     public void addMessageEventRequestListener(MessageEventRequestListener messageEventRequestListener) {
89         synchronized (messageEventRequestListeners) {
90             if (!messageEventRequestListeners.contains(messageEventRequestListener)) {
91                 messageEventRequestListeners.add(messageEventRequestListener);
92             }
93         }
94     }
95
96     /**
97      * Removes a message event request listener. The listener will be fired anytime a request for
98      * event notification is received.
99      *
100      * @param messageEventRequestListener a message event request listener.
101      */

102     public void removeMessageEventRequestListener(MessageEventRequestListener messageEventRequestListener) {
103         synchronized (messageEventRequestListeners) {
104             messageEventRequestListeners.remove(messageEventRequestListener);
105         }
106     }
107
108     /**
109      * Adds a message event notification listener. The listener will be fired anytime a notification
110      * event is received.
111      *
112      * @param messageEventNotificationListener a message event notification listener.
113      */

114     public void addMessageEventNotificationListener(MessageEventNotificationListener messageEventNotificationListener) {
115         synchronized (messageEventNotificationListeners) {
116             if (!messageEventNotificationListeners.contains(messageEventNotificationListener)) {
117                 messageEventNotificationListeners.add(messageEventNotificationListener);
118             }
119         }
120     }
121
122     /**
123      * Removes a message event notification listener. The listener will be fired anytime a notification
124      * event is received.
125      *
126      * @param messageEventNotificationListener a message event notification listener.
127      */

128     public void removeMessageEventNotificationListener(MessageEventNotificationListener messageEventNotificationListener) {
129         synchronized (messageEventNotificationListeners) {
130             messageEventNotificationListeners.remove(messageEventNotificationListener);
131         }
132     }
133
134     /**
135      * Fires message event request listeners.
136      */

137     private void fireMessageEventRequestListeners(
138         String JavaDoc from,
139         String JavaDoc packetID,
140         String JavaDoc methodName) {
141         MessageEventRequestListener[] listeners = null;
142         Method JavaDoc method;
143         synchronized (messageEventRequestListeners) {
144             listeners = new MessageEventRequestListener[messageEventRequestListeners.size()];
145             messageEventRequestListeners.toArray(listeners);
146         }
147         try {
148             method =
149                 MessageEventRequestListener.class.getDeclaredMethod(
150                     methodName,
151                     new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class, MessageEventManager.class });
152             for (int i = 0; i < listeners.length; i++) {
153                 method.invoke(listeners[i], new Object JavaDoc[] { from, packetID, this });
154             }
155         } catch (NoSuchMethodException JavaDoc e) {
156             e.printStackTrace();
157         } catch (InvocationTargetException JavaDoc e) {
158             e.printStackTrace();
159         } catch (IllegalAccessException JavaDoc e) {
160             e.printStackTrace();
161         }
162     }
163
164     /**
165      * Fires message event notification listeners.
166      */

167     private void fireMessageEventNotificationListeners(
168         String JavaDoc from,
169         String JavaDoc packetID,
170         String JavaDoc methodName) {
171         MessageEventNotificationListener[] listeners = null;
172         Method JavaDoc method;
173         synchronized (messageEventNotificationListeners) {
174             listeners =
175                 new MessageEventNotificationListener[messageEventNotificationListeners.size()];
176             messageEventNotificationListeners.toArray(listeners);
177         }
178         try {
179             method =
180                 MessageEventNotificationListener.class.getDeclaredMethod(
181                     methodName,
182                     new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class });
183             for (int i = 0; i < listeners.length; i++) {
184                 method.invoke(listeners[i], new Object JavaDoc[] { from, packetID });
185             }
186         } catch (NoSuchMethodException JavaDoc e) {
187             e.printStackTrace();
188         } catch (InvocationTargetException JavaDoc e) {
189             e.printStackTrace();
190         } catch (IllegalAccessException JavaDoc e) {
191             e.printStackTrace();
192         }
193     }
194
195     private void init() {
196         // Listens for all message event packets and fire the proper message event listeners.
197
packetListener = new PacketListener() {
198             public void processPacket(Packet packet) {
199                 Message message = (Message) packet;
200                 MessageEvent messageEvent =
201                     (MessageEvent) message.getExtension("x", "jabber:x:event");
202                 if (messageEvent.isMessageEventRequest()) {
203                     // Fire event for requests of message events
204
for (Iterator it = messageEvent.getEventTypes(); it.hasNext();)
205                         fireMessageEventRequestListeners(
206                             message.getFrom(),
207                             message.getPacketID(),
208                             ((String JavaDoc) it.next()).concat("NotificationRequested"));
209                 } else
210                     // Fire event for notifications of message events
211
for (Iterator it = messageEvent.getEventTypes(); it.hasNext();)
212                         fireMessageEventNotificationListeners(
213                             message.getFrom(),
214                             messageEvent.getPacketID(),
215                             ((String JavaDoc) it.next()).concat("Notification"));
216
217             };
218
219         };
220         con.addPacketListener(packetListener, packetFilter);
221     }
222
223     /**
224      * Sends the notification that the message was delivered to the sender of the original message
225      *
226      * @param to the recipient of the notification.
227      * @param packetID the id of the message to send.
228      */

229     public void sendDeliveredNotification(String JavaDoc to, String JavaDoc packetID) {
230         // Create the message to send
231
Message msg = new Message(to);
232         // Create a MessageEvent Package and add it to the message
233
MessageEvent messageEvent = new MessageEvent();
234         messageEvent.setDelivered(true);
235         messageEvent.setPacketID(packetID);
236         msg.addExtension(messageEvent);
237         // Send the packet
238
con.sendPacket(msg);
239     }
240
241     /**
242      * Sends the notification that the message was displayed to the sender of the original message
243      *
244      * @param to the recipient of the notification.
245      * @param packetID the id of the message to send.
246      */

247     public void sendDisplayedNotification(String JavaDoc to, String JavaDoc packetID) {
248         // Create the message to send
249
Message msg = new Message(to);
250         // Create a MessageEvent Package and add it to the message
251
MessageEvent messageEvent = new MessageEvent();
252         messageEvent.setDisplayed(true);
253         messageEvent.setPacketID(packetID);
254         msg.addExtension(messageEvent);
255         // Send the packet
256
con.sendPacket(msg);
257     }
258
259     /**
260      * Sends the notification that the receiver of the message is composing a reply
261      *
262      * @param to the recipient of the notification.
263      * @param packetID the id of the message to send.
264      */

265     public void sendComposingNotification(String JavaDoc to, String JavaDoc packetID) {
266         // Create the message to send
267
Message msg = new Message(to);
268         // Create a MessageEvent Package and add it to the message
269
MessageEvent messageEvent = new MessageEvent();
270         messageEvent.setComposing(true);
271         messageEvent.setPacketID(packetID);
272         msg.addExtension(messageEvent);
273         // Send the packet
274
con.sendPacket(msg);
275     }
276
277     /**
278      * Sends the notification that the receiver of the message has cancelled composing a reply.
279      *
280      * @param to the recipient of the notification.
281      * @param packetID the id of the message to send.
282      */

283     public void sendCancelledNotification(String JavaDoc to, String JavaDoc packetID) {
284         // Create the message to send
285
Message msg = new Message(to);
286         // Create a MessageEvent Package and add it to the message
287
MessageEvent messageEvent = new MessageEvent();
288         messageEvent.setCancelled(true);
289         messageEvent.setPacketID(packetID);
290         msg.addExtension(messageEvent);
291         // Send the packet
292
con.sendPacket(msg);
293     }
294
295     public void destroy() {
296         if (con != null) {
297             con.removePacketListener(packetListener);
298         }
299     }
300
301     public void finalize() {
302         destroy();
303     }
304 }
Popular Tags