1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.zookeeper;
20
21 import java.io.IOException;
22 import java.util.List;
23 import java.util.concurrent.atomic.AtomicBoolean;
24
25 import org.apache.zookeeper.CreateMode;
26 import org.apache.zookeeper.KeeperException;
27 import org.apache.zookeeper.KeeperException.AuthFailedException;
28 import org.apache.zookeeper.Watcher;
29 import org.apache.zookeeper.ZooKeeper;
30 import org.apache.zookeeper.data.ACL;
31 import org.apache.zookeeper.data.Stat;
32
33
34
35
36
37 public class AuthFailingZooKeeper extends ZooKeeper {
38 private static final AuthFailedException AUTH_FAILED_EXCEPTION = new AuthFailedException();
39
40
41 private final AtomicBoolean FAILURE_LATCH = new AtomicBoolean(false);
42
43 private final AtomicBoolean IS_AUTH_FAILED = new AtomicBoolean(false);
44
45 public AuthFailingZooKeeper(String connectString, int sessionTimeout, Watcher watcher)
46 throws IOException {
47 super(connectString, sessionTimeout, watcher);
48 }
49
50
51
52
53 public void triggerAuthFailed() {
54 FAILURE_LATCH.set(true);
55 }
56
57 void check() throws KeeperException {
58
59
60 if (IS_AUTH_FAILED.get()) {
61 throw AUTH_FAILED_EXCEPTION;
62 }
63
64 if (!FAILURE_LATCH.get()) {
65 return;
66 }
67
68 IS_AUTH_FAILED.set(true);
69 throw AUTH_FAILED_EXCEPTION;
70 }
71
72 @Override
73 public byte[] getData(String path, Watcher watcher, Stat stat) throws KeeperException,
74 InterruptedException {
75 check();
76 return super.getData(path, watcher, stat);
77 }
78
79 @Override
80 public String create(String path, byte[] data, List<ACL> acl, CreateMode cmode)
81 throws KeeperException, InterruptedException {
82 check();
83 return super.create(path, data, acl, cmode);
84 }
85
86 @Override
87 public Stat exists(String path, boolean watch) throws KeeperException, InterruptedException {
88 check();
89 return super.exists(path, watch);
90 }
91
92 @Override
93 public Stat exists(String path, Watcher watcher) throws KeeperException, InterruptedException {
94 check();
95 return super.exists(path, watcher);
96 }
97
98 @Override
99 public List<String> getChildren(String path, boolean watch)
100 throws KeeperException, InterruptedException {
101 check();
102 return super.getChildren(path, watch);
103 }
104 }