Can any one tell or point me to code to list all the jndi entries in a remote machine
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.
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.
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.
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" } } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With