Browse Source

ZOOKEEPER-2375: Prevent multiple initialization of login object in each ZooKeeperSaslClient instance (yuemeng via rakeshr)

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1733221 13f79535-47bb-0310-9956-ffa450edef68
Rakesh Radhakrishnan 9 years ago
parent
commit
153a7e6406
2 changed files with 14 additions and 7 deletions
  1. 3 0
      CHANGES.txt
  2. 11 7
      src/java/main/org/apache/zookeeper/client/ZooKeeperSaslClient.java

+ 3 - 0
CHANGES.txt

@@ -254,6 +254,9 @@ BUGFIXES:
 
 
   ZOOKEEPER-2243: Supported platforms is completely out of date (cnauroth)
   ZOOKEEPER-2243: Supported platforms is completely out of date (cnauroth)
 
 
+  ZOOKEEPER-2375: Prevent multiple initialization of login object in each
+  ZooKeeperSaslClient instance (yuemeng via rakeshr)
+
 IMPROVEMENTS:
 IMPROVEMENTS:
   ZOOKEEPER-1660 Documentation for Dynamic Reconfiguration (Reed Wanderman-Milne via shralex)  
   ZOOKEEPER-1660 Documentation for Dynamic Reconfiguration (Reed Wanderman-Milne via shralex)  
 
 

+ 11 - 7
src/java/main/org/apache/zookeeper/client/ZooKeeperSaslClient.java

@@ -214,17 +214,21 @@ public class ZooKeeperSaslClient {
         }
         }
     }
     }
 
 
-    synchronized private SaslClient createSaslClient(final String servicePrincipal,
+    private SaslClient createSaslClient(final String servicePrincipal,
                                                      final String loginContext) throws LoginException {
                                                      final String loginContext) throws LoginException {
         try {
         try {
             if (login == null) {
             if (login == null) {
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("JAAS loginContext is: " + loginContext);
+                synchronized (ZooKeeperSaslClient.class) {
+                    if (login == null) {
+                        if (LOG.isDebugEnabled()) {
+                            LOG.debug("JAAS loginContext is: " + loginContext);
+                        }
+                        // note that the login object is static: it's shared amongst all zookeeper-related connections.
+                        // in order to ensure the login is initialized only once, it must be synchronized the code snippet.
+                        login = new Login(loginContext, new ClientCallbackHandler(null));
+                        login.startThreadIfNeeded();
+                    }
                 }
                 }
-                // note that the login object is static: it's shared amongst all zookeeper-related connections.
-                // createSaslClient() must be declared synchronized so that login is initialized only once.
-                login = new Login(loginContext, new ClientCallbackHandler(null));
-                login.startThreadIfNeeded();
             }
             }
             Subject subject = login.getSubject();
             Subject subject = login.getSubject();
             SaslClient saslClient;
             SaslClient saslClient;