From fd7b7479873ea664c0def362d091fc599a1234aa Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 12 Oct 2014 18:53:13 +0200 Subject: [PATCH] Remove init() in MultiUserChat and mark more members 'final' Also use CopyOnWriteArraySet for presenceInterceptors. --- .../smackx/muc/MultiUserChat.java | 219 +++++++++--------- 1 file changed, 106 insertions(+), 113 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java index e43c377d1..70192816f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java @@ -94,12 +94,9 @@ public class MultiUserChat { private static Map> joinedRooms = new WeakHashMap>(); - private XMPPConnection connection; - private String room; - private String subject; - private String nickname = null; - private boolean joined = false; - private Map occupantsMap = new ConcurrentHashMap(); + private final XMPPConnection connection; + private final String room; + private final Map occupantsMap = new ConcurrentHashMap(); private final List invitationRejectionListeners = new ArrayList(); @@ -111,14 +108,17 @@ public class MultiUserChat { new ArrayList(); private final Set messageListeners = new CopyOnWriteArraySet(); private final Set presenceListeners = new CopyOnWriteArraySet(); + private final Set presenceInterceptors = new CopyOnWriteArraySet(); + private final ConnectionDetachedPacketCollector messageCollector = new ConnectionDetachedPacketCollector(); + private final PacketFilter fromRoomFilter; private final PacketListener messageListener; private final PacketListener presenceListener; + private final RoomListenerMultiplexor roomListenerMultiplexor; - private List presenceInterceptors = new ArrayList(); - private RoomListenerMultiplexor roomListenerMultiplexor; - private ConnectionDetachedPacketCollector messageCollector; - + private String subject; + private String nickname = null; + private boolean joined = false; static { XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { @@ -200,7 +200,102 @@ public class MultiUserChat { }; fromRoomFilter = FromMatchesFilter.create(room); - init(); + // Create a listener for subject updates. + PacketListener subjectListener = new PacketListener() { + public void processPacket(Packet packet) { + Message msg = (Message) packet; + // Update the room subject + subject = msg.getSubject(); + // Fire event for subject updated listeners + fireSubjectUpdatedListeners( + msg.getSubject(), + msg.getFrom()); + + } + }; + + // Create a listener for all presence updates. + PacketListener presenceListener = new PacketListener() { + public void processPacket(Packet packet) { + Presence presence = (Presence) packet; + String from = presence.getFrom(); + String myRoomJID = MultiUserChat.this.room + "/" + nickname; + boolean isUserStatusModification = presence.getFrom().equals(myRoomJID); + if (presence.getType() == Presence.Type.available) { + Presence oldPresence = occupantsMap.put(from, presence); + if (oldPresence != null) { + // Get the previous occupant's affiliation & role + MUCUser mucExtension = MUCUser.from(packet); + MUCAffiliation oldAffiliation = mucExtension.getItem().getAffiliation(); + MUCRole oldRole = mucExtension.getItem().getRole(); + // Get the new occupant's affiliation & role + mucExtension = MUCUser.from(packet); + MUCAffiliation newAffiliation = mucExtension.getItem().getAffiliation(); + MUCRole newRole = mucExtension.getItem().getRole(); + // Fire role modification events + checkRoleModifications(oldRole, newRole, isUserStatusModification, from); + // Fire affiliation modification events + checkAffiliationModifications( + oldAffiliation, + newAffiliation, + isUserStatusModification, + from); + } + else { + // A new occupant has joined the room + if (!isUserStatusModification) { + List params = new ArrayList(); + params.add(from); + fireParticipantStatusListeners("joined", params); + } + } + } + else if (presence.getType() == Presence.Type.unavailable) { + occupantsMap.remove(from); + MUCUser mucUser = MUCUser.from(packet); + if (mucUser != null && mucUser.getStatus() != null) { + // Fire events according to the received presence code + checkPresenceCode( + mucUser.getStatus(), + presence.getFrom().equals(myRoomJID), + mucUser, + from); + } else { + // An occupant has left the room + if (!isUserStatusModification) { + List params = new ArrayList(); + params.add(from); + fireParticipantStatusListeners("left", params); + } + } + } + } + }; + + // Listens for all messages that include a MUCUser extension and fire the invitation + // rejection listeners if the message includes an invitation rejection. + PacketListener declinesListener = new PacketListener() { + public void processPacket(Packet packet) { + // Get the MUC User extension + MUCUser mucUser = MUCUser.from(packet); + // Check if the MUCUser informs that the invitee has declined the invitation + if (mucUser.getDecline() != null && + ((Message) packet).getType() != Message.Type.error) { + // Fire event for invitation rejection listeners + fireInvitationRejectionListeners( + mucUser.getDecline().getFrom(), + mucUser.getDecline().getReason()); + } + } + }; + + PacketMultiplexListener packetMultiplexor = new PacketMultiplexListener( + messageCollector, presenceListener, subjectListener, + declinesListener); + + roomListenerMultiplexor = RoomListenerMultiplexor.getRoomMultiplexor(connection); + + roomListenerMultiplexor.addRoom(room, packetMultiplexor); } /** @@ -1938,108 +2033,6 @@ public class MultiUserChat { } } - private void init() { - // Create a collector for incoming messages. - messageCollector = new ConnectionDetachedPacketCollector(); - - // Create a listener for subject updates. - PacketListener subjectListener = new PacketListener() { - public void processPacket(Packet packet) { - Message msg = (Message) packet; - // Update the room subject - subject = msg.getSubject(); - // Fire event for subject updated listeners - fireSubjectUpdatedListeners( - msg.getSubject(), - msg.getFrom()); - - } - }; - - // Create a listener for all presence updates. - PacketListener presenceListener = new PacketListener() { - public void processPacket(Packet packet) { - Presence presence = (Presence) packet; - String from = presence.getFrom(); - String myRoomJID = room + "/" + nickname; - boolean isUserStatusModification = presence.getFrom().equals(myRoomJID); - if (presence.getType() == Presence.Type.available) { - Presence oldPresence = occupantsMap.put(from, presence); - if (oldPresence != null) { - // Get the previous occupant's affiliation & role - MUCUser mucExtension = MUCUser.from(packet); - MUCAffiliation oldAffiliation = mucExtension.getItem().getAffiliation(); - MUCRole oldRole = mucExtension.getItem().getRole(); - // Get the new occupant's affiliation & role - mucExtension = MUCUser.from(packet); - MUCAffiliation newAffiliation = mucExtension.getItem().getAffiliation(); - MUCRole newRole = mucExtension.getItem().getRole(); - // Fire role modification events - checkRoleModifications(oldRole, newRole, isUserStatusModification, from); - // Fire affiliation modification events - checkAffiliationModifications( - oldAffiliation, - newAffiliation, - isUserStatusModification, - from); - } - else { - // A new occupant has joined the room - if (!isUserStatusModification) { - List params = new ArrayList(); - params.add(from); - fireParticipantStatusListeners("joined", params); - } - } - } - else if (presence.getType() == Presence.Type.unavailable) { - occupantsMap.remove(from); - MUCUser mucUser = MUCUser.from(packet); - if (mucUser != null && mucUser.getStatus() != null) { - // Fire events according to the received presence code - checkPresenceCode( - mucUser.getStatus(), - presence.getFrom().equals(myRoomJID), - mucUser, - from); - } else { - // An occupant has left the room - if (!isUserStatusModification) { - List params = new ArrayList(); - params.add(from); - fireParticipantStatusListeners("left", params); - } - } - } - } - }; - - // Listens for all messages that include a MUCUser extension and fire the invitation - // rejection listeners if the message includes an invitation rejection. - PacketListener declinesListener = new PacketListener() { - public void processPacket(Packet packet) { - // Get the MUC User extension - MUCUser mucUser = MUCUser.from(packet); - // Check if the MUCUser informs that the invitee has declined the invitation - if (mucUser.getDecline() != null && - ((Message) packet).getType() != Message.Type.error) { - // Fire event for invitation rejection listeners - fireInvitationRejectionListeners( - mucUser.getDecline().getFrom(), - mucUser.getDecline().getReason()); - } - } - }; - - PacketMultiplexListener packetMultiplexor = new PacketMultiplexListener( - messageCollector, presenceListener, subjectListener, - declinesListener); - - roomListenerMultiplexor = RoomListenerMultiplexor.getRoomMultiplexor(connection); - - roomListenerMultiplexor.addRoom(room, packetMultiplexor); - } - /** * Fires notification events if the role of a room occupant has changed. If the occupant that * changed his role is your occupant then the UserStatusListeners added to this