Skip to main content

[JNDI] Listing all JNDI entries

3 replies [Last post]

First of all, I am a new Glasfish user, so hi everyone :-)
I wrote code to recursively list JNDI in glassfish):

<br />
public class JNDITestServlet extends HttpServlet {</p>
<p>    @Override<br />
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {<br />
        response.setContentType("text/plain");<br />
        PrintWriter out = response.getWriter();<br />
        try {<br />
            InitialContext ictx = new InitialContext();<br />
            Context ctx = (Context) ictx.lookup("java:");<br />
            out.println("java: = " + ctx.getClass().getName());<br />
            printContext(out, ctx, 1);<br />
        } catch (Exception exc) {<br />
            throw new ServletException(exc);<br />
        }<br />
<p>    private void printContext(PrintWriter out, Context ctx, int indent) throws ServletException, IOException, NamingException {<br />
        NamingEnumeration en = ctx.listBindings("");<br />
        while (en.hasMore()) {<br />
            Binding b =;<br />
            char[] tabs = new char[indent];<br />
            Arrays.fill(tabs, '\t');<br />
            out.println(new String(tabs) + b.getName() + " = " + b.getClassName());<br />
            try {<br />
                if (b.getObject() instanceof Context) {<br />
                    printContext(out, (Context) b.getObject(), indent + 1);<br />
                }<br />
            } catch (Exception exc) {<br />
                throw new ServletException(exc);<br />
            }<br />
        }<br />
    }<br />
}<br />

The only thing that happens when I go to the page generated by this servlet is:
java: =
When I debug, it turns out that the enumeration is empty. The same happens no matter if I start with "java:" (as posted), "java:/comp", "java:comp/env" - it only prints the context url and class, and that's it. When I run ths code in tomcat 6.0.18, and start with "java:", the "comp", then "env" context are listed, and their respective bindings are also listed. What am I doing wrong here? Version used:
Version = Sun Java System Application Server 9.1_02
as printed by "asadmin version".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-13

It does not exist in GlassFish. GF only provides a way seeing the global JNDI tree, but not a way of dumping the local one for each particular component. The JNDI tree of one servlet or EJB can be dramatically different from that of another, and you can only "see" that tree from within the component.

I too would like to see a method that fully dumps the JNDI tree, because the naive solution doesn't seem to work.

Joined: 2005-10-28

This functionality already exists in GlassFish :)

Just try asadmin list-jndi-entries. If it gives you nothing, it implies that nothing is available in the JNDI naming service (things are registered lazily here, I believe).



Yes, it does exist indeed, but that's not what I asked about. I would like to list it myself. I am beginning with Glassfish and EJB3, I want to learn it well, so I write this and that, just to see the results.
Please don't tell me I shouldn't / can't / mustn't do this - it's not the point, unless it really can't be done (but it can, as there is a tool for that ;-)).
Maybe someone could tell me which class implements the ValidCommand interface in Glassfish sources, so that I could peek how this is done? (This is how far I got in the sources myself yesterday :-))
Thanks you.