Wednesday, August 22, 2012

Get all JNDI objects in Java


try {
listContext((Context)new InitialContext().lookup(""), "");
} catch (NamingException ex) {
      LOG.warn("JNDI failure: ", ex);
}

/**
* Recursively exhaust the JNDI tree
* @throws NamingException
*/
public static final void listContext(Context ctx, String indent) throws NamingException {
  NamingEnumeration list = ctx.listBindings("");
  while (list.hasMore()) {
      Binding item = (Binding) list.next();
      String className = item.getClassName();
      String name = item.getName();
      LOG.info(indent + className + " " + name);
      Object o = item.getObject();
      if (o instanceof javax.naming.Context) {
      listContext((Context) o, indent + " ");
      }
  }
}