Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code to list all the entries in jndi on remote machine

Tags:

java

jndi

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

like image 230
Anand Sunderraman Avatar asked Feb 24 '10 09:02

Anand Sunderraman


People also ask

What does JNDI lookup () Menthod do?

The Java Naming and Directory Interface (JNDI) is a Java API for a directory service that allows Java software clients to discover and look up data and resources (in the form of Java objects) via a name.

Which file is used for JNDI lookup?

A JAR file implementing JNDI, jndi. jar , is available with OC4J. Your application can take advantage of the JNDI API without having to provide any other libraries or JAR files.

What is JNDI context factory?

You use an initial context factory to obtain an initial context–a reference to a JNDI namespace. Using the initial context, you can use the JNDI API to look up an enterprise bean, resource manager connection factory, environment variable, or other JNDI-accessible object.


2 Answers

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.

like image 139
Steve Avatar answered Sep 18 '22 13:09

Steve


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"       }     }   } } 
like image 28
Nick Grealy Avatar answered Sep 21 '22 13:09

Nick Grealy