jdk/src/share/classes/java/rmi/registry/LocateRegistry.java
changeset 2 90ce3da70b43
child 4512 b3ed7ecf8f55
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 1996-2003 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package java.rmi.registry;
       
    27 
       
    28 import java.rmi.RemoteException;
       
    29 import java.rmi.server.ObjID;
       
    30 import java.rmi.server.RMIClientSocketFactory;
       
    31 import java.rmi.server.RMIServerSocketFactory;
       
    32 import java.rmi.server.RemoteRef;
       
    33 import java.rmi.server.UnicastRemoteObject;
       
    34 import sun.rmi.registry.RegistryImpl;
       
    35 import sun.rmi.server.UnicastRef2;
       
    36 import sun.rmi.server.UnicastRef;
       
    37 import sun.rmi.server.Util;
       
    38 import sun.rmi.transport.LiveRef;
       
    39 import sun.rmi.transport.tcp.TCPEndpoint;
       
    40 
       
    41 /**
       
    42  * <code>LocateRegistry</code> is used to obtain a reference to a bootstrap
       
    43  * remote object registry on a particular host (including the local host), or
       
    44  * to create a remote object registry that accepts calls on a specific port.
       
    45  *
       
    46  * <p> Note that a <code>getRegistry</code> call does not actually make a
       
    47  * connection to the remote host.  It simply creates a local reference to
       
    48  * the remote registry and will succeed even if no registry is running on
       
    49  * the remote host.  Therefore, a subsequent method invocation to a remote
       
    50  * registry returned as a result of this method may fail.
       
    51  *
       
    52  * @author  Ann Wollrath
       
    53  * @author  Peter Jones
       
    54  * @since   JDK1.1
       
    55  * @see     java.rmi.registry.Registry
       
    56  */
       
    57 public final class LocateRegistry {
       
    58 
       
    59     /**
       
    60      * Private constructor to disable public construction.
       
    61      */
       
    62     private LocateRegistry() {}
       
    63 
       
    64     /**
       
    65      * Returns a reference to the the remote object <code>Registry</code> for
       
    66      * the local host on the default registry port of 1099.
       
    67      *
       
    68      * @return reference (a stub) to the remote object registry
       
    69      * @exception RemoteException if the reference could not be created
       
    70      * @since JDK1.1
       
    71      */
       
    72     public static Registry getRegistry()
       
    73         throws RemoteException
       
    74     {
       
    75         return getRegistry(null, Registry.REGISTRY_PORT);
       
    76     }
       
    77 
       
    78     /**
       
    79      * Returns a reference to the the remote object <code>Registry</code> for
       
    80      * the local host on the specified <code>port</code>.
       
    81      *
       
    82      * @param port port on which the registry accepts requests
       
    83      * @return reference (a stub) to the remote object registry
       
    84      * @exception RemoteException if the reference could not be created
       
    85      * @since JDK1.1
       
    86      */
       
    87     public static Registry getRegistry(int port)
       
    88         throws RemoteException
       
    89     {
       
    90         return getRegistry(null, port);
       
    91     }
       
    92 
       
    93     /**
       
    94      * Returns a reference to the remote object <code>Registry</code> on the
       
    95      * specified <code>host</code> on the default registry port of 1099.  If
       
    96      * <code>host</code> is <code>null</code>, the local host is used.
       
    97      *
       
    98      * @param host host for the remote registry
       
    99      * @return reference (a stub) to the remote object registry
       
   100      * @exception RemoteException if the reference could not be created
       
   101      * @since JDK1.1
       
   102      */
       
   103     public static Registry getRegistry(String host)
       
   104         throws RemoteException
       
   105     {
       
   106         return getRegistry(host, Registry.REGISTRY_PORT);
       
   107     }
       
   108 
       
   109     /**
       
   110      * Returns a reference to the remote object <code>Registry</code> on the
       
   111      * specified <code>host</code> and <code>port</code>. If <code>host</code>
       
   112      * is <code>null</code>, the local host is used.
       
   113      *
       
   114      * @param host host for the remote registry
       
   115      * @param port port on which the registry accepts requests
       
   116      * @return reference (a stub) to the remote object registry
       
   117      * @exception RemoteException if the reference could not be created
       
   118      * @since JDK1.1
       
   119      */
       
   120     public static Registry getRegistry(String host, int port)
       
   121         throws RemoteException
       
   122     {
       
   123         return getRegistry(host, port, null);
       
   124     }
       
   125 
       
   126     /**
       
   127      * Returns a locally created remote reference to the remote object
       
   128      * <code>Registry</code> on the specified <code>host</code> and
       
   129      * <code>port</code>.  Communication with this remote registry will
       
   130      * use the supplied <code>RMIClientSocketFactory</code> <code>csf</code>
       
   131      * to create <code>Socket</code> connections to the registry on the
       
   132      * remote <code>host</code> and <code>port</code>.
       
   133      *
       
   134      * @param host host for the remote registry
       
   135      * @param port port on which the registry accepts requests
       
   136      * @param csf  client-side <code>Socket</code> factory used to
       
   137      *      make connections to the registry.  If <code>csf</code>
       
   138      *      is null, then the default client-side <code>Socket</code>
       
   139      *      factory will be used in the registry stub.
       
   140      * @return reference (a stub) to the remote registry
       
   141      * @exception RemoteException if the reference could not be created
       
   142      * @since 1.2
       
   143      */
       
   144     public static Registry getRegistry(String host, int port,
       
   145                                        RMIClientSocketFactory csf)
       
   146         throws RemoteException
       
   147     {
       
   148         Registry registry = null;
       
   149 
       
   150         if (port <= 0)
       
   151             port = Registry.REGISTRY_PORT;
       
   152 
       
   153         if (host == null || host.length() == 0) {
       
   154             // If host is blank (as returned by "file:" URL in 1.0.2 used in
       
   155             // java.rmi.Naming), try to convert to real local host name so
       
   156             // that the RegistryImpl's checkAccess will not fail.
       
   157             try {
       
   158                 host = java.net.InetAddress.getLocalHost().getHostAddress();
       
   159             } catch (Exception e) {
       
   160                 // If that failed, at least try "" (localhost) anyway...
       
   161                 host = "";
       
   162             }
       
   163         }
       
   164 
       
   165         /*
       
   166          * Create a proxy for the registry with the given host, port, and
       
   167          * client socket factory.  If the supplied client socket factory is
       
   168          * null, then the ref type is a UnicastRef, otherwise the ref type
       
   169          * is a UnicastRef2.  If the property
       
   170          * java.rmi.server.ignoreStubClasses is true, then the proxy
       
   171          * returned is an instance of a dynamic proxy class that implements
       
   172          * the Registry interface; otherwise the proxy returned is an
       
   173          * instance of the pregenerated stub class for RegistryImpl.
       
   174          **/
       
   175         LiveRef liveRef =
       
   176             new LiveRef(new ObjID(ObjID.REGISTRY_ID),
       
   177                         new TCPEndpoint(host, port, csf, null),
       
   178                         false);
       
   179         RemoteRef ref =
       
   180             (csf == null) ? new UnicastRef(liveRef) : new UnicastRef2(liveRef);
       
   181 
       
   182         return (Registry) Util.createProxy(RegistryImpl.class, ref, false);
       
   183     }
       
   184 
       
   185     /**
       
   186      * Creates and exports a <code>Registry</code> instance on the local
       
   187      * host that accepts requests on the specified <code>port</code>.
       
   188      *
       
   189      * <p>The <code>Registry</code> instance is exported as if the static
       
   190      * {@link UnicastRemoteObject.exportObject(Remote,int)
       
   191      * UnicastRemoteObject.exportObject} method is invoked, passing the
       
   192      * <code>Registry</code> instance and the specified <code>port</code> as
       
   193      * arguments, except that the <code>Registry</code> instance is
       
   194      * exported with a well-known object identifier, an {@link ObjID}
       
   195      * instance constructed with the value {@link ObjID#REGISTRY_ID}.
       
   196      *
       
   197      * @param port the port on which the registry accepts requests
       
   198      * @return the registry
       
   199      * @exception RemoteException if the registry could not be exported
       
   200      * @since JDK1.1
       
   201      **/
       
   202     public static Registry createRegistry(int port) throws RemoteException {
       
   203         return new RegistryImpl(port);
       
   204     }
       
   205 
       
   206     /**
       
   207      * Creates and exports a <code>Registry</code> instance on the local
       
   208      * host that uses custom socket factories for communication with that
       
   209      * instance.  The registry that is created listens for incoming
       
   210      * requests on the given <code>port</code> using a
       
   211      * <code>ServerSocket</code> created from the supplied
       
   212      * <code>RMIServerSocketFactory</code>.
       
   213      *
       
   214      * <p>The <code>Registry</code> instance is exported as if
       
   215      * the static {@link
       
   216      * UnicastRemoteObject.exportObject(Remote,int,RMIClientSocketFactory,RMIServerSocketFactory)
       
   217      * UnicastRemoteObject.exportObject} method is invoked, passing the
       
   218      * <code>Registry</code> instance, the specified <code>port</code>, the
       
   219      * specified <code>RMIClientSocketFactory</code>, and the specified
       
   220      * <code>RMIServerSocketFactory</code> as arguments, except that the
       
   221      * <code>Registry</code> instance is exported with a well-known object
       
   222      * identifier, an {@link ObjID} instance constructed with the value
       
   223      * {@link ObjID#REGISTRY_ID}.
       
   224      *
       
   225      * @param port port on which the registry accepts requests
       
   226      * @param csf  client-side <code>Socket</code> factory used to
       
   227      *      make connections to the registry
       
   228      * @param ssf  server-side <code>ServerSocket</code> factory
       
   229      *      used to accept connections to the registry
       
   230      * @return the registry
       
   231      * @exception RemoteException if the registry could not be exported
       
   232      * @since 1.2
       
   233      **/
       
   234     public static Registry createRegistry(int port,
       
   235                                           RMIClientSocketFactory csf,
       
   236                                           RMIServerSocketFactory ssf)
       
   237         throws RemoteException
       
   238     {
       
   239         return new RegistryImpl(port, csf, ssf);
       
   240     }
       
   241 }