KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > util > corba > NameService


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

26
27
28 package org.objectweb.mobilitools.util.corba;
29
30
31 import org.omg.CORBA.*;
32 import org.omg.CosNaming.*;
33 import org.omg.CosNaming.NamingContextPackage.*;
34 import java.util.*;
35
36
37 /**
38  * MobiliTools $Name: $, $Id: NameService.java,v 1.1.1.1 2003/03/28 14:48:06 dillense Exp $
39  * <P>
40  * Front-end utility for the CORBA naming service.
41  */

42 public class NameService
43 {
44     // reference to the default name service
45
NamingContext nameServiceRef;
46     static final int LIST_MAX_ONCE = 100;
47
48
49     /**
50         Creates and initializes a new name service front-end.
51         @param orb reference to the current ORB object (see return from ORB.init() call).
52         @exception NameServiceException thrown if the initial reference to a naming service
53             cannot be obtained.
54     */

55     public NameService(ORB orb) throws NameServiceException
56     {
57         org.omg.CORBA.Object JavaDoc objRef = null;
58
59         try
60         {
61             objRef = orb.resolve_initial_references("NameService");
62         }
63         catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc e)
64         {
65             throw new NameServiceException(e);
66         }
67         if (objRef != null)
68         {
69             try
70             {
71                 nameServiceRef = NamingContextHelper.narrow(objRef);
72             }
73             catch (org.omg.CORBA.BAD_PARAM JavaDoc e)
74             {
75                 throw new NameServiceException(e);
76             }
77         }
78         else
79         {
80             throw new NameServiceException();
81         }
82     }
83
84
85     /**
86         Creates and initializes a new name service front-end.
87         @param obj object reference narrowing to a naming context.
88         @exception NameServiceException thrown if the object reference to a naming service
89             cannot be narrowed to a naming context.
90     */

91     public NameService(org.omg.CORBA.Object JavaDoc obj) throws NameServiceException
92     {
93         if (obj != null)
94         {
95             try
96             {
97                 nameServiceRef = NamingContextHelper.narrow(obj);
98             }
99             catch (org.omg.CORBA.BAD_PARAM JavaDoc e)
100             {
101                 throw new NameServiceException(e);
102             }
103         }
104         else
105         {
106             throw new NameServiceException();
107         }
108     }
109
110
111     /**
112         Resolves a String-fied name to the bound object reference.
113         @param name string translation ("context!kind/.../object!kind") of the object's name.
114         @return the object reference bound to the named object.
115         @exception NameServiceException same as resolve(org.omg.CosNaming.NameComponent[])
116         @see #resolve(org.omg.CosNaming.NameComponent[])
117         @see #stringToName(java.lang.String)
118     */

119     public org.omg.CORBA.Object JavaDoc resolve(String JavaDoc name) throws NameServiceException
120     {
121         return resolve(stringToName(name));
122     }
123
124
125     /**
126         Resolves a name to the bound object reference.
127         @param name name of the object in the naming service.
128         @return the object reference bound to the named object.
129         @exception NameServiceException thrown in several failure situations:
130             <UL>
131                 <LI>CORBA system exception,
132                 <LI>the name is invalid,
133                 <LI>the name was not found,
134                 <LI>naming service internal error.
135             </UL>
136             See the exception attribute of the NameServiceException, and refer to the CORBA naming service
137             definitions to get the actual diagnostic.
138     */

139     public org.omg.CORBA.Object JavaDoc resolve(NameComponent[] name) throws NameServiceException
140     {
141         org.omg.CORBA.Object JavaDoc obj = null;
142
143         if (name.length == 0)
144         {
145             return nameServiceRef;
146         }
147
148         try
149         {
150             obj = nameServiceRef.resolve(name);
151         }
152         catch (SystemException e)
153         {
154             throw new NameServiceException(e);
155         }
156         catch (InvalidName e)
157         {
158             throw new NameServiceException(e);
159         }
160         catch (NotFound e)
161         {
162             throw new NameServiceException(e);
163         }
164         catch (CannotProceed e)
165         {
166             throw new NameServiceException(e);
167         }
168         return obj;
169     }
170
171
172     /**
173         Returns an array of objects of contexts bound in the given context name.
174         @param contextName the string-fied name of the context to list ("contex!kind/..." format).
175         @return an array of NSbinding objects listing the objects and contexts found.
176         @exception NameServiceException same as list(org.omg.CORBA.NameComponent[]).
177         @see #list(NameComponent[])
178     */

