Code to list all the entries in jndi on remote machine

JavaJndi

Java Problem Overview


Can any one tell or point me to code to list all the jndi entries in a remote machine

Java Solutions


Solution 1 - Java

It is possible to list all entries of an InitialContext. You can use this snippet:

InitialContext ctx = new InitialContext();
NamingEnumeration<NameClassPair> list = ctx.list("");
while (list.hasMore()) {
  System.out.println(list.next().getName());
}

If you are using an application server, there is usually the option to browse the JNDI tree.

Solution 2 - Java

The previous answers didn't give me the "full picture" (on Tomcat7), so I've thrown together the following amalgamation, which converts the JNDI values to a Tree Map (with toString values):

import javax.naming.*;
...

public static Map toMap(Context ctx) throws NamingException {
    String namespace = ctx instanceof InitialContext ? ctx.getNameInNamespace() : "";
    HashMap<String, Object> map = new HashMap<String, Object>();
    log.info("> Listing namespace: " + namespace);
    NamingEnumeration<NameClassPair> list = ctx.list(namespace);
    while (list.hasMoreElements()) {
        NameClassPair next = list.next();
        String name = next.getName();
        String jndiPath = namespace + name;
        Object lookup;
        try {
            log.info("> Looking up name: " + jndiPath);
            Object tmp = ctx.lookup(jndiPath);
            if (tmp instanceof Context) {
                lookup = toMap((Context) tmp);
            } else {
                lookup = tmp.toString();
            }
        } catch (Throwable t) {
            lookup = t.getMessage();
        }
        map.put(name, lookup);

    }
    return map;
}

Usage:

toMap(new InitialContext());

Gives the following output in Tomcat7:

{
  "comp": {
    "env": {
      "myCustomVar": "foobar"
    },
    "UserTransaction": "Cannot create resource instance",
    "Resources": {
      "index.html": "org.apache.naming.resources.FileDirContext$FileResource@32edeea8",
      "WEB-INF": {
        "ibm-web-ext.xml": "org.apache.naming.resources.FileDirContext$FileResource@6132b73b",
        "ibm-web-bnd.xml": "org.apache.naming.resources.FileDirContext$FileResource@22cf71b7"
      }
    }
  }
}

Solution 3 - Java

I needed to list all JDBC datasources available in a context (tomee context).

In my case, I needed more than list("") (sadly, this didn't work for me) to reach my goal. I found a naming environment list here: Naming Environment for J2EE Application Components

Having this, I got all available JDBC resources using following code snippet:

InitialContext ctx = new InitialContext();
NamingEnumeration<NameClassPair> list = ctx.list("java:comp/env/jdbc");
while (list.hasMore()) {
    System.out.println(list.next().getName());
}

That's all.

I hope this can helps someone else, as helps me.

Solution 4 - Java

I'm using following code (not for production):

public void discoverJndi(String path, Context context) throws TestClientException, NamingException {
    try {
        NamingEnumeration<NameClassPair> list = context.list(path);
        while (list.hasMore()) {
            String name = list.next().getName();
            String child = path.equals("") ? name : path + "/" + name;
            System.out.println(child);
            discoverJndi(child, context);
        }
    } catch (NotContextException e) {}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnand SunderramanView Question on Stackoverflow
Solution 1 - JavaSteveView Answer on Stackoverflow
Solution 2 - JavaNick GrealyView Answer on Stackoverflow
Solution 3 - JavaVielinkoView Answer on Stackoverflow
Solution 4 - JavaAlexeyView Answer on Stackoverflow