home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / LocateRegistry.java < prev    next >
Text File  |  1997-05-20  |  5KB  |  145 lines

  1. /*
  2.  * @(#)LocateRegistry.java    1.7 96/11/18
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  */
  21.  
  22. package java.rmi.registry;
  23.  
  24. import java.rmi.RemoteException;
  25. import java.rmi.UnknownHostException;
  26.  
  27. /**
  28.  * This class is used to obtain the bootstrap Registry on a particular
  29.  * host (including your local host). The following example demonstrates usage
  30.  * (minus exception handling):
  31.  *
  32.  * <br> Server wishes to make itself available to others:
  33.  *    <br> SomeService service = ...; // remote object for service
  34.  *    <br> Registry registry = LocateRegistry.getRegistry();
  35.  *    <br> registry.bind("I Serve", service);
  36.  *
  37.  * <br> The client wishes to make requests of the above service:
  38.  *    <br> Registry registry = LocateRegistry.getRegistry("foo.services.com");
  39.  *    <br> SomeService service = (SomeService)registry.lookup("I Serve");
  40.  *    <br> service.requestService(...);
  41.  *
  42.  * @see Registry
  43.  */
  44. public final class LocateRegistry {
  45.  
  46.     /**
  47.      * Find registry package prefix: assumes that the implementation class
  48.      * RegistryHandler is located in the package defined by the prefix.
  49.      */
  50.     private static String registryPkgPrefix =
  51.         System.getProperty("java.rmi.registry.packagePrefix",
  52.                "sun.rmi.registry");
  53.               
  54.     private static RegistryHandler handler = null;
  55.     
  56.     /*
  57.      * Private constructor to disable public construction.
  58.      */
  59.     private LocateRegistry() {}
  60.  
  61.     /**
  62.      * Returns the remote object Registry for the local host.
  63.      */
  64.     public static Registry getRegistry()
  65.     throws RemoteException
  66.     {
  67.     try {
  68.         return getRegistry(null, Registry.REGISTRY_PORT);
  69.     } catch (UnknownHostException ex) {
  70.         // Can't happen
  71.     }
  72.     return null;
  73.     }
  74.  
  75.     /**
  76.      * Returns the remote object Registry on the current host at the
  77.      * specified port.
  78.      */
  79.     public static Registry getRegistry(int port)
  80.     throws RemoteException
  81.     {
  82.     try {
  83.         return getRegistry(null, port);
  84.     } catch (UnknownHostException ex) {
  85.         // Can't happen
  86.     }
  87.     return null;
  88.     }
  89.     
  90.     /**
  91.      * Returns the remote object Registry on the specified host at a
  92.      * default (i.e., well-known) port number.  If the host
  93.      * <code>String</code> reference is <code>null</code>, the local
  94.      * host is used.
  95.      */
  96.     public static Registry getRegistry(String host)
  97.     throws RemoteException, UnknownHostException
  98.     {
  99.     return getRegistry(host, Registry.REGISTRY_PORT);
  100.     }
  101.     
  102.     /**
  103.      * Returns the remote object Registry on the specified host at the
  104.      * specified port.  If port <= 0, the default Registry port number
  105.      * is used.  If the host <code>String</code> reference is
  106.      * <code>null</code>, the local host is used.
  107.      *
  108.      * @exception UnknownHostException If the host is not known.
  109.      */
  110.     public static Registry getRegistry(String host, int port)
  111.     throws RemoteException, UnknownHostException
  112.     {
  113.     if (handler != null) {
  114.         return handler.registryStub(host, port);
  115.     } else {
  116.         throw new RemoteException("No registry handler present");
  117.     }
  118.     
  119.     }
  120.  
  121.     /**
  122.      * Create and export a registry on the local host.
  123.      *
  124.      * @param port the port on which the registry is to be exported
  125.      * @exception RemoteException If failure occurs during remote
  126.      * object creation.
  127.      */
  128.     public static Registry createRegistry(int port) throws RemoteException
  129.     {
  130.     if (handler != null) {
  131.         return handler.registryImpl(port);
  132.     } else {
  133.         throw new RemoteException("No registry handler present");
  134.     }
  135.     }
  136.  
  137.     static {
  138.     try {
  139.         Class cl = Class.forName(registryPkgPrefix + ".RegistryHandler");
  140.         handler = (RegistryHandler)(cl.newInstance());
  141.     } catch (Exception e) {
  142.     }
  143.     }
  144. }
  145.