From 1735aa50eb1fd78766d790ac44e65a1cb0956af9 Mon Sep 17 00:00:00 2001 From: rcollier Date: Mon, 21 Feb 2011 18:53:22 +0000 Subject: [PATCH] SMACK-269 Added storing links by base and full JID so incoming messages with no thread ID can attempt to match full JID, and then try base JID before creating a new chat. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_2_0@12028 b35dd754-fafc-0310-a699-88a17e54d16e --- .../org/jivesoftware/smack/ChatManager.java | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/source/org/jivesoftware/smack/ChatManager.java b/source/org/jivesoftware/smack/ChatManager.java index a893f4b4e..1523b4fdc 100644 --- a/source/org/jivesoftware/smack/ChatManager.java +++ b/source/org/jivesoftware/smack/ChatManager.java @@ -20,6 +20,13 @@ package org.jivesoftware.smack; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.WeakHashMap; +import java.util.concurrent.CopyOnWriteArraySet; + import org.jivesoftware.smack.filter.AndFilter; import org.jivesoftware.smack.filter.FromContainsFilter; import org.jivesoftware.smack.filter.PacketFilter; @@ -29,9 +36,6 @@ import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.collections.ReferenceMap; -import java.util.*; -import java.util.concurrent.CopyOnWriteArraySet; - /** * The chat manager keeps track of references to all current chats. It will not hold any references * in memory on its own so it is neccesary to keep a reference to the chat object itself. To be @@ -65,14 +69,20 @@ public class ChatManager { /** * Maps thread ID to chat. */ - private Map threadChats = new ReferenceMap(ReferenceMap.HARD, - ReferenceMap.WEAK); + private Map threadChats = Collections.synchronizedMap(new ReferenceMap(ReferenceMap.HARD, + ReferenceMap.WEAK)); /** * Maps jids to chats */ - private Map jidChats = new ReferenceMap(ReferenceMap.HARD, - ReferenceMap.WEAK); + private Map jidChats = Collections.synchronizedMap(new ReferenceMap(ReferenceMap.HARD, + ReferenceMap.WEAK)); + + /** + * Maps base jids to chats + */ + private Map baseJidChats = Collections.synchronizedMap(new ReferenceMap(ReferenceMap.HARD, + ReferenceMap.WEAK)); private Set chatManagerListeners = new CopyOnWriteArraySet(); @@ -161,6 +171,7 @@ public class ChatManager { Chat chat = new Chat(this, userJID, threadID); threadChats.put(threadID, chat); jidChats.put(userJID, chat); + baseJidChats.put(StringUtils.parseBareAddress(userJID), chat); for(ChatManagerListener listener : chatManagerListeners) { listener.chatCreated(chat, createdLocally); @@ -179,8 +190,21 @@ public class ChatManager { return createChat(userJID, threadID, false); } + /** + * Try to get a matching chat for the given user JID. Try the full + * JID map first, the try to match on the base JID if no match is + * found. + * + * @param userJID + * @return + */ private Chat getUserChat(String userJID) { - return jidChats.get(userJID); + Chat match = jidChats.get(userJID); + + if (match == null) { + match = baseJidChats.get(StringUtils.parseBareAddress(userJID)); + } + return match; } public Chat getThreadChat(String thread) {