1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 14:02:06 +01:00

Use WeakReference in MUC NodeInformationProvider

MultiUserChat's NodeInformationProvider must not hold a strong reference
to the Connection instance. Fixes SMACK-540.
This commit is contained in:
Florian Schmaus 2014-02-05 20:04:13 +01:00
parent 1df3841a41
commit 68a570b4fe

View file

@ -27,6 +27,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.WeakHashMap; import java.util.WeakHashMap;
@ -116,12 +117,16 @@ public class MultiUserChat {
// Chat protocol. This information will be used when another client tries to // Chat protocol. This information will be used when another client tries to
// discover whether this client supports MUC or not. // discover whether this client supports MUC or not.
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(discoNamespace); ServiceDiscoveryManager.getInstanceFor(connection).addFeature(discoNamespace);
// Set the NodeInformationProvider that will provide information about the // Set the NodeInformationProvider that will provide information about the
// joined rooms whenever a disco request is received // joined rooms whenever a disco request is received
final WeakReference<Connection> weakRefConnection = new WeakReference<Connection>(connection);
ServiceDiscoveryManager.getInstanceFor(connection).setNodeInformationProvider( ServiceDiscoveryManager.getInstanceFor(connection).setNodeInformationProvider(
discoNode, discoNode,
new NodeInformationProvider() { new NodeInformationProvider() {
public List<DiscoverItems.Item> getNodeItems() { public List<DiscoverItems.Item> getNodeItems() {
Connection connection = weakRefConnection.get();
if (connection == null) return new LinkedList<DiscoverItems.Item>();
List<DiscoverItems.Item> answer = new ArrayList<DiscoverItems.Item>(); List<DiscoverItems.Item> answer = new ArrayList<DiscoverItems.Item>();
Iterator<String> rooms=MultiUserChat.getJoinedRooms(connection); Iterator<String> rooms=MultiUserChat.getJoinedRooms(connection);
while (rooms.hasNext()) { while (rooms.hasNext()) {