org.apache.zookeeper.common.PathUtils Java Examples

The following examples show how to use org.apache.zookeeper.common.PathUtils. 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: ZooKeeperClientConfig.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
public static ZooKeeperClientConfig createConfig(final NiFiProperties nifiProperties) {
    final String connectString = nifiProperties.getProperty(NiFiProperties.ZOOKEEPER_CONNECT_STRING);
    if (connectString == null || connectString.trim().isEmpty()) {
        throw new IllegalStateException("The '" + NiFiProperties.ZOOKEEPER_CONNECT_STRING + "' property is not set in nifi.properties");
    }
    final String cleanedConnectString = cleanConnectString(connectString);
    if (cleanedConnectString.isEmpty()) {
        throw new IllegalStateException("The '" + NiFiProperties.ZOOKEEPER_CONNECT_STRING + "' property is set in nifi.properties but needs to be in pairs of host:port separated by commas");
    }
    final long sessionTimeoutMs = getTimePeriod(nifiProperties, NiFiProperties.ZOOKEEPER_SESSION_TIMEOUT, NiFiProperties.DEFAULT_ZOOKEEPER_SESSION_TIMEOUT);
    final long connectionTimeoutMs = getTimePeriod(nifiProperties, NiFiProperties.ZOOKEEPER_CONNECT_TIMEOUT, NiFiProperties.DEFAULT_ZOOKEEPER_CONNECT_TIMEOUT);
    final String rootPath = nifiProperties.getProperty(NiFiProperties.ZOOKEEPER_ROOT_NODE, NiFiProperties.DEFAULT_ZOOKEEPER_ROOT_NODE);

    try {
        PathUtils.validatePath(rootPath);
    } catch (final IllegalArgumentException e) {
        throw new IllegalArgumentException("The '" + NiFiProperties.ZOOKEEPER_ROOT_NODE + "' property in nifi.properties is set to an illegal value: " + rootPath);
    }

    return new ZooKeeperClientConfig(cleanedConnectString, (int) sessionTimeoutMs, (int) connectionTimeoutMs, rootPath);
}
 
Example #2
Source File: BKNamespaceDriver.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public static String
validateAndGetFullLedgerAllocatorPoolPath(DistributedLogConfiguration conf, URI uri) throws IOException {
    String poolPath = conf.getLedgerAllocatorPoolPath();
    LOG.info("PoolPath is {}", poolPath);
    if (null == poolPath || !poolPath.startsWith(".") || poolPath.endsWith("/")) {
        LOG.error("Invalid ledger allocator pool path specified when enabling ledger allocator pool: {}", poolPath);
        throw new IOException("Invalid ledger allocator pool path specified : " + poolPath);
    }
    String poolName = conf.getLedgerAllocatorPoolName();
    if (null == poolName) {
        LOG.error("No ledger allocator pool name specified when enabling ledger allocator pool.");
        throw new IOException("No ledger allocator name specified when enabling ledger allocator pool.");
    }
    String rootPath = uri.getPath() + "/" + poolPath + "/" + poolName;
    try {
        PathUtils.validatePath(rootPath);
    } catch (IllegalArgumentException iae) {
        LOG.error("Invalid ledger allocator pool path specified when enabling ledger allocator pool: {}", poolPath);
        throw new IOException("Invalid ledger allocator pool path specified : " + poolPath);
    }
    return rootPath;
}
 
Example #3
Source File: BKDistributedLogNamespace.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
static String validateAndGetFullLedgerAllocatorPoolPath(DistributedLogConfiguration conf, URI uri) throws IOException {
    String poolPath = conf.getLedgerAllocatorPoolPath();
    LOG.info("PoolPath is {}", poolPath);
    if (null == poolPath || !poolPath.startsWith(".") || poolPath.endsWith("/")) {
        LOG.error("Invalid ledger allocator pool path specified when enabling ledger allocator pool : {}", poolPath);
        throw new IOException("Invalid ledger allocator pool path specified : " + poolPath);
    }
    String poolName = conf.getLedgerAllocatorPoolName();
    if (null == poolName) {
        LOG.error("No ledger allocator pool name specified when enabling ledger allocator pool.");
        throw new IOException("No ledger allocator name specified when enabling ledger allocator pool.");
    }
    String rootPath = uri.getPath() + "/" + poolPath + "/" + poolName;
    try {
        PathUtils.validatePath(rootPath);
    } catch (IllegalArgumentException iae) {
        LOG.error("Invalid ledger allocator pool path specified when enabling ledger allocator pool : {}", poolPath);
        throw new IOException("Invalid ledger allocator pool path specified : " + poolPath);
    }
    return rootPath;
}
 
