1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62:
63:
68: public class rmiURLContext implements Context
69: {
70:
73: public static final String DEFAULT_REGISTRY_LOCATION = "rmi://localhost:1099";
74:
75:
80: static WeakHashMap registryCache = new WeakHashMap();
81:
82:
85: Properties properties;
86:
87:
93: boolean lookupCalled;
94:
95:
109: public Object addToEnvironment(String key, Object value)
110: {
111: if (key == null || value == null)
112: throw new NullPointerException();
113: return properties.put(key, value);
114: }
115:
116:
125: public Hashtable getEnvironment() throws NamingException
126: {
127: return properties;
128: }
129:
130:
143: public Object removeFromEnvironment(String propName) throws NamingException
144: {
145: return properties.remove(propName);
146: }
147:
148:
153: public Registry getRegistry(String netAddress) throws NamingException
154: {
155: Registry registry;
156:
157: synchronized (registryCache)
158: {
159: registry = (Registry) registryCache.get(netAddress);
160: }
161:
162: if (registry == null)
163: {
164:
165: int colon = netAddress.lastIndexOf(':');
166: int port;
167:
168: try
169: {
170: if (colon >= 0)
171: {
172: port = Integer.parseInt(netAddress.substring(colon + 1));
173: netAddress = netAddress.substring(0, colon);
174: }
175: else
176: port = Registry.REGISTRY_PORT;
177: }
178: catch (NumberFormatException e1)
179: {
180: throw new InvalidNameException(netAddress);
181: }
182:
183: try
184: {
185: registry = LocateRegistry.getRegistry(netAddress, port);
186: }
187: catch (RemoteException e)
188: {
189: throw new CommunicationException(e.toString());
190: }
191:
192: synchronized (registryCache)
193: {
194: registryCache.put(netAddress, registry);
195: }
196: }
197: return registry;
198: }
199:
200:
206: public rmiURLContext(Map props)
207: {
208: properties = new Properties();
209: if (props != null)
210: properties.putAll(props);
211: }
212:
213:
219: public void bind(Name name, Object obj) throws NamingException
220: {
221: bind(name.toString(), obj);
222: }
223:
224:
227: public void bind(String name, Object obj) throws NamingException
228: {
229: try
230: {
231: String [] n = split(name);
232: getRegistry(n[0]).bind(n[1], (Remote) obj);
233: }
234: catch (AccessException e)
235: {
236: throw new NamingException("access:"+e.toString());
237: }
238: catch (RemoteException e)
239: {
240: throw new CommunicationException(e.toString());
241: }
242: catch (AlreadyBoundException e)
243: {
244: throw new NameAlreadyBoundException(name);
245: }
246: catch (ClassCastException c)
247: {
248: throw new NamingException("Only Remote can be bound:"
249: + obj.getClass().getName());
250: }
251: }
252:
253:
256: public Name composeName(Name name, Name prefix) throws NamingException
257: {
258: throw new OperationNotSupportedException();
259: }
260:
261:
264: public String composeName(String name, String prefix) throws NamingException
265: {
266: throw new OperationNotSupportedException();
267: }
268:
269:
273: public Context createSubcontext(Name name) throws NamingException
274: {
275: if (name.size() == 0)
276: return new rmiURLContext(properties);
277: else
278: throw new OperationNotSupportedException();
279: }
280:
281:
285: public Context createSubcontext(String name) throws NamingException
286: {
287: if (name.length() == 0)
288: return new rmiURLContext(properties);
289: else
290: throw new OperationNotSupportedException();
291: }
292:
293:
296: public void destroySubcontext(Name name) throws NamingException
297: {
298: throw new OperationNotSupportedException();
299: }
300:
301:
304: public void destroySubcontext(String name) throws NamingException
305: {
306: throw new OperationNotSupportedException();
307: }
308:
309:
313: public String getNameInNamespace() throws NamingException
314: {
315: return properties.getProperty(Context.PROVIDER_URL,
316: DEFAULT_REGISTRY_LOCATION);
317: }
318:
319:
322: public NameParser getNameParser(Name name) throws NamingException
323: {
324: throw new OperationNotSupportedException();
325: }
326:
327:
330: public NameParser getNameParser(String name) throws NamingException
331: {
332: throw new OperationNotSupportedException();
333: }
334:
335:
341: public NamingEnumeration list(Name name) throws NamingException
342: {
343: return list(name);
344: }
345:
346:
352: public NamingEnumeration list(String name) throws NamingException
353: {
354: try
355: {
356: String [] n = split(name);
357: if (n[1].length() > 0)
358: throw new InvalidNameException(name+", the name part must be empty");
359: return new ListEnumeration(getRegistry(n[0]).list());
360: }
361: catch (Exception e)
362: {
363: throw new NamingException(e.toString());
364: }
365: }
366:
367:
371: public NamingEnumeration listBindings(Name name) throws NamingException
372: {
373: return listBindings(name.toString());
374: }
375:
376:
379: public NamingEnumeration listBindings(String name) throws NamingException
380: {
381: try
382: {
383: String [] n = split(name);
384: if (n[1].length() > 0)
385: throw new InvalidNameException(name+", the name part must be empty");
386:
387: Registry r = getRegistry(n[0]);
388: return new ListBindingsEnumeration(r.list(), r);
389: }
390: catch (Exception e)
391: {
392: throw new NamingException(e.toString());
393: }
394: }
395:
396:
400: public Object lookupLink(Name name) throws NamingException
401: {
402: return lookupLink(name.toString());
403: }
404:
405:
409: public Object lookupLink(String name) throws NamingException
410: {
411: return new ContextContinuation(properties, getRegistry(name));
412: }
413:
414:
423: public void rebind(Name name, Object obj) throws NamingException
424: {
425: rebind(name.toString(), obj);
426: }
427:
428:
436: public void rebind(String name, Object obj) throws NamingException
437: {
438: try
439: {
440: String [] n = split(name);
441: getRegistry(n[0]).rebind(n[1], (Remote) obj);
442: }
443: catch (AccessException e)
444: {
445: throw new NamingException("access:"+e.toString());
446: }
447: catch (RemoteException e)
448: {
449: throw new CommunicationException(e.toString());
450: }
451: catch (ClassCastException c)
452: {
453: throw new NamingException("Only Remote can be bound:"
454: + obj.getClass().getName());
455: }
456: }
457:
458:
463: public void rename(Name oldName, Name newName) throws NamingException
464: {
465: rename(oldName.toString(), newName.toString());
466: }
467:
468:
473: public synchronized void rename(String oldName, String newName)
474: throws NamingException
475: {
476: try
477: {
478: String [] n = split(oldName);
479: Registry r = getRegistry(n[0]);
480: Remote object = r.lookup(n[1]);
481: r.unbind(oldName);
482: try
483: {
484: String [] n2 = split(newName);
485: Registry r2 = getRegistry(n2[0]);
486: r2.bind(n2[1], object);
487: }
488: catch (AlreadyBoundException e)
489: {
490:
491: try
492: {
493: r.bind(oldName, object);
494: }
495: catch (AlreadyBoundException e1)
496: {
497:
498: throw new InternalError();
499: }
500: throw new NameAlreadyBoundException(newName);
501: }
502: }
503: catch (AccessException e)
504: {
505: throw new NamingException(e.toString());
506: }
507: catch (RemoteException e)
508: {
509: throw new CommunicationException(e.toString());
510: }
511: catch (NotBoundException e)
512: {
513: throw new CommunicationException(e.toString());
514: }
515: }
516:
517:
520: public void unbind(Name name) throws NamingException
521: {
522: unbind(name.toString());
523: }
524:
525:
528: public void unbind(String name) throws NamingException
529: {
530: try
531: {
532: String [] n = split(name);
533: getRegistry(n[0]).unbind(n[1]);
534: }
535: catch (AccessException e)
536: {
537: throw new NamingException(e.toString());
538: }
539: catch (RemoteException e)
540: {
541: throw new CommunicationException(e.toString());
542: }
543: catch (NotBoundException e)
544: {
545: throw new CommunicationException(e.toString());
546: }
547: }
548:
549:
552: public void close() throws NamingException
553: {
554: }
555:
556:
563: public Object lookup(Name name) throws NamingException
564: {
565: return lookup(name.toString());
566: }
567:
568:
573: public Object lookup(String name) throws NamingException
574: {
575: try
576: {
577: String [] n = split(name);
578: return getRegistry(n[0]).lookup(n[1]);
579: }
580: catch (AccessException e)
581: {
582: throw new NamingException(e.toString());
583: }
584: catch (RemoteException e)
585: {
586: throw new CommunicationException(e.toString());
587: }
588: catch (NotBoundException e)
589: {
590: throw new NameNotFoundException(name);
591: }
592: }
593:
594:
605: public String[] split(String address) throws NamingException
606: {
607:
608: if (!address.startsWith("rmi://"))
609: throw new InvalidNameException(
610: address
611: + " should be like 'rmi://localhost:1099/name'");
612:
613: String a = address.substring("rmi://".length());
614:
615:
616: int sfx = a.indexOf('/');
617:
618:
619: while (sfx > 0 && a.charAt(sfx - 1) == '\\')
620: sfx = a.indexOf('/', sfx + 1);
621:
622: String net;
623: String name;
624: if (sfx >= 0)
625: {
626: net = a.substring(0, sfx);
627: name = a.substring(sfx + 1);
628: }
629: else
630: {
631: net = a;
632: name = "";
633: }
634:
635: return new String[] { net, name };
636: }
637: }