How to list the objects bound in the IIOP context in JBoss EAP 7 / 6

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 7.x
    • 6.x

Issue

  • How to list the objects bound in the IIOP context in JBoss EAP 7

Resolution

Below is a standalone Java client example that uses the JDK com.sun.jndi.cosnaming.CNCtxFactory and invokes context.list to list out the contents of the IIOP naming context.

This follows up on the example in: How to expose an EJB via IIOP and call it in JBoss EAP 7 / 6

import java.util.Hashtable;
import java.rmi.RMISecurityManager;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.NameClassPair;
import java.util.List;
import java.util.ArrayList;
import java.util.Properties;

public class JDKClient {

    public static void main(String[] args) throws Exception {
        // Setup security for stub downloading.  See conf/security.policy in
        // this project as well as this project's build.xml 'run' target for
        // additional required configuration.
        // Note this can be removed and use -Djava.security.manager instead when running the
        //  standalone java client
        if ( System.getSecurityManager() == null )
        {
            System.setSecurityManager(new RMISecurityManager());
        }

        System.setProperty("com.sun.CORBA.ORBUseDynamicStub", "true");

        listAllIIOPBindings("localhost");
    }

    private static void listAllIIOPBindings(String host) throws NamingException {
        InitialContext ctx = getIIOPInitialContext(host);
        String root = "";
        List<String> contexts = new ArrayList();
        contexts.add("");
        while(!contexts.isEmpty()) {
          root = (root.equals("")) ? contexts.remove(0) : root + "/" + contexts.remove(0);
          // list the context
          NamingEnumeration ne = ctx.list(root);
          // loop through context's children
          while(ne.hasMore()) {
            NameClassPair ncp = (NameClassPair) ne.next();
            String childName = (root.equals("")) ? ncp.getName() : root + "/" + ncp.getName();
            System.out.println(childName); System.out.flush();
            Object o = ctx.lookup(childName);
            // add any Contexts to the context list to search 
            if(o instanceof Context) {
              if(root.equals(""))
                contexts.add(root + ncp.getName());
              else
                contexts.add(root + "/" + ncp.getName());
            }
        }
       }
    }

    protected static InitialContext getIIOPInitialContext(String host) throws NamingException {
        Hashtable<String,String> env = new Hashtable<String,String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
        env.put(Context.PROVIDER_URL, "corbaloc::" + host + ":3528/JBoss/Naming/root");
        return new InitialContext(env);
    }

Related Solutions:

How to expose an EJB via IIOP and call it in JBoss EAP 7 / 6

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.