Bläddra i källkod

ZOOKEEPER-786. Exception in ZooKeeper.toString (Thomas Koch via phunt)

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1170882 13f79535-47bb-0310-9956-ffa450edef68
Patrick D. Hunt 13 år sedan
förälder
incheckning
de7289d3a1
2 ändrade filer med 18 tillägg och 21 borttagningar
  1. 3 0
      CHANGES.txt
  2. 15 21
      src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java

+ 3 - 0
CHANGES.txt

@@ -8,6 +8,9 @@ Backward compatible changes:
 
 BUGFIXES:
 
+  ZOOKEEPER-786. Exception in ZooKeeper.toString
+  (Thomas Koch via phunt)
+
 IMPROVEMENTS:
 
   ZOOKEEPER-1170. Fix compiler (eclipse) warnings: unused imports,

+ 15 - 21
src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java

@@ -20,6 +20,7 @@ package org.apache.zookeeper;
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
+import java.net.Socket;
 import java.net.SocketAddress;
 import java.nio.ByteBuffer;
 import java.nio.channels.SelectionKey;
@@ -43,6 +44,10 @@ public class ClientCnxnSocketNIO extends ClientCnxnSocket {
 
     private SelectionKey sockKey;
 
+    private SocketAddress localSocketAddress;
+
+    private SocketAddress remoteSocketAddress;
+
     ClientCnxnSocketNIO() throws IOException {
         super();
     }
@@ -185,9 +190,7 @@ public class ClientCnxnSocketNIO extends ClientCnxnSocket {
         sock.socket().setSoLinger(false, -1);
         sock.socket().setTcpNoDelay(true);
         sockKey = sock.register(selector, SelectionKey.OP_CONNECT);
-        if (sock.connect(addr)) {
-            sendThread.primeConnection();
-        }
+        sock.connect(addr);
         initialized = false;
 
         /*
@@ -205,15 +208,7 @@ public class ClientCnxnSocketNIO extends ClientCnxnSocket {
      */
     @Override
     SocketAddress getRemoteSocketAddress() {
-        // a lot could go wrong here, so rather than put in a bunch of code
-        // to check for nulls all down the chain let's do it the simple
-        // yet bulletproof way
-        try {
-            return ((SocketChannel) sockKey.channel()).socket()
-                    .getRemoteSocketAddress();
-        } catch (NullPointerException e) {
-            return null;
-        }
+        return remoteSocketAddress;
     }
 
     /**
@@ -224,15 +219,13 @@ public class ClientCnxnSocketNIO extends ClientCnxnSocket {
      */
     @Override
     SocketAddress getLocalSocketAddress() {
-        // a lot could go wrong here, so rather than put in a bunch of code
-        // to check for nulls all down the chain let's do it the simple
-        // yet bulletproof way
-        try {
-            return ((SocketChannel) sockKey.channel()).socket()
-                    .getLocalSocketAddress();
-        } catch (NullPointerException e) {
-            return null;
-        }
+        return localSocketAddress;
+    }
+    
+    private void updateSocketAddresses() {
+        Socket socket = ((SocketChannel) sockKey.channel()).socket();
+        localSocketAddress = socket.getLocalSocketAddress();
+        remoteSocketAddress = socket.getRemoteSocketAddress();
     }
 
     @Override
@@ -257,6 +250,7 @@ public class ClientCnxnSocketNIO extends ClientCnxnSocket {
             if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) {
                 if (sc.finishConnect()) {
                     updateLastSendAndHeard();
+                    updateSocketAddresses();
                     sendThread.primeConnection();
                 }
             } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) {