1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02:00

Rework Smack debugger.

Also fixes SMACK-728.
This commit is contained in:
Florian Schmaus 2017-07-28 11:59:11 +02:00
parent 104146c5ed
commit b8ee8d808f
24 changed files with 328 additions and 390 deletions

View file

@ -54,6 +54,7 @@ import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.compress.packet.Compress;
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
import org.jivesoftware.smack.debugger.SmackDebugger;
import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
import org.jivesoftware.smack.filter.IQReplyFilter;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.filter.StanzaIdFilter;
@ -178,7 +179,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
/**
* The SmackDebugger allows to log and debug XML traffic.
*/
protected SmackDebugger debugger = null;
protected final SmackDebugger debugger;
/**
* The Reader which is used for the debugger.
@ -301,6 +302,12 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
protected AbstractXMPPConnection(ConnectionConfiguration configuration) {
saslAuthentication = new SASLAuthentication(this, configuration);
config = configuration;
SmackDebuggerFactory debuggerFactory = configuration.getDebuggerFactory();
if (debuggerFactory != null) {
debugger = debuggerFactory.create(this);
} else {
debugger = null;
}
// Notify listeners that a new connection has been established
for (ConnectionCreationListener listener : XMPPConnectionRegistry.getConnectionCreationListeners()) {
listener.connectionCreated(this);
@ -568,7 +575,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
// name we are now logged-in as.
// If DEBUG was set to true AFTER the connection was created the debugger
// will be null
if (config.isDebuggerEnabled() && debugger != null) {
if (debugger != null) {
debugger.userHasLogged(user);
}
callConnectionAuthenticatedListener(resumed);
@ -872,6 +879,11 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
*/
@SuppressWarnings("javadoc")
protected void firePacketSendingListeners(final Stanza packet) {
final SmackDebugger debugger = this.debugger;
if (debugger != null) {
debugger.onOutgoingStreamElement(packet);
}
final List<StanzaListener> listenersToNotify = new LinkedList<StanzaListener>();
synchronized (sendListeners) {
for (ListenerWrapper listenerWrapper : sendListeners.values()) {
@ -957,18 +969,10 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
throw new NullPointerException("Reader or writer isn't initialized.");
}
// If debugging is enabled, we open a window and write out all network traffic.
if (config.isDebuggerEnabled()) {
if (debugger == null) {
debugger = SmackConfiguration.createDebugger(this, writer, reader);
}
if (debugger == null) {
LOGGER.severe("Debugging enabled but could not find debugger class");
} else {
// Obtain new reader and writer from the existing debugger
reader = debugger.newConnectionReader(reader);
writer = debugger.newConnectionWriter(writer);
}
if (debugger != null) {
// Obtain new reader and writer from the existing debugger
reader = debugger.newConnectionReader(reader);
writer = debugger.newConnectionWriter(writer);
}
}
@ -1072,6 +1076,12 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
*/
protected void processStanza(final Stanza stanza) throws InterruptedException {
assert (stanza != null);
final SmackDebugger debugger = this.debugger;
if (debugger != null) {
debugger.onIncomingStreamElement(stanza);
}
lastStanzaReceived = System.currentTimeMillis();
// Deliver the incoming packet to listeners.
executorService.executeBlocking(new Runnable() {

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2003-2007 Jive Software.
* Copyright 2003-2007 Jive Software, 2017 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,6 +30,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import javax.security.auth.callback.CallbackHandler;
import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
import org.jivesoftware.smack.packet.Session;
import org.jivesoftware.smack.proxy.ProxyInfo;
import org.jivesoftware.smack.sasl.SASLMechanism;
@ -78,7 +79,7 @@ public abstract class ConnectionConfiguration {
*/
private final CallbackHandler callbackHandler;
private final boolean debuggerEnabled;
private final SmackDebuggerFactory debuggerFactory;
// Holds the socket factory that is used to generate the socket in the connection
private final SocketFactory socketFactory;
@ -158,7 +159,7 @@ public abstract class ConnectionConfiguration {
hostnameVerifier = builder.hostnameVerifier;
sendPresence = builder.sendPresence;
legacySessionDisabled = builder.legacySessionDisabled;
debuggerEnabled = builder.debuggerEnabled;
debuggerFactory = builder.debuggerFactory;
allowNullOrEmptyUsername = builder.allowEmptyOrNullUsername;
enabledSaslMechanisms = builder.enabledSaslMechanisms;
@ -281,13 +282,12 @@ public abstract class ConnectionConfiguration {
}
/**
* Returns true if the new connection about to be establish is going to be debugged. By
* default the value of {@link SmackConfiguration#DEBUG} is used.
* Returns the Smack debugger factory.
*
* @return true if the new connection about to be establish is going to be debugged.
* @return the Smack debugger factory or <code>null</code>
*/
public boolean isDebuggerEnabled() {
return debuggerEnabled;
public SmackDebuggerFactory getDebuggerFactory() {
return debuggerFactory;
}
/**
@ -519,7 +519,7 @@ public abstract class ConnectionConfiguration {
private boolean legacySessionDisabled = false;
private ProxyInfo proxy;
private CallbackHandler callbackHandler;
private boolean debuggerEnabled = SmackConfiguration.DEBUG;
private SmackDebuggerFactory debuggerFactory;
private SocketFactory socketFactory;
private DomainBareJid xmppServiceDomain;
private InetAddress hostAddress;
@ -531,6 +531,9 @@ public abstract class ConnectionConfiguration {
private X509TrustManager customX509TrustManager;
protected Builder() {
if (SmackConfiguration.DEBUG) {
enableDefaultDebugger();
}
}
/**
@ -808,15 +811,20 @@ public abstract class ConnectionConfiguration {
return getThis();
}
public B enableDefaultDebugger() {
this.debuggerFactory = SmackConfiguration.getDefaultSmackDebuggerFactory();
assert this.debuggerFactory != null;
return getThis();
}
/**
* Sets if the new connection about to be establish is going to be debugged. By
* default the value of {@link SmackConfiguration#DEBUG} is used.
*
* @param debuggerEnabled if the new connection about to be establish is going to be debugged.
* Set the Smack debugger factory used to construct Smack debuggers.
*
* @param debuggerFactory the Smack debugger factory.
* @return a reference to this builder.
*/
public B setDebuggerEnabled(boolean debuggerEnabled) {
this.debuggerEnabled = debuggerEnabled;
public B setDebuggerFactory(SmackDebuggerFactory debuggerFactory) {
this.debuggerFactory = debuggerFactory;
return getThis();
}
@ -965,4 +973,5 @@ public abstract class ConnectionConfiguration {
protected abstract B getThis();
}
}

View file

@ -17,8 +17,6 @@
package org.jivesoftware.smack;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -30,7 +28,6 @@ import javax.net.ssl.HostnameVerifier;
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
import org.jivesoftware.smack.debugger.ReflectionDebuggerFactory;
import org.jivesoftware.smack.debugger.SmackDebugger;
import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
import org.jivesoftware.smack.parsing.ExceptionThrowingCallback;
import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
@ -63,8 +60,6 @@ public final class SmackConfiguration {
static boolean smackInitialized = false;
private static SmackDebuggerFactory debuggerFactory = new ReflectionDebuggerFactory();
/**
* Value that indicates whether debugging is enabled. When enabled, a debug
* window will apear for each new connection that will contain the following
@ -80,6 +75,8 @@ public final class SmackConfiguration {
*/
public static boolean DEBUG = false;
private static SmackDebuggerFactory DEFAULT_DEBUGGER_FACTORY = ReflectionDebuggerFactory.INSTANCE;
/**
* The default parsing exception callback is {@link ExceptionThrowingCallback} which will
* throw an exception and therefore disconnect the active connection.
@ -148,6 +145,14 @@ public final class SmackConfiguration {
defaultPacketReplyTimeout = timeout;
}
public static void setDefaultSmackDebuggerFactory(SmackDebuggerFactory debuggerFactory) {
DEFAULT_DEBUGGER_FACTORY = Objects.requireNonNull(debuggerFactory, "Debugger factory must not be null");
}
public static SmackDebuggerFactory getDefaultSmackDebuggerFactory() {
return DEFAULT_DEBUGGER_FACTORY;
}
/**
* Gets the default max size of a stanza(/packet) collector before it will delete
* the older packets.
@ -190,43 +195,6 @@ public final class SmackConfiguration {
}
}
/**
* Sets Smack debugger factory.
*
* @param debuggerFactory new debugger factory implementation to be used by Smack
*/
public static void setDebuggerFactory(SmackDebuggerFactory debuggerFactory) {
SmackConfiguration.debuggerFactory = debuggerFactory;
}
/**
* Get the debugger factory.
*
* @return a debugger factory or <code>null</code>
*/
public static SmackDebuggerFactory getDebuggerFactory() {
return debuggerFactory;
}
/**
* Creates new debugger instance with given arguments as parameters. May
* return <code>null</code> if no DebuggerFactory is set or if the factory
* did not produce a debugger.
*
* @param connection
* @param writer
* @param reader
* @return a new debugger or <code>null</code>
*/
public static SmackDebugger createDebugger(XMPPConnection connection, Writer writer, Reader reader) {
SmackDebuggerFactory factory = getDebuggerFactory();
if (factory == null) {
return null;
} else {
return factory.create(connection, writer, reader);
}
}
/**
* Remove a SASL mechanism from the list to be used.
*

View file

@ -20,9 +20,8 @@ import java.io.Reader;
import java.io.Writer;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.TopLevelStreamElement;
import org.jivesoftware.smack.util.ObservableReader;
import org.jivesoftware.smack.util.ObservableWriter;
import org.jivesoftware.smack.util.ReaderListener;
@ -30,13 +29,10 @@ import org.jivesoftware.smack.util.WriterListener;
import org.jxmpp.jid.EntityFullJid;
public abstract class AbstractDebugger implements SmackDebugger {
public abstract class AbstractDebugger extends SmackDebugger {
public static boolean printInterpreted = false;
private final XMPPConnection connection;
private final StanzaListener listener;
private final ConnectionListener connListener;
private final ReaderListener readerListener;
private final WriterListener writerListener;
@ -44,8 +40,8 @@ public abstract class AbstractDebugger implements SmackDebugger {
private ObservableWriter writer;
private ObservableReader reader;
public AbstractDebugger(final XMPPConnection connection, Writer writer, Reader reader) {
this.connection = connection;
public AbstractDebugger(final XMPPConnection connection) {
super(connection);
// Create a special Reader that wraps the main Reader and logs data to the GUI.
this.reader = new ObservableReader(reader);
@ -67,18 +63,6 @@ public abstract class AbstractDebugger implements SmackDebugger {
};
this.writer.addWriterListener(writerListener);
// Create a thread that will listen for all incoming packets and write them to
// the GUI. This is what we call "interpreted" packet data, since it's the packet
// data as Smack sees it and not as it's coming in as raw XML.
listener = new StanzaListener() {
@Override
public void processStanza(Stanza packet) {
if (printInterpreted) {
log("RCV PKT (" + connection.getConnectionCounter() + "): " + packet.toXML());
}
}
};
connListener = new ConnectionListener() {
@Override
public void connected(XMPPConnection connection) {
@ -173,22 +157,15 @@ public abstract class AbstractDebugger implements SmackDebugger {
}
@Override
public Reader getReader() {
return reader;
public void onIncomingStreamElement(TopLevelStreamElement streamElement) {
if (printInterpreted) {
log("RCV PKT (" + connection.getConnectionCounter() + "): " + streamElement.toXML());
}
}
@Override
public Writer getWriter() {
return writer;
public void onOutgoingStreamElement(TopLevelStreamElement streamElement) {
// Does nothing (yet).
}
@Override
public StanzaListener getReaderListener() {
return listener;
}
@Override
public StanzaListener getWriterListener() {
return null;
}
}

View file

@ -17,9 +17,7 @@
package org.jivesoftware.smack.debugger;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -39,8 +37,8 @@ import org.jivesoftware.smack.XMPPConnection;
public class ConsoleDebugger extends AbstractDebugger {
private final SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm:ss");
public ConsoleDebugger(XMPPConnection connection, Writer writer, Reader reader) {
super(connection, writer, reader);
public ConsoleDebugger(XMPPConnection connection) {
super(connection);
}
@Override
@ -64,4 +62,17 @@ public class ConsoleDebugger extends AbstractDebugger {
log(logMessage + sw);
}
public static final class Factory implements SmackDebuggerFactory {
public static final SmackDebuggerFactory INSTANCE = new Factory();
private Factory() {
}
@Override
public SmackDebugger create(XMPPConnection connection) throws IllegalArgumentException {
return new ConsoleDebugger(connection);
}
}
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2014 Florian Schmaus
* Copyright 2014-2017 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,8 +16,6 @@
*/
package org.jivesoftware.smack.debugger;
import java.io.Reader;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -38,8 +36,8 @@ public class JulDebugger extends AbstractDebugger {
private static final Logger LOGGER = Logger.getLogger(JulDebugger.class.getName());
public JulDebugger(XMPPConnection connection, Writer writer, Reader reader) {
super(connection, writer, reader);
public JulDebugger(XMPPConnection connection) {
super(connection);
}
@Override

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2014 Vyacheslav Blinov
* Copyright 2014 Vyacheslav Blinov, 2017 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -14,12 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.debugger;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -27,10 +23,15 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
public class ReflectionDebuggerFactory implements SmackDebuggerFactory {
public final class ReflectionDebuggerFactory implements SmackDebuggerFactory {
private static final Logger LOGGER = Logger.getLogger(ReflectionDebuggerFactory.class.getName());
private static final String DEBUGGER_CLASS_PROPERTY_NAME = "smack.debuggerClass";
public static final ReflectionDebuggerFactory INSTANCE = new ReflectionDebuggerFactory();
private ReflectionDebuggerFactory() {
}
/**
* Possible default debugger implementations. The order of enumeration is the one in which we try
* to instantiate these.
@ -76,14 +77,14 @@ public class ReflectionDebuggerFactory implements SmackDebuggerFactory {
}
@Override
public SmackDebugger create(XMPPConnection connection, Writer writer, Reader reader) throws IllegalArgumentException {
public SmackDebugger create(XMPPConnection connection) throws IllegalArgumentException {
Class<SmackDebugger> debuggerClass = getDebuggerClass();
if (debuggerClass != null) {
// Create a new debugger instance using 3arg constructor
try {
Constructor<SmackDebugger> constructor = debuggerClass
.getConstructor(XMPPConnection.class, Writer.class, Reader.class);
return constructor.newInstance(connection, writer, reader);
.getConstructor(XMPPConnection.class);
return constructor.newInstance(connection);
} catch (Exception e) {
throw new IllegalArgumentException("Can't initialize the configured debugger!", e);
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2003-2007 Jive Software.
* Copyright 2003-2007 Jive Software, 2017 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,7 +20,8 @@ package org.jivesoftware.smack.debugger;
import java.io.Reader;
import java.io.Writer;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.TopLevelStreamElement;
import org.jxmpp.jid.EntityFullJid;
@ -33,7 +34,13 @@ import org.jxmpp.jid.EntityFullJid;
*
* @author Gaston Dombiak
*/
public interface SmackDebugger {
public abstract class SmackDebugger {
protected final XMPPConnection connection;
protected SmackDebugger(XMPPConnection connection) {
this.connection = connection;
}
/**
* Called when a user has logged in to the server. The user could be an anonymous user, this
@ -42,22 +49,9 @@ public interface SmackDebugger {
*
* @param user the user@host/resource that has just logged in
*/
// TODO: Should be replaced with a connection listener authenticed().
public abstract void userHasLogged(EntityFullJid user);
/**
* Returns the special Reader that wraps the main Reader and logs data to the GUI.
*
* @return the special Reader that wraps the main Reader and logs data to the GUI.
*/
public abstract Reader getReader();
/**
* Returns the special Writer that wraps the main Writer and logs data to the GUI.
*
* @return the special Writer that wraps the main Writer and logs data to the GUI.
*/
public abstract Writer getWriter();
/**
* Returns a new special Reader that wraps the new connection Reader. The connection
* has been secured so the connection is using a new reader and writer. The debugger
@ -79,20 +73,23 @@ public interface SmackDebugger {
public abstract Writer newConnectionWriter(Writer writer);
/**
* Returns the thread that will listen for all incoming packets and write them to the GUI.
* This is what we call "interpreted" stanza(/packet) data, since it's the stanza(/packet) data as Smack sees
* it and not as it's coming in as raw XML.
*
* @return the PacketListener that will listen for all incoming packets and write them to
* the GUI
* Used by the connection to notify about an incoming top level stream element.
* <p>
* This method is invoked right after the incoming stream was parsed.
* </p>
*
* @param streamElement the incoming top level stream element.
*/
public abstract StanzaListener getReaderListener();
public abstract void onIncomingStreamElement(TopLevelStreamElement streamElement);
/**
* Returns the thread that will listen for all outgoing packets and write them to the GUI.
*
* @return the PacketListener that will listen for all sent packets and write them to
* the GUI
* Used by the connection to notify about a outgoing top level stream element.
* <p>
* This method is invoked right before the element is serialized to XML and put into the outgoing stream.
* </p>
*
* @param streamElement the outgoing top level stream element.
*/
public abstract StanzaListener getWriterListener();
public abstract void onOutgoingStreamElement(TopLevelStreamElement streamElement);
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2014 Vyacheslav Blinov
* Copyright 2014 Vyacheslav Blinov, 2017 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -14,20 +14,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.debugger;
import java.io.Reader;
import java.io.Writer;
import org.jivesoftware.smack.XMPPConnection;
public interface SmackDebuggerFactory {
/**
* Initialize the new SmackDebugger instance.
*
* @param connection the XMPP connection this debugger is going to get attached to.
* @throws IllegalArgumentException if the SmackDebugger can't be loaded.
*/
SmackDebugger create(XMPPConnection connection, Writer writer, Reader reader) throws IllegalArgumentException;
SmackDebugger create(XMPPConnection connection) throws IllegalArgumentException;
}