Wednesday, May 12, 2010

JNDI Tree viewer

If you have ever had to work with Weblogic 10... and actually had to use the JNDI tree, you will understand why I have the following piece of code. I know I grabbed bits of it off the net ages ago, no idea where though.

You just need the relevant jar in your classpath and the correct initial context and this should work for all J2EE servers.

If you want the whole JNDI Tree call with no context: printTree("");. If you aren't running on weblogic obviously remove "weblogic/jms".



package bob.blog;

import java.util.Properties;

import javax.naming.CannotProceedException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;


/**
 * The Class JNDITreePrinter.
 */
public class JNDITreePrinter {

    /**
     * The context.
     */
    private Context context = null;

    /**
     * The indent level.
     */
    private int indent = 0;

    /**
     * The main method.
     * 
     * Leave the param blank for the whole tree.
     * 
     * @param args the arguments
     * @throws Exception the exception
     */
    public static void main(String[] args) throws Exception {
    JNDITreePrinter printer = new JNDITreePrinter();
    System.out.println("----JMS---- ");
    printer.printTree("weblogic/jms");
    System.out.println("\n----JDBC---- ");
    printer.printTree("jdbc");
    System.out.println("\n----EJBS---- ");
    printer.printTree("ejb");
    System.out.println("--------");
    }

    /**
     * Instantiates a new jNDI tree printer.
     * 
     * @throws NamingException the naming exception
     */
    public JNDITreePrinter() throws NamingException {
    init();
    }

    /**
     * Inits the context.
     * 
     * @throws NamingException the naming exception
     */
    private void init() throws NamingException {
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    env.put(Context.SECURITY_PRINCIPAL, "user");
    env.put(Context.SECURITY_CREDENTIALS, "pass");
    env.put(Context.PROVIDER_URL, "t3://localhost:7001");
    context = new InitialContext(env);
    }

    /**
     * Prints the jndi tree.
     * 
     * @param ct the ct
     */
    public void printTree(String ct) {
    try {
        printTheEnum(context.list(ct), ct);
    } catch (CannotProceedException ce) {
        ce.printStackTrace();
    } catch (NamingException e) {
        e.printStackTrace();
    }

    }

    /**
     * Prints the NamingEnumeration.
     * 
     * @param namingEnum the naming enum
     * @param parentctx the parentctx to start with
     * @throws NamingException the naming exception
     */
    private void printTheEnum(NamingEnumeration<NameClassPair> namingEnum, String parentctx) throws NamingException {

    while (namingEnum.hasMoreElements()) {
        NameClassPair next = namingEnum.nextElement();
        System.out.println(createNextIndent() + "-->" + next);
        indent += 4;
        printTree((parentctx.length() == 0) ? next.getName() : parentctx + "/" + next.getName());
        indent -= 4;
    }

    }

    /**
     * Creates the next indent.
     * 
     * @return the string
     */
    private String createNextIndent() {
    StringBuilder bobthebuilder = new StringBuilder(indent);
    for (int i = 0; i < indent; i++) {
        bobthebuilder.append(" ");
    }
    return bobthebuilder.toString();
    }
}

1 comment:

Popular Posts

Followers