From a592a12229fa5448c3c52f9ae802220c536efd85 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 15 Mar 2017 15:07:41 +0100 Subject: [PATCH] Make InBandBytestreamManager use weak references to XMPPConnection --- .../ibb/InBandBytestreamManager.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManager.java index e8f28404b..2a2b5d0c3 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManager.java @@ -17,15 +17,16 @@ package org.jivesoftware.smackx.bytestreams.ibb; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Random; +import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; import org.jivesoftware.smack.AbstractConnectionClosedListener; import org.jivesoftware.smack.ConnectionCreationListener; +import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; @@ -79,7 +80,7 @@ import org.jxmpp.jid.Jid; * * @author Henning Staib */ -public final class InBandBytestreamManager implements BytestreamManager { +public final class InBandBytestreamManager extends Manager implements BytestreamManager { /** * Stanzas that can be used to encapsulate In-Band Bytestream data packets. @@ -140,10 +141,7 @@ public final class InBandBytestreamManager implements BytestreamManager { private final static Random randomGenerator = new Random(); /* stores one InBandBytestreamManager for each XMPP connection */ - private final static Map managers = new HashMap(); - - /* XMPP connection */ - private final XMPPConnection connection; + private final static Map managers = new WeakHashMap<>(); /* * assigns a user to a listener that is informed if an In-Band Bytestream request for this user @@ -208,7 +206,7 @@ public final class InBandBytestreamManager implements BytestreamManager { * @param connection the XMPP connection */ private InBandBytestreamManager(XMPPConnection connection) { - this.connection = connection; + super(connection); // register bytestream open packet listener this.initiationListener = new InitiationListener(this); @@ -434,11 +432,13 @@ public final class InBandBytestreamManager implements BytestreamManager { Open byteStreamRequest = new Open(sessionID, this.defaultBlockSize, this.stanza); byteStreamRequest.setTo(targetJID); + final XMPPConnection connection = connection(); + // sending packet will throw exception on timeout or error reply connection.createStanzaCollectorAndSend(byteStreamRequest).nextResultOrThrow(); InBandBytestreamSession inBandBytestreamSession = new InBandBytestreamSession( - this.connection, byteStreamRequest, targetJID); + connection, byteStreamRequest, targetJID); this.sessions.put(sessionID, inBandBytestreamSession); return inBandBytestreamSession; @@ -454,7 +454,7 @@ public final class InBandBytestreamManager implements BytestreamManager { */ protected void replyRejectPacket(IQ request) throws NotConnectedException, InterruptedException { IQ error = IQ.createErrorResponse(request, XMPPError.Condition.not_acceptable); - this.connection.sendStanza(error); + connection().sendStanza(error); } /** @@ -467,7 +467,7 @@ public final class InBandBytestreamManager implements BytestreamManager { */ protected void replyResourceConstraintPacket(IQ request) throws NotConnectedException, InterruptedException { IQ error = IQ.createErrorResponse(request, XMPPError.Condition.resource_constraint); - this.connection.sendStanza(error); + connection().sendStanza(error); } /** @@ -480,7 +480,7 @@ public final class InBandBytestreamManager implements BytestreamManager { */ protected void replyItemNotFoundPacket(IQ request) throws NotConnectedException, InterruptedException { IQ error = IQ.createErrorResponse(request, XMPPError.Condition.item_not_found); - this.connection.sendStanza(error); + connection().sendStanza(error); } /** @@ -501,7 +501,7 @@ public final class InBandBytestreamManager implements BytestreamManager { * @return the XMPP connection */ protected XMPPConnection getConnection() { - return this.connection; + return connection(); } /** @@ -548,6 +548,7 @@ public final class InBandBytestreamManager implements BytestreamManager { * internal status, which includes removing this instance from the managers map. */ private void disableService() { + final XMPPConnection connection = connection(); // remove manager from static managers map managers.remove(connection);