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 RmiContinuation 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:
211: public RmiContinuation(Map props)
212: {
213: properties = new Properties();
214: if (props != null)
215: properties.putAll(props);
216: }
217:
218:
224: public void bind(Name name, Object obj) throws NamingException
225: {
226: bind(name.toString(), obj);
227: }
228:
229:
232: public void bind(String name, Object obj) throws NamingException
233: {
234: try
235: {
236: getRegistry().bind(name, (Remote) obj);
237: }
238: catch (AccessException e)
239: {
240: throw new NamingException("access:"+e.toString());
241: }
242: catch (RemoteException e)
243: {
244: throw new CommunicationException(e.toString());
245: }
246: catch (AlreadyBoundException e)
247: {
248: throw new NameAlreadyBoundException(name);
249: }
250: catch (ClassCastException c)
251: {
252: throw new NamingException("Only Remote can be bound:"
253: + obj.getClass().getName());
254: }
255: }
256:
257:
260: public Name composeName(Name name, Name prefix) throws NamingException
261: {
262: throw new OperationNotSupportedException();
263: }
264:
265:
268: public String composeName(String name, String prefix) throws NamingException
269: {
270: throw new OperationNotSupportedException();
271: }
272:
273:
277: public Context createSubcontext(Name name) throws NamingException
278: {
279: if (name.size() == 0)
280: return new RmiContinuation(properties);
281: else
282: throw new OperationNotSupportedException();
283: }
284:
285:
289: public Context createSubcontext(String name) throws NamingException
290: {
291: if (name.length() == 0)
292: return new RmiContinuation(properties);
293: else
294: throw new OperationNotSupportedException();
295: }
296:
297:
300: public void destroySubcontext(Name name) throws NamingException
301: {
302: throw new OperationNotSupportedException();
303: }
304:
305:
308: public void destroySubcontext(String name) throws NamingException
309: {
310: throw new OperationNotSupportedException();
311: }
312:
313:
317: public String getNameInNamespace() throws NamingException
318: {
319: return properties.getProperty(Context.PROVIDER_URL,
320: DEFAULT_REGISTRY_LOCATION);
321: }
322:
323:
326: public NameParser getNameParser(Name name) throws NamingException
327: {
328: throw new OperationNotSupportedException();
329: }
330:
331:
334: public NameParser getNameParser(String name) throws NamingException
335: {
336: throw new OperationNotSupportedException();
337: }
338:
339:
345: public NamingEnumeration list(Name name) throws NamingException
346: {
347: if (name.size() > 0)
348: throw new OperationNotSupportedException("Only empty name is accepted");
349: return list("");
350: }
351:
352:
358: public NamingEnumeration list(String name) throws NamingException
359: {
360: if (name.length() > 0)
361: throw new OperationNotSupportedException("Only empty name is accepted");
362:
363: try
364: {
365: return new ListEnumeration(getRegistry().list());
366: }
367: catch (Exception e)
368: {
369: throw new NamingException(e.toString());
370: }
371: }
372:
373:
377: public NamingEnumeration listBindings(Name name) throws NamingException
378: {
379: if (name.size() > 0)
380: throw new OperationNotSupportedException("Only empty name is accepted");
381: return listBindings("");
382: }
383:
384:
388: public NamingEnumeration listBindings(String name) throws NamingException
389: {
390: if (name.length() > 0)
391: throw new OperationNotSupportedException("Only empty name is accepted");
392:
393: try
394: {
395: Registry r = getRegistry();
396: return new ListBindingsEnumeration(r.list(), r);
397: }
398: catch (Exception e)
399: {
400: throw new NamingException(e.toString());
401: }
402: }
403:
404:
407: public Object lookupLink(Name name) throws NamingException
408: {
409: throw new OperationNotSupportedException();
410: }
411:
412:
415: public Object lookupLink(String name) throws NamingException
416: {
417: throw new OperationNotSupportedException();
418: }
419:
420:
429: public void rebind(Name name, Object obj) throws NamingException
430: {
431: rebind(name.toString(), obj);
432: }
433:
434:
442: public void rebind(String name, Object obj) throws NamingException
443: {
444: try
445: {
446: getRegistry().rebind(name, (Remote) obj);
447: }
448: catch (AccessException e)
449: {
450: throw new NamingException("access:"+e.toString());
451: }
452: catch (RemoteException e)
453: {
454: throw new CommunicationException(e.toString());
455: }
456: catch (ClassCastException c)
457: {
458: throw new NamingException("Only Remote can be bound:"
459: + obj.getClass().getName());
460: }
461: }
462:
463:
468: public void rename(Name oldName, Name newName) throws NamingException
469: {
470: rename(oldName.toString(), newName.toString());
471: }
472:
473:
478: public synchronized void rename(String oldName, String newName)
479: throws NamingException
480: {
481: try
482: {
483: Registry r = getRegistry();
484: Remote object = r.lookup(oldName);
485: r.unbind(oldName);
486: try
487: {
488: r.bind(newName, object);
489: }
490: catch (AlreadyBoundException e)
491: {
492:
493: try
494: {
495: r.bind(oldName, object);
496: }
497: catch (AlreadyBoundException e1)
498: {
499:
500: throw new InternalError();
501: }
502: throw new NameAlreadyBoundException(newName);
503: }
504: }
505: catch (AccessException e)
506: {
507: throw new NamingException(e.toString());
508: }
509: catch (RemoteException e)
510: {
511: throw new CommunicationException(e.toString());
512: }
513: catch (NotBoundException e)
514: {
515: throw new CommunicationException(e.toString());
516: }
517: }
518:
519:
522: public void unbind(Name name) throws NamingException
523: {
524: unbind(name.toString());
525: }
526:
527:
530: public void unbind(String name) throws NamingException
531: {
532: try
533: {
534: getRegistry().unbind(name);
535: }
536: catch (AccessException e)
537: {
538: throw new NamingException(e.toString());
539: }
540: catch (RemoteException e)
541: {
542: throw new CommunicationException(e.toString());
543: }
544: catch (NotBoundException e)
545: {
546: throw new CommunicationException(e.toString());
547: }
548: }
549:
550:
553: public void close() throws NamingException
554: {
555: removeRegistry();
556: }
557:
558:
565: public Object lookup(Name name) throws NamingException
566: {
567: return lookup(name.toString());
568: }
569:
570:
575: public Object lookup(String name) throws NamingException
576: {
577: try
578: {
579: return getRegistry().lookup(name);
580: }
581: catch (AccessException e)
582: {
583: throw new NamingException(e.toString());
584: }
585: catch (RemoteException e)
586: {
587: throw new CommunicationException(e.toString());
588: }
589: catch (NotBoundException e)
590: {
591: throw new NameNotFoundException(name);
592: }
593: }
594: }