39 |
package java.rmi; |
package java.rmi; |
40 |
|
|
41 |
import java.net.MalformedURLException; |
import java.net.MalformedURLException; |
42 |
|
import java.net.URI; |
43 |
|
import java.net.URISyntaxException; |
44 |
import java.net.URL; |
import java.net.URL; |
45 |
import java.rmi.registry.LocateRegistry; |
import java.rmi.registry.LocateRegistry; |
46 |
import java.rmi.registry.Registry; |
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 { |
public final class Naming { |
80 |
|
|
81 |
/** |
/** |
82 |
* This class isn't intended to be instantiated. |
* This class isn't intended to be instantiated. |
83 |
*/ |
*/ |
100 |
* @throws RemoteException |
* @throws RemoteException |
101 |
*/ |
*/ |
102 |
public static Remote lookup(String name) throws NotBoundException, MalformedURLException, RemoteException { |
public static Remote lookup(String name) throws NotBoundException, MalformedURLException, RemoteException { |
103 |
// hack to accept "rmi://host:port/service" strings |
URL u = parseURL(name); |
104 |
if(name.startsWith("rmi:")){ name = name.substring(4); } |
String serviceName = getName(u); |
105 |
URL u = new URL("http:" + name); |
return (getRegistry(u).lookup(serviceName)); |
|
String filename = u.getFile(); |
|
|
|
|
|
// If the filename begins with a slash we must cut it for |
|
|
// name resolution. |
|
|
if (filename.charAt(0) == '/') |
|
|
return (getRegistry(u).lookup(filename.substring(1))); |
|
|
else |
|
|
return (getRegistry(u).lookup(filename)); |
|
106 |
} |
} |
107 |
|
|
108 |
/** |
/** |
114 |
* @throws RemoteException |
* @throws RemoteException |
115 |
*/ |
*/ |
116 |
public static void bind(String name, Remote obj) throws AlreadyBoundException, MalformedURLException, RemoteException { |
public static void bind(String name, Remote obj) throws AlreadyBoundException, MalformedURLException, RemoteException { |
117 |
URL u = new URL("http:" + name); |
URL u = parseURL(name); |
118 |
String filename = u.getFile(); |
String serviceName = getName(u); |
119 |
// If the filename begins with a slash we must cut it for |
getRegistry(u).bind(serviceName, obj); |
|
// name resolution. |
|
|
if (filename.charAt(0) == '/') |
|
|
getRegistry(u).bind(filename.substring(1), obj); |
|
|
else |
|
|
getRegistry(u).bind(filename, obj); |
|
120 |
} |
} |
121 |
|
|
122 |
/** |
/** |
127 |
* @throws MalformedURLException |
* @throws MalformedURLException |
128 |
*/ |
*/ |
129 |
public static void unbind(String name) throws RemoteException, NotBoundException, MalformedURLException { |
public static void unbind(String name) throws RemoteException, NotBoundException, MalformedURLException { |
130 |
URL u = new URL("http:" + name); |
URL u = parseURL(name); |
131 |
String filename = u.getFile(); |
String serviceName = getName(u); |
132 |
// If the filename begins with a slash we must cut it for |
getRegistry(u).unbind(serviceName); |
|
// name resolution. |
|
|
if (filename.charAt(0) == '/') |
|
|
getRegistry(u).unbind(filename.substring(1)); |
|
|
else |
|
|
getRegistry(u).unbind(filename); |
|
133 |
} |
} |
134 |
|
|
135 |
/** |
/** |
141 |
* @throws MalformedURLException |
* @throws MalformedURLException |
142 |
*/ |
*/ |
143 |
public static void rebind(String name, Remote obj) throws RemoteException, MalformedURLException { |
public static void rebind(String name, Remote obj) throws RemoteException, MalformedURLException { |
144 |
URL u = new URL("http:" + name); |
URL u = parseURL(name); |
145 |
String filename = u.getFile(); |
String serviceName = getName(u); |
146 |
// If the filename begins with a slash we must cut it for |
getRegistry(u).rebind(serviceName, obj); |
|
// name resolution. |
|
|
if (filename.charAt(0) == '/') |
|
|
getRegistry(u).rebind(filename.substring(1), obj); |
|
|
else |
|
|
getRegistry(u).rebind(filename, obj); |
|
147 |
} |
} |
148 |
|
|
149 |
/** |
/** |
154 |
* @throws MalformedURLException |
* @throws MalformedURLException |
155 |
*/ |
*/ |
156 |
public static String[] list(String name) throws RemoteException, MalformedURLException { |
public static String[] list(String name) throws RemoteException, MalformedURLException { |
157 |
return (getRegistry(new URL("http:" + name)).list()); |
return (getRegistry(parseURL(name)).list()); |
158 |
} |
} |
159 |
|
|
160 |
private static Registry getRegistry(URL u) throws RemoteException { |
private static Registry getRegistry(URL u) throws RemoteException { |
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 |
} |
} |