Code to list all the entries in jndi on remote machine

让人想犯罪 __ 提交于 2019-11-27 17:41:50

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.

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"
      }
    }
  }
}

I know, there are lot of time from last answer, but I needed to list all jdbc datasource 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 next 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.

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) {}
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!