1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 10:37:59 +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
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")) {

View file

@ -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<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;
/**
@ -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<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);
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<String, String> attributes = new HashMap<String, String>();
// 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();
}
}

View file

@ -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<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
* 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<PacketInterceptor, PacketFilter> interceptors
= new WeakHashMap<PacketInterceptor, PacketFilter>();
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())));
}

View file

@ -69,7 +69,7 @@ public class ReconnectionManager extends AbstractConnectionListener {
*/
private boolean isReconnectionAllowed() {
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 =
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);
/**
* 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.
* <p>

View file

@ -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;

View file

@ -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<Chat, ChatState> chatStates = new WeakHashMap<Chat, ChatState>();
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;
}

View file

@ -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);
}
/**

View file

@ -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) {