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