1 /*------------------------------------------------------------------------------
  2 Name:      JndiDumper.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 
  7 package org.xmlBlaster.test.util;
  8 
  9 import java.io.PrintStream;
 10 import java.util.HashSet;
 11 import java.util.Hashtable;
 12 
 13 import javax.naming.Context;
 14 import javax.naming.InitialContext;
 15 import javax.naming.NameClassPair;
 16 import javax.naming.NamingEnumeration;
 17 import javax.naming.NamingException;
 18 
 19 import org.apache.naming.NamingService;
 20 
 21 
 22 /**
 23  * JndiDumper is used to analize the specified context.
 24  * 
 25  * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
 26  */
 27 public class JndiDumper {
 28 
 29    /**
 30     * Scans the context recursively
 31     * @param offset
 32     * @param context
 33     * @param out
 34     * @throws NamingException
 35     */
 36    public static void scanContext(String contextName, Context context, PrintStream out) throws NamingException {
 37       HashSet subCtx = new HashSet();
 38       if (context == null) context = new InitialContext();
 39       if (contextName == null) contextName = "/";
 40       out.println(contextName);
 41       StringBuffer buf = new StringBuffer(contextName.length());
 42       for (int i=0; i < contextName.length(); i++) buf.append(" ");
 43       String offset = buf.toString();
 44       NamingEnumeration enumer = context.list("");
 45       while (enumer.hasMore()) {
 46          try {
 47             NameClassPair pair = (NameClassPair)enumer.next();
 48             Object obj = context.lookup(pair.getName());
 49             if (obj instanceof Context) scanContext(contextName + pair.getName() + "/", (Context)obj, out);         
 50             else {
 51                out.println(offset +  pair.getName() + " class='" + pair.getClassName() + "'");            
 52    //            out.println(tmpOffset + " class='" + pair.getClassName() + "'");            
 53             }
 54          }
 55          catch (javax.naming.CommunicationException e) {
 56             out.println("Ignoring problem: " + e.toString());
 57          }
 58       }
 59    }
 60 
 61    /**
 62     * JNDI determines each property's value by merging the values from the following two sources, in order: 
 63     * <p/>
 64     * The first occurrence of the property from the constructor's environment parameter and (for appropriate properties) the applet parameters and system properties. 
 65     * The application resource files (jndi.properties).
 66     * <p/>
 67     * <pre>
 68     * com.sun.jndi.ldap.LdapCtxFactory
 69     * com.sun.jndi.fscontext.RefFSContextFactory
 70     * com.ibm.ejs.ns.jndi.CNInitialContextFactory
 71     * com.ibm.websphere.naming.WsnInitialContextFactory
 72     * org.jnp.interfaces.NamingContextFactory       (JBoss)
 73     *
 74     * java -Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory org.xmlBlaster.test.util.JndiDumper -startNamingService true -fillNames true
 75     *
 76     * Dump rmiregistry:
 77     * java -Djava.naming.factory.initial=com.sun.jndi.rmi.registry.RegistryContextFactory -Djava.naming.provider.url=rmi://localhost:1099 org.xmlBlaster.test.util.JndiDumper -fillNames false
 78     * JBoss:
 79     * java -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory  -Djava.naming.provider.url=jnp://localhost:1099 org.xmlBlaster.test.util.JndiDumper -fillNames false
 80     * </pre>
 81     */
 82    public static void main(String[] args) {
 83       try {
 84          boolean startNamingService = false;
 85          boolean fillNames = true;
 86          String factory = null;
 87          for (int i=0; i<args.length-1; i++) {
 88             if (args[i].equalsIgnoreCase("-startNamingService")) {
 89                startNamingService = (new Boolean(args[++i])).booleanValue();
 90             }
 91             else if (args[i].equalsIgnoreCase("-java.naming.factory.initial")) {
 92                factory = args[++i];
 93             }
 94             else if (args[i].equalsIgnoreCase("-fillNames")) {
 95                fillNames = (new Boolean(args[++i])).booleanValue();
 96             }
 97          }
 98 
 99          NamingService namingService = null;
100          if (startNamingService) {
101             namingService = new NamingService();
102             namingService.start();
103             System.out.println("Started an ebedded naming service.");
104          }
105 
106          Hashtable properties = new Hashtable();
107          if (factory != null) {
108             properties.put("java.naming.factory.initial", factory);
109             System.out.println("Forcing java.naming.factory.initial=" + factory);
110          }
111          else {
112             System.out.println("Using System.getProperty(java.naming.factory.initial)=" + System.getProperty("java.naming.factory.initial"));
113          }
114          
115          InitialContext context = new InitialContext(properties);
116          if (fillNames) {
117             context.bind("first", new String("first"));
118             context.bind("second", new String("first"));
119             context.bind("third", new String("first"));
120             context.createSubcontext("dir1");
121             context.createSubcontext("dir2");
122             context.createSubcontext("dir3");
123             context.bind("dir1/first", new String("first"));
124             context.bind("dir1/second", new String("first"));
125             context.bind("dir1/third", new String("first"));
126                      
127             Context ctx = (Context)context.lookup("dir2");
128             ctx.bind("first", new String("first"));
129             ctx.bind("second", new String("first"));
130             ctx.bind("third", new String("first"));
131          }
132          
133          System.out.println("================JNDI CONTENT START=============");
134          JndiDumper.scanContext(null, context, System.out);
135          System.out.println("================JNDI CONTENT END  =============");
136 
137          if (namingService != null) namingService.stop();
138       }
139       catch (Exception ex) {
140          ex.getMessage();
141          ex.printStackTrace();
142       }
143    }
144 }


syntax highlighted by Code2HTML, v. 0.9.1