mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-12 05:51:08 +01:00
getInstanceFor() for ChatManager and AccountManager
This commit is contained in:
parent
6197f6200f
commit
4db967079f
9 changed files with 97 additions and 102 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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())));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ public class ReconnectionManager extends AbstractConnectionListener {
|
|||
*/
|
||||
private boolean isReconnectionAllowed() {
|
||||
return !done && !connection.isConnected()
|
||||
&& connection.isReconnectionAllowed();
|
||||
&& connection.getConfiguration().isReconnectionAllowed();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue