1. /*
  2. * @(#)Naming.java 1.11 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.rmi;
  8. import java.rmi.registry.*;
  9. import java.net.URL;
  10. import java.net.MalformedURLException;
  11. /**
  12. * The <code>Naming</code> class provides methods for storing and obtaining
  13. * references to remote objects in the remote object registry. The
  14. * <code>Naming</code> class's methods take, as one of their arguments, a name
  15. * that is URL formatted <code>java.lang.String</code> of the form:
  16. *
  17. * <PRE>
  18. * //host:port/name
  19. * </PRE>
  20. *
  21. * <P>where <code>host</code> is the host (remote or local) where the registry
  22. * is located, <code>port</code> is the port number on which the registry
  23. * accepts calls, and where <code>name</code> is a simple string uninterpreted
  24. * by the registry. Both <code>host</code> and <code>port</code> are optional.
  25. * If <code>host</code> is omitted, the host defaults to the local host. If
  26. * <code>port</code> is omitted, then the port defaults to 1099, the
  27. * "well-known" port that RMI's registry, <code>rmiregistry</code>, uses.
  28. *
  29. * <P><em>Binding</em> a name for a remote object is associating or
  30. * registering a name for a remote object that can be used at a later time to
  31. * look up that remote object. A remote object can be associated with a name
  32. * using the <code>Naming</code> class's <code>bind</code> or
  33. * <code>rebind</code> methods.
  34. *
  35. * <P>Once a remote object is registered (bound) with the RMI registry on the
  36. * local host, callers on a remote (or local) host can lookup the remote
  37. * object by name, obtain its reference, and then invoke remote methods on the
  38. * object. A registry may be shared by all servers running on a host or an
  39. * individual server process may create and use its own registry if desired
  40. * (see <code>java.rmi.registry.LocateRegistry.createRegistry</code> method
  41. * for details).
  42. *
  43. * @version 1.11, 11/29/01
  44. * @author Ann Wollrath
  45. * @author Roger Riggs
  46. * @since JDK1.1
  47. * @see java.rmi.registry.Registry
  48. * @see java.rmi.registry.LocateRegistry
  49. * @see java.rmi.registry.LocateRegistry#createRegistry(int)
  50. */
  51. public final class Naming {
  52. /**
  53. * Disallow anyone from creating one of these
  54. */
  55. private Naming() {}
  56. /**
  57. * Returns a reference, a stub, for the remote object associated
  58. * with the specified <code>name</code>.
  59. *
  60. * @param name a URL-formatted name for the remote object
  61. * @return a reference for a remote object
  62. * @exception NotBoundException if name is not currently bound
  63. * @exception RemoteException if registry could not be contacted
  64. * @exception AccessException if this operation is not permitted (if
  65. * originating from a non-local host, for example)
  66. * @since JDK1.1
  67. */
  68. public static Remote lookup(String name)
  69. throws NotBoundException,
  70. java.net.MalformedURLException,
  71. RemoteException
  72. {
  73. URL url = cleanURL(name);
  74. Registry registry = getRegistry(url);
  75. String file = getName(url);
  76. if (file == null)
  77. return registry;
  78. return registry.lookup(file);
  79. }
  80. /**
  81. * Binds the specified <code>name</code> to a remote object.
  82. *
  83. * @param name a URL-formatted name for the remote object
  84. * @param obj a reference for the remote object (usually a stub)
  85. * @exception AlreadyBoundException if name is already bound
  86. * @exception MalformedURLException if the name is not an appropriately
  87. * formatted URL
  88. * @exception RemoteException if registry could not be contacted
  89. * @exception AccessException if this operation is not permitted (if
  90. * originating from a non-local host, for example)
  91. * @since JDK1.1
  92. */
  93. public static void bind(String name, Remote obj)
  94. throws AlreadyBoundException,
  95. java.net.MalformedURLException,
  96. RemoteException
  97. {
  98. URL url = cleanURL(name);
  99. Registry registry = getRegistry(url);
  100. if (obj == null)
  101. throw new NullPointerException("cannot bind to null");
  102. registry.bind(getName(url), obj);
  103. }
  104. /**
  105. * Destroys the binding for the specified name that is associated
  106. * with a remote object.
  107. *
  108. * @param name a URL-formatted name associated with a remote object
  109. * @exception NotBoundException if name is not currently bound
  110. * @exception MalformedURLException if the name is not an appropriately
  111. * formatted URL
  112. * @exception RemoteException if registry could not be contacted
  113. * @exception AccessException if this operation is not permitted (if
  114. * originating from a non-local host, for example)
  115. * @since JDK1.1
  116. */
  117. public static void unbind(String name)
  118. throws RemoteException,
  119. NotBoundException,
  120. java.net.MalformedURLException
  121. {
  122. URL url = cleanURL(name);
  123. Registry registry = getRegistry(url);
  124. registry.unbind(getName(url));
  125. }
  126. /**
  127. * Rebinds the specified name to a new remote object. Any existing
  128. * binding for the name is replaced.
  129. *
  130. * @param name a URL-formatted name associated with the remote object
  131. * @param obj new remote object to associate with the name
  132. * @exception MalformedURLException if the name is not an appropriately
  133. * formatted URL
  134. * @exception RemoteException if registry could not be contacted
  135. * @exception AccessException if this operation is not permitted (if
  136. * originating from a non-local host, for example)
  137. * @since JDK1.1
  138. */
  139. public static void rebind(String name, Remote obj)
  140. throws RemoteException, java.net.MalformedURLException
  141. {
  142. URL url = cleanURL(name);
  143. Registry registry = getRegistry(url);
  144. if (obj == null)
  145. throw new NullPointerException("cannot bind to null");
  146. registry.rebind(getName(url), obj);
  147. }
  148. /**
  149. * Returns an array of the names bound in the registry. The names are
  150. * URL-formatted strings. The array contains a snapshot of the names
  151. * present in the registry at the time of the call.
  152. *
  153. * @param name a URL-formatted name that specifies the remote registry
  154. * @return an array of names (in the appropriate URL format) bound
  155. * in the registry
  156. * @exception MalformedURLException if the name is not an appropriately
  157. * formatted URL
  158. * @exception RemoteException if registry could not be contacted.
  159. * @since JDK1.1
  160. */
  161. public static String[] list(String name)
  162. throws RemoteException, java.net.MalformedURLException
  163. {
  164. URL url = cleanURL(name);
  165. Registry registry = getRegistry(url);
  166. String host = url.getHost();
  167. int port = url.getPort();
  168. String prefix = "rmi:";
  169. if (port > 0 || !host.equals(""))
  170. prefix += "//" + host;
  171. if (port > 0)
  172. prefix += ":" + port;
  173. prefix += "/";
  174. String[] names = registry.list();
  175. for (int i = 0; i < names.length; i++) {
  176. names[i] = prefix + names[i];
  177. }
  178. return names;
  179. }
  180. /**
  181. * Returns a registry reference obtained from information in the URL.
  182. */
  183. private static Registry getRegistry(URL url)
  184. throws RemoteException
  185. {
  186. String host = url.getHost();
  187. int port = url.getPort();
  188. return LocateRegistry.getRegistry(host, port);
  189. }
  190. /**
  191. * Extracts only the name portion from the specified URL.
  192. */
  193. private static String getName(URL url)
  194. {
  195. String name = url.getFile();
  196. if (name == null || name.equals("/"))
  197. return null;
  198. return name.substring(1);
  199. }
  200. /**
  201. * Creates an HTTP URL from the specified "name" URL to be used in
  202. * obtaining a registry reference. It checks for and removes the
  203. * "rmi:" protocol if it was specified as part of the "name".
  204. *
  205. * @exception MalformedURLException if hostname in url contains '#' or
  206. * if incorrect protocol specified
  207. */
  208. private static URL cleanURL(String name)
  209. throws java.net.MalformedURLException
  210. {
  211. URL url = new URL("http:");
  212. // Anchors (i.e. '#') are meaningless in rmi URLs - disallow them
  213. if (name.indexOf('#') >= 0) {
  214. throw new MalformedURLException
  215. ("Invalid character, '#', in URL: " + name);
  216. }
  217. // remove the approved protocol
  218. if (name.startsWith("rmi:"))
  219. name = name.substring(4);
  220. // No protocol must remain
  221. int colon = name.indexOf(':');
  222. if (colon >= 0 && colon < name.indexOf('/') )
  223. throw new java.net.MalformedURLException("No protocol needed");
  224. url = new URL(url, name);
  225. return url;
  226. }
  227. }