1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-23 06:42:05 +01:00

getInstanceFor() for ChatManager and AccountManager

This commit is contained in:
Florian Schmaus 2014-03-17 20:46:08 +01:00
parent 6197f6200f
commit 4db967079f
9 changed files with 97 additions and 102 deletions

View file

@ -153,8 +153,7 @@ public class BOSHPacketReader implements BOSHClientResponseListener {
// The server supports sessions // The server supports sessions
connection.getSASLAuthentication().sessionsSupported(); connection.getSASLAuthentication().sessionsSupported();
} else if (parser.getName().equals("register")) { } else if (parser.getName().equals("register")) {
connection.getAccountManager().setSupportsAccountCreation( AccountManager.getInstance(connection).setSupportsAccountCreation(true);
true);
} }
} else if (eventType == XmlPullParser.END_TAG) { } else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("features")) { if (parser.getName().equals("features")) {

View file

@ -21,6 +21,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.WeakHashMap;
import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException; 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. * Allows creation and management of accounts on an XMPP server.
* *
* @see XMPPConnection#getAccountManager()
* @author Matt Tucker * @author Matt Tucker
*/ */
public class AccountManager { public class AccountManager extends Manager {
private XMPPConnection connection; private static final Map<XMPPConnection, AccountManager> INSTANCES = new WeakHashMap<XMPPConnection, AccountManager>();
/**
* 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; private Registration info = null;
/** /**
@ -51,8 +65,9 @@ public class AccountManager {
* *
* @param connection a connection to a XMPP server. * @param connection a connection to a XMPP server.
*/ */
public AccountManager(XMPPConnection connection) { private AccountManager(XMPPConnection connection) {
this.connection = connection; super(connection);
INSTANCES.put(connection, this);
} }
/** /**
@ -199,11 +214,11 @@ public class AccountManager {
throws NoResponseException, XMPPErrorException { throws NoResponseException, XMPPErrorException {
Registration reg = new Registration(); Registration reg = new Registration();
reg.setType(IQ.Type.SET); reg.setType(IQ.Type.SET);
reg.setTo(connection.getServiceName()); reg.setTo(connection().getServiceName());
attributes.put("username", username); attributes.put("username", username);
attributes.put("password", password); attributes.put("password", password);
reg.setAttributes(attributes); 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 { public void changePassword(String newPassword) throws NoResponseException, XMPPErrorException {
Registration reg = new Registration(); Registration reg = new Registration();
reg.setType(IQ.Type.SET); reg.setType(IQ.Type.SET);
reg.setTo(connection.getServiceName()); reg.setTo(connection().getServiceName());
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
map.put("username",StringUtils.parseName(connection.getUser())); map.put("username",StringUtils.parseName(connection().getUser()));
map.put("password",newPassword); map.put("password",newPassword);
reg.setAttributes(map); reg.setAttributes(map);
connection.createPacketCollectorAndSend(reg).nextResultOrThrow(); connection().createPacketCollectorAndSend(reg).nextResultOrThrow();
} }
/** /**
@ -238,12 +253,12 @@ public class AccountManager {
public void deleteAccount() throws NoResponseException, XMPPErrorException { public void deleteAccount() throws NoResponseException, XMPPErrorException {
Registration reg = new Registration(); Registration reg = new Registration();
reg.setType(IQ.Type.SET); reg.setType(IQ.Type.SET);
reg.setTo(connection.getServiceName()); reg.setTo(connection().getServiceName());
Map<String, String> attributes = new HashMap<String, String>(); Map<String, String> attributes = new HashMap<String, String>();
// To delete an account, we add a single attribute, "remove", that is blank. // To delete an account, we add a single attribute, "remove", that is blank.
attributes.put("remove", ""); attributes.put("remove", "");
reg.setAttributes(attributes); 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 { private synchronized void getRegistrationInfo() throws NoResponseException, XMPPErrorException {
Registration reg = new Registration(); Registration reg = new Registration();
reg.setTo(connection.getServiceName()); reg.setTo(connection().getServiceName());
info = (Registration) connection.createPacketCollectorAndSend(reg).nextResultOrThrow(); info = (Registration) connection().createPacketCollectorAndSend(reg).nextResultOrThrow();
} }
} }

View file

@ -42,7 +42,9 @@ import org.jivesoftware.smack.util.StringUtils;
* *
* @author Alexander Wenckus * @author Alexander Wenckus
*/ */
public class ChatManager { public class ChatManager extends Manager{
private static final Map<XMPPConnection, ChatManager> INSTANCES = new WeakHashMap<XMPPConnection, ChatManager>();
/** /**
* Sets the default behaviour for allowing 'normal' messages to be used in chats. As some clients don't set * 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. * 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; 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 * 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. * the incoming message does not have a thread id.
@ -105,10 +120,8 @@ public class ChatManager {
private Map<PacketInterceptor, PacketFilter> interceptors private Map<PacketInterceptor, PacketFilter> interceptors
= new WeakHashMap<PacketInterceptor, PacketFilter>(); = new WeakHashMap<PacketInterceptor, PacketFilter>();
private XMPPConnection connection; private ChatManager(XMPPConnection connection) {
super(connection);
ChatManager(XMPPConnection connection) {
this.connection = connection;
PacketFilter filter = new PacketFilter() { PacketFilter filter = new PacketFilter() {
public boolean accept(Packet packet) { public boolean accept(Packet packet) {
@ -139,6 +152,7 @@ public class ChatManager {
deliverMessage(chat, message); deliverMessage(chat, message);
} }
}, filter); }, filter);
INSTANCES.put(connection, this);
} }
/** /**
@ -310,13 +324,13 @@ public class ChatManager {
} }
// Ensure that messages being sent have a proper FROM value // Ensure that messages being sent have a proper FROM value
if (message.getFrom() == null) { if (message.getFrom() == null) {
message.setFrom(connection.getUser()); message.setFrom(connection().getUser());
} }
connection.sendPacket(message); connection().sendPacket(message);
} }
PacketCollector createPacketCollector(Chat chat) { 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()))); FromMatchesFilter.create(chat.getParticipant())));
} }

View file

@ -69,7 +69,7 @@ public class ReconnectionManager extends AbstractConnectionListener {
*/ */
private boolean isReconnectionAllowed() { private boolean isReconnectionAllowed() {
return !done && !connection.isConnected() return !done && !connection.isConnected()
&& connection.isReconnectionAllowed(); && connection.getConfiguration().isReconnectionAllowed();
} }
/** /**

View file

@ -163,16 +163,6 @@ public abstract class XMPPConnection {
protected final Map<PacketInterceptor, InterceptorWrapper> interceptors = protected final Map<PacketInterceptor, InterceptorWrapper> interceptors =
new ConcurrentHashMap<PacketInterceptor, InterceptorWrapper>(); new ConcurrentHashMap<PacketInterceptor, InterceptorWrapper>();
/**
* 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); 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 * 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 * 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); 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. * Returns the roster for the user.
* <p> * <p>

View file

@ -49,25 +49,25 @@ public class ChatConnectionTest {
@Test @Test
public void validateDefaultSetNormalIncluded() { public void validateDefaultSetNormalIncluded() {
ChatManager.setDefaultIsNormalIncluded(false); ChatManager.setDefaultIsNormalIncluded(false);
assertFalse(getConnection().getChatManager().isNormalIncluded()); assertFalse(ChatManager.getInstanceFor(getConnection()).isNormalIncluded());
ChatManager.setDefaultIsNormalIncluded(true); ChatManager.setDefaultIsNormalIncluded(true);
assertTrue(getConnection().getChatManager().isNormalIncluded()); assertTrue(ChatManager.getInstanceFor(getConnection()).isNormalIncluded());
} }
@Test @Test
public void validateDefaultSetMatchMode() { public void validateDefaultSetMatchMode() {
ChatManager.setDefaultMatchMode(MatchMode.NONE); ChatManager.setDefaultMatchMode(MatchMode.NONE);
assertEquals(MatchMode.NONE, getConnection().getChatManager().getMatchMode()); assertEquals(MatchMode.NONE, ChatManager.getInstanceFor(getConnection()).getMatchMode());
ChatManager.setDefaultMatchMode(MatchMode.BARE_JID); ChatManager.setDefaultMatchMode(MatchMode.BARE_JID);
assertEquals(MatchMode.BARE_JID, getConnection().getChatManager().getMatchMode()); assertEquals(MatchMode.BARE_JID, ChatManager.getInstanceFor(getConnection()).getMatchMode());
} }
@Test @Test
public void validateMessageTypeWithDefaults() { public void validateMessageTypeWithDefaults() {
DummyConnection dc = getConnection(); DummyConnection dc = getConnection();
ChatManager cm = dc.getChatManager(); ChatManager cm = ChatManager.getInstanceFor(dc);
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
cm.addChatListener(listener); cm.addChatListener(listener);
Message incomingChat = createChatPacket("134", true); Message incomingChat = createChatPacket("134", true);
@ -76,7 +76,7 @@ public class ChatConnectionTest {
assertNotNull(listener.getNewChat()); assertNotNull(listener.getNewChat());
dc = getConnection(); dc = getConnection();
cm = dc.getChatManager(); cm = ChatManager.getInstanceFor(dc);
listener = new TestChatManagerListener(); listener = new TestChatManagerListener();
cm.addChatListener(listener); cm.addChatListener(listener);
incomingChat = createChatPacket("134", true); incomingChat = createChatPacket("134", true);
@ -85,7 +85,7 @@ public class ChatConnectionTest {
assertNotNull(listener.getNewChat()); assertNotNull(listener.getNewChat());
dc = getConnection(); dc = getConnection();
cm = dc.getChatManager(); cm = ChatManager.getInstanceFor(dc);
listener = new TestChatManagerListener(); listener = new TestChatManagerListener();
cm.addChatListener(listener); cm.addChatListener(listener);
incomingChat = createChatPacket("134", true); incomingChat = createChatPacket("134", true);
@ -94,7 +94,7 @@ public class ChatConnectionTest {
assertNull(listener.getNewChat()); assertNull(listener.getNewChat());
dc = getConnection(); dc = getConnection();
cm = dc.getChatManager(); cm = ChatManager.getInstanceFor(dc);
listener = new TestChatManagerListener(); listener = new TestChatManagerListener();
cm.addChatListener(listener); cm.addChatListener(listener);
incomingChat = createChatPacket("134", true); incomingChat = createChatPacket("134", true);
@ -106,7 +106,7 @@ public class ChatConnectionTest {
@Test @Test
public void validateMessageTypeWithNoNormal() { public void validateMessageTypeWithNoNormal() {
DummyConnection dc = getConnection(); DummyConnection dc = getConnection();
ChatManager cm = dc.getChatManager(); ChatManager cm = ChatManager.getInstanceFor(dc);
cm.setNormalIncluded(false); cm.setNormalIncluded(false);
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
cm.addChatListener(listener); cm.addChatListener(listener);
@ -116,7 +116,7 @@ public class ChatConnectionTest {
assertNotNull(listener.getNewChat()); assertNotNull(listener.getNewChat());
dc = getConnection(); dc = getConnection();
cm = dc.getChatManager(); cm = ChatManager.getInstanceFor(dc);
cm.setNormalIncluded(false); cm.setNormalIncluded(false);
listener = new TestChatManagerListener(); listener = new TestChatManagerListener();
cm.addChatListener(listener); cm.addChatListener(listener);
@ -133,7 +133,8 @@ public class ChatConnectionTest {
DummyConnection con = getConnection(); DummyConnection con = getConnection();
TestMessageListener msgListener = new TestMessageListener(); TestMessageListener msgListener = new TestMessageListener();
TestChatManagerListener listener = new TestChatManagerListener(msgListener); TestChatManagerListener listener = new TestChatManagerListener(msgListener);
con.getChatManager().addChatListener(listener); ChatManager cm = ChatManager.getInstanceFor(con);
cm.addChatListener(listener);
Packet incomingChat = createChatPacket(null, true); Packet incomingChat = createChatPacket(null, true);
processServerMessage(incomingChat, con); processServerMessage(incomingChat, con);
Chat newChat = listener.getNewChat(); Chat newChat = listener.getNewChat();
@ -155,7 +156,7 @@ public class ChatConnectionTest {
DummyConnection con = getConnection(); DummyConnection con = getConnection();
TestMessageListener msgListener = new TestMessageListener(); TestMessageListener msgListener = new TestMessageListener();
TestChatManagerListener listener = new TestChatManagerListener(msgListener); TestChatManagerListener listener = new TestChatManagerListener(msgListener);
ChatManager cm = con.getChatManager(); ChatManager cm = ChatManager.getInstanceFor(con);
cm.setMatchMode(MatchMode.SUPPLIED_JID); cm.setMatchMode(MatchMode.SUPPLIED_JID);
cm.addChatListener(listener); cm.addChatListener(listener);
Packet incomingChat = createChatPacket(null, true); Packet incomingChat = createChatPacket(null, true);
@ -183,7 +184,7 @@ public class ChatConnectionTest {
DummyConnection con = getConnection(); DummyConnection con = getConnection();
TestMessageListener msgListener = new TestMessageListener(); TestMessageListener msgListener = new TestMessageListener();
TestChatManagerListener listener = new TestChatManagerListener(msgListener); TestChatManagerListener listener = new TestChatManagerListener(msgListener);
ChatManager cm = con.getChatManager(); ChatManager cm = ChatManager.getInstanceFor(con);
cm.setMatchMode(MatchMode.NONE); cm.setMatchMode(MatchMode.NONE);
cm.addChatListener(listener); cm.addChatListener(listener);
Packet incomingChat = createChatPacket(null, true); Packet incomingChat = createChatPacket(null, true);
@ -218,8 +219,9 @@ public class ChatConnectionTest {
@Test @Test
public void chatFoundWhenNoThreadFullJid() { public void chatFoundWhenNoThreadFullJid() {
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); ChatManager cm = ChatManager.getInstanceFor(connection);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); cm.addChatListener(listener);
Chat outgoing = cm.createChat("you@testserver", null);
Packet incomingChat = createChatPacket(null, true); Packet incomingChat = createChatPacket(null, true);
processServerMessage(incomingChat); processServerMessage(incomingChat);
@ -236,8 +238,9 @@ public class ChatConnectionTest {
@Test @Test
public void chatFoundWhenNoThreadBaseJid() { public void chatFoundWhenNoThreadBaseJid() {
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); ChatManager cm = ChatManager.getInstanceFor(connection);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); cm.addChatListener(listener);
Chat outgoing = cm.createChat("you@testserver", null);
Packet incomingChat = createChatPacket(null, false); Packet incomingChat = createChatPacket(null, false);
processServerMessage(incomingChat); processServerMessage(incomingChat);
@ -254,8 +257,9 @@ public class ChatConnectionTest {
@Test @Test
public void chatFoundWithSameThreadFullJid() { public void chatFoundWithSameThreadFullJid() {
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); ChatManager cm = ChatManager.getInstanceFor(connection);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); cm.addChatListener(listener);
Chat outgoing = cm.createChat("you@testserver", null);
Packet incomingChat = createChatPacket(outgoing.getThreadID(), true); Packet incomingChat = createChatPacket(outgoing.getThreadID(), true);
processServerMessage(incomingChat); processServerMessage(incomingChat);
@ -272,8 +276,9 @@ public class ChatConnectionTest {
@Test @Test
public void chatFoundWithSameThreadBaseJid() { public void chatFoundWithSameThreadBaseJid() {
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); ChatManager cm = ChatManager.getInstanceFor(connection);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); cm.addChatListener(listener);
Chat outgoing = cm.createChat("you@testserver", null);
Packet incomingChat = createChatPacket(outgoing.getThreadID(), false); Packet incomingChat = createChatPacket(outgoing.getThreadID(), false);
processServerMessage(incomingChat); processServerMessage(incomingChat);
@ -290,8 +295,9 @@ public class ChatConnectionTest {
@Test @Test
public void chatNotFoundWithDiffThreadBaseJid() { public void chatNotFoundWithDiffThreadBaseJid() {
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); ChatManager cm = ChatManager.getInstanceFor(connection);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); cm.addChatListener(listener);
Chat outgoing = cm.createChat("you@testserver", null);
Packet incomingChat = createChatPacket(outgoing.getThreadID() + "ff", false); Packet incomingChat = createChatPacket(outgoing.getThreadID() + "ff", false);
processServerMessage(incomingChat); processServerMessage(incomingChat);
@ -308,8 +314,9 @@ public class ChatConnectionTest {
@Test @Test
public void chatNotFoundWithDiffThreadFullJid() { public void chatNotFoundWithDiffThreadFullJid() {
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
connection.getChatManager().addChatListener(listener); ChatManager cm = ChatManager.getInstanceFor(connection);
Chat outgoing = connection.getChatManager().createChat("you@testserver", null); cm.addChatListener(listener);
Chat outgoing = cm.createChat("you@testserver", null);
Packet incomingChat = createChatPacket(outgoing.getThreadID() + "ff", true); Packet incomingChat = createChatPacket(outgoing.getThreadID() + "ff", true);
processServerMessage(incomingChat); processServerMessage(incomingChat);
@ -323,7 +330,7 @@ public class ChatConnectionTest {
public void chatNotMatchedWithTypeNormal() { public void chatNotMatchedWithTypeNormal() {
TestChatManagerListener listener = new TestChatManagerListener(); TestChatManagerListener listener = new TestChatManagerListener();
DummyConnection con = getConnection(); DummyConnection con = getConnection();
ChatManager cm = con.getChatManager(); ChatManager cm = ChatManager.getInstanceFor(con);
cm.setNormalIncluded(false); cm.setNormalIncluded(false);
cm.addChatListener(listener); cm.addChatListener(listener);
@ -336,7 +343,7 @@ public class ChatConnectionTest {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private ChatManager getChatManager(boolean includeNormal, MatchMode mode) { private ChatManager getChatManager(boolean includeNormal, MatchMode mode) {
ChatManager cm = getConnection().getChatManager(); ChatManager cm = ChatManager.getInstanceFor(getConnection());
cm.setMatchMode(mode); cm.setMatchMode(mode);
cm.setNormalIncluded(includeNormal); cm.setNormalIncluded(includeNormal);
return cm; return cm;

View file

@ -21,6 +21,7 @@ import java.util.Map;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener; import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.Manager;
@ -80,10 +81,13 @@ public class ChatStateManager extends Manager {
*/ */
private final Map<Chat, ChatState> chatStates = new WeakHashMap<Chat, ChatState>(); private final Map<Chat, ChatState> chatStates = new WeakHashMap<Chat, ChatState>();
private final ChatManager chatManager;
private ChatStateManager(XMPPConnection connection) { private ChatStateManager(XMPPConnection connection) {
super(connection); super(connection);
connection.getChatManager().addOutgoingMessageInterceptor(outgoingInterceptor, filter); chatManager = ChatManager.getInstanceFor(connection);
connection.getChatManager().addChatListener(incomingInterceptor); chatManager.addOutgoingMessageInterceptor(outgoingInterceptor, filter);
chatManager.addChatListener(incomingInterceptor);
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE); ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE);
INSTANCES.put(connection, this); INSTANCES.put(connection, this);
@ -148,7 +152,7 @@ public class ChatStateManager extends Manager {
public void interceptPacket(Packet packet) { public void interceptPacket(Packet packet) {
Message message = (Message) packet; Message message = (Message) packet;
Chat chat = connection().getChatManager().getThreadChat(message.getThread()); Chat chat = chatManager.getThreadChat(message.getThread());
if (chat == null) { if (chat == null) {
return; return;
} }

View file

@ -34,6 +34,7 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.AbstractConnectionListener; import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.MessageListener; import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketCollector; import org.jivesoftware.smack.PacketCollector;
@ -1627,7 +1628,7 @@ public class MultiUserChat {
* @return new Chat for sending private messages to a given room occupant. * @return new Chat for sending private messages to a given room occupant.
*/ */
public Chat createPrivateChat(String occupant, MessageListener listener) { public Chat createPrivateChat(String occupant, MessageListener listener) {
return connection.getChatManager().createChat(occupant, listener); return ChatManager.getInstanceFor(connection).createChat(occupant, listener);
} }
/** /**

View file

@ -359,7 +359,7 @@ class PacketReader {
connection.setAvailableCompressionMethods(PacketParserUtils.parseCompressionMethods(parser)); connection.setAvailableCompressionMethods(PacketParserUtils.parseCompressionMethods(parser));
} }
else if (parser.getName().equals("register")) { else if (parser.getName().equals("register")) {
connection.getAccountManager().setSupportsAccountCreation(true); AccountManager.getInstance(connection).setSupportsAccountCreation(true);
} }
} }
else if (eventType == XmlPullParser.END_TAG) { else if (eventType == XmlPullParser.END_TAG) {