home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / rmi / registry / LocateRegistry.java next >
Encoding:
Java Source  |  1998-03-20  |  3.7 KB  |  123 lines

  1. /*
  2.  * @(#)LocateRegistry.java    1.11 98/03/18
  3.  *
  4.  * Copyright 1996, 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.rmi.registry;
  16.  
  17. import java.rmi.RemoteException;
  18. import java.rmi.UnknownHostException;
  19. import java.rmi.server.ObjID;
  20.  
  21. /**
  22.  * This class is used to obtain the bootstrap Registry on a particular
  23.  * host (including your local host). The following example demonstrates usage
  24.  * (minus exception handling):
  25.  *
  26.  * <br> Server wishes to make itself available to others:
  27.  *    <br> SomeService service = ...; // remote object for service
  28.  *    <br> Registry registry = LocateRegistry.getRegistry();
  29.  *    <br> registry.bind("I Serve", service);
  30.  *
  31.  * <br> The client wishes to make requests of the above service:
  32.  *    <br> Registry registry = LocateRegistry.getRegistry("foo.services.com");
  33.  *    <br> SomeService service = (SomeService)registry.lookup("I Serve");
  34.  *    <br> service.requestService(...);
  35.  *
  36.  * @see Registry
  37.  */
  38. public abstract class LocateRegistry {
  39.     /**
  40.      * Returns the remote object Registry for the local host.
  41.      */
  42.     public static Registry getRegistry()
  43.     throws RemoteException
  44.     {
  45.     try {
  46.         return getRegistry(null, Registry.REGISTRY_PORT);
  47.     } catch (UnknownHostException ex) {
  48.         // Can't happen
  49.     }
  50.     return null;
  51.     }
  52.  
  53.     /**
  54.      * Returns the remote object Registry on the current host at the
  55.      * specified port.
  56.      */
  57.     public static Registry getRegistry(int port)
  58.     throws RemoteException
  59.     {
  60.     try {
  61.         return getRegistry(null, port);
  62.     } catch (UnknownHostException ex) {
  63.         // Can't happen
  64.     }
  65.     return null;
  66.     }
  67.     
  68.     /**
  69.      * Returns the remote object Registry on the specified host at a
  70.      * default (i.e., well-known) port number.  If the host
  71.      * <code>String</code> reference is <code>null</code>, the local
  72.      * host is used.
  73.      */
  74.     public static Registry getRegistry(String host)
  75.     throws RemoteException, UnknownHostException
  76.     {
  77.     return getRegistry(host, Registry.REGISTRY_PORT);
  78.     }
  79.     
  80.     /**
  81.      * Returns the remote object Registry on the specified host at the
  82.      * specified port.  If port <= 0, the default Registry port number
  83.      * is used.  If the host <code>String</code> reference is
  84.      * <code>null</code>, the local host is used.
  85.      *
  86.      * @exception UnknownHostException If the host is not known.
  87.      */
  88.     public static Registry getRegistry(String host, int port)
  89.     throws RemoteException, UnknownHostException
  90.     {
  91.     if (port <= 0)
  92.         port = Registry.REGISTRY_PORT;
  93.     
  94.     if (host == null || host.length() == 0) {
  95.         // If host is blank (as returned by "file:" URL in 1.0.2 used in
  96.         // java.rmi.Naming), try to convert to real local host name so
  97.         // that the RegistryImpl's checkAccess will not fail.
  98.         try {
  99.         host = java.net.InetAddress.getLocalHost().getHostName();
  100.         } catch (Exception e) {
  101.         // If that failed, at least try "" (localhost) anyway...
  102.         host = "";
  103.         }
  104.     }
  105.  
  106.     return (Registry)sun.rmi.server.RemoteProxy.
  107.         getStub("sun.rmi.registry.RegistryImpl", ObjID.REGISTRY_ID,
  108.             host, port);
  109.     }
  110.  
  111.     /**
  112.      * Create and export a registry on the local host.
  113.      *
  114.      * @param port the port on which the registry is to be exported
  115.      * @exception RemoteException If failure occurs during remote
  116.      * object creation.
  117.      */
  118.     public static Registry createRegistry(int port) throws RemoteException
  119.     {
  120.     return new sun.rmi.registry.RegistryImpl(port);
  121.     }
  122. }
  123.