Example usage for org.apache.zookeeper.common PathUtils validatePath

List of usage examples for org.apache.zookeeper.common PathUtils validatePath

Introduction

In this page you can find the example usage for org.apache.zookeeper.common PathUtils validatePath.

Prototype

public static void validatePath(String path) throws IllegalArgumentException 

Source Link

Document

Validate the provided znode path string

Usage

From source file:com.boundary.zoocreeper.ZooKeeperPathOptionHandler.java

License:Apache License

@Override
public int parseArguments(Parameters params) throws CmdLineException {
    String param = params.getParameter(0);
    try {// w w w .  j a  va2 s .c  om
        PathUtils.validatePath(param);
        setter.addValue(param);
        return 1;
    } catch (IllegalArgumentException e) {
        throw new CmdLineException(owner,
                String.format("\"%s\" is not a valid value for \"%s\"", param, params.getParameter(-1)));
    }
}

From source file:com.comcast.viper.flume2storm.zookeeper.ZkUtilies.java

License:Apache License

/**
 * Builds a valid (guaranteed) ZNode path made of the components passed in
 * parameter. This method handles the path separator between component, so it
 * can be called with or without them.//  w w w . j  ava2  s .  com
 * 
 * @param components
 *          A bunch of ZNode path elements. Some may be null.
 * @return The concatenated path of all the elements
 * @throws IllegalArgumentException
 *           if the path is invalid (empty for example)
 */
public static String buildZkPath(final String... components) {
    Preconditions.checkArgument(components != null, "No path element specified");
    boolean isFirst = true;
    final StringBuilder result = new StringBuilder();
    for (int i = 0; i < components.length; i++) {
        if (StringUtils.isEmpty(components[i])) {
            continue;
        }
        assert components[i] != null;
        // Checking path separator
        if (isFirst) {
            // First element must start with /
            if (!components[i].startsWith(SEPARATOR)) {
                result.append(SEPARATOR);
            }
            result.append(components[i]);
        } else {
            if (!SEPARATOR_CHAR.equals(result.charAt(result.length() - 1))
                    && !components[i].startsWith(SEPARATOR)) {
                result.append(SEPARATOR);
                result.append(components[i]);
            } else if (SEPARATOR_CHAR.equals(result.charAt(result.length() - 1))
                    && components[i].startsWith(SEPARATOR)) {
                result.append(components[i].substring(1));
            } else {
                result.append(components[i]);
            }
        }
        isFirst = false;
    }
    final String path = result.toString();
    PathUtils.validatePath(path);
    return path;
}

From source file:com.netflix.curator.framework.imps.NamespaceImpl.java

License:Apache License

NamespaceImpl(CuratorFrameworkImpl client, String namespace) {
    if (namespace != null) {
        try {/*from w  w w. j  a v a2s . c  o m*/
            PathUtils.validatePath("/" + namespace);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid namespace: " + namespace);
        }
    }

    this.client = client;
    this.namespace = namespace;
    ensurePath = (namespace != null) ? new EnsurePath(ZKPaths.makePath("/", namespace)) : null;
}

From source file:com.netflix.curator.framework.recipes.locks.LockInternals.java

License:Apache License

LockInternals(CuratorFramework client, LockInternalsDriver driver, String path, String lockName,
        int maxLeases) {
    this.driver = driver;
    this.lockName = lockName;
    this.maxLeases = maxLeases;
    PathUtils.validatePath(path);

    this.client = client;
    this.basePath = path;
    this.path = ZKPaths.makePath(path, lockName);
}

From source file:com.netflix.curator.utils.ZKPaths.java

License:Apache License

/**
 * Given a full path, return the node name. i.e. "/one/two/three" will return "three"
 * /*from w ww .ja  v  a  2 s .  c  o m*/
 * @param path the path
 * @return the node
 */
public static String getNodeFromPath(String path) {
    PathUtils.validatePath(path);
    int i = path.lastIndexOf('/');
    if (i < 0) {
        return path;
    }
    if ((i + 1) >= path.length()) {
        return "";
    }
    return path.substring(i + 1);
}

From source file:com.netflix.curator.utils.ZKPaths.java

License:Apache License

/**
 * Given a full path, return the node name and its path. i.e. "/one/two/three" will return {"/one/two", "three"}
 *
 * @param path the path/*from  w ww. ja v  a2 s  . c o  m*/
 * @return the node
 */
public static PathAndNode getPathAndNode(String path) {
    PathUtils.validatePath(path);
    int i = path.lastIndexOf('/');
    if (i < 0) {
        return new PathAndNode(path, "");
    }
    if ((i + 1) >= path.length()) {
        return new PathAndNode("/", "");
    }
    String node = path.substring(i + 1);
    String parentPath = (i > 0) ? path.substring(0, i) : "/";
    return new PathAndNode(parentPath, node);
}

From source file:com.netflix.curator.utils.ZKPaths.java

License:Apache License

/**
 * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish
 * between directories and files. So, every node in the path is created. The data for each node is an empty blob
 *
 * @param zookeeper the client//from  w  ww  .  ja va  2 s  .  c  om
 * @param path      path to ensure
 * @param makeLastNode if true, all nodes are created. If false, only the parent nodes are created
 * @throws InterruptedException thread interruption
 * @throws org.apache.zookeeper.KeeperException
 *                              Zookeeper errors
 */