179     public NSbinding[] list(String JavaDoc contextName) throws NameServiceException
180     {
181         return list(stringToName(contextName));
182     }
183
184
185     /**
186         Returns an array of objects of contexts bound in the given context name.
187         @param contextName name of the context to list.
188         @return an array of NSbinding objects listing the objects and contexts found.
189         @exception NameServiceException thrown if a communication exception occurs
190             while getting the content list of the specified context, or if the specified
191             context cannot be narrowed to a naming context. In the former case, the actual
192             exception object is attached to the NameServiceException. Since some bindings may
193             disappear while resolving the object references, the resulting exceptions are
194             ignored.
195     */

196     public NSbinding[] list(NameComponent[] contextName) throws NameServiceException
197     {
198         NamingContext context = null;
199         BindingListHolder listHolder = new BindingListHolder();
200         BindingIteratorHolder iteratorHolder = new BindingIteratorHolder();
201         NSbinding element;
202         int size;
203
204         try
205         {
206             context = NamingContextHelper.narrow(resolve(contextName));
207         }
208         catch (org.omg.CORBA.BAD_PARAM JavaDoc e)
209         {
210             throw new NameServiceException(e);
211         }
212         try
213         {
214             context.list(LIST_MAX_ONCE, listHolder, iteratorHolder);
215         }
216         catch (SystemException e)
217         {
218             throw new NameServiceException(e);
219         }
220
221         Vector vect = new Vector(LIST_MAX_ONCE, LIST_MAX_ONCE);
222         boolean again = true;
223         while (again)
224         {
225             for (int i=0 ; i<listHolder.value.length ; ++i)
226             {
227                 try
228                 {
229                     element = new NSbinding(
230                         listHolder.value[i].binding_name[listHolder.value[i].binding_name.length-1].id,
231                         listHolder.value[i].binding_name[listHolder.value[i].binding_name.length-1].kind,
232                         (listHolder.value[i].binding_type.value() == BindingType._nobject
233                         ? NSbinding.OBJECT : NSbinding.CONTEXT),
234                         context.resolve(listHolder.value[i].binding_name));
235                     vect.addElement(element);
236                 }
237                 catch (SystemException e)
238                 {
239                     throw new NameServiceException(e);
240                 }
241                 catch (InvalidName e)
242                 {
243                 }
244                 catch (NotFound e)
245                 {
246                 }
247                 catch (CannotProceed e)
248                 {
249                     throw new NameServiceException(e);
250                 }
251             }
252             if (iteratorHolder.value != null)
253             {
254                 try
255                 {
256                     again = iteratorHolder.value.next_n(LIST_MAX_ONCE, listHolder);
257                 }
258                 catch (SystemException e)
259                 {
260                     throw new NameServiceException(e);
261                 }
262             }
263             else
264             {
265                 again = false;
266             }
267         }
268         NSbinding[] result = new NSbinding[vect.size()];
269         Enumeration lister = vect.elements();
270         for (int i=0 ; lister.hasMoreElements() ; ++i)
271         {
272             result[i] = (NSbinding)lister.nextElement();
273         }
274         return result;
275     }
276
277
278     public void makePath(String JavaDoc name) throws NameServiceException
279     {
280         makePath(stringToName(name));
281     }
282
283
284     public void makePath(NameComponent[] name) throws NameServiceException
285     {
286         NamingContext nc = nameServiceRef;
287         NameComponent[] step = new NameComponent[1];
288
289         for (int i=0 ; i<name.length ; ++i)
290         {
291             step[0] = name[i];
292             try
293             {
294                 nc = nc.bind_new_context(step);
295             }
296             catch (AlreadyBound e)
297             {
298                 try
299                 {
300                     nc = NamingContextHelper.narrow(nc.resolve(step));
301                 }
302                 catch (SystemException e2)
303                 {
304                     throw new NameServiceException(e);
305                 }
306                 catch (InvalidName e2)
307                 {
308                     throw new NameServiceException(e);
309                 }
310                 catch (NotFound e2)
311                 {
312                     throw new NameServiceException(e);
313                 }
314                 catch (CannotProceed e2)
315                 {
316                     throw new NameServiceException(e);
317                 }
318             }
319             catch (SystemException e)
320             {
321                 throw new NameServiceException(e);
322             }
323             catch (InvalidName e)
324             {
325                 throw new NameServiceException(e);
326             }
327             catch (NotFound e)
328             {
329                 throw new NameServiceException(e);
330             }
331             catch (CannotProceed e)
332             {
333                 throw new NameServiceException(e);
334             }
335         }
336     }
337
338
339     public void bind(String JavaDoc name, org.omg.CORBA.Object JavaDoc obj) throws NameServiceException
340     {
341         bind(stringToName(name), obj, false);
342     }
343
344
345     public void bind(NameComponent[] name, org.omg.CORBA.Object JavaDoc obj) throws NameServiceException
346     {
347         bind(name, obj, false);
348     }
349
350
351     public void rebind(String JavaDoc name, org.omg.CORBA.Object JavaDoc obj) throws NameServiceException
352     {
353         bind(stringToName(name), obj, true);
354     }
355
356
357     public void rebind(NameComponent[] name, org.omg.CORBA.Object JavaDoc obj) throws NameServiceException
358     {
359         bind(name, obj, true);
360     }
361
362
363     public void unbind(String JavaDoc name) throws NameServiceException
364     {
365         unbind(stringToName(name));
366     }
367
368
369     public void unbind(NameComponent[] name) throws NameServiceException
370     {
371         try
372         {
373             nameServiceRef.unbind(name);
374         }
375         catch (SystemException e)
376         {
377             throw new NameServiceException(e);
378         }
379         catch (InvalidName e)
380         {
381             throw new NameServiceException(e);
382         }
383         catch (NotFound e)
384         {
385             throw new NameServiceException(e);
386         }
387         catch (CannotProceed e)
388         {
389             throw new NameServiceException(e);
390         }
391     }
392
393
394     public void destroy(String JavaDoc name) throws NameServiceException
395     {
396         destroy(stringToName(name));
397     }
398
399
400     public void destroy(NameComponent[] name) throws NameServiceException
401     {
402         try
403         {
404             NamingContext nc = NamingContextHelper.narrow(nameServiceRef.resolve(name));
405             unbind(name);
406             nc.destroy();
407         }
408         catch (SystemException e)
409         {
410             throw new NameServiceException(e);
411         }
412         catch (InvalidName e)
413         {
414             throw new NameServiceException(e);
415         }
416         catch (NotFound e)
417         {
418             throw new NameServiceException(e);
419         }
420         catch (CannotProceed e)
421         {
422             throw new NameServiceException(e);
423         }
424         catch (NotEmpty e)
425         {
426             throw new NameServiceException(e);
427         }
428         catch (NullPointerException JavaDoc e)
429         {
430             throw new NameServiceException(e);
431         }
432     }
433
434
435     public void deepUnbind(String JavaDoc name) throws NameServiceException
436     {
437         deepUnbind(stringToName(name));
438     }
439
440
441     public void deepUnbind(NameComponent[] name) throws NameServiceException
442     {
443         if (isContext(name))
444         {
445             NSbinding[] bindings = list(name);
446             NameComponent[] dependency = new NameComponent[1 + name.length];
447             int i;
448
449             for (i=0 ; i<name.length ; ++i)
450             {
451                 dependency[i] = name[i];
452             }
453             for (i=0 ; i<bindings.length ; ++i)
454             {
455                 dependency[name.length] = new NameComponent(bindings[i].name, bindings[i].kind);
456                 deepUnbind(dependency);
457             }
458             destroy(name);
459         }
460         else
461         {
462             unbind(name);
463         }
464     }
465
466
467     public void bind(NameComponent[] name, org.omg.CORBA.Object JavaDoc obj, boolean force)
468         throws NameServiceException
469     {
470         if (name.length > 0)
471         {
472             NameComponent[] path = new NameComponent[name.length - 1];
473             for (int i=0 ; i<path.length ; ++i)
474             {
475                 path[i] = name[i];
476             }
477             makePath(path);
478             NameComponent[] singleName = new NameComponent[1];
479             singleName[0] = name[name.length-1];
480             NamingContext context = NamingContextHelper.narrow(resolve(path));
481             try
482             {
483                 if (force)
484                 {
485                     if (isContext(obj))
486                     {
487                         context.rebind_context(singleName, NamingContextHelper.narrow(obj));
488                     }
489                     else
490                     {
491                         context.rebind(singleName, obj);
492                     }
493                 }
494                 else
495                 {
496                     if (isContext(obj))
497                     {
498                         context.bind_context(singleName, NamingContextHelper.narrow(obj));
499                     }
500                     else
501                     {
502                         context.bind(singleName, obj);
503                     }
504                 }
505             }
506             catch (SystemException e)
507             {
508                 throw new NameServiceException(e);
509             }
510             catch (InvalidName e)
511             {
512                 throw new NameServiceException(e);
513             }
514             catch (NotFound e)
515             {
516                 throw new NameServiceException(e);
517             }
518             catch (CannotProceed e)
519             {
520                 throw new NameServiceException(e);
521             }
522             catch (AlreadyBound e)
523             {
524                 throw new NameServiceException(e);
525             }
526         }
527         else
528         {
529             throw new NameServiceException("The provided name is empty.");
530         }
531     }
532
533
534     static public boolean isContext(org.omg.CORBA.Object JavaDoc obj)
535     {
536         if (obj instanceof NamingContext)
537         {
538             return true;
539         }
540         try
541         {
542             NamingContextHelper.narrow(obj);
543         }
544         catch (org.omg.CORBA.BAD_PARAM JavaDoc e)
545         {
546             return false;
547         }
548         return true;
549     }
550
551
552     public boolean isContext(String JavaDoc name) throws NameServiceException
553     {
554         return NameService.isContext(resolve(name));
555     }
556
557
558     public boolean isContext(NameComponent[] name) throws NameServiceException
559     {
560         return NameService.isContext(resolve(name));
561     }
562
563
564     static public String JavaDoc nameToString(NameComponent[] name)
565     {
566         String JavaDoc strName = "";
567
568         for (int i=0 ; i<name.length ; ++i)
569         {
570             strName = strName + name[i].id;
571             if (name[i].kind.length() != 0)
572             {
573                 strName = strName + "!" + name[i].kind;
574             }
575             strName = strName + "/";
576         }
577         return strName;
578     }
579
580
581     static public NameComponent[] stringToName(String JavaDoc strName)
582     {
583         StringTokenizer parser = new StringTokenizer(strName, "/", false);
584         NameComponent[] name = new NameComponent[parser.countTokens()];
585         StringTokenizer subParser;
586         String JavaDoc token;
587
588         for (int i=0 ; i<name.length ; ++i)
589         {
590             subParser = new StringTokenizer(parser.nextToken(), "!", true);
591             name[i] = new NameComponent();
592             if (subParser.hasMoreTokens())
593             {
594                 token = subParser.nextToken();
595                 if (token.equals("!"))
596                 {
597                     name[i].id = "";
598                 }
599                 else
600                 {
601                     name[i].id = token;
602                 }
603                 if (subParser.hasMoreTokens())
604                 {
605                     token = subParser.nextToken();
606                     if (token.equals("!"))
607                     {
608                         if (subParser.hasMoreTokens())
609                         {
610                             name[i].kind = subParser.nextToken();
611                         }
612                         else
613                         {
614                             name[i].kind = "";
615                         }
616                     }
617                     else
618                     {
619                         name[i].kind = token;
620                     }
621                 }
622                 else
623                 {
624                     name[i].kind = "";
625                 }
626             }
627             else
628             {
629                 name[i].id = "";
630                 name[i].kind = "";
631             }
632         }
633         return name;
634     }
635 }
636
Popular Tags