Example #4
Source File: ZooKeeperUpdatingPersistentDirectory.java    From helios with Apache License 2.0 6 votes vote down vote up
public byte[] put(final String key, final byte[] value) throws InterruptedException {
  Preconditions.checkArgument(key.indexOf('/') == -1);
  PathUtils.validatePath(ZKPaths.makePath(path, key));
  final byte[] prev;
  synchronized (lock) {
    final Map<String, byte[]> mutable = Maps.newHashMap(entries.get());
    prev = mutable.put(key, value);
    try {
      entries.set(ImmutableMap.copyOf(mutable));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  reactor.signal();
  return prev;
}
 
Example #5
Source File: ZooKeeperUpdatingPersistentDirectory.java    From helios with Apache License 2.0 6 votes vote down vote up
private byte[] remove(final String key) throws InterruptedException {
  Preconditions.checkArgument(key.indexOf('/') == -1);
  PathUtils.validatePath(ZKPaths.makePath(path, key));
  final byte[] value;
  synchronized (lock) {
    final Map<String, byte[]> mutable = Maps.newHashMap(entries.get());
    value = mutable.remove(key);
    try {
      entries.set(ImmutableMap.copyOf(mutable));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  reactor.signal();
  return value;
}
 
Example #6
Source File: ZooKeeperPathOptionHandler.java    From zoocreeper with Apache License 2.0 5 votes vote down vote up
@Override
public int parseArguments(Parameters params) throws CmdLineException {
    String param = params.getParameter(0);
    try {
        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)));
    }
}
 
Example #7
Source File: RegistryImpl.java    From Scribengin with GNU Affero General Public License v3.0 5 votes vote down vote up
public void rcopy(String path, String toPath) throws RegistryException {
  try {
    PathUtils.validatePath(path);
    List<String> tree = ZKUtil.listSubTreeBFS(zkClient, realPath(path));
    for (int i = 0; i < tree.size(); i++) {
      String selPath = tree.get(i);
      String selToPath = selPath.replace(path, toPath);
      byte[] data = zkClient.getData(selPath, false, new Stat()) ;
      zkClient.create(selToPath, data, DEFAULT_ACL, toCreateMode(NodeCreateMode.PERSISTENT)) ;
    }
  } catch (InterruptedException | KeeperException e) {
    throw new RegistryException(ErrorCode.Unknown, e) ;
  }
}
 
Example #8
Source File: RegistryImpl.java    From Scribengin with GNU Affero General Public License v3.0 5 votes vote down vote up
public List<String> findDencendantRealPaths(String path) throws RegistryException {
  checkConnected();
  try {
    PathUtils.validatePath(realPath(path));
    return ZKUtil.listSubTreeBFS(zkClient, realPath(path));
  } catch (InterruptedException | KeeperException e) {
    throw new RegistryException(ErrorCode.Unknown, e) ;
  }
}
 
Example #9
Source File: RegistryImpl.java    From Scribengin with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void rdelete(String path) throws RegistryException {
  checkConnected();
  try {
    PathUtils.validatePath(path);
    List<String> tree = ZKUtil.listSubTreeBFS(zkClient, realPath(path));
    for (int i = tree.size() - 1; i >= 0 ; --i) {
      //Delete the leaves first and eventually get rid of the root
      zkClient.delete(tree.get(i), -1); //Delete all versions of the node with -1.
    }
  } catch (InterruptedException | KeeperException e) {
    throw new RegistryException(ErrorCode.Unknown, e) ;
  }
}
 
Example #10
Source File: MesosLogStreamModule.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
public MesosLogStreamModule(Options options, ZooKeeperConfig zkClientConfig) {
  this.options = options;
  requireArg(options.logPath, "native_log_file_path");
  requireArg(options.zkLogGroupPath, "native_log_zk_group_path");
  PathUtils.validatePath(options.zkLogGroupPath);
  this.zkClientConfig = zkClientConfig;
}
 
Example #11
Source File: ZooKeeperConnection.java    From util with Apache License 2.0 5 votes vote down vote up
public static String getParent(String path) {
    PathUtils.validatePath(path);
    if (path.equals("/")) {
        throw new IllegalArgumentException("parent of / is undefined");
    }
    final int index = path.lastIndexOf('/');
    if (index == 0) {
        return "/";
    }
    return path.substring(0, index);
}
 
Example #12
Source File: ZooKeeperConnection.java    From util with Apache License 2.0 5 votes vote down vote up
public static String getName(String path) {
    PathUtils.validatePath(path);
    if (path.equals("/")) {
        throw new IllegalArgumentException("name of / is undefined");
    }
    final int index = path.lastIndexOf('/');
    return path.substring(index+1);
}
 
Example #13
Source File: ZooKeeperConnection.java    From util with Apache License 2.0 5 votes vote down vote up
public static String buildPath(String parent, String firstPart, String... restOfParts) {
    PathUtils.validatePath(parent);
    if (firstPart.contains("/")) throw new IllegalArgumentException("only parent may contain / character");
    String path = (parent.equals("/") ? parent : parent+"/")+firstPart;
    for (String part : restOfParts) {
        if (part.contains("/")) throw new IllegalArgumentException("only parent may contain / character");
        path = path+"/"+part;
    }
    PathUtils.validatePath(path);
    return path;
}
 
Example #14
Source File: ZooKeeperClientConfig.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static ZooKeeperClientConfig createConfig(final NiFiProperties nifiProperties) {
    final String connectString = nifiProperties.getProperty(NiFiProperties.ZOOKEEPER_CONNECT_STRING);
    if (connectString == null || connectString.trim().isEmpty()) {
        throw new IllegalStateException("The '" + NiFiProperties.ZOOKEEPER_CONNECT_STRING + "' property is not set in nifi.properties");
    }
    final String cleanedConnectString = cleanConnectString(connectString);
    if (cleanedConnectString.isEmpty()) {
        throw new IllegalStateException("The '" + NiFiProperties.ZOOKEEPER_CONNECT_STRING +
                "' property is set in nifi.properties but needs to be in pairs of host:port separated by commas");
    }
    final long sessionTimeoutMs = getTimePeriod(nifiProperties, NiFiProperties.ZOOKEEPER_SESSION_TIMEOUT, NiFiProperties.DEFAULT_ZOOKEEPER_SESSION_TIMEOUT);
    final long connectionTimeoutMs = getTimePeriod(nifiProperties, NiFiProperties.ZOOKEEPER_CONNECT_TIMEOUT, NiFiProperties.DEFAULT_ZOOKEEPER_CONNECT_TIMEOUT);
    final String rootPath = nifiProperties.getProperty(NiFiProperties.ZOOKEEPER_ROOT_NODE, NiFiProperties.DEFAULT_ZOOKEEPER_ROOT_NODE);
    final String authType = nifiProperties.getProperty(NiFiProperties.ZOOKEEPER_AUTH_TYPE,NiFiProperties.DEFAULT_ZOOKEEPER_AUTH_TYPE);
    final String authPrincipal = nifiProperties.getKerberosServicePrincipal();
    final String removeHostFromPrincipal = nifiProperties.getProperty(NiFiProperties.ZOOKEEPER_KERBEROS_REMOVE_HOST_FROM_PRINCIPAL,
            NiFiProperties.DEFAULT_ZOOKEEPER_KERBEROS_REMOVE_HOST_FROM_PRINCIPAL);
    final String removeRealmFromPrincipal = nifiProperties.getProperty(NiFiProperties.ZOOKEEPER_KERBEROS_REMOVE_REALM_FROM_PRINCIPAL,
            NiFiProperties.DEFAULT_ZOOKEEPER_KERBEROS_REMOVE_REALM_FROM_PRINCIPAL);

    try {
        PathUtils.validatePath(rootPath);
    } catch (final IllegalArgumentException e) {
        throw new IllegalArgumentException("The '" + NiFiProperties.ZOOKEEPER_ROOT_NODE + "' property in nifi.properties is set to an illegal value: " + rootPath);
    }

    return new ZooKeeperClientConfig(cleanedConnectString, (int) sessionTimeoutMs, (int) connectionTimeoutMs, rootPath, authType, authPrincipal, removeHostFromPrincipal, removeRealmFromPrincipal);
}
 
Example #15
Source File: ZPathImpl.java    From curator with Apache License 2.0 5 votes vote down vote up
private static void validate(String nodeName)
{
    if ( isParameter(Objects.requireNonNull(nodeName, "nodeName cannot be null")) )
    {
        return;
    }
    if ( nodeName.equals(PATH_SEPARATOR) )
    {
        return;
    }
    PathUtils.validatePath(PATH_SEPARATOR + nodeName);
}
 
Example #16
Source File: AbstractCuratorFrameworkBuilder.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static String validateZNodePath(String znodePath) {
    try {
        PathUtils.validatePath(znodePath);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("znodePath: " + znodePath +
                                           " (reason: " + e.getMessage() + ')');
    }
    return znodePath;
}
 
Example #17
Source File: ZooKeeperPathUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Validates a ZooKeeper path.
 */
public static String validatePath(String path, String name) {
    requireNonNull(path, name);
    if (path.indexOf('/') > 0) {
        throw new IllegalArgumentException(name + " cannot have '/'. " + name + ": " + path);
    }
    try {
        // Simply prepend '/' to validate the path.
        PathUtils.validatePath('/' + path);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(name + ": " + path + " (reason: " + e.getMessage() + ')');
    }
    return path;
}
 
Example #18
Source File: ZkMapStore.java    From emodb with Apache License 2.0 5 votes vote down vote up
private String toPath(String key) {
    checkArgument(key.indexOf('/') == -1, "Keys may not contain '/'.");
    // The key may contain special characters which are invalid in ZooKeeper paths.  Encode them
    String encodedKey = encodeKey(key);
    String path = ZKPaths.makePath(_zkPath, encodedKey);
    PathUtils.validatePath(path);
    return path;
}
 
Example #19
Source File: RegistryTestHelper.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Assert the path is valid by ZK rules
 * @param path path to check
 */
public static void assertValidZKPath(String path) {
  try {
    PathUtils.validatePath(path);
  } catch (IllegalArgumentException e) {
    throw new IllegalArgumentException("Invalid Path " + path + ": " + e, e);
  }
}
 
Example #20
Source File: RegistryPathUtils.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Validate ZK path with the path itself included in
 * the exception text
 * @param path path to validate
 * @return the path parameter
 * @throws InvalidPathnameException if the pathname is invalid.
 */
public static String validateZKPath(String path) throws
    InvalidPathnameException {
  try {
    PathUtils.validatePath(path);

  } catch (IllegalArgumentException e) {
    throw new InvalidPathnameException(path,
        "Invalid Path \"" + path + "\" : " + e, e);
  }
  return path;
}
 
Example #21
Source File: RegistryTestHelper.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Assert the path is valid by ZK rules
 * @param path path to check
 */
public static void assertValidZKPath(String path) {
  try {
    PathUtils.validatePath(path);
  } catch (IllegalArgumentException e) {
    throw new IllegalArgumentException("Invalid Path " + path + ": " + e, e);
  }
}
 
Example #22
Source File: RegistryPathUtils.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Validate ZK path with the path itself included in
 * the exception text
 * @param path path to validate
 * @return the path parameter
 * @throws InvalidPathnameException if the pathname is invalid.
 */
public static String validateZKPath(String path) throws
    InvalidPathnameException {
  try {
    PathUtils.validatePath(path);

  } catch (IllegalArgumentException e) {
    throw new InvalidPathnameException(path,
        "Invalid Path \"" + path + "\" : " + e, e);
  }
  return path;
}
 
Example #23
Source File: PathFactory.java    From helios with Apache License 2.0 4 votes vote down vote up
public PathFactory(final String base, final String... parts) {
  final String joined = join(base, parts);
  this.base = joined.startsWith("/") ? joined : "/" + joined;
  PathUtils.validatePath(base);
}
 
Example #24
Source File: ZkCopy.java    From helix with Apache License 2.0 4 votes vote down vote up
private static void zkCopy(HelixZkClient srcClient, String srcRootPath, HelixZkClient dstClient,
    String dstRootPath) {
  // Strip off tailing "/"
  if (!srcRootPath.equals("/") && srcRootPath.endsWith("/")) {
    srcRootPath = srcRootPath.substring(0, srcRootPath.length() - 1);
  }

  if (!dstRootPath.equals("/") && dstRootPath.endsWith("/")) {
    dstRootPath = dstRootPath.substring(0, dstRootPath.length() - 1);
  }

  // Validate paths
  PathUtils.validatePath(srcRootPath);
  PathUtils.validatePath(dstRootPath);

  if (srcRootPath.equals(dstRootPath)) {
    logger.info("srcPath == dstPath. Skip copying");
    return;
  }

  if (srcRootPath.startsWith(dstRootPath) || dstRootPath.startsWith(srcRootPath)) {
    throw new IllegalArgumentException(
        "srcPath/dstPath can't be prefix of dstPath/srcPath, was srcPath: " + srcRootPath
            + ", dstPath: " + dstRootPath);
  }

  // Recursive copy using BFS
  List<String> queue = new LinkedList<String>();
  String root = "";
  copy(srcClient, srcRootPath, dstClient, dstRootPath, Arrays.asList(root));

  queue.add(root);
  while (!queue.isEmpty()) {
    String path = queue.remove(0);
    String fromPath = concatenate(srcRootPath, path);

    List<String> children = srcClient.getChildren(fromPath);
    List<String> paths = new ArrayList<String>();
    if (children != null && children.size() > 0) {
      for (String child : children) {
        String childPath = concatenate(path, child);
        paths.add(childPath);
      }
      copy(srcClient, srcRootPath, dstClient, dstRootPath, paths);
      queue.addAll(paths);
    }
  }
}
 
Example #25
Source File: ZooKeeperUtils.java    From attic-aurora with Apache License 2.0 2 votes vote down vote up
/**
 * Validate and return a normalized zookeeper path which doesn't contain consecutive slashes and
 * never ends with a slash (except for root path).
 *
 * @param path the path to be normalized
 * @return normalized path string
 */
static String normalizePath(String path) {
  String normalizedPath = path.replaceAll("//+", "/").replaceFirst("(.+)/$", "$1");
  PathUtils.validatePath(normalizedPath);
  return normalizedPath;
}