diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java index 3dbf07ea254..266f9bb9298 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java @@ -814,7 +814,12 @@ public boolean isConnected() { * @throws IOException * in cases of network failure * @throws IllegalArgumentException - * if an invalid chroot path is specified + * if any of the following is true: + * */ public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) throws IOException { this(connectString, sessionTimeout, watcher, false); @@ -863,7 +868,12 @@ public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) thro * @throws IOException * in cases of network failure * @throws IllegalArgumentException - * if an invalid chroot path is specified + * if any of the following is true: + * */ public ZooKeeper( String connectString, @@ -928,7 +938,12 @@ public ZooKeeper( * @throws IOException * in cases of network failure * @throws IllegalArgumentException - * if an invalid chroot path is specified + * if any of the following is true: + * */ public ZooKeeper( String connectString, @@ -996,7 +1011,12 @@ public ZooKeeper( * @throws IOException * in cases of network failure * @throws IllegalArgumentException - * if an invalid chroot path is specified + * if any of the following is true: + * */ public ZooKeeper( String connectString, @@ -1011,6 +1031,7 @@ public ZooKeeper( sessionTimeout, watcher); + validateWatcher(watcher); if (clientConfig == null) { clientConfig = new ZKClientConfig(); } @@ -1100,7 +1121,12 @@ protected ClientCnxn createConnection( * @throws IOException * in cases of network failure * @throws IllegalArgumentException - * if an invalid chroot path is specified + * if any of the following is true: + * */ public ZooKeeper( String connectString, @@ -1162,7 +1188,12 @@ public ZooKeeper( * @throws IOException * in cases of network failure * @throws IllegalArgumentException - * if an invalid chroot path is specified + * if any of the following is true: + * */ public ZooKeeper( String connectString, @@ -1228,8 +1259,13 @@ public ZooKeeper( * password for this session * * @throws IOException in cases of network failure - * @throws IllegalArgumentException if an invalid chroot path is specified - * @throws IllegalArgumentException for an invalid list of ZooKeeper hosts + * @throws IllegalArgumentException + * if any of the following is true: + * */ public ZooKeeper( String connectString, @@ -1302,7 +1338,13 @@ public ZooKeeper( * @param aHostProvider * use this as HostProvider to enable custom behaviour. * @throws IOException in cases of network failure - * @throws IllegalArgumentException if an invalid chroot path is specified + * @throws IllegalArgumentException + * if any of the following is true: + * */ public ZooKeeper( String connectString, @@ -1389,7 +1431,12 @@ public ZooKeeper( * configuring properties differently compared to other instances * @throws IOException in cases of network failure * @throws IllegalArgumentException if an invalid chroot path is specified - * + * if any of the following is true: + * * @since 3.5.5 */ public ZooKeeper( @@ -1410,6 +1457,7 @@ public ZooKeeper( Long.toHexString(sessionId), (sessionPasswd == null ? "" : "")); + validateWatcher(watcher); if (clientConfig == null) { clientConfig = new ZKClientConfig(); } @@ -1493,7 +1541,13 @@ public ZooKeeper( * allowed while write requests are not. It continues seeking for * majority in the background. * @throws IOException in cases of network failure - * @throws IllegalArgumentException if an invalid chroot path is specified + * @throws IllegalArgumentException + * if any of the following is true: + * */ public ZooKeeper( String connectString, @@ -1581,10 +1635,12 @@ public void addAuthInfo(String scheme, byte[] auth) { /** * Specify the default watcher for the connection (overrides the one * specified during construction). + * @throws IllegalArgumentException if watcher is null * * @param watcher */ public synchronized void register(Watcher watcher) { + validateWatcher(watcher); watchManager.defaultWatcher = watcher; } diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ZooKeeperTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ZooKeeperTest.java index 8fc3df2505e..48e2468d161 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/ZooKeeperTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ZooKeeperTest.java @@ -726,4 +726,28 @@ public void testWaitForConnection() throws Exception { assertTrue("ZooKeeperMain does not wait until the specified timeout", endTime - startTime >= timeout); } + + @Test + public void testInvalidWatcherRegistration() throws Exception { + String nullWatcherMsg = "Invalid Watcher, shouldn't be null!"; + final ZooKeeper zk = createClient(); + try { + zk.register(null); + fail("Should validate null watcher!"); + } catch (IllegalArgumentException iae) { + assertEquals(nullWatcherMsg, iae.getMessage()); + } + try { + new ZooKeeper(hostPort, 10000, null, false); + fail("Should validate null watcher!"); + } catch (IllegalArgumentException iae) { + assertEquals(nullWatcherMsg, iae.getMessage()); + } + try { + new ZooKeeper(hostPort, 10000, null, 10L, "".getBytes(), false); + fail("Should validate null watcher!"); + } catch (IllegalArgumentException iae) { + assertEquals(nullWatcherMsg, iae.getMessage()); + } + } } diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumDigestTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumDigestTest.java index 691b45513a7..2f7873de5d0 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumDigestTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumDigestTest.java @@ -203,7 +203,7 @@ private void startServers(List sids) throws InterruptedException { private void triggerOps(int sid, String prefix) throws Exception { TxnLogDigestTest.performOperations(servers.zk[sid], prefix); - servers.restartClient(sid, null); + servers.restartClient(sid, event -> { }); waitForOne(servers.zk[sid], States.CONNECTED); } diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverMasterTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverMasterTest.java index ee54a7ae57b..2d7735cdddd 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverMasterTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverMasterTest.java @@ -413,7 +413,7 @@ public void testRevalidation() throws Exception { public void testInOrderCommits() throws Exception { setUp(-1); - zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT, null); + zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT, event -> { }); for (int i = 0; i < 10; i++) { zk.create("/bulk" + i, ("Initial data of some size").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);