Back to the RMI Plug-in for Eclipse

Class RegistryDelegate

java.lang.Object
  extended by java.rmi.server.RemoteObject
      extended by java.rmi.server.RemoteServer
          extended by java.rmi.server.UnicastRemoteObject
              extended by RegistryDelegate
All Implemented Interfaces:
Serializable, Registry, Remote

public class RegistryDelegate
extends UnicastRemoteObject
implements Registry

This class implements the registry delegate concept.

In order to enforce some level of security, the standard RMI registry implementation (e.g. rmiregistry.exe) only allows processes on the same host to register objects in the registry (think of a bank running a registry on one of its servers, and doesn't want anybody modifying it). So, by design, if a process tries to bind(String, Remote) an object to a remote registry, an exception will be thrown.

However, the design of a distributed system may require remote clients to register themselves in a central registry. If such system is deployed in a controlled and trusted environment (e.g., a firewalled intranet with tight access control), the security risk may be acceptable.

The simplest technical solution to the remote registration problem is to have a registry delegate. A registry delegate is an object that serves as a proxy for the real registry. The delegate itself usually appears in the registry under a well known name. It implements the Registry interface and simply delegates all method calls to the appropriate methods of the real registry. The delegate is allowed to perform bind and unbind operations because it is running on the same host as the registry.

The common scenario for starting a registry and creating the delegate is starting a class with the following main(String[]) method:

   public static void main(String[] args) throws AccessException, RemoteException, AlreadyBoundException {
     if (System.getSecurityManager() == null) {
         System.setSecurityManager(new RMISecurityManager());
     }

     Registry registry = LocateRegistry.createRegistry(REGISTRY_PORT);
     registry.bind(DELEGATE_NAME, new RegistryDelegate());
     
     do {
          try {
              Thread.sleep(Long.MAX_VALUE);
          } catch (InterruptedException e) {
              ; // do nothing
          } catch (Throwable e) {
              e.printStackTrace();
              System.exit(1);
          }
      } while(true);
   }
 
The common usage scenario looks something like:
  Registry remoteRegistry = LocateRegistry.getRegistry("remotehost.mycompany.com");
  Registry delegate = (Registry) remoteRegistry.lookup(DELEGATE_NAME);
  delegate.bind("someName", new SomeRemoteObject());
 
The getRegistryDelegate(String) method is a helper method that fetches the registry delegate for you.

The main(String[]) method of this class will create a local registry on the default port, create a registry delegate and bind it under the well known name that you chose in the wizard (DELEGATE_NAME).

Author:
Genady Beryozkin, rmi-info@genady.net
See Also:
Getting started with RMI, RMI FAQ on JGuru, RMI Mailing List post, RMI Mailing List post , Serialized Form

Field Summary
static String DELEGATE_NAME
          The name under which the delegate appears in the registry
private  Registry localRegistry
          The local resistry.
 
Fields inherited from class java.rmi.server.RemoteObject
ref
 
Fields inherited from interface java.rmi.registry.Registry
REGISTRY_PORT
 
Constructor Summary
RegistryDelegate()
          Create a delegate for a local registry that is bound to the default local port (1099).
RegistryDelegate(int port)
          Create a delegate for a local registry that is bound to a user specified port.
RegistryDelegate(Registry reg)
          Create a delegate for a user provided registry instance.
 
Method Summary
 void bind(String name, Remote obj)
           
static Registry getRegistryDelegate(String remoteHost)
          This method retrieves the registry delegate from a registry that is running on a remote host.
static Registry getRegistryDelegate(String remoteHost, int remotePort)
          This method retrieves the registry delegate from a registry that is running on a remote host.
 String[] list()
           
 Remote lookup(String name)
           
static void main(String[] args)
          A simple way to run a registry and bind a registry delegate.
 void rebind(String name, Remote obj)
           
 void unbind(String name)
           
 
Methods inherited from class java.rmi.server.UnicastRemoteObject
clone, exportObject, exportObject, exportObject, unexportObject
 
Methods inherited from class java.rmi.server.RemoteServer
getClientHost, getLog, setLog
 
Methods inherited from class java.rmi.server.RemoteObject
equals, getRef, hashCode, toString, toStub
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

localRegistry

private Registry localRegistry
The local resistry.


DELEGATE_NAME

public static final String DELEGATE_NAME
The name under which the delegate appears in the registry

See Also:
Constant Field Values
Constructor Detail

RegistryDelegate

public RegistryDelegate()
                 throws RemoteException
Create a delegate for a local registry that is bound to the default local port (1099).

Throws:
RemoteException - if any error occurs.

RegistryDelegate

public RegistryDelegate(int port)
                 throws RemoteException
Create a delegate for a local registry that is bound to a user specified port.

Parameters:
port - the local registry port.
Throws:
RemoteException - if any error occurs.

RegistryDelegate

public RegistryDelegate(Registry reg)
                 throws RemoteException
Create a delegate for a user provided registry instance. The registry is assumed to be a local registry, as there is no point in creating a delegate for a remote registry.

Parameters:
reg - the registry for which a delegate is to be created.
Throws:
RemoteException - if any error occurrs.
Method Detail

bind

public void bind(String name,
                 Remote obj)
          throws RemoteException,
                 AlreadyBoundException,
                 AccessException
Specified by:
bind in interface Registry
Throws:
RemoteException
AlreadyBoundException
AccessException

list

public String[] list()
              throws RemoteException,
                     AccessException
Specified by:
list in interface Registry
Throws:
RemoteException
AccessException

lookup

public Remote lookup(String name)
              throws RemoteException,
                     NotBoundException,
                     AccessException
Specified by:
lookup in interface Registry
Throws:
RemoteException
NotBoundException
AccessException

rebind

public void rebind(String name,
                   Remote obj)
            throws RemoteException,
                   AccessException
Specified by:
rebind in interface Registry
Throws:
RemoteException
AccessException

unbind

public void unbind(String name)
            throws RemoteException,
                   NotBoundException,
                   AccessException
Specified by:
unbind in interface Registry
Throws:
RemoteException
NotBoundException
AccessException

getRegistryDelegate

public static Registry getRegistryDelegate(String remoteHost,
                                           int remotePort)
                                    throws RemoteException,
                                           NotBoundException
This method retrieves the registry delegate from a registry that is running on a remote host.

Parameters:
remoteHost - name of the remote host.
remotePort - port on which the registry accepts requests.
Returns:
a reference to the registry delegate.
Throws:
RemoteException - if we could not connect to the registry.
NotBoundException - if the delegate does not appear in the registry.

getRegistryDelegate

public static Registry getRegistryDelegate(String remoteHost)
                                    throws RemoteException,
                                           NotBoundException
This method retrieves the registry delegate from a registry that is running on a remote host.

Parameters:
remoteHost - name of the remote host.
Returns:
a reference to the registry delegate.
Throws:
RemoteException - if we could not connect to the registry.
NotBoundException - if the delegate does not appear in the registry.

main

public static void main(String[] args)
                 throws AccessException,
                        RemoteException,
                        AlreadyBoundException
A simple way to run a registry and bind a registry delegate. This method should be modified and adapted for the specific needs of a project.

Throws:
AccessException
RemoteException
AlreadyBoundException

Back to the RMI Plug-in for Eclipse