2 Copyright (c) 1996, 1997, 1998, 1999, 2004, 2006
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
42 import java
.net
.MalformedURLException
;
44 import java
.net
.URISyntaxException
;
46 import java
.rmi
.registry
.LocateRegistry
;
47 import java
.rmi
.registry
.Registry
;
51 * The <code>Naming</code> class handles interactions with RMI registries.
52 * Each method takes a URL in <code>String</code> form, which points to
53 * the RMI registry. The scheme of the URL is irrelevant. The relevant
57 * <code>//host:port/name</code>
60 * which tells the method how to locate and access the registry. The host
61 * and port are both optional, and default to `localhost' and the standard
62 * RMI registry port (1099) respectively. The name is simply a string
63 * used to refer to a particular service hosted by the registry. The
64 * registry does not attempt to interpret this further.
67 * RMI services are registered using one of these names, and the same name
68 * is later used by the client to lookup the service and access its methods.
69 * Registries can be shared by multiple services, or a service can create
70 * its own registry using <code>createRegistry()</code>.
73 * @author Original author unknown.
74 * @author Ingo Proetel (proetel@aicas.com)
75 * @author Guilhem Lavaux (guilhem@kaffe.org)
76 * @author Jeroen Frijters (jeroen@frijters.net)
77 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
80 public final class Naming
83 * This class isn't intended to be instantiated.
90 * Looks for the remote object that is associated with the named service.
91 * Name and location is given in form of a URL without a scheme:
94 * //host:port/service-name
97 * The port is optional.
99 * @param name the service name and location
100 * @return Remote-object that implements the named service
101 * @throws NotBoundException if no object implements the service
102 * @throws MalformedURLException
103 * @throws RemoteException
105 public static Remote
lookup(String name
) throws NotBoundException
,
106 MalformedURLException
, RemoteException
108 URL u
= parseURL(name
);
109 String serviceName
= getName(u
);
110 return (getRegistry(u
).lookup(serviceName
));
114 * Try to bind the given object to the given service name.
118 * @throws AlreadyBoundException
119 * @throws MalformedURLException
120 * @throws RemoteException
122 public static void bind(String name
, Remote obj
)
123 throws AlreadyBoundException
, MalformedURLException
, RemoteException
125 URL u
= parseURL(name
);
126 String serviceName
= getName(u
);
127 getRegistry(u
).bind(serviceName
, obj
);
131 * Remove a binding for a given service name.
134 * @throws RemoteException
135 * @throws NotBoundException
136 * @throws MalformedURLException
138 public static void unbind(String name
) throws RemoteException
,
139 NotBoundException
, MalformedURLException
141 URL u
= parseURL(name
);
142 String serviceName
= getName(u
);
143 getRegistry(u
).unbind(serviceName
);
147 * Forces the binding between the given Remote-object and the given service
148 * name, even if there was already an object bound to this name.
152 * @throws RemoteException
153 * @throws MalformedURLException
155 public static void rebind(String name
, Remote obj
) throws RemoteException
,
156 MalformedURLException
158 URL u
= parseURL(name
);
159 String serviceName
= getName(u
);
160 getRegistry(u
).rebind(serviceName
, obj
);
164 * Lists all services at the named registry.
166 * @param name url that specifies the registry
167 * @return list of services at the name registry
168 * @throws RemoteException
169 * @throws MalformedURLException
171 public static String
[] list(String name
) throws RemoteException
,
172 MalformedURLException
174 return (getRegistry(parseURL(name
)).list());
177 private static Registry
getRegistry(URL u
) throws RemoteException
179 if (u
.getPort() == - 1)
181 return (LocateRegistry
.getRegistry(u
.getHost()));
185 return (LocateRegistry
.getRegistry(u
.getHost(), u
.getPort()));
190 * Parses the supplied URL and converts it to use the HTTP protocol. From an
191 * RMI perspective, the scheme is irrelevant and we want to be able to create
192 * a URL for which a handler is available.
194 * @param name the URL in String form.
195 * @throws MalformedURLException if the URL is invalid.
197 private static URL
parseURL(String name
) throws MalformedURLException
201 URI uri
= new URI(name
);
202 String host
= uri
.getHost();
203 int port
= uri
.getPort();
204 String query
= uri
.getQuery();
205 String path
= uri
.getPath();
206 return new URL("http", (host
== null ?
"localhost" : host
),
207 (port
== - 1 ?
1099 : port
), uri
.getPath()
208 + (query
== null ?
"" : query
));
210 catch (URISyntaxException e
)
212 throw new MalformedURLException("The URL syntax was invalid: "
218 * Checks that the URL contains a name, and removes any leading slashes.
220 * @param url the URL to check.
221 * @throws MalformedURLException if no name is specified.
223 private static String
getName(URL url
) throws MalformedURLException
225 String filename
= url
.getFile();
226 if (filename
.length() == 0)
227 throw new MalformedURLException("No path specified: " + url
);
228 // If the filename begins with a slash we must cut it for
230 if (filename
.charAt(0) == '/')
231 return filename
.substring(1);