Exemple #1
0
  /**
   * Rebuild the point-in-time snapshot of the children objects and set watches on new
   * children. @Param event may be null on the first initialization.
   */
  private void processParentEvent(WatchedEvent event) throws Exception {
    // get current children snapshot and reset this watch.
    Set<String> children = new TreeSet<String>(m_zk.getChildren(m_rootNode, m_parentWatch));
    // intersect to get newChildren and update m_lastChildren to the current set.
    Set<String> newChildren = new HashSet<String>(children);
    newChildren.removeAll(m_lastChildren);
    m_lastChildren = children;

    List<ByteArrayCallback> callbacks = new ArrayList<ByteArrayCallback>();
    for (String child : children) {
      ByteArrayCallback cb = new ByteArrayCallback();
      // set watches on new children.
      if (newChildren.contains(child)) {
        m_zk.getData(ZKUtil.joinZKPath(m_rootNode, child), m_childWatch, cb, null);
      } else {
        m_zk.getData(ZKUtil.joinZKPath(m_rootNode, child), false, cb, null);
      }

      callbacks.add(cb);
    }

    HashMap<String, JSONObject> cache = new HashMap<String, JSONObject>();
    for (ByteArrayCallback callback : callbacks) {
      try {
        byte payload[] = callback.getData();
        JSONObject jsObj = new JSONObject(new String(payload, "UTF-8"));
        cache.put(callback.getPath(), jsObj);
      } catch (KeeperException.NoNodeException e) {
        // child may have been deleted between the parent trigger and getData.
      }
    }

    m_publicCache.set(ImmutableMap.copyOf(cache));
  }
Exemple #2
0
 /** Helper to produce a valid path from variadic strings. */
 public static String path(String... components) {
   String path = components[0];
   for (int i = 1; i < components.length; i++) {
     path = ZKUtil.joinZKPath(path, components[i]);
   }
   return path;
 }