Java Code Examples for org.apache.curator.utils.ZKPaths#PathAndNode

The following examples show how to use org.apache.curator.utils.ZKPaths#PathAndNode . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CuratorZookeeperClient.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
private String getPath(String value, boolean includeEndPath) {
    assertPathHasLength(value);

    if (value.length() == 1 && value.charAt(0) == '/') {
        return value;
    }

    if (value.charAt(value.length() - 1) == '/') {
        return value.substring(0, value.length() - 1);
    }

    if (includeEndPath) {
        return value;
    } else {
        ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(value);
        return pathAndNode.getPath();
    }
}
 
Example 2
Source File: CreateBuilderImpl.java    From xian with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
String adjustPath(String path) throws Exception
{
    if ( doProtected )
    {
        ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(path);
        String name = getProtectedPrefix(protectedId) + pathAndNode.getNode();
        path = ZKPaths.makePath(pathAndNode.getPath(), name);
    }
    return path;
}
 
Example 3
Source File: UsageListing.java    From exhibitor with Apache License 2.0 5 votes vote down vote up
public UsageListing(Exhibitor exhibitor, String startPath, int maxChildren)
{
    if ( startPath.trim().length() == 0 )
    {
        startPath = "/";
    }
    ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(startPath);
    this.exhibitor = exhibitor;
    this.startPath = ZKPaths.makePath(pathAndNode.getPath(), pathAndNode.getNode());
    this.maxChildren = maxChildren;
}
 
Example 4
Source File: ProtectedUtils.java    From curator with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to provide a path removing Curator's generated protected prefix if present in the ZNode name
 *
 * @param path ZNode path
 * @return string without Curator's generated protected prefix if present in ZNode name; original string if prefix not present
 */
public static String normalizePath(final String path)
{
    final ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(path);
    final String name = pathAndNode.getNode();
    return isProtectedZNode(name) ? ZKPaths.makePath(pathAndNode.getPath(), normalize(name)) : path;
}
 
Example 5
Source File: ProtectedUtils.java    From curator with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a given path to protected format
 *
 * @param path complete path to be converted (e.g. '/root/path1')
 * @param protectedId UUID canonical text representation used in protection mode (e.g. '53345f98-9423-4e0c-a7b5-9f819e3ec2e1') 
 * @return path with protected mode prefix (e.g. '/root/_c_53345f98-9423-4e0c-a7b5-9f819e3ec2e1-path1')
 *         or the same path if protectedId is {@code null}
 */
public static String toProtectedZNodePath(final String path, final String protectedId)
{
    if ( protectedId == null )
    {
        return path;
    }
    final ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(path);
    return ZKPaths.makePath(pathAndNode.getPath(), toProtectedZNode(pathAndNode.getNode(), protectedId));
}
 
Example 6
Source File: CuratorZookeeperClientTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void createOrSetNodeTest() throws Exception {
    ZooKeeper zooKeeper = createZookeeper();

    try {
        String message = createTestMessage();
        String testNodePath = createTestNodePath() + "/temp";

        ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(testNodePath);
        String path = pathAndNode.getPath();
        String node = pathAndNode.getNode();

        try {
            curatorZookeeperClient.createOrSetNode(new CreateNodeMessage(testNodePath, message.getBytes()));
            Assert.fail();
        } catch (Exception e) {
        }

        boolean existNode = isExistNode(zooKeeper, path);
        Assert.assertFalse(existNode);

        existNode = isExistNode(zooKeeper, testNodePath);
        Assert.assertFalse(existNode);

        curatorZookeeperClient.createOrSetNode(new CreateNodeMessage(testNodePath, message.getBytes(), true));

        existNode = isExistNode(zooKeeper, testNodePath);
        Assert.assertTrue(existNode);

        curatorZookeeperClient.delete(testNodePath);
    } finally {
        if (zooKeeper != null) {
            zooKeeper.close();
        }
    }
}
 
Example 7
Source File: CuratorZookeeperClientTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Test
public void getChildrenTest() throws Exception {
    ZooKeeper zooKeeper = createZookeeper();
    try {
        String testNodePath = createTestNodePath();

        ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(testNodePath);

        List<String> childrenNode = curatorZookeeperClient.getChildNodeList(pathAndNode.getPath(), true);
        Assert.assertTrue(childrenNode.isEmpty());

        zooKeeper.create(testNodePath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

        boolean await = AWAIT_UTILS.await(new TestAwaitTaskUtils() {
            @Override
            public boolean checkCompleted() {
                return eventHoldingZookeeperEventWatcher.getLastWatchedEvent() != null;
            }
        });
        Assert.assertTrue(await);

        WatchedEvent lastWatchedEvent = eventHoldingZookeeperEventWatcher.getLastWatchedEvent();
        Assert.assertEquals(Watcher.Event.EventType.NodeChildrenChanged, lastWatchedEvent.getType());

        childrenNode = curatorZookeeperClient.getChildNodeList(pathAndNode.getPath(), false);
        Assert.assertTrue(!childrenNode.isEmpty());
    } finally {
        if (zooKeeper != null) {
            zooKeeper.close();
        }
    }
}
 
Example 8
Source File: InMemoryZookeeperClient.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void createPath(String value) throws PinpointZookeeperException, InterruptedException {
    ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(value);
    contents.put(pathAndNode.getPath(), EMPTY_BYTE);
}
 
Example 9
Source File: CuratorCacheAccessor.java    From curator with Apache License 2.0 3 votes vote down vote up
/**
 * Filter for a ChildData stream. Only ChildDatas with the given parent path
 * pass the filter. This is useful to stream one level below a given path in the cache.
 * e.g. to stream only the first level below the root of the cache.
 *
 * <code><pre>
 * CuratorCache cache = ...
 * cache.stream().filter(parentPathFilter(root))... // etc
 * </pre></code>
 *
 * @param parentPath the parent path to filter on
 * @return filtered stream
 */
static Predicate<ChildData> parentPathFilter(String parentPath)
{
    return d -> {
        ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(d.getPath());
        return pathAndNode.getPath().equals(parentPath);
    };
}