0

I want to list() or listBindings() for everything in Glassfish JNDI. I want the whole tree, either as an object, or just print to the console.

Definitely the EJB is deployed, and therefore is in the JNDI tree for Glassfish:

thufir@doge:~$ 
thufir@doge:~$ asadmin list-applications
SimpleEJB  <ear, ejb>  
Command list-applications executed successfully.
thufir@doge:~$ 
thufir@doge:~$ appclient -client NetBeansProjects/corba/JNDI/dist/JNDI.jar 
starting..
printing tree...
{org.omg.CORBA.ORBInitialPort=3700, java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, Context.SECURITY_CREDENTIALS=pass123, org.omg.CORBA.ORBInitialHost=localhost, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, Context.SECURITY_PRINCIPAL=user1}
..done
thufir@doge:~$ 

But, how do I actually connect to the Glassfish JNDI tree?

package jndi;

import javax.naming.Binding;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

public class JndiTree {

    public static void main(String[] args) throws NamingException {
        System.out.println("starting..");
        new JndiTree().printJndiTree();
        System.out.println("..done");
    }

    private void printJndiTree() throws NamingException {
        System.out.println("printing tree...");
        InitialContext context = new InitialContext();
        System.out.println(context.getEnvironment().toString());
        NamingEnumeration list = context.list("");
        NamingEnumeration<Binding> bindings = context.listBindings("");
        while (list.hasMore()) {
            NameClassPair nameClassPair = (NameClassPair) list.next();
            System.out.println(nameClassPair);
        }
        while (bindings.hasMore()) {
            Binding binding = bindings.next();
            System.out.println(binding.getClassName());
            System.out.println(binding.toString());
        }
    }
}

Certainly, I'm not the only one to want to print JNDI the tree:

http://tripoverit.blogspot.ca/2007/03/print-jndi-tree.html

As Glassfish logs show, it has a JNDI name:

[2015-03-12T04:56:36.156-0700] [glassfish 4.1] [INFO] [AS-EJB-00054] [javax.enterprise.ejb.container] [tid: _ThreadID=42 _ThreadName=admin-listener(2)] [timeMillis: 1426161396156] [levelValue: 800] [[
  Portable JNDI names for EJB NewSessionBean: [java:global/SimpleEJB/SimpleEJB-ejb/NewSessionBean, java:global/SimpleEJB/SimpleEJB-ejb/NewSessionBean!jkl.NewSessionBeanLocal]]]

This is in the context of understanding CORBA lookup's:

CORBA lookup on a LAN hangs

All of the above code is on the broker. Also, jndi-entries on the broker are:

thufir@doge:~$ 
thufir@doge:~$ asadmin list-jndi-entries
java:global: com.sun.enterprise.naming.impl.TransientContext
arrakis: org.glassfish.resourcebase.resources.api.ResourceProxy
UserTransaction: com.sun.enterprise.transaction.startup.TransactionLifecycleService$2
concurrent: com.sun.enterprise.naming.impl.TransientContext
com.sun.enterprise.container.common.spi.util.InjectionManager: com.sun.enterprise.container.common.impl.util.InjectionManagerImpl
jms: com.sun.enterprise.naming.impl.TransientContext
ejb: com.sun.enterprise.naming.impl.TransientContext
jdbc: com.sun.enterprise.naming.impl.TransientContext
Command list-jndi-entries executed successfully.
thufir@doge:~$ 

The following utility:

private List<JndiEntry> browse(final String path) throws NamingException {
    final JndiCallback<List<JndiEntry>> contextCallback = new JndiCallback<List<JndiEntry>>() {
        @Override
        public List<JndiEntry> doInContext(final Context context) throws NamingException {
            if (JAVA_GLOBAL.equals(path)) {
                // Do a little trick to handle "java:global"
                final NamingEnumeration<Binding> root = context.listBindings("");
                Context javaGlobalContext = null;
                while (root.hasMore()) {
                    final Binding binding = root.next();
                    if (JAVA_GLOBAL.equals(binding.getName())) {
                        final Object obj = binding.getObject();
                        if (obj instanceof Context) {
                            javaGlobalContext = (Context) obj;
                        }
                        break;
                    }
                }
                if (javaGlobalContext != null) {
                    return examineBindings(javaGlobalContext, path, javaGlobalContext.listBindings(""));
                }
                logger.warning("Unable to browse \"" + JAVA_GLOBAL + "\" namespace!");
                return emptyList();
            }
            return examineBindings(context, path, context.listBindings(path));
        }
    };
    return jndiTemplate.execute(contextCallback);
}

http://code.google.com/p/jndi-view/source/browse/trunk/src/main/java/jndi/view/JndiView.java

seems to, somehow, get all the entries for the tree. I'm not sure how...

Community
  • 1
  • 1
Thufir
  • 8,216
  • 28
  • 125
  • 273

0 Answers0