1 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 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 53 public RosterExchangeManager(XMPPConnection con) { 54 this.con = con; 55 init(); 56 } 57 58 64 public void addRosterListener(RosterExchangeListener rosterExchangeListener) { 65 synchronized (rosterExchangeListeners) { 66 if (!rosterExchangeListeners.contains(rosterExchangeListener)) { 67 rosterExchangeListeners.add(rosterExchangeListener); 68 } 69 } 70 } 71 72 78 public void removeRosterListener(RosterExchangeListener rosterExchangeListener) { 79 synchronized (rosterExchangeListeners) { 80 rosterExchangeListeners.remove(rosterExchangeListener); 81 } 82 } 83 84 91 public void send(Roster roster, String targetUserID) { 92 Message msg = new Message(targetUserID); 94 RosterExchange rosterExchange = new RosterExchange(roster); 96 msg.addExtension(rosterExchange); 97 98 con.sendPacket(msg); 100 } 101 102 108 public void send(RosterEntry rosterEntry, String targetUserID) { 109 Message msg = new Message(targetUserID); 111 RosterExchange rosterExchange = new RosterExchange(); 113 rosterExchange.addRosterEntry(rosterEntry); 114 msg.addExtension(rosterExchange); 115 116 con.sendPacket(msg); 118 } 119 120 127 public void send(RosterGroup rosterGroup, String targetUserID) { 128 Message msg = new Message(targetUserID); 130 RosterExchange rosterExchange = new RosterExchange(); 132 for (Iterator it = rosterGroup.getEntries(); it.hasNext();) 133 rosterExchange.addRosterEntry((RosterEntry) it.next()); 134 msg.addExtension(rosterExchange); 135 136 con.sendPacket(msg); 138 } 139 140 143 private void fireRosterExchangeListeners(String 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 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 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 |