1 7 package java.rmi; 8 9 import java.rmi.registry.*; 10 import java.net.MalformedURLException ; 11 import java.net.URI ; 12 import java.net.URISyntaxException ; 13 14 55 public final class Naming { 56 59 private Naming() {} 60 61 74 public static Remote lookup(String name) 75 throws NotBoundException , 76 java.net.MalformedURLException , 77 RemoteException 78 { 79 ParsedNamingURL parsed = parseURL(name); 80 Registry registry = getRegistry(parsed); 81 82 if (parsed.name == null) 83 return registry; 84 return registry.lookup(parsed.name); 85 } 86 87 100 public static void bind(String name, Remote obj) 101 throws AlreadyBoundException , 102 java.net.MalformedURLException , 103 RemoteException 104 { 105 ParsedNamingURL parsed = parseURL(name); 106 Registry registry = getRegistry(parsed); 107 108 if (obj == null) 109 throw new NullPointerException ("cannot bind to null"); 110 111 registry.bind(parsed.name, obj); 112 } 113 114 127 public static void unbind(String name) 128 throws RemoteException , 129 NotBoundException , 130 java.net.MalformedURLException 131 { 132 ParsedNamingURL parsed = parseURL(name); 133 Registry registry = getRegistry(parsed); 134 135 registry.unbind(parsed.name); 136 } 137 138 151 public static void rebind(String name, Remote obj) 152 throws RemoteException , java.net.MalformedURLException 153 { 154 ParsedNamingURL parsed = parseURL(name); 155 Registry registry = getRegistry(parsed); 156 157 if (obj == null) 158 throw new NullPointerException ("cannot bind to null"); 159 160 registry.rebind(parsed.name, obj); 161 } 162 163 178 public static String [] list(String name) 179 throws RemoteException , java.net.MalformedURLException 180 { 181 ParsedNamingURL parsed = parseURL(name); 182 Registry registry = getRegistry(parsed); 183 184 String prefix = ""; 185 if (parsed.port > 0 || !parsed.host.equals("")) 186 prefix += "//" + parsed.host; 187 if (parsed.port > 0) 188 prefix += ":" + parsed.port; 189 prefix += "/"; 190 191 String [] names = registry.list(); 192 for (int i = 0; i < names.length; i++) { 193 names[i] = prefix + names[i]; 194 } 195 return names; 196 } 197 198 201 private static Registry getRegistry(ParsedNamingURL parsed) 202 throws RemoteException 203 { 204 return LocateRegistry.getRegistry(parsed.host, parsed.port); 205 } 206 207 216 private static ParsedNamingURL parseURL(String str) 217 throws MalformedURLException 218 { 219 try { 220 URI uri = new URI (str); 221 if (uri.getFragment() != null) { 222 throw new MalformedURLException ( 223 "invalid character, '#', in URL name: " + str); 224 } else if (uri.getQuery() != null) { 225 throw new MalformedURLException ( 226 "invalid character, '?', in URL name: " + str); 227 } else if (uri.getUserInfo() != null) { 228 throw new MalformedURLException ( 229 "invalid character, '@', in URL host: " + str); 230 } 231 String scheme = uri.getScheme(); 232 if (scheme != null && !scheme.equals("rmi")) { 233 throw new MalformedURLException ("invalid URL scheme: " + str); 234 } 235 236 String name = uri.getPath(); 237 if (name != null) { 238 if (name.startsWith("/")) { 239 name = name.substring(1); 240 } 241 if (name.length() == 0) { 242 name = null; 243 } 244 } 245 246 String host = uri.getHost(); 247 if (host == null) { 248 host = ""; 249 if (uri.getPort() == -1) { 250 255 String authority = uri.getAuthority(); 256 if (authority != null && authority.startsWith(":")) { 257 authority = "localhost" + authority; 258 uri = new URI (null, authority, null, null, null); 259 } 260 } 261 } 262 int port = uri.getPort(); 263 if (port == -1) { 264 port = Registry.REGISTRY_PORT; 265 } 266 return new ParsedNamingURL(host, port, name); 267 268 } catch (URISyntaxException ex) { 269 throw (MalformedURLException ) new MalformedURLException ( 270 "invalid URL string: " + str).initCause(ex); 271 } 272 } 273 274 277 private static class ParsedNamingURL { 278 String host; 279 int port; 280 String name; 281 282 ParsedNamingURL(String host, int port, String name) { 283 this.host = host; 284 this.port = port; 285 this.name = name; 286 } 287 } 288 } 289 | Popular Tags |