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

Improve logging of failed connection attempts

also improve XMPPTCPConnection.connectUsingConfiguration(): Try further
hostAddresses if getAllByName failes.

Fixes SMACK-711
This commit is contained in:
Florian Schmaus 2016-01-07 19:22:03 +01:00
parent 658a671cbe
commit 7a8d66e9e6
3 changed files with 71 additions and 36 deletions

View file

@ -16,13 +16,20 @@
*/
package org.jivesoftware.smack.util.dns;
import java.net.InetAddress;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.jivesoftware.smack.SmackException.ConnectionException;
import org.jivesoftware.smack.util.Objects;
public class HostAddress {
private final String fqdn;
private final int port;
private Exception exception;
private final Map<InetAddress, Exception> exceptions = new LinkedHashMap<>();
/**
* Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222
@ -64,8 +71,13 @@ public class HostAddress {
return port;
}
public void setException(Exception e) {
this.exception = e;
public void setException(Exception exception) {
setException(null, exception);
}
public void setException(InetAddress inetAddress, Exception exception) {
Exception old = exceptions.put(inetAddress, exception);
assert(old == null);
}
/**
@ -75,8 +87,8 @@ public class HostAddress {
*
* @return the Exception causing this HostAddress to fail
*/
public Exception getException() {
return this.exception;
public Map<InetAddress, Exception> getExceptions() {
return Collections.unmodifiableMap(exceptions);
}
@Override
@ -109,9 +121,24 @@ public class HostAddress {
}
public String getErrorMessage() {
if (exception == null) {
if (exceptions.isEmpty()) {
return "No error logged";
}
return "'" + toString() + "' failed because " + exception.toString();
StringBuilder sb = new StringBuilder();
sb.append('\'').append(toString()).append("' failed because: ");
Iterator<Entry<InetAddress, Exception>> iterator = exceptions.entrySet().iterator();
while (iterator.hasNext()) {
Entry<InetAddress, Exception> entry = iterator.next();
InetAddress inetAddress = entry.getKey();
if (inetAddress != null) {
sb.append(entry.getKey()).append(" exception: ");
}
sb.append(entry.getValue());
if (iterator.hasNext()) {
sb.append(", ");
}
}
return sb.toString();
}
}

View file

@ -41,7 +41,7 @@ public class SmackExceptionTest {
ConnectionException connectionException = ConnectionException.from(failedAddresses);
String message = connectionException.getMessage();
assertEquals("The following addresses failed: 'foo.bar.example:1234' failed because java.lang.Exception: Failed for some reason, 'barz.example:5678' failed because java.lang.Exception: Failed for some other reason",
assertEquals("The following addresses failed: 'foo.bar.example:1234' failed because: java.lang.Exception: Failed for some reason, 'barz.example:5678' failed because: java.lang.Exception: Failed for some other reason",
message);
}