本文整理汇总了Java中org.apache.zookeeper.ZooKeeper.addAuthInfo方法的典型用法代码示例。如果您正苦于以下问题:Java ZooKeeper.addAuthInfo方法的具体用法?Java ZooKeeper.addAuthInfo怎么用?Java ZooKeeper.addAuthInfo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.zookeeper.ZooKeeper
的用法示例。
在下文中一共展示了ZooKeeper.addAuthInfo方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testBadAuthNotifiesWatch
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testBadAuthNotifiesWatch() throws Exception {
ZooKeeper zk = createClient();
try {
zk.addAuthInfo("FOO", "BAR".getBytes());
zk.getData("/path1", false, null);
Assert.fail("Should get auth state error");
} catch(KeeperException.AuthFailedException e) {
if(!authFailed.await(CONNECTION_TIMEOUT,
TimeUnit.MILLISECONDS))
{
Assert.fail("Should have called my watcher");
}
}
finally {
zk.close();
}
}
示例2: getNewZooKeeper
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Get a new zookeeper client instance. protected so that test class can
* inherit and pass in a mock object for zookeeper
*
* @return new zookeeper client instance
* @throws IOException
* @throws KeeperException zookeeper connectionloss exception
*/
protected synchronized ZooKeeper getNewZooKeeper() throws IOException,
KeeperException {
// Unfortunately, the ZooKeeper constructor connects to ZooKeeper and
// may trigger the Connected event immediately. So, if we register the
// watcher after constructing ZooKeeper, we may miss that event. Instead,
// we construct the watcher first, and have it block any events it receives
// before we can set its ZooKeeper reference.
watcher = new WatcherWithClientRef();
ZooKeeper zk = new ZooKeeper(zkHostPort, zkSessionTimeout, watcher);
watcher.setZooKeeperRef(zk);
// Wait for the asynchronous success/failure. This may throw an exception
// if we don't connect within the session timeout.
watcher.waitForZKConnectionEvent(zkSessionTimeout);
for (ZKAuthInfo auth : zkAuthInfo) {
zk.addAuthInfo(auth.getScheme(), auth.getAuth());
}
return zk;
}
示例3: testBadAuthThenSendOtherCommands
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testBadAuthThenSendOtherCommands() throws Exception {
ZooKeeper zk = createClient();
try {
zk.addAuthInfo("INVALID", "BAR".getBytes());
zk.exists("/foobar", false);
zk.getData("/path1", false, null);
Assert.fail("Should get auth state error");
} catch(KeeperException.AuthFailedException e) {
if(!authFailed.await(CONNECTION_TIMEOUT,
TimeUnit.MILLISECONDS))
{
Assert.fail("Should have called my watcher");
}
}
finally {
zk.close();
}
}
示例4: testSuperACL
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testSuperACL() throws Exception {
ZooKeeper zk = createClient();
try {
zk.addAuthInfo("digest", "pat:pass".getBytes());
zk.create("/path1", null, Ids.CREATOR_ALL_ACL,
CreateMode.PERSISTENT);
zk.close();
// verify super can do anything and ignores ACLs
zk = createClient();
zk.addAuthInfo("digest", "super:test".getBytes());
zk.getData("/path1", false, null);
zk.setACL("/path1", Ids.READ_ACL_UNSAFE, -1);
zk.create("/path1/foo", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
zk.setACL("/path1", Ids.OPEN_ACL_UNSAFE, -1);
} finally {
zk.close();
}
}
示例5: preAuth
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public void preAuth() throws Exception {
ZooKeeper zk = createClient();
zk.addAuthInfo("key", "25".getBytes());
try {
createNodePrintAcl(zk, "/pre", "testPreAuth");
zk.setACL("/", Ids.CREATOR_ALL_ACL, -1);
zk.getChildren("/", false);
zk.create("/abc", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
zk.setData("/abc", "testData1".getBytes(), -1);
zk.create("/key", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
zk.setData("/key", "5".getBytes(), -1);
Thread.sleep(1000);
} catch (KeeperException e) {
Assert.fail("test failed :" + e);
} finally {
zk.close();
}
}
示例6: createZookeeper
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
private void createZookeeper(final CountDownLatch connectionLatch) throws Exception {
zk = new ZooKeeper(this.properties.getProperty(keys.zkConnectString
.toString()), Integer.parseInt(this.properties
.getProperty(keys.zkSessionTimeout.toString())),
new Watcher() {
public void process(WatchedEvent event) {
sessionEvent(connectionLatch, event);
}
});
String authString = this.properties.getProperty(keys.userName.toString())
+ ":"+ this.properties.getProperty(keys.password.toString());
this.isCheckParentPath = Boolean.parseBoolean(this.properties.getProperty(keys.isCheckParentPath.toString(),"true"));
zk.addAuthInfo("digest", authString.getBytes());
acl.clear();
acl.add(new ACL(ZooDefs.Perms.ALL, new Id("digest",
DigestAuthenticationProvider.generateDigest(authString))));
acl.add(new ACL(ZooDefs.Perms.READ, Ids.ANYONE_ID_UNSAFE));
}
示例7: testDisconnectedAddAuth
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testDisconnectedAddAuth() throws Exception {
File tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
SyncRequestProcessor.setSnapCount(1000);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
try {
LOG.info("starting up the zookeeper server .. waiting");
Assert.assertTrue("waiting for server being up",
ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
try {
zk.addAuthInfo("digest", "pat:test".getBytes());
zk.setACL("/", Ids.CREATOR_ALL_ACL, -1);
} finally {
zk.close();
}
} finally {
f.shutdown();
zks.shutdown();
Assert.assertTrue("waiting for server down",
ClientBase.waitForServerDown(HOSTPORT,
ClientBase.CONNECTION_TIMEOUT));
}
}
示例8: testDisconnectedAddAuth
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testDisconnectedAddAuth() throws Exception {
File tmpDir = ClientBase.createTmpDir();
ClientBase.setupTestEnv();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
SyncRequestProcessor.setSnapCount(1000);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
try {
LOG.info("starting up the zookeeper server .. waiting");
Assert.assertTrue("waiting for server being up",
ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
try {
zk.addAuthInfo("digest", "pat:test".getBytes());
zk.setACL("/", Ids.CREATOR_ALL_ACL, -1);
} finally {
zk.close();
}
} finally {
f.shutdown();
zks.shutdown();
Assert.assertTrue("waiting for server down",
ClientBase.waitForServerDown(HOSTPORT,
ClientBase.CONNECTION_TIMEOUT));
}
}
示例9: validAuth
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public void validAuth() throws Exception {
ZooKeeper zk = createClient();
// any multiple of 5 will do...
zk.addAuthInfo("key", "25".getBytes());
try {
createNodePrintAcl(zk, "/valid", "testValidAuth");
zk.getData("/abc", false, null);
zk.setData("/abc", "testData3".getBytes(), -1);
} catch (KeeperException.AuthFailedException e) {
Assert.fail("test failed :" + e);
} finally {
zk.close();
}
}
示例10: validAuth2
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public void validAuth2() throws Exception {
ZooKeeper zk = createClient();
// any multiple of 5 will do...
zk.addAuthInfo("key", "125".getBytes());
try {
createNodePrintAcl(zk, "/valid2", "testValidAuth2");
zk.getData("/abc", false, null);
zk.setData("/abc", "testData3".getBytes(), -1);
} catch (KeeperException.AuthFailedException e) {
Assert.fail("test failed :" + e);
} finally {
zk.close();
}
}
示例11: testAsync
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testAsync()
throws IOException, InterruptedException, KeeperException
{
ZooKeeper zk = null;
zk = createClient();
try {
zk.addAuthInfo("digest", "ben:passwd".getBytes());
zk.create("/ben", new byte[0], Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT, this, results);
zk.create("/ben/2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results);
zk.delete("/ben", -1, this, results);
zk.create("/ben2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results);
zk.getData("/ben2", false, this, results);
synchronized (results) {
while (results.size() < 5) {
results.wait();
}
}
Assert.assertEquals(0, (int) results.get(0));
Assert.assertEquals(Code.NOAUTH, Code.get(results.get(1)));
Assert.assertEquals(0, (int) results.get(2));
Assert.assertEquals(0, (int) results.get(3));
Assert.assertEquals(0, (int) results.get(4));
} finally {
zk.close();
}
zk = createClient();
try {
zk.addAuthInfo("digest", "ben:passwd2".getBytes());
try {
zk.getData("/ben2", false, new Stat());
Assert.fail("Should have received a permission error");
} catch (KeeperException e) {
Assert.assertEquals(Code.NOAUTH, e.code());
}
} finally {
zk.close();
}
zk = createClient();
try {
zk.addAuthInfo("digest", "ben:passwd".getBytes());
zk.getData("/ben2", false, new Stat());
} finally {
zk.close();
}
}
示例12: testAsync
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testAsync() throws Exception
{
ZooKeeper zk = null;
zk = createClient();
try {
zk.addAuthInfo("digest", "ben:passwd".getBytes());
zk.create("/ben", new byte[0], Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT, this, results);
zk.create("/ben/2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results);
zk.delete("/ben", -1, this, results);
zk.create("/ben2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results);
zk.getData("/ben2", false, this, results);
synchronized (results) {
while (results.size() < 5) {
results.wait();
}
}
Assert.assertEquals(0, (int) results.get(0));
Assert.assertEquals(Code.NOAUTH, Code.get(results.get(1)));
Assert.assertEquals(0, (int) results.get(2));
Assert.assertEquals(0, (int) results.get(3));
Assert.assertEquals(0, (int) results.get(4));
} finally {
zk.close();
}
zk = createClient();
try {
zk.addAuthInfo("digest", "ben:passwd2".getBytes());
try {
zk.getData("/ben2", false, new Stat());
Assert.fail("Should have received a permission error");
} catch (KeeperException e) {
Assert.assertEquals(Code.NOAUTH, e.code());
}
} finally {
zk.close();
}
zk = createClient();
try {
zk.addAuthInfo("digest", "ben:passwd".getBytes());
zk.getData("/ben2", false, new Stat());
} finally {
zk.close();
}
}