Source for java.rmi.Naming

   1: /* Naming.java --
   2:    Copyright (c) 1996, 1997, 1998, 1999, 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10:  
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.rmi;
  40: 
  41: import java.net.MalformedURLException;
  42: import java.net.URI;
  43: import java.net.URISyntaxException;
  44: import java.net.URL;
  45: import java.rmi.registry.LocateRegistry;
  46: import java.rmi.registry.Registry;
  47: 
  48: /**
  49:  * <p>
  50:  * The <code>Naming</code> class handles interactions with RMI registries.
  51:  * Each method takes a URL in <code>String</code> form, which points to
  52:  * the RMI registry.  The scheme of the URL is irrelevant.  The relevant
  53:  * part is:
  54:  * </p>
  55:  * <p>
  56:  * <code>//host:port/name</code>
  57:  * </p>
  58:  * <p>
  59:  * which tells the method how to locate and access the registry.  The host
  60:  * and port are both optional, and default to `localhost' and the standard
  61:  * RMI registry port (1099) respectively.  The name is simply a string
  62:  * used to refer to a particular service hosted by the registry.  The
  63:  * registry does not attempt to interpret this further.
  64:  * </p>
  65:  * <p>
  66:  * RMI services are registered using one of these names, and the same name
  67:  * is later used by the client to lookup the service and access its methods. 
  68:  * Registries can be shared by multiple services, or a service can create
  69:  * its own registry using <code>createRegistry()</code>.
  70:  * </p>
  71:  *
  72:  * @author Original author unknown.
  73:  * @author Ingo Proetel (proetel@aicas.com)
  74:  * @author Guilhem Lavaux (guilhem@kaffe.org)
  75:  * @author Jeroen Frijters (jeroen@frijters.net)
  76:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  77:  * @since 1.1
  78:  */
  79: public final class Naming {
  80: 
  81:   /**
  82:    * This class isn't intended to be instantiated.
  83:    */
  84:   private Naming() {}
  85: 
  86: /**
  87:  * Looks for the remote object that is associated with the named service.
  88:  * Name and location is given in form of a URL without a scheme:
  89:  *
  90:  * <pre>
  91:  * //host:port/service-name
  92:  * </pre>
  93:  *  
  94:  * The port is optional.
  95:  * 
  96:  * @param name the service name and location
  97:  * @return Remote-object that implements the named service
  98:  * @throws NotBoundException if no object implements the service
  99:  * @throws MalformedURLException 
 100:  * @throws RemoteException
 101:  */
 102: public static Remote lookup(String name) throws NotBoundException, MalformedURLException, RemoteException {
 103:         URL u = parseURL(name);
 104:     String serviceName = getName(u);
 105:     return (getRegistry(u).lookup(serviceName));
 106: }
 107: 
 108: /**
 109:  * Try to bind the given object to the given service name. 
 110:  * @param name
 111:  * @param obj
 112:  * @throws AlreadyBoundException
 113:  * @throws MalformedURLException
 114:  * @throws RemoteException
 115:  */
 116: public static void bind(String name, Remote obj) throws AlreadyBoundException, MalformedURLException, RemoteException {
 117:         URL u = parseURL(name);
 118:     String serviceName = getName(u);
 119:     getRegistry(u).bind(serviceName, obj);
 120: }
 121: 
 122: /**
 123:  * Remove a binding for a given service name.
 124:  * @param name
 125:  * @throws RemoteException
 126:  * @throws NotBoundException
 127:  * @throws MalformedURLException
 128:  */
 129: public static void unbind(String name) throws RemoteException, NotBoundException, MalformedURLException {
 130:         URL u = parseURL(name);
 131:     String serviceName = getName(u);
 132:     getRegistry(u).unbind(serviceName);
 133: }
 134: 
 135: /**
 136:  * Forces the binding between the given Remote-object and the given service name, even 
 137:  * if there was already an object bound to this name. 
 138:  * @param name
 139:  * @param obj
 140:  * @throws RemoteException
 141:  * @throws MalformedURLException
 142:  */
 143: public static void rebind(String name, Remote obj) throws RemoteException, MalformedURLException {
 144:         URL u = parseURL(name);
 145:     String serviceName = getName(u);
 146:     getRegistry(u).rebind(serviceName, obj);
 147: }
 148: 
 149: /**
 150:  * Lists all services at the named registry.
 151:  * @param name url that specifies the registry
 152:  * @return list of services at the name registry
 153:  * @throws RemoteException
 154:  * @throws MalformedURLException
 155:  */
 156: public static String[] list(String name) throws RemoteException, MalformedURLException {
 157:     return (getRegistry(parseURL(name)).list());
 158: }
 159: 
 160: private static Registry getRegistry(URL u) throws RemoteException {
 161:     if (u.getPort() == -1) {
 162:         return (LocateRegistry.getRegistry(u.getHost()));
 163:     }
 164:     else {
 165:         return (LocateRegistry.getRegistry(u.getHost(), u.getPort()));
 166:     }
 167: }
 168: 
 169:   /**
 170:    * Parses the supplied URL and converts it to use the HTTP
 171:    * protocol.  From an RMI perspective, the scheme is irrelevant
 172:    * and we want to be able to create a URL for which a handler is
 173:    * available.
 174:    *
 175:    * @param name the URL in String form.
 176:    * @throws MalformedURLException if the URL is invalid.
 177:    */
 178:   private static URL parseURL(String name)
 179:     throws MalformedURLException
 180:   {
 181:     try
 182:       {
 183:     URI uri = new URI(name);
 184:     String host = uri.getHost();
 185:     int port = uri.getPort();
 186:     String query = uri.getQuery();
 187:     String path = uri.getPath();
 188:     return new URL("http", 
 189:                (host == null ? "localhost" : host),
 190:                (port == -1 ? 1099 : port),
 191:                uri.getPath() + (query == null ? "" : query));
 192:       }
 193:     catch (URISyntaxException e)
 194:       {
 195:     throw new MalformedURLException("The URL syntax was invalid: " + 
 196:                     e.getMessage());
 197:       }
 198:   }
 199: 
 200:   /**
 201:    * Checks that the URL contains a name, and removes any leading
 202:    * slashes.
 203:    *
 204:    * @param url the URL to check.
 205:    * @throws MalformedURLException if no name is specified.
 206:    */ 
 207:   private static String getName(URL url)
 208:     throws MalformedURLException
 209:   {
 210:     String filename = url.getFile();
 211:     if (filename.length() == 0)
 212:       throw new MalformedURLException("No path specified: " + url);
 213:     // If the filename begins with a slash we must cut it for
 214:     // name resolution.
 215:     if (filename.charAt(0) == '/')
 216:       return filename.substring(1);
 217:     return filename;
 218:   }
 219: 
 220: }