Listing Remote Objects
Home ] Up ] Remote Objects ] Remote Method Calls ] Components ] The RMI Registry ] A Simple Example ] [ Listing Remote Objects ] Extended Example ] Caveats ] Passing Objects in RMI ] Remote Callbacks ]

 

 

It can be useful to determine what objects are bound in the RMI registry.

Here's a program that does just that.  It's based on the textbook example, but augmented slightly.

package rmi;

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

/**
 * Class to list all the current RMI bindings
 *
 * @author Bryan Higgs
 * @version 1.0
 */
public class ShowRmiBindings
{
  /**
   * Main entry point
   * @param args the command line arguments
   *             args[0] contains the host name
   *             args[1] contains the port number
   */
  public static void main(String[] args)
  {
    String host = "localhost";  // the default
    int port = 1099;            // the default
    if (args.length > 0)
    {
      host = args[0];
      if (args.length > 1)
      {
        try
        {
          port = Integer.parseInt(args[1]);
        }
        catch (NumberFormatException nfe)
        {
          System.err.println("Port number " + args[1] + " invalid");
          return;
        }
      }
    }
    
    // Construct the rmi URL
    String rmiURL = "rmi://" + host + ":" + port;
    
    try
    {
      // Get the naming context
      Context namingContext = new InitialContext();
      // Get an emumeration of all RMI objects
      NamingEnumeration<NameClassPair> enumeration 
                            = namingContext.list(rmiURL);
      // Print them out
      while (enumeration.hasMore())
      {
        System.out.println(enumeration.next().getName());
      }
    }
    catch (NamingException ne)
    {
      ne.printStackTrace();
    }
  }
}
 
The page was last updated February 19, 2008