@Override public Boolean setPrefix(final String prefix, final String namespace) { if (data.containsKey(prefix)) { return false; } data.put(prefix, namespace); try { client.create().orSetData().forPath(ZNODE_NAMESPACES + PATH_SEPARATOR + prefix, namespace.getBytes(UTF_8)); return true; } catch (final Exception ex) { LOGGER.error("Unable to set data: {}", ex.getMessage()); } return false; }
public void write(String key, byte[] bytes) { try { client.create().orSetData().creatingParentsIfNeeded().forPath(key, bytes); } catch (Exception e) { log.error("ZooKeeperWriteProvider.write", e); throw new RuntimeException(e); } } }
private Map<String, String> init(final String filePath) throws Exception { final Map<String, String> namespaces = new HashMap<>(); if (nonNull(filePath)) { try (final FileInputStream input = new FileInputStream(new File(filePath))) { // TODO - JDK9 InputStream::readAllBytes final Map<String, String> ns = read(IOUtils.toByteArray(input)); for (final Map.Entry<String, String> entry : ns.entrySet()) { client.create().orSetData().forPath(ZNODE_NAMESPACES + PATH_SEPARATOR + entry.getKey(), entry.getValue().getBytes(UTF_8)); namespaces.put(entry.getKey(), entry.getValue()); } } catch (final IOException ex) { LOGGER.warn("Unable to read provided file: {}", ex.getMessage()); } } readTree(cache.getCurrentChildren(ZNODE_NAMESPACES)).forEach(namespaces::put); return namespaces; }
/** * Create a zookeeper-based namespace service * @param client the curator client * @param cache the treecache * @param filePath the file */ public Namespaces(final CuratorFramework client, final TreeCache cache, final String filePath) { requireNonNull(cache, "TreeCache may not be null!"); this.client = client; this.cache = cache; try { this.client.create().orSetData().forPath(ZNODE_NAMESPACES); this.cache.getListenable().addListener((c, e) -> { final Map<String, ChildData> tree = cache.getCurrentChildren(ZNODE_NAMESPACES); readTree(tree).forEach(data::put); }); init(filePath).forEach(data::put); } catch (final Exception ex) { LOGGER.error("Could not create a zk node cache: {}", ex); throw new RuntimeTrellisException(ex); } }
String path = "/xio/watched/node-init"; client.create().orSetData().creatingParentsIfNeeded().forPath(path, payload.getBytes());
client.create().orSetData().creatingParentsIfNeeded().forPath(path, payload.getBytes());
client.create().orSetData().creatingParentsIfNeeded().forPath(path, bytes);