Code to list all the entries in jndi on remote machine
Asked Answered
L

4

51

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

Lysol answered 24/2, 2010 at 9:40 Comment(0)
A
85

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.

Atomizer answered 24/2, 2010 at 9:54 Comment(2)
this doesn't the complete tree in tomcat. Only what the user has created within that instance. Is there a way to do that on tomcat?Idiophone
@John - added the following answer which (hopefully) addresses your issue (better late than never).Marked
M
30

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"
      }
    }
  }
}
Marked answered 4/5, 2016 at 7:21 Comment(2)
This is a great way of finding out where you are.Scag
Why can't namespace always be an empty string?Shih
E
22

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.

Emasculate answered 16/6, 2015 at 17:44 Comment(0)
N
3

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) {}
}
Neoteny answered 5/2, 2018 at 11:45 Comment(2)
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Haslet
TestClientException isn't a java exception.Apomixis

© 2022 - 2024 — McMap. All rights reserved.