Beispiel #1
0
  /**
   * Clear the directory sub-tree starting with the node represented by the supplied distinguished
   * name.
   *
   * @param ctx The DirContext to use for cleaning the tree.
   * @param name the distinguished name of the root node.
   * @throws NamingException if anything goes wrong removing the sub-tree.
   */
  public static void clearSubContexts(DirContext ctx, Name name) throws NamingException {

    NamingEnumeration enumeration = null;
    try {
      enumeration = ctx.listBindings(name);
      while (enumeration.hasMore()) {
        Binding element = (Binding) enumeration.next();
        Name childName = LdapUtils.newLdapName(element.getName());
        childName = LdapUtils.prepend(childName, name);

        try {
          ctx.destroySubcontext(childName);
        } catch (ContextNotEmptyException e) {
          clearSubContexts(ctx, childName);
          ctx.destroySubcontext(childName);
        }
      }
    } catch (NamingException e) {
      e.printStackTrace();
    } finally {
      try {
        enumeration.close();
      } catch (Exception e) {
        // Never mind this
      }
    }
  }
  /** List resource paths (recursively), and store all of them in the given Set. */
  private static void listPaths(Set set, DirContext resources, String path) throws NamingException {

    Enumeration childPaths = resources.listBindings(path);
    while (childPaths.hasMoreElements()) {
      Binding binding = (Binding) childPaths.nextElement();
      String name = binding.getName();
      String childPath = path + "/" + name;
      set.add(childPath);
      Object object = binding.getObject();
      if (object instanceof DirContext) {
        listPaths(set, resources, childPath);
      }
    }
  }
  /** List resource paths (recursively), and store all of them in the given Set. */
  private static void listCollectionPaths(Set set, DirContext resources, String path)
      throws NamingException {

    Enumeration childPaths = resources.listBindings(path);
    while (childPaths.hasMoreElements()) {
      Binding binding = (Binding) childPaths.nextElement();
      String name = binding.getName();
      StringBuffer childPath = new StringBuffer(path);
      if (!"/".equals(path) && !path.endsWith("/")) childPath.append("/");
      childPath.append(name);
      Object object = binding.getObject();
      if (object instanceof DirContext) {
        childPath.append("/");
      }
      set.add(childPath.toString());
    }
  }