diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 9325de80d7..ad40bc2dd2 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -5,6 +5,7 @@ import java.io.Closeable; import java.io.IOException; import java.net.Socket; +import java.net.SocketAddress; import java.net.SocketException; import java.nio.ByteBuffer; import java.nio.CharBuffer; @@ -37,6 +38,8 @@ public class Connection implements Closeable { private int soTimeout = 0; private int infiniteSoTimeout = 0; private boolean broken = false; + private boolean strValActive; + private String strVal; public Connection() { this(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT); @@ -72,6 +75,63 @@ public String toString() { return "Connection{" + socketFactory + "}"; } + public String toIdentityString() { + if (strValActive == broken && strVal != null) { + return strVal; + } + + int id = hashCode(); + String classInfo = getClass().toString(); + + if (socket == null) { + StringBuilder buf = new StringBuilder(56) + .append("[") + .append(classInfo) + .append(", id: 0x") + .append(id) + .append(']'); + return buf.toString(); + } + + SocketAddress remoteAddr = socket.getRemoteSocketAddress(); + SocketAddress localAddr = socket.getLocalSocketAddress(); + if (remoteAddr != null) { + StringBuilder buf = new StringBuilder(101) + .append("[") + .append(classInfo) + .append(", id: 0x") + .append(id) + .append(", L:") + .append(localAddr) + .append(broken? " ! " : " - ") + .append("R:") + .append(remoteAddr) + .append(']'); + strVal = buf.toString(); + } else if (localAddr != null) { + StringBuilder buf = new StringBuilder(64) + .append("[") + .append(classInfo) + .append(", id: 0x") + .append(id) + .append(", L:") + .append(localAddr) + .append(']'); + strVal = buf.toString(); + } else { + StringBuilder buf = new StringBuilder(56) + .append("[") + .append(classInfo) + .append(", id: 0x") + .append(id) + .append(']'); + strVal = buf.toString(); + } + + strValActive = broken; + return strVal; + } + public final RedisProtocol getRedisProtocol() { return protocol; } diff --git a/src/test/java/redis/clients/jedis/ConnectionTest.java b/src/test/java/redis/clients/jedis/ConnectionTest.java index 28eba8100c..c57248db53 100644 --- a/src/test/java/redis/clients/jedis/ConnectionTest.java +++ b/src/test/java/redis/clients/jedis/ConnectionTest.java @@ -1,6 +1,7 @@ package redis.clients.jedis; import org.junit.After; +import org.junit.Assert; import org.junit.Test; import redis.clients.jedis.exceptions.JedisConnectionException; @@ -40,4 +41,14 @@ public void checkCloseable() { client.connect(); client.close(); } + + @Test + public void checkIdentityString() { + client = new Connection("localhost", 6379); + Assert.assertFalse(client.toIdentityString().contains("6379")); + client.connect(); + Assert.assertTrue(client.toIdentityString().contains("6379")); + client.close(); + Assert.assertTrue(client.toIdentityString().contains("6379")); + } }