thufir

Untitled

Mar 12th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. package foo;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import javax.naming.Context;
  6. import javax.naming.InitialContext;
  7. import javax.naming.NameClassPair;
  8. import javax.naming.NamingEnumeration;
  9. import javax.naming.NamingException;
  10.  
  11. public class JNDITree {
  12.  
  13. private Context context = null;
  14.  
  15. public static void main(String[] args) throws Exception {
  16. System.out.println("starting..");
  17. new JNDITree().printJNDITree("");
  18. System.out.println("DONE");
  19. }
  20.  
  21. public JNDITree() throws NamingException {
  22. setEnv();
  23. }
  24.  
  25. private void setEnv() throws NamingException {
  26. Map<String, String> env = new HashMap();
  27. context = new InitialContext();
  28. System.out.println(context.getEnvironment().toString());
  29. }
  30.  
  31. private void printJNDITree(String ct) {
  32. try {
  33. printNE(context.list(ct), ct);
  34. } catch (NamingException e) {
  35. System.out.println(e.toString());
  36. }
  37. }
  38.  
  39. private void printNE(NamingEnumeration ne, String parentctx) throws NamingException {
  40. while (ne.hasMoreElements()) {
  41. NameClassPair next = (NameClassPair) ne.nextElement();
  42. printEntry(next);
  43. increaseIndent();
  44. printJNDITree((parentctx.length() == 0) ? next.getName() : parentctx + "/" + next.getName());
  45. decreaseIndent();
  46. }
  47. }
  48.  
  49. private void printEntry(NameClassPair next) {
  50. System.out.println(printIndent() + "-->" + next);
  51. }
  52.  
  53. private int indentLevel = 0;
  54.  
  55. private void increaseIndent() {
  56. indentLevel += 4;
  57. }
  58.  
  59. private void decreaseIndent() {
  60. indentLevel -= 4;
  61. }
  62.  
  63. private String printIndent() {
  64. StringBuffer buf = new StringBuffer(indentLevel);
  65. for (int i = 0; i < indentLevel; i++) {
  66. buf.append(" ");
  67. }
  68. return buf.toString();
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment