From 49573866ab1f841f036a16fe53f404780662c5b8 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 3 Nov 2013 21:49:58 +0000 Subject: [PATCH] SMACK-462 Use getInstace() ConnectionCreationListener Prevent duplicate instances of a manager by using it's getInstance() method instead of the manager's constructor. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_2@13800 b35dd754-fafc-0310-a699-88a17e54d16e --- source/org/jivesoftware/smack/PrivacyListManager.java | 8 +++++--- .../jivesoftware/smack/keepalive/KeepAliveManager.java | 2 +- .../jivesoftware/smackx/commands/AdHocCommandManager.java | 2 +- source/org/jivesoftware/smackx/ping/PingManager.java | 2 +- .../smackx/receipts/DeliveryReceiptManager.java | 4 ++-- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/source/org/jivesoftware/smack/PrivacyListManager.java b/source/org/jivesoftware/smack/PrivacyListManager.java index 88d9aed83..32d039018 100644 --- a/source/org/jivesoftware/smack/PrivacyListManager.java +++ b/source/org/jivesoftware/smack/PrivacyListManager.java @@ -58,7 +58,7 @@ public class PrivacyListManager { // instance when the connection is closed. Connection.addConnectionCreationListener(new ConnectionCreationListener() { public void connectionCreated(Connection connection) { - new PrivacyListManager(connection); + getInstanceFor(connection); } }); } @@ -129,8 +129,10 @@ public class PrivacyListManager { * @param connection the connection used to look for the proper PrivacyListManager. * @return the PrivacyListManager associated with a given Connection. */ - public static PrivacyListManager getInstanceFor(Connection connection) { - return instances.get(connection); + public static synchronized PrivacyListManager getInstanceFor(Connection connection) { + PrivacyListManager plm = instances.get(connection); + if (plm == null) plm = new PrivacyListManager(connection); + return plm; } /** diff --git a/source/org/jivesoftware/smack/keepalive/KeepAliveManager.java b/source/org/jivesoftware/smack/keepalive/KeepAliveManager.java index a3cb4475f..254a0419b 100644 --- a/source/org/jivesoftware/smack/keepalive/KeepAliveManager.java +++ b/source/org/jivesoftware/smack/keepalive/KeepAliveManager.java @@ -59,7 +59,7 @@ public class KeepAliveManager { if (SmackConfiguration.getKeepAliveInterval() > 0) { Connection.addConnectionCreationListener(new ConnectionCreationListener() { public void connectionCreated(Connection connection) { - new KeepAliveManager(connection); + getInstanceFor(connection); } }); } diff --git a/source/org/jivesoftware/smackx/commands/AdHocCommandManager.java b/source/org/jivesoftware/smackx/commands/AdHocCommandManager.java index 71698e311..28401d67d 100755 --- a/source/org/jivesoftware/smackx/commands/AdHocCommandManager.java +++ b/source/org/jivesoftware/smackx/commands/AdHocCommandManager.java @@ -83,7 +83,7 @@ public class AdHocCommandManager { static { Connection.addConnectionCreationListener(new ConnectionCreationListener() { public void connectionCreated(Connection connection) { - new AdHocCommandManager(connection); + getAddHocCommandsManager(connection); } }); } diff --git a/source/org/jivesoftware/smackx/ping/PingManager.java b/source/org/jivesoftware/smackx/ping/PingManager.java index 6882545fd..068206698 100644 --- a/source/org/jivesoftware/smackx/ping/PingManager.java +++ b/source/org/jivesoftware/smackx/ping/PingManager.java @@ -60,7 +60,7 @@ public class PingManager { static { Connection.addConnectionCreationListener(new ConnectionCreationListener() { public void connectionCreated(Connection connection) { - new PingManager(connection); + getInstanceFor(connection); } }); } diff --git a/source/org/jivesoftware/smackx/receipts/DeliveryReceiptManager.java b/source/org/jivesoftware/smackx/receipts/DeliveryReceiptManager.java index 125b87e63..0b4a6aae6 100644 --- a/source/org/jivesoftware/smackx/receipts/DeliveryReceiptManager.java +++ b/source/org/jivesoftware/smackx/receipts/DeliveryReceiptManager.java @@ -47,7 +47,7 @@ public class DeliveryReceiptManager implements PacketListener { static { Connection.addConnectionCreationListener(new ConnectionCreationListener() { public void connectionCreated(Connection connection) { - new DeliveryReceiptManager(connection); + getInstanceFor(connection); } }); } @@ -74,7 +74,7 @@ public class DeliveryReceiptManager implements PacketListener { * * @return the DeliveryReceiptManager instance for the given connection */ - synchronized public static DeliveryReceiptManager getInstanceFor(Connection connection) { + public static synchronized DeliveryReceiptManager getInstanceFor(Connection connection) { DeliveryReceiptManager receiptManager = instances.get(connection); if (receiptManager == null) {