1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| package com.bai.javaapi;
import org.apache.zookeeper.*; import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.Id; import org.apache.zookeeper.data.Stat;
import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit;
public class AuthControlDemo implements Watcher {
private final static String CONNECTSTRING = ""; private static CountDownLatch countDownLatch = new CountDownLatch(1); private static CountDownLatch countDownLatch2 = new CountDownLatch(1); private static ZooKeeper zooKeeper; private static Stat stat = new Stat();
public static void main(String[] args) throws IOException, InterruptedException, KeeperException { zooKeeper = new ZooKeeper(CONNECTSTRING, 5000, new AuthControlDemo()); countDownLatch.await();
ACL digestAcl = new ACL(ZooDefs.Perms.CREATE, new Id("digest", "root:root")); List<ACL> acls = new ArrayList<ACL>(); acls.add(digestAcl);
zooKeeper.create("/auth1", "123".getBytes(), acls, CreateMode.PERSISTENT); zooKeeper.addAuthInfo("digest", "root:root".getBytes());
zooKeeper.create("/auth", "123".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); zooKeeper.create("/auth/auth1-1", "456".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
ZooKeeper zooKeeper1 = new ZooKeeper(CONNECTSTRING, 5000, new AuthControlDemo()); countDownLatch2.await(); zooKeeper1.addAuthInfo("digest", "root:root".getBytes()); zooKeeper1.delete("/auth/auth1-1", -1); zooKeeper1.delete("/auth", -1); zooKeeper1.delete("/auth1", -1);
}
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) { if (Event.EventType.None == watchedEvent.getType() && null == watchedEvent.getPath()) { countDownLatch.countDown(); countDownLatch2.countDown(); System.out.println(watchedEvent.getState() + "--->" + watchedEvent.getType()); } } } }
|