Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package foo;
- import java.util.HashMap;
- import java.util.Map;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NameClassPair;
- import javax.naming.NamingEnumeration;
- import javax.naming.NamingException;
- public class JNDITree {
- private Context context = null;
- public static void main(String[] args) throws Exception {
- System.out.println("starting..");
- new JNDITree().printJNDITree("");
- System.out.println("DONE");
- }
- public JNDITree() throws NamingException {
- setEnv();
- }
- private void setEnv() throws NamingException {
- Map<String, String> env = new HashMap();
- context = new InitialContext();
- System.out.println(context.getEnvironment().toString());
- }
- private void printJNDITree(String ct) {
- try {
- printNE(context.list(ct), ct);
- } catch (NamingException e) {
- System.out.println(e.toString());
- }
- }
- private void printNE(NamingEnumeration ne, String parentctx) throws NamingException {
- while (ne.hasMoreElements()) {
- NameClassPair next = (NameClassPair) ne.nextElement();
- printEntry(next);
- increaseIndent();
- printJNDITree((parentctx.length() == 0) ? next.getName() : parentctx + "/" + next.getName());
- decreaseIndent();
- }
- }
- private void printEntry(NameClassPair next) {
- System.out.println(printIndent() + "-->" + next);
- }
- private int indentLevel = 0;
- private void increaseIndent() {
- indentLevel += 4;
- }
- private void decreaseIndent() {
- indentLevel -= 4;
- }
- private String printIndent() {
- StringBuffer buf = new StringBuffer(indentLevel);
- for (int i = 0; i < indentLevel; i++) {
- buf.append(" ");
- }
- return buf.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment