1 |
771 |
jeremybenn |
/* Naming.java --
|
2 |
|
|
Copyright (c) 1996, 1997, 1998, 1999, 2004, 2006
|
3 |
|
|
Free Software Foundation, Inc.
|
4 |
|
|
|
5 |
|
|
This file is part of GNU Classpath.
|
6 |
|
|
|
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)
|
10 |
|
|
any later version.
|
11 |
|
|
|
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.
|
16 |
|
|
|
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
|
20 |
|
|
02110-1301 USA.
|
21 |
|
|
|
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
|
25 |
|
|
combination.
|
26 |
|
|
|
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. */
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
package java.rmi;
|
41 |
|
|
|
42 |
|
|
import java.net.MalformedURLException;
|
43 |
|
|
import java.net.URI;
|
44 |
|
|
import java.net.URISyntaxException;
|
45 |
|
|
import java.net.URL;
|
46 |
|
|
import java.rmi.registry.LocateRegistry;
|
47 |
|
|
import java.rmi.registry.Registry;
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* <p>
|
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
|
54 |
|
|
* part is:
|
55 |
|
|
* </p>
|
56 |
|
|
* <p>
|
57 |
|
|
* <code>//host:port/name</code>
|
58 |
|
|
* </p>
|
59 |
|
|
* <p>
|
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.
|
65 |
|
|
* </p>
|
66 |
|
|
* <p>
|
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>.
|
71 |
|
|
* </p>
|
72 |
|
|
*
|
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)
|
78 |
|
|
* @since 1.1
|
79 |
|
|
*/
|
80 |
|
|
public final class Naming
|
81 |
|
|
{
|
82 |
|
|
/**
|
83 |
|
|
* This class isn't intended to be instantiated.
|
84 |
|
|
*/
|
85 |
|
|
private Naming()
|
86 |
|
|
{
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
/**
|
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:
|
92 |
|
|
*
|
93 |
|
|
* <pre>
|
94 |
|
|
* //host:port/service-name
|
95 |
|
|
* </pre>
|
96 |
|
|
*
|
97 |
|
|
* The port is optional.
|
98 |
|
|
*
|
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
|
104 |
|
|
*/
|
105 |
|
|
public static Remote lookup(String name) throws NotBoundException,
|
106 |
|
|
MalformedURLException, RemoteException
|
107 |
|
|
{
|
108 |
|
|
URL u = parseURL(name);
|
109 |
|
|
String serviceName = getName(u);
|
110 |
|
|
return (getRegistry(u).lookup(serviceName));
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
/**
|
114 |
|
|
* Try to bind the given object to the given service name.
|
115 |
|
|
*
|
116 |
|
|
* @param name
|
117 |
|
|
* @param obj
|
118 |
|
|
* @throws AlreadyBoundException
|
119 |
|
|
* @throws MalformedURLException
|
120 |
|
|
* @throws RemoteException
|
121 |
|
|
*/
|
122 |
|
|
public static void bind(String name, Remote obj)
|
123 |
|
|
throws AlreadyBoundException, MalformedURLException, RemoteException
|
124 |
|
|
{
|
125 |
|
|
URL u = parseURL(name);
|
126 |
|
|
String serviceName = getName(u);
|
127 |
|
|
getRegistry(u).bind(serviceName, obj);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
/**
|
131 |
|
|
* Remove a binding for a given service name.
|
132 |
|
|
*
|
133 |
|
|
* @param name
|
134 |
|
|
* @throws RemoteException
|
135 |
|
|
* @throws NotBoundException
|
136 |
|
|
* @throws MalformedURLException
|
137 |
|
|
*/
|
138 |
|
|
public static void unbind(String name) throws RemoteException,
|
139 |
|
|
NotBoundException, MalformedURLException
|
140 |
|
|
{
|
141 |
|
|
URL u = parseURL(name);
|
142 |
|
|
String serviceName = getName(u);
|
143 |
|
|
getRegistry(u).unbind(serviceName);
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
/**
|
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.
|
149 |
|
|
*
|
150 |
|
|
* @param name
|
151 |
|
|
* @param obj
|
152 |
|
|
* @throws RemoteException
|
153 |
|
|
* @throws MalformedURLException
|
154 |
|
|
*/
|
155 |
|
|
public static void rebind(String name, Remote obj) throws RemoteException,
|
156 |
|
|
MalformedURLException
|
157 |
|
|
{
|
158 |
|
|
URL u = parseURL(name);
|
159 |
|
|
String serviceName = getName(u);
|
160 |
|
|
getRegistry(u).rebind(serviceName, obj);
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
/**
|
164 |
|
|
* Lists all services at the named registry.
|
165 |
|
|
*
|
166 |
|
|
* @param name url that specifies the registry
|
167 |
|
|
* @return list of services at the name registry
|
168 |
|
|
* @throws RemoteException
|
169 |
|
|
* @throws MalformedURLException
|
170 |
|
|
*/
|
171 |
|
|
public static String[] list(String name) throws RemoteException,
|
172 |
|
|
MalformedURLException
|
173 |
|
|
{
|
174 |
|
|
return (getRegistry(parseURL(name)).list());
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
private static Registry getRegistry(URL u) throws RemoteException
|
178 |
|
|
{
|
179 |
|
|
if (u.getPort() == - 1)
|
180 |
|
|
{
|
181 |
|
|
return (LocateRegistry.getRegistry(u.getHost()));
|
182 |
|
|
}
|
183 |
|
|
else
|
184 |
|
|
{
|
185 |
|
|
return (LocateRegistry.getRegistry(u.getHost(), u.getPort()));
|
186 |
|
|
}
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
/**
|
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.
|
193 |
|
|
*
|
194 |
|
|
* @param name the URL in String form.
|
195 |
|
|
* @throws MalformedURLException if the URL is invalid.
|
196 |
|
|
*/
|
197 |
|
|
private static URL parseURL(String name) throws MalformedURLException
|
198 |
|
|
{
|
199 |
|
|
try
|
200 |
|
|
{
|
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));
|
209 |
|
|
}
|
210 |
|
|
catch (URISyntaxException e)
|
211 |
|
|
{
|
212 |
|
|
throw new MalformedURLException("The URL syntax was invalid: "
|
213 |
|
|
+ e.getMessage());
|
214 |
|
|
}
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
/**
|
218 |
|
|
* Checks that the URL contains a name, and removes any leading slashes.
|
219 |
|
|
*
|
220 |
|
|
* @param url the URL to check.
|
221 |
|
|
* @throws MalformedURLException if no name is specified.
|
222 |
|
|
*/
|
223 |
|
|
private static String getName(URL url) throws MalformedURLException
|
224 |
|
|
{
|
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
|
229 |
|
|
// name resolution.
|
230 |
|
|
if (filename.charAt(0) == '/')
|
231 |
|
|
return filename.substring(1);
|
232 |
|
|
return filename;
|
233 |
|
|
}
|
234 |
|
|
}
|