Fix RosterEntry.setNames(String)

also mark the method synchornized, to prevent race conditions with the
name.

Fixes SMACK-662.
This commit is contained in:
Florian Schmaus 2015-04-30 11:43:54 +02:00
parent 4eb322608e
commit 65dd6c9777
1 changed files with 10 additions and 3 deletions

View File

@ -94,7 +94,7 @@ public final class RosterEntry extends Manager {
* @throws XMPPErrorException
* @throws NoResponseException
*/
public void setName(String name) throws NotConnectedException, NoResponseException, XMPPErrorException {
public synchronized void setName(String name) throws NotConnectedException, NoResponseException, XMPPErrorException {
// Do nothing if the name hasn't changed.
if (name != null && name.equals(this.name)) {
return;
@ -102,7 +102,10 @@ public final class RosterEntry extends Manager {
RosterPacket packet = new RosterPacket();
packet.setType(IQ.Type.set);
packet.addRosterItem(toRosterItem(this));
// Create a new roster item with the current RosterEntry and the *new* name. Note that we can't set the name of
// RosterEntry right away, as otherwise the updated event wont get fired, because equalsDeep would return true.
packet.addRosterItem(toRosterItem(this, name));
connection().createPacketCollectorAndSend(packet).nextResultOrThrow();
// We have received a result response to the IQ set, the name was successfully changed
@ -246,7 +249,11 @@ public final class RosterEntry extends Manager {
}
static RosterPacket.Item toRosterItem(RosterEntry entry) {
RosterPacket.Item item = new RosterPacket.Item(entry.getUser(), entry.getName());
return toRosterItem(entry, entry.getName());
}
private static RosterPacket.Item toRosterItem(RosterEntry entry, String name) {
RosterPacket.Item item = new RosterPacket.Item(entry.getUser(), name);
item.setItemType(entry.getType());
item.setItemStatus(entry.getStatus());
// Set the correct group names for the item.