Bootstrapping the Registry The RMI Registry Is an RMI Server

lookup either throws an exception or returns a single stub to the calling application. This stub is a serialized copy of the stub bound into the registry under logical_name . And, hence, the client can use this stub to directly call methods on the server, without using the registry ever again. list returns an array of strings. These strings are the complete URLs, not just the logical names, of all the servers bound into the registry.

14.3 The RMI Registry Is an RMI Server

The RMI registry is implemented as an RMI server. Underlying Naming s static methods is an interface that extends Remote and an implementation of that interface. In Java 2, these are: The java.rmi.registry.Registry interface This extends Remote and defines the methods an implementation of Registry must implement. The sun.rmi.registry.RegistryImpl implementation class This is an actual RMI server that implements the Registry interface. The Registry interface is straightforward. It defines five methods, each of which maps directly to one of the static methods defined in Naming : public interface Registry extends Remote { public static final int REGISTRY_PORT = 1099; public Remote lookupString name throws RemoteException, NotBoundException, AccessException; public void bindString name, Remote obj thr ows RemoteException, AlreadyBoundException, AccessException; public void unbindString name throws RemoteException, NotBoundException, AccessException; public void rebindString name, Remote obj throws RemoteException, AccessException; public String[] list throws RemoteException, AccessException; }

14.3.1 Bootstrapping the Registry

Given that the RMI registry is an RMI server, with both an interface and an implementation, many people wonder why Naming was defined. Why go through the trouble of making static methods that simply redirect to a standard implementation? The answer is that we use Naming and the static methods because the bootstrapping problem exists for any server, even a naming service. The problem the designers of RMI had to overcome was enabling a client to get an initial reference to the RMI registry. Their solution was to define two additional classes: Naming and LocateRegistry . Naming and LocateRegistry play the following roles: • Naming serves as a static and public mirror of every registry. Because the methods are static, you dont need to create an instance of Naming . Instead, you simply call class methods. • LocateRegistry handles the initial connection to a running Registry . That is, LocateRegistry is a class that knows how to create a stub for Registry . LocateRegistry is defined in the java.rmi.registry package and implements the following seven static methods: public static Registry createRegistryint port public static Registry createRegistryint port, RMIClientSocketFactory csf, RMIServerSocketFactory ssf public static Registry getRegistry public static getRegistryint port public static Registry getRegistryString host public static Registry getRegistryString host, int port public static Registry getRegistryString host, int port, RMIClientSocketFactory csf The create methods all create and return a running instance of the class RegistryImpl , defined in the sun.rmi.registry package. The get methods attempt to establish a connection to an already existing registry. If the registry exists, the get methods return a stub. Given Naming and LocateRegistry , the RMI solution to the bootstrapping problem works as follows: 1. A static method on Naming , which has the same name as a method defined in the Registry interface, is handed a URL. 2. The URL is parsed, and the machineport information is forwarded to LocateRegistry , which returns a stub for the registry running on that machine and port. 3. Naming uses the stub returned by LocateRegistry to invoke the correct method on the registry. Well discuss LocateRegistry s create methods in more detail later.

14.4 Examining the Registry