@Override public List<PropertyItem> findProperties(String node) { LOGGER.debug("Find properties in node: [{}]", node); List<PropertyItem> properties = Lists.newArrayList(); try { Stat stat = getClient().checkExists().forPath(node); if (stat != null) { GetChildrenBuilder childrenBuilder = getClient().getChildren(); List<String> children = childrenBuilder.forPath(node); GetDataBuilder dataBuilder = getClient().getData(); if (children != null) { for (String child : children) { String propPath = ZKPaths.makePath(node, child); PropertyItem item = new PropertyItem(child, new String(dataBuilder.forPath(propPath), Charsets.UTF_8)); properties.add(item); } } } } catch (Exception e) { throw Throwables.propagate(e); } return properties; }
@Override public List<String> listChildren(String node) { LOGGER.debug("Find children of node: [{}]", node); List<String> children = null; try { Stat stat = getClient().checkExists().forPath(node); if (stat != null) { GetChildrenBuilder childrenBuilder = getClient().getChildren(); children = childrenBuilder.forPath(node); } } catch (Exception e) { throw Throwables.propagate(e); } return children; }
@Test public void testCanReadFromZookeeper() throws Exception { CuratorFramework curatorFramework = mock(CuratorFramework.class); ExistsBuilder existsBuilder = mock(ExistsBuilder.class); GetDataBuilder getDataBuilder = mock(GetDataBuilder.class); GetChildrenBuilder getChildrenBuilder = mock(GetChildrenBuilder.class); when(getDataBuilder.forPath(ConfigurationType.GLOBAL.getZookeeperRoot())).thenReturn(mockGlobalData()); when(curatorFramework.checkExists()).thenReturn(existsBuilder); when(curatorFramework.getData()).thenReturn(getDataBuilder); when(curatorFramework.getChildren()).thenReturn(getChildrenBuilder); when(getChildrenBuilder.forPath(anyString())).thenReturn(Collections.<String> emptyList()); Configuration configuration = new Configuration(Paths.get("foo")); configuration.curatorFramework = curatorFramework; configuration.update(); checkResult(configuration); }
/** * 加载节点并监听节点变化 */ void loadNode() { final String nodePath = ZKPaths.makePath(configProfile.getVersionedRootNode(), node); final GetChildrenBuilder childrenBuilder = client.getChildren(); try { final List<String> children = childrenBuilder.watched().forPath(nodePath); if (children != null) { final Map<String, String> configs = Maps.newHashMap(); for (String child : children) { final Pair<String, String> keyValue = loadKey(ZKPaths.makePath(nodePath, child)); if (keyValue != null) { configs.put(keyValue.getKey(), keyValue.getValue()); } } cleanAndPutAll(configs); } } catch (Exception e) { throw Throwables.propagate(e); } }
/** * Tests that if we attempt to delete a node that doesnt actually exist * just silently returns. * * To simulate a race condition we do this using mocks. */ @Test public void testDeleteNodeIfNoChildren_withNodeThatDoesntExist() throws Exception { final String basePath = "/testDeleteNodeIfNoChildren_withNodeThatDoesntExist"; final CuratorFramework mockCurator = mock(CuratorFramework.class); // Exists builder should return true saying our basePath exists. final ExistsBuilder mockExistsBuilder = mock(ExistsBuilder.class); when(mockExistsBuilder.forPath(eq(basePath))).thenReturn(new Stat()); when(mockCurator.checkExists()).thenReturn(mockExistsBuilder); // When we look for children, make sure it returns an empty list. final GetChildrenBuilder mockGetChildrenBuilder = mock(GetChildrenBuilder.class); when(mockGetChildrenBuilder.forPath(eq(basePath))).thenReturn(new ArrayList<>()); when(mockCurator.getChildren()).thenReturn(mockGetChildrenBuilder); // When we go to delete the actual node, we toss a no-node exception. // This effectively simulates a race condition between checking if the node exists (our mock above says yes) // and it being removed before we call delete on it. final DeleteBuilder mockDeleteBuilder = mock(DeleteBuilder.class); when(mockDeleteBuilder.forPath(eq(basePath))).thenThrow(new KeeperException.NoNodeException()); when(mockCurator.delete()).thenReturn(mockDeleteBuilder); // Now create our helper final CuratorHelper curatorHelper = new CuratorHelper(mockCurator); // Call our method curatorHelper.deleteNodeIfNoChildren(basePath); }
/** * 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); } }
@Override protected List<String> query(final boolean setListener) throws NakadiRuntimeException { final GetChildrenBuilder builder = curatorFramework.getChildren(); if (setListener) { builder.usingWatcher(this); } try { return builder.forPath(key); } catch (final Exception ex) { throw new NakadiRuntimeException(ex); } }
private ZooKeeperHolder createZooKeeperHolder() throws Exception { // GetChildrenBuilder final GetChildrenBuilder getChildrenBuilder = mock(GetChildrenBuilder.class); when(getChildrenBuilder.forPath("/brokers/topics")).thenReturn(allTopics()); // Curator Framework final CuratorFramework curatorFramework = mock(CuratorFramework.class); when(curatorFramework.getChildren()).thenReturn(getChildrenBuilder); // ZooKeeperHolder final ZooKeeperHolder zkHolder = mock(ZooKeeperHolder.class); when(zkHolder.get()).thenReturn(curatorFramework); return zkHolder; }
@Test public void defaultSpringApplicationNameWorks() { CuratorFramework curator = mock(CuratorFramework.class); when(curator.getChildren()).thenReturn(mock(GetChildrenBuilder.class)); ZookeeperPropertySourceLocator locator = new ZookeeperPropertySourceLocator(curator, new ZookeeperConfigProperties()); locator.locate(new MockEnvironment()); }
/** * Recursively expand the path into the supplied string builder, increasing * the indentation by {@link #INDENT} as it proceeds (depth first) down * the tree * @param builder string build to append to * @param path path to examine * @param indent current indentation */ private void expand(StringBuilder builder, String path, int indent) { try { GetChildrenBuilder childrenBuilder = curator.getChildren(); List<String> children = childrenBuilder.forPath(path); for (String child : children) { String childPath = path + "/" + child; String body; Stat stat = curator.checkExists().forPath(childPath); StringBuilder bodyBuilder = new StringBuilder(256); bodyBuilder.append(" [") .append(stat.getDataLength()) .append("]"); if (stat.getEphemeralOwner() > 0) { bodyBuilder.append("*"); } if (verbose) { // verbose: extract ACLs builder.append(" -- "); List<ACL> acls = curator.getACL().forPath(childPath); for (ACL acl : acls) { builder.append(RegistrySecurity.aclToString(acl)); builder.append(" "); } } body = bodyBuilder.toString(); // print each child append(builder, indent, ' '); builder.append('/').append(child); builder.append(body); builder.append('\n'); // recurse expand(builder, childPath, indent + INDENT); } } catch (Exception e) { builder.append(e.toString()).append("\n"); } }
@Override public GetChildrenBuilder getChildren() { return new MockGetChildrenBuilder(); }
@Override public GetChildrenBuilder getChildren() { return namespaceDelegate().getChildren(); }