mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-26 05:52:06 +01:00
Refactor Managers to subclass abstract Manager
Also use ServiceDiscoveryManager.supportsFeature() where possible.
This commit is contained in:
parent
93eea8ab8d
commit
f7fc38e1f4
9 changed files with 105 additions and 162 deletions
|
@ -16,13 +16,13 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.carbons;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.filter.IQReplyFilter;
|
||||
|
@ -31,7 +31,6 @@ import org.jivesoftware.smack.packet.Message;
|
|||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smackx.carbons.packet.CarbonExtension;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
||||
|
||||
/**
|
||||
* Packet extension for XEP-0280: Message Carbons. This class implements
|
||||
|
@ -43,7 +42,7 @@ import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
|||
*
|
||||
* @author Georg Lukas
|
||||
*/
|
||||
public class CarbonManager {
|
||||
public class CarbonManager extends Manager {
|
||||
|
||||
private static Map<Connection, CarbonManager> instances =
|
||||
Collections.synchronizedMap(new WeakHashMap<Connection, CarbonManager>());
|
||||
|
@ -56,13 +55,12 @@ public class CarbonManager {
|
|||
});
|
||||
}
|
||||
|
||||
private WeakReference<Connection> weakRefConnection;
|
||||
private volatile boolean enabled_state = false;
|
||||
|
||||
private CarbonManager(Connection connection) {
|
||||
super(connection);
|
||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
sdm.addFeature(CarbonExtension.NAMESPACE);
|
||||
weakRefConnection = new WeakReference<Connection>(connection);
|
||||
instances.put(connection, this);
|
||||
}
|
||||
|
||||
|
@ -99,11 +97,9 @@ public class CarbonManager {
|
|||
* @return true if supported
|
||||
*/
|
||||
public boolean isSupportedByServer() {
|
||||
Connection connection = weakRefConnection.get();
|
||||
try {
|
||||
DiscoverInfo result = ServiceDiscoveryManager
|
||||
.getInstanceFor(connection).discoverInfo(connection.getServiceName());
|
||||
return result.containsFeature(CarbonExtension.NAMESPACE);
|
||||
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(
|
||||
connection().getServiceName(), CarbonExtension.NAMESPACE);
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
return false;
|
||||
|
@ -119,20 +115,19 @@ public class CarbonManager {
|
|||
* @param new_state whether carbons should be enabled or disabled
|
||||
*/
|
||||
public void sendCarbonsEnabled(final boolean new_state) {
|
||||
final Connection connection = weakRefConnection.get();
|
||||
IQ setIQ = carbonsEnabledIQ(new_state);
|
||||
|
||||
connection.addPacketListener(new PacketListener() {
|
||||
connection().addPacketListener(new PacketListener() {
|
||||
public void processPacket(Packet packet) {
|
||||
IQ result = (IQ)packet;
|
||||
if (result.getType() == IQ.Type.RESULT) {
|
||||
enabled_state = new_state;
|
||||
}
|
||||
connection.removePacketListener(this);
|
||||
connection().removePacketListener(this);
|
||||
}
|
||||
}, new IQReplyFilter(setIQ, connection));
|
||||
}, new IQReplyFilter(setIQ, connection()));
|
||||
|
||||
connection.sendPacket(setIQ);
|
||||
connection().sendPacket(setIQ);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -150,10 +145,9 @@ public class CarbonManager {
|
|||
public void setCarbonsEnabled(final boolean new_state) throws XMPPException {
|
||||
if (enabled_state == new_state) return;
|
||||
|
||||
Connection connection = weakRefConnection.get();
|
||||
IQ setIQ = carbonsEnabledIQ(new_state);
|
||||
|
||||
connection.createPacketCollectorAndSend(setIQ).nextResultOrThrow();
|
||||
connection().createPacketCollectorAndSend(setIQ).nextResultOrThrow();
|
||||
enabled_state = new_state;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2009 Jonas Ådahl, 2011-2013 Florian Schmaus
|
||||
* Copyright © 2009 Jonas Ådahl, 2011-2014 Florian Schmaus
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -19,6 +19,7 @@ package org.jivesoftware.smackx.caps;
|
|||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.ConnectionListener;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.PacketInterceptor;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
|
@ -58,7 +59,6 @@ import java.util.TreeSet;
|
|||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
|
@ -67,7 +67,7 @@ import java.security.NoSuchAlgorithmException;
|
|||
*
|
||||
* @author Florian Schmaus
|
||||
*/
|
||||
public class EntityCapsManager {
|
||||
public class EntityCapsManager extends Manager {
|
||||
|
||||
public static final String NAMESPACE = "http://jabber.org/protocol/caps";
|
||||
public static final String ELEMENT = "c";
|
||||
|
@ -110,7 +110,6 @@ public class EntityCapsManager {
|
|||
}
|
||||
}
|
||||
|
||||
private WeakReference<Connection> weakRefConnection;
|
||||
private ServiceDiscoveryManager sdm;
|
||||
private boolean entityCapsEnabled;
|
||||
private String currentCapsVersion;
|
||||
|
@ -221,7 +220,7 @@ public class EntityCapsManager {
|
|||
}
|
||||
|
||||
private EntityCapsManager(Connection connection) {
|
||||
this.weakRefConnection = new WeakReference<Connection>(connection);
|
||||
super(connection);
|
||||
this.sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
instances.put(connection, this);
|
||||
|
||||
|
@ -384,16 +383,8 @@ public class EntityCapsManager {
|
|||
* @param jid
|
||||
* @return
|
||||
*/
|
||||
public boolean areEntityCapsSupported(String jid) {
|
||||
if (jid == null)
|
||||
return false;
|
||||
|
||||
try {
|
||||
DiscoverInfo result = sdm.discoverInfo(jid);
|
||||
return result.containsFeature(NAMESPACE);
|
||||
} catch (XMPPException e) {
|
||||
return false;
|
||||
}
|
||||
public boolean areEntityCapsSupported(String jid) throws XMPPException {
|
||||
return sdm.supportsFeature(jid, NAMESPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -401,8 +392,8 @@ public class EntityCapsManager {
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean areEntityCapsSupportedByServer() {
|
||||
return areEntityCapsSupported(weakRefConnection.get().getServiceName());
|
||||
public boolean areEntityCapsSupportedByServer() throws XMPPException {
|
||||
return areEntityCapsSupported(connection().getServiceName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -422,7 +413,7 @@ public class EntityCapsManager {
|
|||
* the local users extended info
|
||||
*/
|
||||
public void updateLocalEntityCaps() {
|
||||
Connection connection = weakRefConnection.get();
|
||||
Connection connection = connection();
|
||||
|
||||
DiscoverInfo discoverInfo = new DiscoverInfo();
|
||||
discoverInfo.setType(IQ.Type.RESULT);
|
||||
|
|
|
@ -17,13 +17,13 @@
|
|||
|
||||
package org.jivesoftware.smackx.chatstates;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import org.jivesoftware.smack.Chat;
|
||||
import org.jivesoftware.smack.ChatManagerListener;
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.MessageListener;
|
||||
import org.jivesoftware.smack.PacketInterceptor;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
|
@ -50,13 +50,13 @@ import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
|||
* @see org.jivesoftware.smackx.chatstates.ChatState
|
||||
* @see org.jivesoftware.smackx.chatstates.packet.ChatStateExtension
|
||||
*/
|
||||
public class ChatStateManager {
|
||||
public class ChatStateManager extends Manager {
|
||||
public static final String NAMESPACE = "http://jabber.org/protocol/chatstates";
|
||||
|
||||
private static final Map<Connection, WeakReference<ChatStateManager>> managers =
|
||||
new WeakHashMap<Connection, WeakReference<ChatStateManager>>();
|
||||
private static final Map<Connection, ChatStateManager> INSTANCES =
|
||||
new WeakHashMap<Connection, ChatStateManager>();
|
||||
|
||||
private static final PacketFilter filter = new NotFilter(
|
||||
new PacketExtensionFilter("http://jabber.org/protocol/chatstates"));
|
||||
private static final PacketFilter filter = new NotFilter(new PacketExtensionFilter(NAMESPACE));
|
||||
|
||||
/**
|
||||
* Returns the ChatStateManager related to the Connection and it will create one if it does
|
||||
|
@ -65,27 +65,13 @@ public class ChatStateManager {
|
|||
* @param connection the connection to return the ChatStateManager
|
||||
* @return the ChatStateManager related the the connection.
|
||||
*/
|
||||
public static ChatStateManager getInstance(final Connection connection) {
|
||||
if(connection == null) {
|
||||
return null;
|
||||
}
|
||||
synchronized (managers) {
|
||||
ChatStateManager manager;
|
||||
WeakReference<ChatStateManager> ref = managers.get(connection);
|
||||
|
||||
if (ref == null) {
|
||||
public static synchronized ChatStateManager getInstance(final Connection connection) {
|
||||
ChatStateManager manager = INSTANCES.get(connection);
|
||||
if (manager == null) {
|
||||
manager = new ChatStateManager(connection);
|
||||
manager.init();
|
||||
managers.put(connection, new WeakReference<ChatStateManager>(manager));
|
||||
}
|
||||
else
|
||||
manager = ref.get();
|
||||
|
||||
return manager;
|
||||
}
|
||||
}
|
||||
|
||||
private final Connection connection;
|
||||
|
||||
private final OutgoingMessageInterceptor outgoingInterceptor = new OutgoingMessageInterceptor();
|
||||
|
||||
|
@ -98,18 +84,15 @@ public class ChatStateManager {
|
|||
new ReferenceMap<Chat, ChatState>(ReferenceMap.WEAK, ReferenceMap.HARD);
|
||||
|
||||
private ChatStateManager(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
private void init() {
|
||||
connection.getChatManager().addOutgoingMessageInterceptor(outgoingInterceptor,
|
||||
filter);
|
||||
super(connection);
|
||||
connection.getChatManager().addOutgoingMessageInterceptor(outgoingInterceptor, filter);
|
||||
connection.getChatManager().addChatListener(incomingInterceptor);
|
||||
|
||||
ServiceDiscoveryManager.getInstanceFor(connection)
|
||||
.addFeature("http://jabber.org/protocol/chatstates");
|
||||
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE);
|
||||
INSTANCES.put(connection, this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the current state of the provided chat. This method will send an empty bodied Message
|
||||
* packet with the state attached as a {@link org.jivesoftware.smack.packet.PacketExtension}, if
|
||||
|
@ -142,12 +125,12 @@ public class ChatStateManager {
|
|||
|
||||
ChatStateManager that = (ChatStateManager) o;
|
||||
|
||||
return connection.equals(that.connection);
|
||||
return connection().equals(that.connection());
|
||||
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return connection.hashCode();
|
||||
return connection().hashCode();
|
||||
}
|
||||
|
||||
private boolean updateChatState(Chat chat, ChatState newState) {
|
||||
|
@ -171,7 +154,7 @@ public class ChatStateManager {
|
|||
|
||||
public void interceptPacket(Packet packet) {
|
||||
Message message = (Message) packet;
|
||||
Chat chat = connection.getChatManager().getThreadChat(message.getThread());
|
||||
Chat chat = connection().getChatManager().getThreadChat(message.getThread());
|
||||
if (chat == null) {
|
||||
return;
|
||||
}
|
||||
|
@ -188,8 +171,7 @@ public class ChatStateManager {
|
|||
}
|
||||
|
||||
public void processMessage(Chat chat, Message message) {
|
||||
PacketExtension extension
|
||||
= message.getExtension("http://jabber.org/protocol/chatstates");
|
||||
PacketExtension extension = message.getExtension(NAMESPACE);
|
||||
if (extension == null) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -35,7 +35,6 @@ import org.jivesoftware.smackx.disco.packet.DiscoverItems;
|
|||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
|
||||
import org.jivesoftware.smackx.xdata.Form;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
@ -54,10 +53,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
*
|
||||
* @author Gabriel Guardincerri
|
||||
*/
|
||||
public class AdHocCommandManager {
|
||||
private static final String DISCO_NAMESPACE = "http://jabber.org/protocol/commands";
|
||||
|
||||
private static final String discoNode = DISCO_NAMESPACE;
|
||||
public class AdHocCommandManager extends Manager {
|
||||
public static final String NAMESPACE = "http://jabber.org/protocol/commands";
|
||||
|
||||
/**
|
||||
* The session time out in seconds.
|
||||
|
@ -97,11 +94,6 @@ public class AdHocCommandManager {
|
|||
return ahcm;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Connection that this instances of AdHocCommandManager manages
|
||||
*/
|
||||
private final WeakReference<Connection> connection;
|
||||
|
||||
/**
|
||||
* Map a command node with its AdHocCommandInfo. Note: Key=command node,
|
||||
* Value=command. Command node matches the node attribute sent by command
|
||||
|
@ -125,7 +117,7 @@ public class AdHocCommandManager {
|
|||
private Thread sessionsSweeper;
|
||||
|
||||
private AdHocCommandManager(Connection connection) {
|
||||
this.connection = new WeakReference<Connection>(connection);
|
||||
super(connection);
|
||||
this.serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
|
||||
// Register the new instance and associate it with the connection
|
||||
|
@ -136,13 +128,13 @@ public class AdHocCommandManager {
|
|||
// This information will be used when another client tries to
|
||||
// discover whether this client supports AdHoc-Commands or not.
|
||||
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(
|
||||
DISCO_NAMESPACE);
|
||||
NAMESPACE);
|
||||
|
||||
// Set the NodeInformationProvider that will provide information about
|
||||
// which AdHoc-Commands are registered, whenever a disco request is
|
||||
// received
|
||||
ServiceDiscoveryManager.getInstanceFor(connection)
|
||||
.setNodeInformationProvider(discoNode,
|
||||
.setNodeInformationProvider(NAMESPACE,
|
||||
new NodeInformationProvider() {
|
||||
public List<DiscoverItems.Item> getNodeItems() {
|
||||
|
||||
|
@ -221,7 +213,7 @@ public class AdHocCommandManager {
|
|||
* @param factory a factory to create new instances of the command.
|
||||
*/
|
||||
public void registerCommand(String node, final String name, LocalCommandFactory factory) {
|
||||
AdHocCommandInfo commandInfo = new AdHocCommandInfo(node, name, connection.get().getUser(), factory);
|
||||
AdHocCommandInfo commandInfo = new AdHocCommandInfo(node, name, connection().getUser(), factory);
|
||||
|
||||
commands.put(node, commandInfo);
|
||||
// Set the NodeInformationProvider that will provide information about
|
||||
|
@ -234,7 +226,7 @@ public class AdHocCommandManager {
|
|||
|
||||
public List<String> getNodeFeatures() {
|
||||
List<String> answer = new ArrayList<String>();
|
||||
answer.add(DISCO_NAMESPACE);
|
||||
answer.add(NAMESPACE);
|
||||
// TODO: check if this service is provided by the
|
||||
// TODO: current connection.
|
||||
answer.add("jabber:x:data");
|
||||
|
@ -266,7 +258,7 @@ public class AdHocCommandManager {
|
|||
* @throws XMPPException if the operation failed for some reason.
|
||||
*/
|
||||
public DiscoverItems discoverCommands(String jid) throws XMPPException {
|
||||
return serviceDiscoveryManager.discoverItems(jid, discoNode);
|
||||
return serviceDiscoveryManager.discoverItems(jid, NAMESPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -287,7 +279,7 @@ public class AdHocCommandManager {
|
|||
discoverItems.addItem(item);
|
||||
}
|
||||
|
||||
serviceDiscoveryManager.publishItems(jid, discoNode, discoverItems);
|
||||
serviceDiscoveryManager.publishItems(jid, NAMESPACE, discoverItems);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -301,7 +293,7 @@ public class AdHocCommandManager {
|
|||
* @return a local instance equivalent to the remote command.
|
||||
*/
|
||||
public RemoteCommand getRemoteCommand(String jid, String node) {
|
||||
return new RemoteCommand(connection.get(), node, jid);
|
||||
return new RemoteCommand(connection(), node, jid);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -449,7 +441,7 @@ public class AdHocCommandManager {
|
|||
}
|
||||
|
||||
// Sends the response packet
|
||||
connection.get().sendPacket(response);
|
||||
connection().sendPacket(response);
|
||||
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
|
@ -564,7 +556,7 @@ public class AdHocCommandManager {
|
|||
executingCommands.remove(sessionId);
|
||||
}
|
||||
|
||||
connection.get().sendPacket(response);
|
||||
connection().sendPacket(response);
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
// If there is an exception caused by the next, complete,
|
||||
|
@ -620,7 +612,7 @@ public class AdHocCommandManager {
|
|||
private void respondError(AdHocCommandData response, XMPPError error) {
|
||||
response.setType(IQ.Type.ERROR);
|
||||
response.setError(error);
|
||||
connection.get().sendPacket(response);
|
||||
connection().sendPacket(response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,7 +17,11 @@
|
|||
|
||||
package org.jivesoftware.smackx.disco;
|
||||
|
||||
import org.jivesoftware.smack.*;
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
import org.jivesoftware.smack.filter.PacketTypeFilter;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
|
@ -30,8 +34,15 @@ import org.jivesoftware.smackx.disco.packet.DiscoverItems;
|
|||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
|
||||
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
|
@ -45,7 +56,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
*
|
||||
* @author Gaston Dombiak
|
||||
*/
|
||||
public class ServiceDiscoveryManager {
|
||||
public class ServiceDiscoveryManager extends Manager {
|
||||
|
||||
private static final String DEFAULT_IDENTITY_NAME = "Smack";
|
||||
private static final String DEFAULT_IDENTITY_CATEGORY = "client";
|
||||
|
@ -61,7 +72,6 @@ public class ServiceDiscoveryManager {
|
|||
private static Map<Connection, ServiceDiscoveryManager> instances =
|
||||
Collections.synchronizedMap(new WeakHashMap<Connection, ServiceDiscoveryManager>());
|
||||
|
||||
private WeakReference<Connection> connection;
|
||||
private final Set<String> features = new HashSet<String>();
|
||||
private DataForm extendedInfo = null;
|
||||
private Map<String, NodeInformationProvider> nodeInformationProviders =
|
||||
|
@ -94,8 +104,7 @@ public class ServiceDiscoveryManager {
|
|||
* @param connection the connection to which a ServiceDiscoveryManager is going to be created.
|
||||
*/
|
||||
private ServiceDiscoveryManager(Connection connection) {
|
||||
this.connection = new WeakReference<Connection>(connection);
|
||||
|
||||
super(connection);
|
||||
// Register the new instance and associate it with the connection
|
||||
instances.put(connection, this);
|
||||
|
||||
|
@ -106,7 +115,7 @@ public class ServiceDiscoveryManager {
|
|||
PacketFilter packetFilter = new PacketTypeFilter(DiscoverItems.class);
|
||||
PacketListener packetListener = new PacketListener() {
|
||||
public void processPacket(Packet packet) {
|
||||
Connection connection = ServiceDiscoveryManager.this.connection.get();
|
||||
Connection connection = connection();
|
||||
if (connection == null) return;
|
||||
DiscoverItems discoverItems = (DiscoverItems) packet;
|
||||
// Send back the items defined in the client if the request is of type GET
|
||||
|
@ -143,7 +152,7 @@ public class ServiceDiscoveryManager {
|
|||
packetFilter = new PacketTypeFilter(DiscoverInfo.class);
|
||||
packetListener = new PacketListener() {
|
||||
public void processPacket(Packet packet) {
|
||||
Connection connection = ServiceDiscoveryManager.this.connection.get();
|
||||
Connection connection = connection();
|
||||
if (connection == null) return;
|
||||
DiscoverInfo discoverInfo = (DiscoverInfo) packet;
|
||||
// Answer the client's supported features if the request is of the GET type
|
||||
|
@ -530,16 +539,13 @@ public class ServiceDiscoveryManager {
|
|||
* @throws XMPPException if the operation failed for some reason.
|
||||
*/
|
||||
public DiscoverInfo discoverInfo(String entityID, String node) throws XMPPException {
|
||||
Connection connection = ServiceDiscoveryManager.this.connection.get();
|
||||
if (connection == null) throw new XMPPException("Connection instance already gc'ed");
|
||||
|
||||
// Discover the entity's info
|
||||
DiscoverInfo disco = new DiscoverInfo();
|
||||
disco.setType(IQ.Type.GET);
|
||||
disco.setTo(entityID);
|
||||
disco.setNode(node);
|
||||
|
||||
Packet result = connection.createPacketCollectorAndSend(disco).nextResultOrThrow();
|
||||
Packet result = connection().createPacketCollectorAndSend(disco).nextResultOrThrow();
|
||||
|
||||
return (DiscoverInfo) result;
|
||||
}
|
||||
|
@ -566,16 +572,13 @@ public class ServiceDiscoveryManager {
|
|||
* @throws XMPPException if the operation failed for some reason.
|
||||
*/
|
||||
public DiscoverItems discoverItems(String entityID, String node) throws XMPPException {
|
||||
Connection connection = ServiceDiscoveryManager.this.connection.get();
|
||||
if (connection == null) throw new XMPPException("Connection instance already gc'ed");
|
||||
|
||||
// Discover the entity's items
|
||||
DiscoverItems disco = new DiscoverItems();
|
||||
disco.setType(IQ.Type.GET);
|
||||
disco.setTo(entityID);
|
||||
disco.setNode(node);
|
||||
|
||||
Packet result = connection.createPacketCollectorAndSend(disco).nextResultOrThrow();
|
||||
Packet result = connection().createPacketCollectorAndSend(disco).nextResultOrThrow();
|
||||
return (DiscoverItems) result;
|
||||
}
|
||||
|
||||
|
@ -635,14 +638,11 @@ public class ServiceDiscoveryManager {
|
|||
*/
|
||||
public void publishItems(String entityID, String node, DiscoverItems discoverItems)
|
||||
throws XMPPException {
|
||||
Connection connection = ServiceDiscoveryManager.this.connection.get();
|
||||
if (connection == null) throw new XMPPException("Connection instance already gc'ed");
|
||||
|
||||
discoverItems.setType(IQ.Type.SET);
|
||||
discoverItems.setTo(entityID);
|
||||
discoverItems.setNode(node);
|
||||
|
||||
connection.createPacketCollectorAndSend(discoverItems).nextResultOrThrow();
|
||||
connection().createPacketCollectorAndSend(discoverItems).nextResultOrThrow();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,12 +17,12 @@
|
|||
|
||||
package org.jivesoftware.smackx.iqversion;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.filter.AndFilter;
|
||||
import org.jivesoftware.smack.filter.IQTypeFilter;
|
||||
|
@ -48,15 +48,14 @@ import org.jivesoftware.smackx.iqversion.packet.Version;
|
|||
*
|
||||
* @author Georg Lukas
|
||||
*/
|
||||
public class VersionManager {
|
||||
public class VersionManager extends Manager {
|
||||
private static final Map<Connection, VersionManager> instances =
|
||||
Collections.synchronizedMap(new WeakHashMap<Connection, VersionManager>());
|
||||
|
||||
private Version own_version;
|
||||
private WeakReference<Connection> weakRefConnection;
|
||||
|
||||
private VersionManager(final Connection connection) {
|
||||
this.weakRefConnection = new WeakReference<Connection>(connection);
|
||||
super(connection);
|
||||
instances.put(connection, this);
|
||||
|
||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
|
@ -73,7 +72,7 @@ public class VersionManager {
|
|||
Version reply = new Version(own_version);
|
||||
reply.setPacketID(packet.getPacketID());
|
||||
reply.setTo(packet.getFrom());
|
||||
weakRefConnection.get().sendPacket(reply);
|
||||
connection().sendPacket(reply);
|
||||
}
|
||||
}
|
||||
, new AndFilter(new PacketTypeFilter(Version.class), new IQTypeFilter(Type.GET)));
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.ping;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
@ -29,6 +28,7 @@ import java.util.logging.Logger;
|
|||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.ConnectionListener;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.SmackError;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
|
@ -39,7 +39,6 @@ import org.jivesoftware.smack.filter.PacketTypeFilter;
|
|||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.IQ.Type;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
||||
import org.jivesoftware.smackx.ping.packet.Ping;
|
||||
import org.jivesoftware.smackx.ping.packet.Pong;
|
||||
|
||||
|
@ -52,7 +51,7 @@ import org.jivesoftware.smackx.ping.packet.Pong;
|
|||
* @author Florian Schmaus
|
||||
* @see <a href="http://www.xmpp.org/extensions/xep-0199.html">XEP-0199:XMPP Ping</a>
|
||||
*/
|
||||
public class PingManager {
|
||||
public class PingManager extends Manager {
|
||||
public static final String NAMESPACE = "urn:xmpp:ping";
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(PingManager.class.getName());
|
||||
|
@ -118,10 +117,8 @@ public class PingManager {
|
|||
*/
|
||||
private long lastSuccessfulManualPing = -1;
|
||||
|
||||
private WeakReference<Connection> weakRefConnection;
|
||||
|
||||
private PingManager(Connection connection) {
|
||||
weakRefConnection = new WeakReference<Connection>(connection);
|
||||
super(connection);
|
||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
sdm.addFeature(PingManager.NAMESPACE);
|
||||
INSTANCES.put(connection, this);
|
||||
|
@ -130,9 +127,8 @@ public class PingManager {
|
|||
// Send a Pong for every Ping
|
||||
@Override
|
||||
public void processPacket(Packet packet) {
|
||||
Connection connection = weakRefConnection.get();
|
||||
Pong pong = new Pong(packet);
|
||||
connection.sendPacket(pong);
|
||||
connection().sendPacket(pong);
|
||||
}
|
||||
}, PING_PACKET_FILTER);
|
||||
connection.addConnectionListener(new ConnectionListener() {
|
||||
|
@ -170,12 +166,11 @@ public class PingManager {
|
|||
*/
|
||||
public boolean ping(String jid, long pingTimeout) {
|
||||
Ping ping = new Ping(jid);
|
||||
Connection connection = weakRefConnection.get();
|
||||
try {
|
||||
connection.createPacketCollectorAndSend(ping).nextResultOrThrow();
|
||||
connection().createPacketCollectorAndSend(ping).nextResultOrThrow();
|
||||
}
|
||||
catch (XMPPException exc) {
|
||||
return (jid.equals(connection.getServiceName()) && (exc.getSmackError() != SmackError.NO_RESPONSE_FROM_SERVER));
|
||||
return (jid.equals(connection().getServiceName()) && (exc.getSmackError() != SmackError.NO_RESPONSE_FROM_SERVER));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -188,8 +183,7 @@ public class PingManager {
|
|||
* @return true if a reply was received from the entity, false otherwise.
|
||||
*/
|
||||
public boolean ping(String jid) {
|
||||
Connection connection = weakRefConnection.get();
|
||||
return ping(jid, connection.getPacketReplyTimeout());
|
||||
return ping(jid, connection().getPacketReplyTimeout());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -200,9 +194,7 @@ public class PingManager {
|
|||
* @throws XMPPException An XMPP related error occurred during the request
|
||||
*/
|
||||
public boolean isPingSupported(String jid) throws XMPPException {
|
||||
Connection connection = weakRefConnection.get();
|
||||
DiscoverInfo result = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(jid);
|
||||
return result.containsFeature(PingManager.NAMESPACE);
|
||||
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, PingManager.NAMESPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -229,8 +221,7 @@ public class PingManager {
|
|||
* @return
|
||||
*/
|
||||
public boolean pingMyServer(boolean notifyListeners) {
|
||||
Connection connection = weakRefConnection.get();
|
||||
boolean res = ping(connection.getServiceName());
|
||||
boolean res = ping(connection().getServiceName());
|
||||
if (res) {
|
||||
pongReceived();
|
||||
} else if (notifyListeners) {
|
||||
|
@ -296,8 +287,7 @@ public class PingManager {
|
|||
maybeStopPingServerTask();
|
||||
if (pingInterval > 0) {
|
||||
LOGGER.fine("Scheduling ServerPingTask in " + pingInterval + " seconds");
|
||||
Connection connection = weakRefConnection.get();
|
||||
nextAutomaticPing = connection.schedule(pingServerRunnable, pingInterval, TimeUnit.SECONDS);
|
||||
nextAutomaticPing = connection().schedule(pingServerRunnable, pingInterval, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -318,7 +308,7 @@ public class PingManager {
|
|||
|
||||
public void run() {
|
||||
LOGGER.fine("ServerPingTask run()");
|
||||
Connection connection = weakRefConnection.get();
|
||||
Connection connection = connection();
|
||||
if (connection == null) {
|
||||
// connection has been collected by GC
|
||||
// which means we can stop the thread by breaking the loop
|
||||
|
|
|
@ -16,8 +16,16 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.privacy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.filter.*;
|
||||
|
@ -26,9 +34,6 @@ import org.jivesoftware.smack.packet.Packet;
|
|||
import org.jivesoftware.smackx.privacy.packet.Privacy;
|
||||
import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* A PrivacyListManager is used by XMPP clients to block or allow communications from other
|
||||
* users. Use the manager to: <ul>
|
||||
|
@ -42,13 +47,12 @@ import java.util.*;
|
|||
*
|
||||
* @author Francisco Vives
|
||||
*/
|
||||
public class PrivacyListManager {
|
||||
public class PrivacyListManager extends Manager {
|
||||
|
||||
// Keep the list of instances of this class.
|
||||
private static Map<Connection, PrivacyListManager> instances = Collections
|
||||
.synchronizedMap(new WeakHashMap<Connection, PrivacyListManager>());
|
||||
|
||||
private WeakReference<Connection> connection;
|
||||
private final List<PrivacyListListener> listeners = new ArrayList<PrivacyListListener>();
|
||||
PacketFilter packetFilter = new AndFilter(new IQTypeFilter(IQ.Type.SET),
|
||||
new PacketExtensionFilter("query", "jabber:iq:privacy"));
|
||||
|
@ -71,7 +75,7 @@ public class PrivacyListManager {
|
|||
* @param connection the XMPP connection.
|
||||
*/
|
||||
private PrivacyListManager(final Connection connection) {
|
||||
this.connection = new WeakReference<Connection>(connection);
|
||||
super(connection);
|
||||
// Register the new instance and associate it with the connection
|
||||
instances.put(connection, this);
|
||||
|
||||
|
@ -121,7 +125,7 @@ public class PrivacyListManager {
|
|||
* @return the userJID that owns the privacy
|
||||
*/
|
||||
private String getUser() {
|
||||
return connection.get().getUser();
|
||||
return connection().getUser();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -146,13 +150,11 @@ public class PrivacyListManager {
|
|||
* @exception XMPPException if the request or the answer failed, it raises an exception.
|
||||
*/
|
||||
private Privacy getRequest(Privacy requestPrivacy) throws XMPPException {
|
||||
Connection connection = PrivacyListManager.this.connection.get();
|
||||
if (connection == null) throw new XMPPException("Connection instance already gc'ed");
|
||||
// The request is a get iq type
|
||||
requestPrivacy.setType(Privacy.Type.GET);
|
||||
requestPrivacy.setFrom(this.getUser());
|
||||
|
||||
Privacy privacyAnswer = (Privacy) connection.createPacketCollectorAndSend(requestPrivacy).nextResultOrThrow();
|
||||
Privacy privacyAnswer = (Privacy) connection().createPacketCollectorAndSend(requestPrivacy).nextResultOrThrow();
|
||||
return privacyAnswer;
|
||||
}
|
||||
|
||||
|
@ -166,14 +168,11 @@ public class PrivacyListManager {
|
|||
* @exception XMPPException if the request or the answer failed, it raises an exception.
|
||||
*/
|
||||
private Packet setRequest(Privacy requestPrivacy) throws XMPPException {
|
||||
Connection connection = PrivacyListManager.this.connection.get();
|
||||
if (connection == null)
|
||||
throw new XMPPException("Connection instance already gc'ed");
|
||||
// The request is a get iq type
|
||||
requestPrivacy.setType(Privacy.Type.SET);
|
||||
requestPrivacy.setFrom(this.getUser());
|
||||
|
||||
return connection.createPacketCollectorAndSend(requestPrivacy).nextResultOrThrow();
|
||||
return connection().createPacketCollectorAndSend(requestPrivacy).nextResultOrThrow();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.receipts;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
@ -25,13 +24,13 @@ import java.util.WeakHashMap;
|
|||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.Manager;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.filter.PacketExtensionFilter;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
||||
|
||||
/**
|
||||
* Manager for XEP-0184: Message Delivery Receipts. This class implements
|
||||
|
@ -40,7 +39,7 @@ import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
|||
*
|
||||
* @author Georg Lukas
|
||||
*/
|
||||
public class DeliveryReceiptManager implements PacketListener {
|
||||
public class DeliveryReceiptManager extends Manager implements PacketListener {
|
||||
|
||||
private static Map<Connection, DeliveryReceiptManager> instances =
|
||||
Collections.synchronizedMap(new WeakHashMap<Connection, DeliveryReceiptManager>());
|
||||
|
@ -53,15 +52,14 @@ public class DeliveryReceiptManager implements PacketListener {
|
|||
});
|
||||
}
|
||||
|
||||
private WeakReference<Connection> weakRefConnection;
|
||||
private boolean auto_receipts_enabled = false;
|
||||
private Set<ReceiptReceivedListener> receiptReceivedListeners = Collections
|
||||
.synchronizedSet(new HashSet<ReceiptReceivedListener>());
|
||||
|
||||
private DeliveryReceiptManager(Connection connection) {
|
||||
super(connection);
|
||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
sdm.addFeature(DeliveryReceipt.NAMESPACE);
|
||||
weakRefConnection = new WeakReference<Connection>(connection);
|
||||
instances.put(connection, this);
|
||||
|
||||
// register listener for delivery receipts and requests
|
||||
|
@ -92,11 +90,9 @@ public class DeliveryReceiptManager implements PacketListener {
|
|||
* @return true if supported
|
||||
*/
|
||||
public boolean isSupported(String jid) {
|
||||
Connection connection = weakRefConnection.get();
|
||||
try {
|
||||
DiscoverInfo result =
|
||||
ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(jid);
|
||||
return result.containsFeature(DeliveryReceipt.NAMESPACE);
|
||||
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid,
|
||||
DeliveryReceipt.NAMESPACE);
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
return false;
|
||||
|
@ -121,7 +117,7 @@ public class DeliveryReceiptManager implements PacketListener {
|
|||
DeliveryReceiptRequest drr = (DeliveryReceiptRequest)packet.getExtension(
|
||||
DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE);
|
||||
if (drr != null) {
|
||||
Connection connection = weakRefConnection.get();
|
||||
Connection connection = connection();
|
||||
Message ack = new Message(packet.getFrom(), Message.Type.normal);
|
||||
ack.addExtension(new DeliveryReceipt(packet.getPacketID()));
|
||||
connection.sendPacket(ack);
|
||||
|
|
Loading…
Reference in a new issue