From 4db967079ff0c41870d71c0dbe8862552b0daf39 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 17 Mar 2014 20:46:08 +0100 Subject: [PATCH] getInstanceFor() for ChatManager and AccountManager --- .../jivesoftware/smack/BOSHPacketReader.java | 3 +- .../jivesoftware/smack/AccountManager.java | 43 ++++++++----- .../org/jivesoftware/smack/ChatManager.java | 30 ++++++--- .../smack/ReconnectionManager.java | 2 +- .../jivesoftware/smack/XMPPConnection.java | 45 -------------- .../smack/ChatConnectionTest.java | 61 +++++++++++-------- .../smackx/chatstates/ChatStateManager.java | 10 ++- .../smackx/muc/MultiUserChat.java | 3 +- .../org/jivesoftware/smack/PacketReader.java | 2 +- 9 files changed, 97 insertions(+), 102 deletions(-) diff --git a/bosh/src/main/java/org/jivesoftware/smack/BOSHPacketReader.java b/bosh/src/main/java/org/jivesoftware/smack/BOSHPacketReader.java index ba8a21b1a..0811106fd 100644 --- a/bosh/src/main/java/org/jivesoftware/smack/BOSHPacketReader.java +++ b/bosh/src/main/java/org/jivesoftware/smack/BOSHPacketReader.java @@ -153,8 +153,7 @@ public class BOSHPacketReader implements BOSHClientResponseListener { // The server supports sessions connection.getSASLAuthentication().sessionsSupported(); } else if (parser.getName().equals("register")) { - connection.getAccountManager().setSupportsAccountCreation( - true); + AccountManager.getInstance(connection).setSupportsAccountCreation(true); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals("features")) { diff --git a/core/src/main/java/org/jivesoftware/smack/AccountManager.java b/core/src/main/java/org/jivesoftware/smack/AccountManager.java index 3f86cb7e0..a853b7e79 100644 --- a/core/src/main/java/org/jivesoftware/smack/AccountManager.java +++ b/core/src/main/java/org/jivesoftware/smack/AccountManager.java @@ -21,6 +21,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.WeakHashMap; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; @@ -31,11 +32,24 @@ import org.jivesoftware.smack.util.StringUtils; /** * Allows creation and management of accounts on an XMPP server. * - * @see XMPPConnection#getAccountManager() * @author Matt Tucker */ -public class AccountManager { - private XMPPConnection connection; +public class AccountManager extends Manager { + private static final Map INSTANCES = new WeakHashMap(); + + /** + * Returns the AccountManager instance associated with a given XMPPConnection. + * + * @param connection the connection used to look for the proper ServiceDiscoveryManager. + * @return the AccountManager associated with a given XMPPConnection. + */ + public static synchronized AccountManager getInstance(XMPPConnection connection) { + AccountManager accountManager = INSTANCES.get(connection); + if (accountManager == null) + accountManager = new AccountManager(connection); + return accountManager; + } + private Registration info = null; /** @@ -51,8 +65,9 @@ public class AccountManager { * * @param connection a connection to a XMPP server. */ - public AccountManager(XMPPConnection connection) { - this.connection = connection; + private AccountManager(XMPPConnection connection) { + super(connection); + INSTANCES.put(connection, this); } /** @@ -199,11 +214,11 @@ public class AccountManager { throws NoResponseException, XMPPErrorException { Registration reg = new Registration(); reg.setType(IQ.Type.SET); - reg.setTo(connection.getServiceName()); + reg.setTo(connection().getServiceName()); attributes.put("username", username); attributes.put("password", password); reg.setAttributes(attributes); - connection.createPacketCollectorAndSend(reg).nextResultOrThrow(); + connection().createPacketCollectorAndSend(reg).nextResultOrThrow(); } /** @@ -218,12 +233,12 @@ public class AccountManager { public void changePassword(String newPassword) throws NoResponseException, XMPPErrorException { Registration reg = new Registration(); reg.setType(IQ.Type.SET); - reg.setTo(connection.getServiceName()); + reg.setTo(connection().getServiceName()); Map map = new HashMap(); - map.put("username",StringUtils.parseName(connection.getUser())); + map.put("username",StringUtils.parseName(connection().getUser())); map.put("password",newPassword); reg.setAttributes(map); - connection.createPacketCollectorAndSend(reg).nextResultOrThrow(); + connection().createPacketCollectorAndSend(reg).nextResultOrThrow(); } /** @@ -238,12 +253,12 @@ public class AccountManager { public void deleteAccount() throws NoResponseException, XMPPErrorException { Registration reg = new Registration(); reg.setType(IQ.Type.SET); - reg.setTo(connection.getServiceName()); + reg.setTo(connection().getServiceName()); Map attributes = new HashMap(); // To delete an account, we add a single attribute, "remove", that is blank. attributes.put("remove", ""); reg.setAttributes(attributes); - connection.createPacketCollectorAndSend(reg).nextResultOrThrow(); + connection().createPacketCollectorAndSend(reg).nextResultOrThrow(); } /** @@ -256,7 +271,7 @@ public class AccountManager { */ private synchronized void getRegistrationInfo() throws NoResponseException, XMPPErrorException { Registration reg = new Registration(); - reg.setTo(connection.getServiceName()); - info = (Registration) connection.createPacketCollectorAndSend(reg).nextResultOrThrow(); + reg.setTo(connection().getServiceName()); + info = (Registration) connection().createPacketCollectorAndSend(reg).nextResultOrThrow(); } } diff --git a/core/src/main/java/org/jivesoftware/smack/ChatManager.java b/core/src/main/java/org/jivesoftware/smack/ChatManager.java index 6bb216cf2..0ca95ad7b 100644 --- a/core/src/main/java/org/jivesoftware/smack/ChatManager.java +++ b/core/src/main/java/org/jivesoftware/smack/ChatManager.java @@ -42,7 +42,9 @@ import org.jivesoftware.smack.util.StringUtils; * * @author Alexander Wenckus */ -public class ChatManager { +public class ChatManager extends Manager{ + private static final Map INSTANCES = new WeakHashMap(); + /** * Sets the default behaviour for allowing 'normal' messages to be used in chats. As some clients don't set * the message type to chat, the type normal has to be accepted to allow chats with these clients. @@ -54,6 +56,19 @@ public class ChatManager { */ private static MatchMode defaultMatchMode = MatchMode.BARE_JID; + /** + * Returns the ChatManager instance associated with a given XMPPConnection. + * + * @param connection the connection used to look for the proper ServiceDiscoveryManager. + * @return the ChatManager associated with a given XMPPConnection. + */ + public static synchronized ChatManager getInstanceFor(XMPPConnection connection) { + ChatManager manager = INSTANCES.get(connection); + if (manager == null) + manager = new ChatManager(connection); + return manager; + } + /** * Defines the different modes under which a match will be attempted with an existing chat when * the incoming message does not have a thread id. @@ -105,10 +120,8 @@ public class ChatManager { private Map interceptors = new WeakHashMap(); - private XMPPConnection connection; - - ChatManager(XMPPConnection connection) { - this.connection = connection; + private ChatManager(XMPPConnection connection) { + super(connection); PacketFilter filter = new PacketFilter() { public boolean accept(Packet packet) { @@ -139,6 +152,7 @@ public class ChatManager { deliverMessage(chat, message); } }, filter); + INSTANCES.put(connection, this); } /** @@ -310,13 +324,13 @@ public class ChatManager { } // Ensure that messages being sent have a proper FROM value if (message.getFrom() == null) { - message.setFrom(connection.getUser()); + message.setFrom(connection().getUser()); } - connection.sendPacket(message); + connection().sendPacket(message); } PacketCollector createPacketCollector(Chat chat) { - return connection.createPacketCollector(new AndFilter(new ThreadFilter(chat.getThreadID()), + return connection().createPacketCollector(new AndFilter(new ThreadFilter(chat.getThreadID()), FromMatchesFilter.create(chat.getParticipant()))); } diff --git a/core/src/main/java/org/jivesoftware/smack/ReconnectionManager.java b/core/src/main/java/org/jivesoftware/smack/ReconnectionManager.java index f19a62ce4..b90fc4054 100644 --- a/core/src/main/java/org/jivesoftware/smack/ReconnectionManager.java +++ b/core/src/main/java/org/jivesoftware/smack/ReconnectionManager.java @@ -69,7 +69,7 @@ public class ReconnectionManager extends AbstractConnectionListener { */ private boolean isReconnectionAllowed() { return !done && !connection.isConnected() - && connection.isReconnectionAllowed(); + && connection.getConfiguration().isReconnectionAllowed(); } /** diff --git a/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java b/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java index 58c2549a0..3024e080d 100644 --- a/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java +++ b/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java @@ -163,16 +163,6 @@ public abstract class XMPPConnection { protected final Map interceptors = new ConcurrentHashMap(); - /** - * The AccountManager allows creation and management of accounts on an XMPP server. - */ - private AccountManager accountManager = null; - - /** - * The ChatManager keeps track of references to all current chats. - */ - private ChatManager chatManager = null; - /** * */ @@ -337,16 +327,6 @@ public abstract class XMPPConnection { abstract void sendPacketInternal(Packet packet); - /** - * Returns if the reconnection mechanism is allowed to be used. By default - * reconnection is allowed. - * - * @return true if the reconnection mechanism is allowed to be used. - */ - protected boolean isReconnectionAllowed() { - return config.isReconnectionAllowed(); - } - /** * Returns true if network traffic is being compressed. When using stream compression network * traffic can be reduced up to 90%. Therefore, stream compression is ideal when using a slow @@ -465,31 +445,6 @@ public abstract class XMPPConnection { firePacketSendingListeners(packet); } - /** - * Returns an account manager instance for this connection. - * - * @return an account manager for this connection. - */ - public AccountManager getAccountManager() { - if (accountManager == null) { - accountManager = new AccountManager(this); - } - return accountManager; - } - - /** - * Returns a chat manager instance for this connection. The ChatManager manages all incoming and - * outgoing chats on the current connection. - * - * @return a chat manager instance for this connection. - */ - public synchronized ChatManager getChatManager() { - if (this.chatManager == null) { - this.chatManager = new ChatManager(this); - } - return this.chatManager; - } - /** * Returns the roster for the user. *

diff --git a/core/src/test/java/org/jivesoftware/smack/ChatConnectionTest.java b/core/src/test/java/org/jivesoftware/smack/ChatConnectionTest.java index 1656ae68e..dd04dc1ed 100644 --- a/core/src/test/java/org/jivesoftware/smack/ChatConnectionTest.java +++ b/core/src/test/java/org/jivesoftware/smack/ChatConnectionTest.java @@ -49,25 +49,25 @@ public class ChatConnectionTest { @Test public void validateDefaultSetNormalIncluded() { ChatManager.setDefaultIsNormalIncluded(false); - assertFalse(getConnection().getChatManager().isNormalIncluded()); + assertFalse(ChatManager.getInstanceFor(getConnection()).isNormalIncluded()); ChatManager.setDefaultIsNormalIncluded(true); - assertTrue(getConnection().getChatManager().isNormalIncluded()); + assertTrue(ChatManager.getInstanceFor(getConnection()).isNormalIncluded()); } @Test public void validateDefaultSetMatchMode() { ChatManager.setDefaultMatchMode(MatchMode.NONE); - assertEquals(MatchMode.NONE, getConnection().getChatManager().getMatchMode()); + assertEquals(MatchMode.NONE, ChatManager.getInstanceFor(getConnection()).getMatchMode()); ChatManager.setDefaultMatchMode(MatchMode.BARE_JID); - assertEquals(MatchMode.BARE_JID, getConnection().getChatManager().getMatchMode()); + assertEquals(MatchMode.BARE_JID, ChatManager.getInstanceFor(getConnection()).getMatchMode()); } @Test public void validateMessageTypeWithDefaults() { DummyConnection dc = getConnection(); - ChatManager cm = dc.getChatManager(); + ChatManager cm = ChatManager.getInstanceFor(dc); TestChatManagerListener listener = new TestChatManagerListener(); cm.addChatListener(listener); Message incomingChat = createChatPacket("134", true); @@ -76,7 +76,7 @@ public class ChatConnectionTest { assertNotNull(listener.getNewChat()); dc = getConnection(); - cm = dc.getChatManager(); + cm = ChatManager.getInstanceFor(dc); listener = new TestChatManagerListener(); cm.addChatListener(listener); incomingChat = createChatPacket("134", true); @@ -85,7 +85,7 @@ public class ChatConnectionTest { assertNotNull(listener.getNewChat()); dc = getConnection(); - cm = dc.getChatManager(); + cm = ChatManager.getInstanceFor(dc); listener = new TestChatManagerListener(); cm.addChatListener(listener); incomingChat = createChatPacket("134", true); @@ -94,7 +94,7 @@ public class ChatConnectionTest { assertNull(listener.getNewChat()); dc = getConnection(); - cm = dc.getChatManager(); + cm = ChatManager.getInstanceFor(dc); listener = new TestChatManagerListener(); cm.addChatListener(listener); incomingChat = createChatPacket("134", true); @@ -106,7 +106,7 @@ public class ChatConnectionTest { @Test public void validateMessageTypeWithNoNormal() { DummyConnection dc = getConnection(); - ChatManager cm = dc.getChatManager(); + ChatManager cm = ChatManager.getInstanceFor(dc); cm.setNormalIncluded(false); TestChatManagerListener listener = new TestChatManagerListener(); cm.addChatListener(listener); @@ -116,7 +116,7 @@ public class ChatConnectionTest { assertNotNull(listener.getNewChat()); dc = getConnection(); - cm = dc.getChatManager(); + cm = ChatManager.getInstanceFor(dc); cm.setNormalIncluded(false); listener = new TestChatManagerListener(); cm.addChatListener(listener); @@ -133,7 +133,8 @@ public class ChatConnectionTest { DummyConnection con = getConnection(); TestMessageListener msgListener = new TestMessageListener(); TestChatManagerListener listener = new TestChatManagerListener(msgListener); - con.getChatManager().addChatListener(listener); + ChatManager cm = ChatManager.getInstanceFor(con); + cm.addChatListener(listener); Packet incomingChat = createChatPacket(null, true); processServerMessage(incomingChat, con); Chat newChat = listener.getNewChat(); @@ -155,7 +156,7 @@ public class ChatConnectionTest { DummyConnection con = getConnection(); TestMessageListener msgListener = new TestMessageListener(); TestChatManagerListener listener = new TestChatManagerListener(msgListener); - ChatManager cm = con.getChatManager(); + ChatManager cm = ChatManager.getInstanceFor(con); cm.setMatchMode(MatchMode.SUPPLIED_JID); cm.addChatListener(listener); Packet incomingChat = createChatPacket(null, true); @@ -183,7 +184,7 @@ public class ChatConnectionTest { DummyConnection con = getConnection(); TestMessageListener msgListener = new TestMessageListener(); TestChatManagerListener listener = new TestChatManagerListener(msgListener); - ChatManager cm = con.getChatManager(); + ChatManager cm = ChatManager.getInstanceFor(con); cm.setMatchMode(MatchMode.NONE); cm.addChatListener(listener); Packet incomingChat = createChatPacket(null, true); @@ -218,8 +219,9 @@ public class ChatConnectionTest { @Test public void chatFoundWhenNoThreadFullJid() { TestChatManagerListener listener = new TestChatManagerListener(); - connection.getChatManager().addChatListener(listener); - Chat outgoing = connection.getChatManager().createChat("you@testserver", null); + ChatManager cm = ChatManager.getInstanceFor(connection); + cm.addChatListener(listener); + Chat outgoing = cm.createChat("you@testserver", null); Packet incomingChat = createChatPacket(null, true); processServerMessage(incomingChat); @@ -236,8 +238,9 @@ public class ChatConnectionTest { @Test public void chatFoundWhenNoThreadBaseJid() { TestChatManagerListener listener = new TestChatManagerListener(); - connection.getChatManager().addChatListener(listener); - Chat outgoing = connection.getChatManager().createChat("you@testserver", null); + ChatManager cm = ChatManager.getInstanceFor(connection); + cm.addChatListener(listener); + Chat outgoing = cm.createChat("you@testserver", null); Packet incomingChat = createChatPacket(null, false); processServerMessage(incomingChat); @@ -254,8 +257,9 @@ public class ChatConnectionTest { @Test public void chatFoundWithSameThreadFullJid() { TestChatManagerListener listener = new TestChatManagerListener(); - connection.getChatManager().addChatListener(listener); - Chat outgoing = connection.getChatManager().createChat("you@testserver", null); + ChatManager cm = ChatManager.getInstanceFor(connection); + cm.addChatListener(listener); + Chat outgoing = cm.createChat("you@testserver", null); Packet incomingChat = createChatPacket(outgoing.getThreadID(), true); processServerMessage(incomingChat); @@ -272,8 +276,9 @@ public class ChatConnectionTest { @Test public void chatFoundWithSameThreadBaseJid() { TestChatManagerListener listener = new TestChatManagerListener(); - connection.getChatManager().addChatListener(listener); - Chat outgoing = connection.getChatManager().createChat("you@testserver", null); + ChatManager cm = ChatManager.getInstanceFor(connection); + cm.addChatListener(listener); + Chat outgoing = cm.createChat("you@testserver", null); Packet incomingChat = createChatPacket(outgoing.getThreadID(), false); processServerMessage(incomingChat); @@ -290,8 +295,9 @@ public class ChatConnectionTest { @Test public void chatNotFoundWithDiffThreadBaseJid() { TestChatManagerListener listener = new TestChatManagerListener(); - connection.getChatManager().addChatListener(listener); - Chat outgoing = connection.getChatManager().createChat("you@testserver", null); + ChatManager cm = ChatManager.getInstanceFor(connection); + cm.addChatListener(listener); + Chat outgoing = cm.createChat("you@testserver", null); Packet incomingChat = createChatPacket(outgoing.getThreadID() + "ff", false); processServerMessage(incomingChat); @@ -308,8 +314,9 @@ public class ChatConnectionTest { @Test public void chatNotFoundWithDiffThreadFullJid() { TestChatManagerListener listener = new TestChatManagerListener(); - connection.getChatManager().addChatListener(listener); - Chat outgoing = connection.getChatManager().createChat("you@testserver", null); + ChatManager cm = ChatManager.getInstanceFor(connection); + cm.addChatListener(listener); + Chat outgoing = cm.createChat("you@testserver", null); Packet incomingChat = createChatPacket(outgoing.getThreadID() + "ff", true); processServerMessage(incomingChat); @@ -323,7 +330,7 @@ public class ChatConnectionTest { public void chatNotMatchedWithTypeNormal() { TestChatManagerListener listener = new TestChatManagerListener(); DummyConnection con = getConnection(); - ChatManager cm = con.getChatManager(); + ChatManager cm = ChatManager.getInstanceFor(con); cm.setNormalIncluded(false); cm.addChatListener(listener); @@ -336,7 +343,7 @@ public class ChatConnectionTest { @SuppressWarnings("unused") private ChatManager getChatManager(boolean includeNormal, MatchMode mode) { - ChatManager cm = getConnection().getChatManager(); + ChatManager cm = ChatManager.getInstanceFor(getConnection()); cm.setMatchMode(mode); cm.setNormalIncluded(includeNormal); return cm; diff --git a/extensions/src/main/java/org/jivesoftware/smackx/chatstates/ChatStateManager.java b/extensions/src/main/java/org/jivesoftware/smackx/chatstates/ChatStateManager.java index a90cf5b4a..9a9b53a8b 100644 --- a/extensions/src/main/java/org/jivesoftware/smackx/chatstates/ChatStateManager.java +++ b/extensions/src/main/java/org/jivesoftware/smackx/chatstates/ChatStateManager.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.WeakHashMap; import org.jivesoftware.smack.Chat; +import org.jivesoftware.smack.ChatManager; import org.jivesoftware.smack.ChatManagerListener; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.Manager; @@ -80,10 +81,13 @@ public class ChatStateManager extends Manager { */ private final Map chatStates = new WeakHashMap(); + private final ChatManager chatManager; + private ChatStateManager(XMPPConnection connection) { super(connection); - connection.getChatManager().addOutgoingMessageInterceptor(outgoingInterceptor, filter); - connection.getChatManager().addChatListener(incomingInterceptor); + chatManager = ChatManager.getInstanceFor(connection); + chatManager.addOutgoingMessageInterceptor(outgoingInterceptor, filter); + chatManager.addChatListener(incomingInterceptor); ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE); INSTANCES.put(connection, this); @@ -148,7 +152,7 @@ public class ChatStateManager extends Manager { public void interceptPacket(Packet packet) { Message message = (Message) packet; - Chat chat = connection().getChatManager().getThreadChat(message.getThread()); + Chat chat = chatManager.getThreadChat(message.getThread()); if (chat == null) { return; } diff --git a/extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java b/extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java index 04ee4dc9c..700c1aa4a 100644 --- a/extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java +++ b/extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java @@ -34,6 +34,7 @@ import java.util.logging.Logger; import org.jivesoftware.smack.AbstractConnectionListener; import org.jivesoftware.smack.Chat; +import org.jivesoftware.smack.ChatManager; import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.MessageListener; import org.jivesoftware.smack.PacketCollector; @@ -1627,7 +1628,7 @@ public class MultiUserChat { * @return new Chat for sending private messages to a given room occupant. */ public Chat createPrivateChat(String occupant, MessageListener listener) { - return connection.getChatManager().createChat(occupant, listener); + return ChatManager.getInstanceFor(connection).createChat(occupant, listener); } /** diff --git a/tcp/src/main/java/org/jivesoftware/smack/PacketReader.java b/tcp/src/main/java/org/jivesoftware/smack/PacketReader.java index 17b737bd0..74543e43b 100644 --- a/tcp/src/main/java/org/jivesoftware/smack/PacketReader.java +++ b/tcp/src/main/java/org/jivesoftware/smack/PacketReader.java @@ -359,7 +359,7 @@ class PacketReader { connection.setAvailableCompressionMethods(PacketParserUtils.parseCompressionMethods(parser)); } else if (parser.getName().equals("register")) { - connection.getAccountManager().setSupportsAccountCreation(true); + AccountManager.getInstance(connection).setSupportsAccountCreation(true); } } else if (eventType == XmlPullParser.END_TAG) {