From 6a43bab4f53862845dbc567945f4a02d2b2951b2 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sat, 10 Jan 2015 01:23:20 +0100 Subject: [PATCH] Remove protected getConnectionListeners() Also remove reconnectionFailed() from XMPPBOSHConnection, only ReconnectionManager should call it. Add and fix javadoc of ConnectionListener. --- .../smack/bosh/XMPPBOSHConnection.java | 15 +++--- .../smack/AbstractXMPPConnection.java | 19 ++----- .../smack/ConnectionListener.java | 50 ++++++++++++------- .../jivesoftware/smack/DummyConnection.java | 4 +- 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java b/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java index 7e7980ee8..c4916d5fc 100644 --- a/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java +++ b/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java @@ -34,7 +34,6 @@ import org.jivesoftware.smack.SmackException.ConnectionException; import org.jivesoftware.smack.XMPPException.StreamErrorException; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.ConnectionCreationListener; -import org.jivesoftware.smack.ConnectionListener; import org.jivesoftware.smack.Roster; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.Element; @@ -475,17 +474,15 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection { } } else { - try { if (wasAuthenticated) { - login(); + try { + login(); + } + catch (Exception e) { + throw new RuntimeException(e); + } } notifyReconnection(); - } - catch (Exception e) { - for (ConnectionListener listener : getConnectionListeners()) { - listener.reconnectionFailed(e); - } - } } } else { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java index 552936690..1bd737f22 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java @@ -679,15 +679,6 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { connectionListeners.remove(connectionListener); } - /** - * Get the collection of listeners that are interested in connection events. - * - * @return a collection of listeners interested on connection events. - */ - protected Collection getConnectionListeners() { - return connectionListeners; - } - @Override public PacketCollector createPacketCollectorAndSend(IQ packet) throws NotConnectedException { PacketFilter packetFilter = new IQReplyFilter(packet, this); @@ -1116,13 +1107,13 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { } protected void callConnectionConnectedListener() { - for (ConnectionListener listener : getConnectionListeners()) { + for (ConnectionListener listener : connectionListeners) { listener.connected(this); } } protected void callConnectionAuthenticatedListener(boolean resumed) { - for (ConnectionListener listener : getConnectionListeners()) { + for (ConnectionListener listener : connectionListeners) { try { listener.authenticated(this, resumed); } catch (Exception e) { @@ -1134,7 +1125,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { } void callConnectionClosedListener() { - for (ConnectionListener listener : getConnectionListeners()) { + for (ConnectionListener listener : connectionListeners) { try { listener.connectionClosed(); } @@ -1148,7 +1139,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { protected void callConnectionClosedOnErrorListener(Exception e) { LOGGER.log(Level.WARNING, "Connection closed with error", e); - for (ConnectionListener listener : getConnectionListeners()) { + for (ConnectionListener listener : connectionListeners) { try { listener.connectionClosedOnError(e); } @@ -1165,7 +1156,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { */ protected void notifyReconnection() { // Notify connection listeners of the reconnection. - for (ConnectionListener listener : getConnectionListeners()) { + for (ConnectionListener listener : connectionListeners) { try { listener.reconnectionSuccessful(); } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/ConnectionListener.java b/smack-core/src/main/java/org/jivesoftware/smack/ConnectionListener.java index 08da787ff..89fce66e4 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/ConnectionListener.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/ConnectionListener.java @@ -29,20 +29,26 @@ package org.jivesoftware.smack; public interface ConnectionListener { /** - * TODO - * @param connection + * Notification that the connection has been successfully connected to the remote endpoint (e.g. the XMPP server). + *

+ * Note that the connection is likely not yet authenticated and therefore only limited operations like registering + * an account may be possible. + *

+ * + * @param connection the XMPPConnection which successfully connected to its endpoint. */ public void connected(XMPPConnection connection); /** - * TODO - * @param connection + * Notification that the connection has been authenticated. + * + * @param connection the XMPPConnection which successfully authenticated. + * @param resumed true if a previous XMPP session's stream was resumed. */ public void authenticated(XMPPConnection connection, boolean resumed); /** - * Notification that the connection was closed normally or that the reconnection - * process has been aborted. + * Notification that the connection was closed normally. */ public void connectionClosed(); @@ -54,23 +60,33 @@ public interface ConnectionListener { * @param e the exception. */ public void connectionClosedOnError(Exception e); - - /** - * The connection will retry to reconnect in the specified number of seconds. - * - * @param seconds remaining seconds before attempting a reconnection. - */ - public void reconnectingIn(int seconds); - + /** * The connection has reconnected successfully to the server. Connections will * reconnect to the server when the previous socket connection was abruptly closed. */ public void reconnectionSuccessful(); - + + // The next two methods *must* only be invoked by ReconnectionManager + /** - * An attempt to connect to the server has failed. The connection will keep trying - * reconnecting to the server in a moment. + * The connection will retry to reconnect in the specified number of seconds. + *

+ * Note: This method is only called if {@link ReconnectionManager#isAutomaticReconnectEnabled()} returns true, i.e. + * only when the reconnection manager is enabled for the connection. + *

+ * + * @param seconds remaining seconds before attempting a reconnection. + */ + public void reconnectingIn(int seconds); + + /** + * An attempt to connect to the server has failed. The connection will keep trying reconnecting to the server in a + * moment. + *

+ * Note: This method is only called if {@link ReconnectionManager#isAutomaticReconnectEnabled()} returns true, i.e. + * only when the reconnection manager is enabled for the connection. + *

* * @param e the exception that caused the reconnection to fail. */ diff --git a/smack-core/src/test/java/org/jivesoftware/smack/DummyConnection.java b/smack-core/src/test/java/org/jivesoftware/smack/DummyConnection.java index cd4407b39..04dfbc7e7 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/DummyConnection.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/DummyConnection.java @@ -92,9 +92,7 @@ public class DummyConnection extends AbstractXMPPConnection { roster = null; authenticated = false; - for (ConnectionListener listener : getConnectionListeners()) { - listener.connectionClosed(); - } + callConnectionClosedListener(); reconnect = true; }