KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
24
25 import org.jivesoftware.smack.*;
26 import org.jivesoftware.smack.filter.*;
27 import org.jivesoftware.smack.packet.*;
28 import org.jivesoftware.smackx.packet.RosterExchange;
29
30 /**
31  *
32  * Manages Roster exchanges. A RosterExchangeManager provides a high level access to send
33  * rosters, roster groups and roster entries to XMPP clients. It also provides an easy way
34  * to hook up custom logic when entries are received from another XMPP client through
35  * RosterExchangeListeners.
36  *
37  * @author Gaston Dombiak
38  */

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

53     public RosterExchangeManager(XMPPConnection con) {
54         this.con = con;
55         init();
56     }
57
58     /**
59      * Adds a listener to roster exchanges. The listener will be fired anytime roster entries
60      * are received from remote XMPP clients.
61      *
62      * @param rosterExchangeListener a roster exchange listener.
63      */

64     public void addRosterListener(RosterExchangeListener rosterExchangeListener) {
65         synchronized (rosterExchangeListeners) {
66             if (!rosterExchangeListeners.contains(rosterExchangeListener)) {
67                 rosterExchangeListeners.add(rosterExchangeListener);
68             }
69         }
70     }
71
72     /**
73      * Removes a listener from roster exchanges. The listener will be fired anytime roster
74      * entries are received from remote XMPP clients.
75      *
76      * @param rosterExchangeListener a roster exchange listener..
77      */

78     public void removeRosterListener(RosterExchangeListener rosterExchangeListener) {
79         synchronized (rosterExchangeListeners) {
80             rosterExchangeListeners.remove(rosterExchangeListener);
81         }
82     }
83
84     /**
85      * Sends a roster to userID. All the entries of the roster will be sent to the
86      * target user.
87      *
88      * @param roster the roster to send
89      * @param targetUserID the user that will receive the roster entries
90      */

91     public void send(Roster roster, String JavaDoc targetUserID) {
92         // Create a new message to send the roster
93
Message msg = new Message(targetUserID);
94         // Create a RosterExchange Package and add it to the message
95
RosterExchange rosterExchange = new RosterExchange(roster);
96         msg.addExtension(rosterExchange);
97
98         // Send the message that contains the roster
99
con.sendPacket(msg);
100     }
101
102     /**
103      * Sends a roster entry to userID.
104      *
105      * @param rosterEntry the roster entry to send
106      * @param targetUserID the user that will receive the roster entries
107      */

108     public void send(RosterEntry rosterEntry, String JavaDoc targetUserID) {
109         // Create a new message to send the roster
110
Message msg = new Message(targetUserID);
111         // Create a RosterExchange Package and add it to the message
112
RosterExchange rosterExchange = new RosterExchange();
113         rosterExchange.addRosterEntry(rosterEntry);
114         msg.addExtension(rosterExchange);
115
116         // Send the message that contains the roster
117
con.sendPacket(msg);
118     }
119
120     /**
121      * Sends a roster group to userID. All the entries of the group will be sent to the
122      * target user.
123      *
124      * @param rosterGroup the roster group to send
125      * @param targetUserID the user that will receive the roster entries
126      */

127     public void send(RosterGroup rosterGroup, String JavaDoc targetUserID) {
128         // Create a new message to send the roster
129
Message msg = new Message(targetUserID);
130         // Create a RosterExchange Package and add it to the message
131
RosterExchange rosterExchange = new RosterExchange();
132         for (Iterator it = rosterGroup.getEntries(); it.hasNext();)
133             rosterExchange.addRosterEntry((RosterEntry) it.next());
134         msg.addExtension(rosterExchange);
135
136         // Send the message that contains the roster
137
con.sendPacket(msg);
138     }
139
140     /**
141      * Fires roster exchange listeners.
142      */

143     private void fireRosterExchangeListeners(String JavaDoc from, Iterator remoteRosterEntries) {
144         RosterExchangeListener[] listeners = null;
145         synchronized (rosterExchangeListeners) {
146             listeners = new RosterExchangeListener[rosterExchangeListeners.size()];
147             rosterExchangeListeners.toArray(listeners);
148         }
149         for (int i = 0; i < listeners.length; i++) {
150             listeners[i].entriesReceived(from, remoteRosterEntries);
151         }
152     }
153
154     private void init() {
155         // Listens for all roster exchange packets and fire the roster exchange listeners.
156
packetListener = new PacketListener() {
157             public void processPacket(Packet packet) {
158                 Message message = (Message) packet;
159                 RosterExchange rosterExchange =
160                     (RosterExchange) message.getExtension("x", "jabber:x:roster");
161                 // Fire event for roster exchange listeners
162
fireRosterExchangeListeners(message.getFrom(), rosterExchange.getRosterEntries());
163             };
164
165         };
166         con.addPacketListener(packetListener, packetFilter);
167     }
168
169     public void destroy() {
170         if (con != null)
171             con.removePacketListener(packetListener);
172
173     }
174     public void finalize() {
175         destroy();
176     }
177 }
Popular Tags