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:
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61:
62:
67: public class ContextContinuation implements Context
68: {
69:
72: public static final String DEFAULT_REGISTRY_LOCATION = "rmi://localhost:1099";
73:
74:
78: Registry registry;
79:
80:
83: Properties properties;
84:
85:
91: boolean lookupCalled;
92:
93:
107: public Object addToEnvironment(String key, Object value)
108: {
109: removeRegistry();
110: if (key == null || value == null)
111: throw new NullPointerException();
112: return properties.put(key, value);
113: }
114:
115:
124: public Hashtable getEnvironment() throws NamingException
125: {
126: return properties;
127: }
128:
129:
142: public Object removeFromEnvironment(String propName) throws NamingException
143: {
144: removeRegistry();
145: return properties.remove(propName);
146: }
147:
148:
151: public void removeRegistry()
152: {
153: registry = null;
154: }
155:
156:
161: public Registry getRegistry() throws NamingException
162: {
163: if (registry == null)
164: {
165: String address = properties.getProperty(Context.PROVIDER_URL,
166: DEFAULT_REGISTRY_LOCATION);
167:
168:
169: if (!address.startsWith("rmi://"))
170: throw new InvalidNameException(address);
171:
172: String a = address.substring("rmi://".length());
173:
174:
175: int colon = a.lastIndexOf(':');
176: int port;
177:
178: try
179: {
180: if (colon >=0)
181: {
182: port = Integer.parseInt(a.substring(colon+1));
183: a = a.substring(0, colon);
184: }
185: else
186: port = Registry.REGISTRY_PORT;
187: }
188: catch (NumberFormatException e1)
189: {
190: throw new InvalidNameException(address);
191: }
192:
193: try
194: {
195: registry = LocateRegistry.getRegistry(a, port);
196: }
197: catch (RemoteException e)
198: {
199: throw new CommunicationException(e.toString());
200: }
201: }
202: return registry;
203: }
204:
205:
213: public ContextContinuation(Map props, Registry initialRegistry)
214: {
215: properties = new Properties();
216: if (props != null)
217: properties.putAll(props);
218: registry = initialRegistry;
219: }
220:
221:
227: public void bind(Name name, Object obj) throws NamingException
228: {
229: bind(name.toString(), obj);
230: }
231:
232:
235: public void bind(String name, Object obj) throws NamingException
236: {
237: try
238: {
239: getRegistry().bind(name, (Remote) obj);
240: }
241: catch (AccessException e)
242: {
243: throw new NamingException("access:"+e.toString());
244: }
245: catch (RemoteException e)
246: {
247: throw new CommunicationException(e.toString());
248: }
249: catch (AlreadyBoundException e)
250: {
251: throw new NameAlreadyBoundException(name);
252: }
253: catch (ClassCastException c)
254: {
255: throw new NamingException("Only Remote can be bound:"
256: + obj.getClass().getName());
257: }
258: }
259:
260:
263: public Name composeName(Name name, Name prefix) throws NamingException
264: {
265: throw new OperationNotSupportedException();
266: }
267:
268:
271: public String composeName(String name, String prefix) throws NamingException
272: {
273: throw new OperationNotSupportedException();
274: }
275:
276:
280: public Context createSubcontext(Name name) throws NamingException
281: {
282: if (name.size() == 0)
283: return new rmiURLContext(properties);
284: else
285: throw new OperationNotSupportedException();
286: }
287:
288:
292: public Context createSubcontext(String name) throws NamingException
293: {
294: if (name.length() == 0)
295: return new rmiURLContext(properties);
296: else
297: throw new OperationNotSupportedException();
298: }
299:
300:
303: public void destroySubcontext(Name name) throws NamingException
304: {
305: throw new OperationNotSupportedException();
306: }
307:
308:
311: public void destroySubcontext(String name) throws NamingException
312: {
313: throw new OperationNotSupportedException();
314: }
315:
316:
320: public String getNameInNamespace() throws NamingException
321: {
322: return properties.getProperty(Context.PROVIDER_URL,
323: DEFAULT_REGISTRY_LOCATION);
324: }
325:
326:
329: public NameParser getNameParser(Name name) throws NamingException
330: {
331: throw new OperationNotSupportedException();
332: }
333:
334:
337: public NameParser getNameParser(String name) throws NamingException
338: {
339: throw new OperationNotSupportedException();
340: }
341:
342:
348: public NamingEnumeration list(Name name) throws NamingException
349: {
350: if (name.size() > 0)
351: throw new OperationNotSupportedException("Only empty name is accepted");
352: return list("");
353: }
354:
355:
361: public NamingEnumeration list(String name) throws NamingException
362: {
363: if (name.length() > 0)
364: throw new OperationNotSupportedException("Only empty name is accepted");
365:
366: try
367: {
368: return new ListEnumeration(getRegistry().list());
369: }
370: catch (Exception e)
371: {
372: throw new NamingException(e.toString());
373: }
374: }
375:
376:
380: public NamingEnumeration listBindings(Name name) throws NamingException
381: {
382: if (name.size() > 0)
383: throw new OperationNotSupportedException("Only empty name is accepted");
384: return listBindings("");
385: }
386:
387:
391: public NamingEnumeration listBindings(String name) throws NamingException
392: {
393: if (name.length() > 0)
394: throw new OperationNotSupportedException("Only empty name is accepted");
395:
396: try
397: {
398: Registry r = getRegistry();
399: return new ListBindingsEnumeration(r.list(), r);
400: }
401: catch (Exception e)
402: {
403: throw new NamingException(e.toString());
404: }
405: }
406:
407:
410: public Object lookupLink(Name name) throws NamingException
411: {
412: throw new OperationNotSupportedException();
413: }
414:
415:
418: public Object lookupLink(String name) throws NamingException
419: {
420: throw new OperationNotSupportedException();
421: }
422:
423:
432: public void rebind(Name name, Object obj) throws NamingException
433: {
434: rebind(name.toString(), obj);
435: }
436:
437:
445: public void rebind(String name, Object obj) throws NamingException
446: {
447: try
448: {
449: getRegistry().rebind(name, (Remote) obj);
450: }
451: catch (AccessException e)
452: {
453: throw new NamingException("access:"+e.toString());
454: }
455: catch (RemoteException e)
456: {
457: throw new CommunicationException(e.toString());
458: }
459: catch (ClassCastException c)
460: {
461: throw new NamingException("Only Remote can be bound:"
462: + obj.getClass().getName());
463: }
464: }
465:
466:
471: public void rename(Name oldName, Name newName) throws NamingException
472: {
473: rename(oldName.toString(), newName.toString());
474: }
475:
476:
481: public synchronized void rename(String oldName, String newName)
482: throws NamingException
483: {
484: try
485: {
486: Registry r = getRegistry();
487: Remote object = r.lookup(oldName);
488: r.unbind(oldName);
489: try
490: {
491: r.bind(newName, object);
492: }
493: catch (AlreadyBoundException e)
494: {
495:
496: try
497: {
498: r.bind(oldName, object);
499: }
500: catch (AlreadyBoundException e1)
501: {
502:
503: throw new InternalError();
504: }
505: throw new NameAlreadyBoundException(newName);
506: }
507: }
508: catch (AccessException e)
509: {
510: throw new NamingException(e.toString());
511: }
512: catch (RemoteException e)
513: {
514: throw new CommunicationException(e.toString());
515: }
516: catch (NotBoundException e)
517: {
518: throw new CommunicationException(e.toString());
519: }
520: }
521:
522:
525: public void unbind(Name name) throws NamingException
526: {
527: unbind(name.toString());
528: }
529:
530:
533: public void unbind(String name) throws NamingException
534: {
535: try
536: {
537: getRegistry().unbind(name);
538: }
539: catch (AccessException e)
540: {
541: throw new NamingException(e.toString());
542: }
543: catch (RemoteException e)
544: {
545: throw new CommunicationException(e.toString());
546: }
547: catch (NotBoundException e)
548: {
549: throw new CommunicationException(e.toString());
550: }
551: }
552:
553:
556: public void close() throws NamingException
557: {
558: removeRegistry();
559: }
560:
561:
568: public Object lookup(Name name) throws NamingException
569: {
570: return lookup(name.toString());
571: }
572:
573:
578: public Object lookup(String name) throws NamingException
579: {
580: try
581: {
582: return getRegistry().lookup(name);
583: }
584: catch (AccessException e)
585: {
586: throw new NamingException(e.toString());
587: }
588: catch (RemoteException e)
589: {
590: throw new CommunicationException(e.toString());
591: }
592: catch (NotBoundException e)
593: {
594: throw new NameNotFoundException(name);
595: }
596: }
597: }