1. /*
  2. * @(#)LocateRegistry.java 1.33 03/12/19
  3. *
  4. * Copyright 2004 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. import java.rmi.server.RemoteRef;
  13. import java.rmi.server.UnicastRemoteObject;
  14. import sun.rmi.registry.RegistryImpl;
  15. import sun.rmi.server.UnicastRef2;
  16. import sun.rmi.server.UnicastRef;
  17. import sun.rmi.server.Util;
  18. import sun.rmi.transport.LiveRef;
  19. import sun.rmi.transport.tcp.TCPEndpoint;
  20. /**
  21. * <code>LocateRegistry</code> is used to obtain a reference to a bootstrap
  22. * remote object registry on a particular host (including the local host), or
  23. * to create a remote object registry that accepts calls on a specific port.
  24. *
  25. * <p> Note that a <code>getRegistry</code> call does not actually make a
  26. * connection to the remote host. It simply creates a local reference to
  27. * the remote registry and will succeed even if no registry is running on
  28. * the remote host. Therefore, a subsequent method invocation to a remote
  29. * registry returned as a result of this method may fail.
  30. *
  31. * @version 1.33, 12/19/03
  32. * @author Ann Wollrath
  33. * @author Peter Jones
  34. * @since JDK1.1
  35. * @see java.rmi.registry.Registry
  36. */
  37. public final class LocateRegistry {
  38. /**
  39. * Private constructor to disable public construction.
  40. */
  41. private LocateRegistry() {}
  42. /**
  43. * Returns a reference to the the remote object <code>Registry</code> for
  44. * the local host on the default registry port of 1099.
  45. *
  46. * @return reference (a stub) to the remote object registry
  47. * @exception RemoteException if the reference could not be created
  48. * @since JDK1.1
  49. */
  50. public static Registry getRegistry()
  51. throws RemoteException
  52. {
  53. return getRegistry(null, Registry.REGISTRY_PORT);
  54. }
  55. /**
  56. * Returns a reference to the the remote object <code>Registry</code> for
  57. * the local host on the specified <code>port</code>.
  58. *
  59. * @param port port on which the registry accepts requests
  60. * @return reference (a stub) to the remote object registry
  61. * @exception RemoteException if the reference could not be created
  62. * @since JDK1.1
  63. */
  64. public static Registry getRegistry(int port)
  65. throws RemoteException
  66. {
  67. return getRegistry(null, port);
  68. }
  69. /**
  70. * Returns a reference to the remote object <code>Registry</code> on the
  71. * specified <code>host</code> on the default registry port of 1099. If
  72. * <code>host</code> is <code>null</code>, the local host is used.
  73. *
  74. * @param host host for the remote registry
  75. * @return reference (a stub) to the remote object registry
  76. * @exception RemoteException if the reference could not be created
  77. * @since JDK1.1
  78. */
  79. public static Registry getRegistry(String host)
  80. throws RemoteException
  81. {
  82. return getRegistry(host, Registry.REGISTRY_PORT);
  83. }
  84. /**
  85. * Returns a reference to the remote object <code>Registry</code> on the
  86. * specified <code>host</code> and <code>port</code>. If <code>host</code>
  87. * is <code>null</code>, the local host is used.
  88. *
  89. * @param host host for the remote registry
  90. * @param port port on which the registry accepts requests
  91. * @return reference (a stub) to the remote object registry
  92. * @exception RemoteException if the reference could not be created
  93. * @since JDK1.1
  94. */
  95. public static Registry getRegistry(String host, int port)
  96. throws RemoteException
  97. {
  98. return getRegistry(host, port, null);
  99. }
  100. /**
  101. * Returns a locally created remote reference to the remote object
  102. * <code>Registry</code> on the specified <code>host</code> and
  103. * <code>port</code>. Communication with this remote registry will
  104. * use the supplied <code>RMIClientSocketFactory</code> <code>csf</code>
  105. * to create <code>Socket</code> connections to the registry on the
  106. * remote <code>host</code> and <code>port</code>.
  107. *
  108. * @param host host for the remote registry
  109. * @param port port on which the registry accepts requests
  110. * @param csf client-side <code>Socket</code> factory used to
  111. * make connections to the registry. If <code>csf</code>
  112. * is null, then the default client-side <code>Socket</code>
  113. * factory will be used in the registry stub.
  114. * @return reference (a stub) to the remote registry
  115. * @exception RemoteException if the reference could not be created
  116. * @since 1.2
  117. */
  118. public static Registry getRegistry(String host, int port,
  119. RMIClientSocketFactory csf)
  120. throws RemoteException
  121. {
  122. Registry registry = null;
  123. if (port <= 0)
  124. port = Registry.REGISTRY_PORT;
  125. if (host == null || host.length() == 0) {
  126. // If host is blank (as returned by "file:" URL in 1.0.2 used in
  127. // java.rmi.Naming), try to convert to real local host name so
  128. // that the RegistryImpl's checkAccess will not fail.
  129. try {
  130. host = java.net.InetAddress.getLocalHost().getHostAddress();
  131. } catch (Exception e) {
  132. // If that failed, at least try "" (localhost) anyway...
  133. host = "";
  134. }
  135. }
  136. /*
  137. * Create a proxy for the registry with the given host, port, and
  138. * client socket factory. If the supplied client socket factory is
  139. * null, then the ref type is a UnicastRef, otherwise the ref type
  140. * is a UnicastRef2. If the property
  141. * java.rmi.server.ignoreStubClasses is true, then the proxy
  142. * returned is an instance of a dynamic proxy class that implements
  143. * the Registry interface; otherwise the proxy returned is an
  144. * instance of the pregenerated stub class for RegistryImpl.
  145. **/
  146. LiveRef liveRef =
  147. new LiveRef(new ObjID(ObjID.REGISTRY_ID),
  148. new TCPEndpoint(host, port, csf, null),
  149. false);
  150. RemoteRef ref =
  151. (csf == null) ? new UnicastRef(liveRef) : new UnicastRef2(liveRef);
  152. return (Registry) Util.createProxy(RegistryImpl.class, ref, false);
  153. }
  154. /**
  155. * Creates and exports a <code>Registry</code> instance on the local
  156. * host that accepts requests on the specified <code>port</code>.
  157. *
  158. * <p>The <code>Registry</code> instance is exported as if the static
  159. * {@link UnicastRemoteObject.exportObject(Remote,int)
  160. * UnicastRemoteObject.exportObject} method is invoked, passing the
  161. * <code>Registry</code> instance and the specified <code>port</code> as
  162. * arguments, except that the <code>Registry</code> instance is
  163. * exported with a well-known object identifier, an {@link ObjID}
  164. * instance constructed with the value {@link ObjID#REGISTRY_ID}.
  165. *
  166. * @param port the port on which the registry accepts requests
  167. * @return the registry
  168. * @exception RemoteException if the registry could not be exported
  169. * @since JDK1.1
  170. **/
  171. public static Registry createRegistry(int port) throws RemoteException {
  172. return new RegistryImpl(port);
  173. }
  174. /**
  175. * Creates and exports a <code>Registry</code> instance on the local
  176. * host that uses custom socket factories for communication with that
  177. * instance. The registry that is created listens for incoming
  178. * requests on the given <code>port</code> using a
  179. * <code>ServerSocket</code> created from the supplied
  180. * <code>RMIServerSocketFactory</code>.
  181. *
  182. * <p>The <code>Registry</code> instance is exported as if
  183. * the static {@link
  184. * UnicastRemoteObject.exportObject(Remote,int,RMIClientSocketFactory,RMIServerSocketFactory)
  185. * UnicastRemoteObject.exportObject} method is invoked, passing the
  186. * <code>Registry</code> instance, the specified <code>port</code>, the
  187. * specified <code>RMIClientSocketFactory</code>, and the specified
  188. * <code>RMIServerSocketFactory</code> as arguments, except that the
  189. * <code>Registry</code> instance is exported with a well-known object
  190. * identifier, an {@link ObjID} instance constructed with the value
  191. * {@link ObjID#REGISTRY_ID}.
  192. *
  193. * @param port port on which the registry accepts requests
  194. * @param csf client-side <code>Socket</code> factory used to
  195. * make connections to the registry
  196. * @param ssf server-side <code>ServerSocket</code> factory
  197. * used to accept connections to the registry
  198. * @return the registry
  199. * @exception RemoteException if the registry could not be exported
  200. * @since 1.2
  201. **/
  202. public static Registry createRegistry(int port,
  203. RMIClientSocketFactory csf,
  204. RMIServerSocketFactory ssf)
  205. throws RemoteException
  206. {
  207. return new RegistryImpl(port, csf, ssf);
  208. }
  209. }