public static void mkdirs(ZooKeeper zookeeper, String path, boolean makeLastNode)
        throws InterruptedException, KeeperException {
    PathUtils.validatePath(path);

    int pos = 1; // skip first slash, root is guaranteed to exist
    do {
        pos = path.indexOf('/', pos + 1);

        if (pos == -1) {
            if (makeLastNode) {
                pos = path.length();
            } else {
                break;
            }
        }

        String subPath = path.substring(0, pos);
        if (zookeeper.exists(subPath, false) == null) {
            try {
                zookeeper.create(subPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            } catch (KeeperException.NodeExistsException e) {
                // ignore... someone else has created it since we checked
            }
        }

    } while (pos < path.length());
}

From source file:com.netflix.exhibitor.standalone.ExhibitorCreator.java

License:Apache License

private ConfigProvider getZookeeperProvider(CommandLine commandLine, String useHostname,
        Properties defaultProperties) throws Exception {
    String connectString = commandLine.getOptionValue(ZOOKEEPER_CONFIG_INITIAL_CONNECT_STRING);
    String path = commandLine.getOptionValue(ZOOKEEPER_CONFIG_BASE_PATH);
    String retrySpec = commandLine.getOptionValue(ZOOKEEPER_CONFIG_RETRY, DEFAULT_ZOOKEEPER_CONFIG_RETRY);
    if ((path == null) || (connectString == null)) {
        log.error("Both " + ZOOKEEPER_CONFIG_INITIAL_CONNECT_STRING + " and " + ZOOKEEPER_CONFIG_BASE_PATH
                + " are required when the configtype is zookeeper");
        return null;
    }/*from w w  w .j  av  a  2  s.com*/

    try {
        PathUtils.validatePath(path);
    } catch (IllegalArgumentException e) {
        log.error("Invalid " + ZOOKEEPER_CONFIG_BASE_PATH + ": " + path);
        return null;
    }

    String[] retryParts = retrySpec.split("\\:");
    if (retryParts.length != 2) {
        log.error("Bad " + ZOOKEEPER_CONFIG_RETRY + " value: " + retrySpec);
        return null;
    }

    int baseSleepTimeMs;
    int maxRetries;
    try {
        baseSleepTimeMs = Integer.parseInt(retryParts[0]);
        maxRetries = Integer.parseInt(retryParts[1]);
    } catch (NumberFormatException e) {
        log.error("Bad " + ZOOKEEPER_CONFIG_RETRY + " value: " + retrySpec);
        return null;
    }

    int exhibitorPort;
    try {
        exhibitorPort = commandLine.hasOption(ZOOKEEPER_CONFIG_EXHIBITOR_PORT)
                ? Integer.parseInt(commandLine.getOptionValue(ZOOKEEPER_CONFIG_EXHIBITOR_PORT))
                : 0;
    } catch (NumberFormatException e) {
        log.error("Bad " + ZOOKEEPER_CONFIG_EXHIBITOR_PORT + " value: "
                + commandLine.getOptionValue(ZOOKEEPER_CONFIG_EXHIBITOR_PORT));
        return null;
    }

    int pollingMs;
    try {
        pollingMs = Integer.parseInt(
                commandLine.getOptionValue(ZOOKEEPER_CONFIG_POLLING, DEFAULT_ZOOKEEPER_CONFIG_POLLING));
    } catch (NumberFormatException e) {
        log.error("Bad " + ZOOKEEPER_CONFIG_POLLING + " value: "
                + commandLine.getOptionValue(ZOOKEEPER_CONFIG_POLLING, DEFAULT_ZOOKEEPER_CONFIG_POLLING));
        return null;
    }

    String exhibitorRestPath = commandLine.getOptionValue(ZOOKEEPER_CONFIG_EXHIBITOR_URI_PATH,
            DEFAULT_ZOOKEEPER_CONFIG_EXHIBITOR_URI_PATH);
    CuratorFramework client = makeCurator(connectString, baseSleepTimeMs, maxRetries, exhibitorPort,
            exhibitorRestPath, pollingMs);
    if (client == null) {
        return null;
    }

    client.start();
    closeables.add(client);
    return new ZookeeperConfigProvider(client, path, defaultProperties, useHostname);
}

From source file:com.proofpoint.zookeeper.ZookeeperUtils.java

License:Apache License

/**
 * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish
 * between directories and files. So, every node in the path is created. The data for each node is an empty blob
 *
 * @param zookeeper the client//from  www  . j a  va  2s  .  co m
 * @param path path to ensure
 * @throws InterruptedException thread interruption
 * @throws org.apache.zookeeper.KeeperException Zookeeper errors
 */
public static void mkdirs(ZooKeeper zookeeper, String path) throws InterruptedException, KeeperException {
    PathUtils.validatePath(path);

    int pos = 1; // skip first slash, root is guaranteed to exist
    do {
        pos = path.indexOf('/', pos + 1);

        if (pos == -1) {
            pos = path.length();
        }

        String subPath = path.substring(0, pos);
        if (zookeeper.exists(subPath, false) == null) {
            try {
                zookeeper.create(subPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            } catch (KeeperException.NodeExistsException e) {
                // ignore... someone else has created it since we checked
            }
        }

    } while (pos < path.length());
}

From source file:com.spotify.helios.servicescommon.coordination.PathFactory.java

License:Apache License

public PathFactory(final String base, final String... parts) {
    final String joined = join(base, parts);
    this.base = joined.startsWith("/") ? joined : "/" + joined;
    PathUtils.validatePath(base);
}