1. /*
  2. * @(#)LocateRegistry.java 1.22 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.rmi.registry;
  11. import java.rmi.RemoteException;
  12. import java.rmi.server.ObjID;
  13. import java.rmi.server.RMIClientSocketFactory;
  14. import java.rmi.server.RMIServerSocketFactory;
  15. /**
  16. * <code>LocateRegistry</code> is used to obtain a reference to a bootstrap
  17. * remote object registry on a particular host (including the local host), or
  18. * to create a remote object registry that accepts calls on a specific port.
  19. *
  20. * <p> Note that a <code>getRegistry</code> call does not actually make a
  21. * connection to the remote host. It simply creates a local reference to
  22. * the remote registry and will succeed even if no registry is running on
  23. * the remote host. Therefore, a subsequent method invocation to a remote
  24. * registry returned as a result of this method may fail.
  25. *
  26. * @version 1.22, 02/02/00
  27. * @author Ann Wollrath
  28. * @author Peter Jones
  29. * @since JDK1.1
  30. * @see java.rmi.registry.Registry
  31. */
  32. public final class LocateRegistry {
  33. /**
  34. * Private constructor to disable public construction.
  35. */
  36. private LocateRegistry() {}
  37. /**
  38. * Returns a reference to the the remote object <code>Registry</code> for
  39. * the local host on the default registry port of 1099.
  40. *
  41. * @return reference (a stub) to the remote object registry
  42. * @exception RemoteException if the reference could not be created
  43. * @since JDK1.1
  44. */
  45. public static Registry getRegistry()
  46. throws RemoteException
  47. {
  48. return getRegistry(null, Registry.REGISTRY_PORT);
  49. }
  50. /**
  51. * Returns a reference to the the remote object <code>Registry</code> for
  52. * the local host on the specified <code>port</code>.
  53. *
  54. * @param port port on which the registry accepts requests
  55. * @return reference (a stub) to the remote object registry
  56. * @exception RemoteException if the reference could not be created
  57. * @since JDK1.1
  58. */
  59. public static Registry getRegistry(int port)
  60. throws RemoteException
  61. {
  62. return getRegistry(null, port);
  63. }
  64. /**
  65. * Returns a reference to the remote object <code>Registry</code> on the
  66. * specified <code>host</code> on the default registry port of 1099. If
  67. * <code>host</code> is <code>null</code>, the local host is used.
  68. *
  69. * @param host host for the remote registry
  70. * @return reference (a stub) to the remote object registry
  71. * @exception RemoteException if the reference could not be created
  72. * @since JDK1.1
  73. */
  74. public static Registry getRegistry(String host)
  75. throws RemoteException
  76. {
  77. return getRegistry(host, Registry.REGISTRY_PORT);
  78. }
  79. /**
  80. * Returns a reference to the remote object <code>Registry</code> on the
  81. * specified <code>host</code> and <code>port</code>. If <code>host</code>
  82. * is <code>null</code>, the local host is used.
  83. *
  84. * @param host host for the remote registry
  85. * @param port port on which the registry accepts requests
  86. * @return reference (a stub) to the remote object registry
  87. * @exception RemoteException if the reference could not be created
  88. * @since JDK1.1
  89. */
  90. public static Registry getRegistry(String host, int port)
  91. throws RemoteException
  92. {
  93. return getRegistry(host, port, null);
  94. }
  95. /**
  96. * Returns a locally created remote reference to the remote object
  97. * <code>Registry</code> on the specified <code>host</code> and
  98. * <code>port</code>. Communication with this remote registry will
  99. * use the supplied <code>RMIClientSocketFactory</code> <code>csf</code>
  100. * to create <code>Socket</code> connections to the registry on the
  101. * remote <code>host</code> and <code>port</code>.
  102. *
  103. * @param host host for the remote registry
  104. * @param port port on which the registry accepts requests
  105. * @param csf client-side <code>Socket</code> factory used to
  106. * make connections to the registry. If <code>csf</code>
  107. * is null, then the default client-side <code>Socket</code>
  108. * factory will be used in the registry stub.
  109. * @return reference (a stub) to the remote registry
  110. * @exception RemoteException if the reference could not be created
  111. * @since 1.2
  112. */
  113. public static Registry getRegistry(String host, int port,
  114. RMIClientSocketFactory csf)
  115. throws RemoteException
  116. {
  117. Registry registry = null;
  118. if (port <= 0)
  119. port = Registry.REGISTRY_PORT;
  120. if (host == null || host.length() == 0) {
  121. // If host is blank (as returned by "file:" URL in 1.0.2 used in
  122. // java.rmi.Naming), try to convert to real local host name so
  123. // that the RegistryImpl's checkAccess will not fail.
  124. try {
  125. host = java.net.InetAddress.getLocalHost().getHostAddress();
  126. } catch (Exception e) {
  127. // If that failed, at least try "" (localhost) anyway...
  128. host = "";
  129. }
  130. }
  131. if (csf == null) {
  132. registry = (Registry) sun.rmi.server.RemoteProxy.
  133. getStub("sun.rmi.registry.RegistryImpl", ObjID.REGISTRY_ID,
  134. host, port);
  135. } else {
  136. registry = (Registry) sun.rmi.server.RemoteProxy.
  137. getStub("sun.rmi.registry.RegistryImpl", ObjID.REGISTRY_ID,
  138. host, port, csf);
  139. }
  140. return registry;
  141. }
  142. /**
  143. * Creates and exports a <code>Registry</code> on the local host
  144. * that accepts requests on the specified <code>port</code>.
  145. *
  146. * @param port the port on which the registry accepts requests
  147. * @return the registry
  148. * @exception RemoteException if the registry could not be exported
  149. * @since JDK1.1
  150. */
  151. public static Registry createRegistry(int port) throws RemoteException
  152. {
  153. return new sun.rmi.registry.RegistryImpl(port);
  154. }
  155. /**
  156. * Creates and exports a <code>Registry</code> on the local host that
  157. * uses custom socket factories for communication with that registry.
  158. * The registry that is created listens for incoming requests on the
  159. * given <code>port</code> using a <code>ServerSocket</code> created
  160. * from the supplied <code>RMIServerSocketFactory</code>. A client
  161. * that receives a reference to this registry will use a
  162. * <code>Socket</code> created from the supplied
  163. * <code>RMIClientSocketFactory</code>.
  164. *
  165. * @param port port on which the registry accepts requests
  166. * @param csf client-side <code>Socket</code> factory used to
  167. * make connections to the registry
  168. * @param ssf server-side <code>ServerSocket</code> factory
  169. * used to accept connections to the registry
  170. * @return the registry
  171. * @exception RemoteException if the registry could not be exported
  172. * @since 1.2
  173. */
  174. public static Registry createRegistry(int port,
  175. RMIClientSocketFactory csf,
  176. RMIServerSocketFactory ssf)
  177. throws RemoteException
  178. {
  179. return new sun.rmi.registry.RegistryImpl(port, csf, ssf);
  180. }
  181. }