/** * Probe for a path existing. * * @param path path of operation * @return true if the path was visible from the ZK server * queried. * @throws IOException on any exception other than * {@link PathNotFoundException} */ public boolean zkPathExists(String path) throws IOException { checkServiceLive(); try { // if zkStat(path) returns without throwing an exception, the return value // is guaranteed to be not null zkStat(path); return true; } catch (PathNotFoundException e) { return false; } catch (IOException e) { throw e; } }
/** * Probe for a path existing * @param path path of operation * @return true if the path was visible from the ZK server * queried. * @throws IOException on any exception other than * {@link PathNotFoundException} */ public boolean zkPathExists(String path) throws IOException { checkServiceLive(); try { // if zkStat(path) returns without throwing an exception, the return value // is guaranteed to be not null zkStat(path); return true; } catch (PathNotFoundException e) { return false; } catch (IOException e) { throw e; } }
/** * Read data on a path * @param path path of operation * @return the data * @throws IOException read failure */ public byte[] zkRead(String path) throws IOException { checkServiceLive(); String fullpath = createFullPath(path); try { if (LOG.isDebugEnabled()) { LOG.debug("Reading {}", fullpath); } return curator.getData().forPath(fullpath); } catch (Exception e) { throw operationFailure(fullpath, "read()", e); } }
/** * Read data on a path. * * @param path path of operation * @return the data * @throws IOException read failure */ public byte[] zkRead(String path) throws IOException { checkServiceLive(); String fullpath = createFullPath(path); try { if (LOG.isDebugEnabled()) { LOG.debug("Reading {}", fullpath); } return curator.getData().forPath(fullpath); } catch (Exception e) { throw operationFailure(fullpath, "read()", e); } }
/** * List all children of a path * @param path path of operation * @return a possibly empty list of children * @throws IOException */ public List<String> zkList(String path) throws IOException { checkServiceLive(); String fullpath = createFullPath(path); try { if (LOG.isDebugEnabled()) { LOG.debug("ls {}", fullpath); } GetChildrenBuilder builder = curator.getChildren(); List<String> children = builder.forPath(fullpath); return children; } catch (Exception e) { throw operationFailure(path, "ls()", e); } }
/** * List all children of a path. * * @param path path of operation * @return a possibly empty list of children * @throws IOException */ public List<String> zkList(String path) throws IOException { checkServiceLive(); String fullpath = createFullPath(path); try { if (LOG.isDebugEnabled()) { LOG.debug("ls {}", fullpath); } GetChildrenBuilder builder = curator.getChildren(); List<String> children = builder.forPath(fullpath); return children; } catch (Exception e) { throw operationFailure(path, "ls()", e); } }
/** * Update the data for a path * @param path path of operation * @param data new data * @throws IOException */ public void zkUpdate(String path, byte[] data) throws IOException { Preconditions.checkArgument(data != null, "null data"); checkServiceLive(); path = createFullPath(path); try { if (LOG.isDebugEnabled()) { LOG.debug("Updating {} with {} bytes", path, data.length); } curator.setData().forPath(path, data); } catch (Exception e) { throw operationFailure(path, "update()", e); } }
/** * Update the data for a path. * * @param path path of operation * @param data new data * @throws IOException */ public void zkUpdate(String path, byte[] data) throws IOException { Preconditions.checkArgument(data != null, "null data"); checkServiceLive(); path = createFullPath(path); try { if (LOG.isDebugEnabled()) { LOG.debug("Updating {} with {} bytes", path, data.length); } curator.setData().forPath(path, data); } catch (Exception e) { throw operationFailure(path, "update()", e); } }
/** * Stat the file. * * @param path path of operation * @return a curator stat entry * @throws IOException on a failure * @throws PathNotFoundException if the path was not found */ public Stat zkStat(String path) throws IOException { checkServiceLive(); String fullpath = createFullPath(path); Stat stat; try { if (LOG.isDebugEnabled()) { LOG.debug("Stat {}", fullpath); } stat = curator.checkExists().forPath(fullpath); } catch (Exception e) { throw operationFailure(fullpath, "read()", e); } if (stat == null) { throw new PathNotFoundException(path); } return stat; }
/** * Stat the file * @param path path of operation * @return a curator stat entry * @throws IOException on a failure * @throws PathNotFoundException if the path was not found */ public Stat zkStat(String path) throws IOException { checkServiceLive(); String fullpath = createFullPath(path); Stat stat; try { if (LOG.isDebugEnabled()) { LOG.debug("Stat {}", fullpath); } stat = curator.checkExists().forPath(fullpath); } catch (Exception e) { throw operationFailure(fullpath, "read()", e); } if (stat == null) { throw new PathNotFoundException(path); } return stat; }
/** * Create a path with given data. byte[0] is used for a path * without data * @param path path of operation * @param data initial data * @param acls * @throws IOException */ public void zkCreate(String path, CreateMode mode, byte[] data, List<ACL> acls) throws IOException { Preconditions.checkArgument(data != null, "null data"); checkServiceLive(); String fullpath = createFullPath(path); try { if (LOG.isDebugEnabled()) { LOG.debug("Creating {} with {} bytes of data and ACL {}", fullpath, data.length, new RegistrySecurity.AclListInfo(acls)); } curator.create().withMode(mode).withACL(acls).forPath(fullpath, data); } catch (Exception e) { throw operationFailure(fullpath, "create()", e, acls); } }
/** * Get the ACLs of a path * @param path path of operation * @return a possibly empty list of ACLs * @throws IOException */ public List<ACL> zkGetACLS(String path) throws IOException { checkServiceLive(); String fullpath = createFullPath(path); List<ACL> acls; try { if (LOG.isDebugEnabled()) { LOG.debug("GetACLS {}", fullpath); } acls = curator.getACL().forPath(fullpath); } catch (Exception e) { throw operationFailure(fullpath, "read()", e); } if (acls == null) { throw new PathNotFoundException(path); } return acls; }
/** * Create a path with given data. byte[0] is used for a path * without data. * * @param path path of operation * @param data initial data * @param acls * @throws IOException */ public void zkCreate(String path, CreateMode mode, byte[] data, List<ACL> acls) throws IOException { Preconditions.checkArgument(data != null, "null data"); checkServiceLive(); String fullpath = createFullPath(path); try { if (LOG.isDebugEnabled()) { LOG.debug("Creating {} with {} bytes of data and ACL {}", fullpath, data.length, new RegistrySecurity.AclListInfo(acls)); } curator.create().withMode(mode).withACL(acls).forPath(fullpath, data); } catch (Exception e) { throw operationFailure(fullpath, "create()", e, acls); } }
List<ACL> acls) throws IOException { checkServiceLive(); path = createFullPath(path); if (acls == null || acls.isEmpty()) {
boolean recursive, BackgroundCallback backgroundCallback) throws IOException { checkServiceLive(); String fullpath = createFullPath(path); try {
/** * Get the ACLs of a path. * * @param path path of operation * @return a possibly empty list of ACLs * @throws IOException */ public List<ACL> zkGetACLS(String path) throws IOException { checkServiceLive(); String fullpath = createFullPath(path); List<ACL> acls; try { if (LOG.isDebugEnabled()) { LOG.debug("GetACLS {}", fullpath); } acls = curator.getACL().forPath(fullpath); } catch (Exception e) { throw operationFailure(fullpath, "read()", e); } if (acls == null) { throw new PathNotFoundException(path); } return acls; }
List<ACL> acls) throws IOException { checkServiceLive(); path = createFullPath(path); if (acls == null || acls.isEmpty()) {
boolean recursive, BackgroundCallback backgroundCallback) throws IOException { checkServiceLive(); String fullpath = createFullPath(path); try {
/** * Create or update an entry * @param path path * @param data data * @param acl ACL for path -used when creating a new entry * @param overwrite enable overwrite * @throws IOException * @return true if the entry was created, false if it was simply updated. */ public boolean zkSet(String path, CreateMode mode, byte[] data, List<ACL> acl, boolean overwrite) throws IOException { Preconditions.checkArgument(data != null, "null data"); checkServiceLive(); if (!zkPathExists(path)) { zkCreate(path, mode, data, acl); return true; } else { if (overwrite) { zkUpdate(path, data); return false; } else { throw new FileAlreadyExistsException(path); } } }
/** * Create or update an entry. * * @param path path * @param data data * @param acl ACL for path -used when creating a new entry * @param overwrite enable overwrite * @return true if the entry was created, false if it was simply updated. * @throws IOException */ public boolean zkSet(String path, CreateMode mode, byte[] data, List<ACL> acl, boolean overwrite) throws IOException { Preconditions.checkArgument(data != null, "null data"); checkServiceLive(); if (!zkPathExists(path)) { zkCreate(path, mode, data, acl); return true; } else { if (overwrite) { zkUpdate(path, data); return false; } else { throw new FileAlreadyExistsException(path); } } }