Salome HOME
a7768ec2adec35536ae5fc64ed61fbd10b4fcdb0
[modules/kernel.git] / src / NamingService / SALOME_NamingService.cxx
1 //  SALOME NamingService : wrapping NamingService services
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.1 of the License.
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 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : SALOME_NamingService.cxx
25 //  Author : Estelle Deville
26 //  Module : SALOME
27 //  $Header$
28
29 #include "SALOME_NamingService.hxx"
30 #include "ServiceUnreachable.hxx"
31
32 #include "utilities.h"
33
34 #include <iostream>
35 #include <cstring>
36 #include <string>
37
38 using namespace std;
39
40 // ============================================================================
41 /*! \brief Default Constructor without ORB reference.
42  *
43  *  After Default Constructor, one needs to initialize ORB.
44  *  \sa init_orb(CORBA::ORB_ptr orb), SALOME_NamingService(CORBA::ORB_ptr orb)
45  */ 
46 // ============================================================================
47
48 SALOME_NamingService::SALOME_NamingService()
49 {
50   MESSAGE("SALOME_NamingService default constructor");
51   _orb = CORBA::ORB::_nil();
52   _root_context = CosNaming::NamingContext::_nil();
53 }
54
55 // ============================================================================
56 /*! \brief Standard Constructor, with ORB reference. 
57  * 
58  * Initializes the naming service root context
59  * \param orb CORBA::ORB_ptr arguments
60  */ 
61 // ============================================================================
62
63 SALOME_NamingService::SALOME_NamingService(CORBA::ORB_ptr orb)
64 {
65   MESSAGE("SALOME_NamingService creation");
66   _orb = orb ;
67   _initialize_root_context();
68 }
69
70 // ============================================================================
71 /*! \brief Standard destructor.
72  *
73  *  The standard destructor does nothing special.
74  */ 
75 // ============================================================================
76
77 SALOME_NamingService::~SALOME_NamingService()
78 {
79   // Problem MESSAGE with singleton: late destruction,
80   // after trace system destruction ?
81   //MESSAGE("SALOME_NamingService destruction");
82 }
83
84 // ============================================================================
85 /*! \brief initializes ORB reference and naming service root context.
86  * 
87  *  Initializes ORB reference and naming service root context.
88  *  For use after default constructor.
89  *  \param orb CORBA::ORB_ptr arguments
90  */ 
91 // ============================================================================
92
93 void SALOME_NamingService::init_orb(CORBA::ORB_ptr orb)
94 {
95   MESSAGE("SALOME_NamingService initialisation");
96
97   Utils_Locker lock (&_myMutex);
98   _orb = orb;
99
100   _initialize_root_context();
101 }
102
103 // ============================================================================
104 /*! \brief Registers a CORBA object reference under a path.
105  *
106  * Registers a CORBA object reference under a path. If the path ends with '/',
107  * only a directory is created.
108  * If the NamingService is out, the exception ServiceUnreachable is thrown.
109  * \param ObjRef CORBA object reference to associate to the path. To create
110  *               only a directory, give nil pointer.
111  * \param Path   A relative or absolute pathname to store the object reference.
112  *               If the pathname begins with a '/', pathname is taken
113  *               as an absolute pathname. Else, pathname is taken as a relative
114  *               path, to current context. Prefer absolute pathname, relative
115  *               pathname are not safe, when SALOME_NamingService object is
116  *               shared or use in multithreaded context. 
117  *               If the path ends with '/', only a directory is created.
118  * \sa           Change_Directory(const char* Path),
119  *               Create_Directory(const char* Path)
120  *               CORBA::Object_ptr Resolve(const char* Path)
121  */ 
122 // ============================================================================
123
124 void SALOME_NamingService::Register(CORBA::Object_ptr ObjRef,
125                                     const char* Path)
126   throw(ServiceUnreachable)
127 {
128   MESSAGE("BEGIN OF Register: " << Path);
129
130   Utils_Locker lock (&_myMutex);
131
132   // --- _current_context is replaced to the _root_context
133   //     if the Path begins whith '/'
134
135   if (Path[0] == '/')
136     {
137       _current_context = _root_context;
138     }
139
140   // --- the resolution of the directory path has to be done
141   //      to place the current_context to the correct node
142
143   CosNaming::Name context_name;
144   vector<string> splitPath;
145   int dimension_resultat = _createContextNameDir(Path,
146                                                  context_name,
147                                                  splitPath,
148                                                  true);
149
150   CORBA::Boolean not_exist = false;
151
152   if (dimension_resultat > 0)
153     {
154       // A directory is treated (not only an object name)
155       // test if the directory where ObjRef should be recorded already exists
156       // If not, create the new context
157
158       try
159         {
160           CORBA::Object_var obj = _current_context->resolve(context_name);
161           _current_context = CosNaming::NamingContext::_narrow(obj);
162         }
163
164       catch (CosNaming::NamingContext::NotFound &)
165         {
166           // --- failed to resolve, therefore assume cold start
167           not_exist = true;
168         }
169
170       catch (CosNaming::NamingContext::InvalidName &)
171         {
172           INFOS("Register() : CosNaming::NamingContext::InvalidName");
173         }
174
175       catch (CosNaming::NamingContext::CannotProceed &)
176         {
177           INFOS("Register() : CosNaming::NamingContext::CannotProceed");
178         }
179
180       catch (CORBA::SystemException&)
181         {
182           INFOS("Register() : CORBA::SystemException: "
183                 << "unable to contact the naming service");
184           throw ServiceUnreachable();
185         }
186
187       if (not_exist)
188         {
189           try
190             {
191               context_name.length(1);
192               for (int i = 0 ; i < dimension_resultat ;i++)
193                 {
194                   context_name[0].id =
195                     CORBA::string_dup(splitPath[i].c_str());
196                   context_name[0].kind = CORBA::string_dup("dir");
197                   // SCRUTE(_context_name[0].id);
198                   // --- check if the path is created
199                   try
200                     {
201                       // --- if the context is already created, nothing to do
202                       CORBA::Object_var obj =
203                         _current_context->resolve(context_name);
204                       _current_context =
205                         CosNaming::NamingContext::_narrow(obj);
206                     }
207
208                   catch (CosNaming::NamingContext::NotFound &)
209                     {
210                       // --- the context must be created
211                       CosNaming::NamingContext_var temp_context =
212                         _current_context->bind_new_context(context_name);
213                       _current_context = temp_context;
214                     }
215                 }
216             }
217
218           catch (CosNaming::NamingContext::AlreadyBound&)
219             {
220               INFOS("Register() : CosNaming::NamingContext::AlreadyBound");
221             }
222
223           catch (CosNaming::NamingContext::NotFound& ex)
224             {
225               CosNaming::Name n = ex.rest_of_name;
226
227               if (ex.why == CosNaming::NamingContext::missing_node)
228                 INFOS("Register() : " << (char *) n[0].id
229                       << " (" << (char *) n[0].kind << ") not found");
230
231               if (ex.why == CosNaming::NamingContext::not_context)
232                 INFOS("Register() : " << (char *) n[0].id
233                       << " (" << (char *) n[0].kind
234                       << ") is not a context");
235
236               if (ex.why == CosNaming::NamingContext::not_object)
237                 INFOS("Register() : " << (char *) n[0].id
238                       << " (" << (char *) n[0].kind
239                       << ") is not an object");
240             }
241
242           catch (CosNaming::NamingContext::CannotProceed&)
243             {
244               INFOS("Register(): CosNaming::NamingContext::CannotProceed");
245             }
246
247           catch (CosNaming::NamingContext::InvalidName&)
248             {
249               INFOS("Register(): CosNaming::NamingContext::InvalidName");
250             }
251
252           catch (CORBA::SystemException&)
253             {
254               INFOS("Register():CORBA::SystemException: "
255                     << "unable to contact the naming service");
256               throw ServiceUnreachable();
257             }
258         }
259     }
260
261   // --- The current directory is now the directory where the object should
262   //     be recorded
263
264   int sizePath = splitPath.size();
265   if (sizePath > dimension_resultat)
266     {
267       ASSERT(sizePath == dimension_resultat+1);
268       context_name.length(1);
269
270       try
271         {
272           // --- the last element is an object and not a directory
273
274           context_name[0].id =
275             CORBA::string_dup(splitPath[dimension_resultat].c_str());
276           context_name[0].kind = CORBA::string_dup("object");
277           //SCRUTE(context_name[0].id);
278
279           _current_context->bind(context_name, ObjRef);
280         }
281
282       catch (CosNaming::NamingContext::NotFound& ex)
283         {
284           CosNaming::Name n = ex.rest_of_name;
285
286           if (ex.why == CosNaming::NamingContext::missing_node)
287             INFOS("Register() : " << (char *) n[0].id
288                   << " (" << (char *) n[0].kind << ") not found");
289
290           if (ex.why == CosNaming::NamingContext::not_context)
291             INFOS("Register() : " << (char *) n[0].id
292                   << " (" << (char *) n[0].kind
293                   << ") is not a context");
294
295           if (ex.why == CosNaming::NamingContext::not_object)
296             INFOS("Register() : " << (char *) n[0].id
297                   << " (" << (char *) n[0].kind
298                   << ") is not an object");
299         }
300
301       catch (CosNaming::NamingContext::CannotProceed&)
302         {
303           INFOS("Register(): CosNaming::NamingContext::CannotProceed");
304         }
305
306       catch (CosNaming::NamingContext::InvalidName&)
307         {
308           INFOS("Register(): CosNaming::NamingContext::InvalidName");
309         }
310
311       catch (CosNaming::NamingContext::AlreadyBound&)
312         {
313           INFOS("Register(): CosNaming::NamingContext::AlreadyBound, "
314                 << "object will be rebind");
315           _current_context->rebind(context_name, ObjRef);
316         }
317
318       catch (CORBA::SystemException&)
319         {
320           INFOS("!!!Register(): CORBA::SystemException: "
321                 << "unable to contact the naming service");
322           throw ServiceUnreachable();
323         }
324     }
325 }
326
327 // ============================================================================
328 /*! \brief get the CORBA object reference associated to a name.
329  * 
330  *  get the CORBA object reference associated to a complete name with a path.
331  *  If the NamingService is out, the exception ServiceUnreachable is thrown 
332  * \param Path pathname. If the pathname begins with a '/', pathname is taken
333  *             as an absolute pathname. Else, pathname is taken as a relative
334  *             path, to current context. Prefer absolute pathname, relative
335  *             pathname are not safe, when SALOME_NamingService object is
336  *             shared or use in multithreaded context.
337  * \return the object reference if it exists under the pathname,
338  *             or nil reference in other cases.
339  * \sa         Register(CORBA::Object_ptr ObjRef, const char* Path),
340  *             Change_Directory(const char* Path)
341  */ 
342 // ============================================================================
343
344 CORBA::Object_ptr SALOME_NamingService::Resolve(const char* Path)
345   throw(ServiceUnreachable)
346 {
347 //   MESSAGE("BEGIN OF Resolve: " << Path);
348
349   Utils_Locker lock (&_myMutex);
350
351   // --- _current_context is replaced to the _root_context
352   //     if the Path begins whith '/'
353
354   if (Path[0] == '/')
355     {
356       _current_context = _root_context;
357     }
358
359   // --- the resolution of the directory path has to be done
360   //     to place the current_context to the correct node
361
362   CosNaming::Name context_name;
363   vector<string> splitPath;
364   int dimension_resultat = _createContextNameDir(Path,
365                                                  context_name,
366                                                  splitPath,
367                                                  false);
368
369   ASSERT(!CORBA::is_nil(_current_context));
370
371   CORBA::Object_ptr obj =  CORBA::Object::_nil();
372
373   try
374     {
375       obj = _current_context->resolve(context_name);
376     }
377
378   catch (CosNaming::NamingContext::NotFound& ex)
379     {
380       CosNaming::Name n = ex.rest_of_name;
381
382       if (ex.why == CosNaming::NamingContext::missing_node)
383         INFOS("Resolve() : " << (char *) n[0].id
384               << " (" << (char *) n[0].kind << ") not found");
385
386       if (ex.why == CosNaming::NamingContext::not_context)
387         INFOS("Resolve() : "
388               << (char *) n[0].id << " (" << (char *) n[0].kind
389               << ") is not a context");
390
391       if (ex.why == CosNaming::NamingContext::not_object)
392         INFOS("Resolve() : " << (char *) n[0].id
393               << " (" << (char *) n[0].kind
394               << ") is not an object");
395     }
396
397   catch (CosNaming::NamingContext::CannotProceed&)
398     {
399       INFOS("Resolve(): CosNaming::NamingContext::CannotProceed");
400     }
401
402   catch (CosNaming::NamingContext::InvalidName&)
403     {
404       INFOS("Resolve(): CosNaming::NamingContext::InvalidName");
405     }
406
407   catch (CORBA::SystemException&)
408     {
409       INFOS("Resolve():CORBA::SystemException : unable to contact"
410             << "the naming service");
411       throw ServiceUnreachable();
412     }
413
414   return obj;
415 }
416
417 // ============================================================================
418 /*! \brief get the CORBA object reference associated to an uncomplete name.
419  *
420  *  get the CORBA object reference associated to an uncomplete name with a
421  *  path. Look for the first occurence of name*.
422  *  If the NamingService is out, the exception ServiceUnreachable is thrown 
423  * \param Path pathname under the form "/path/name" (Absolute reference !)
424  *             search the fist reference like "/path(.dir)/name*(.kind)"
425  * \return     the object reference if found, or nil reference.
426  * \sa         Resolve(const char* Path)
427  */
428 // ============================================================================
429
430 CORBA::Object_ptr SALOME_NamingService::ResolveFirst(const char* Path)
431   throw(ServiceUnreachable)
432 {
433 //   MESSAGE("ResolveFirst");
434
435   Utils_Locker lock (&_myMutex);
436 //   SCRUTE(Path);
437
438   string thePath = Path;
439   string basePath = "";
440   string name = thePath;
441
442   string::size_type idx = thePath.rfind('/');
443
444   if (idx != string::npos) // at least one '/' found
445     {
446       basePath = thePath.substr(0, idx);
447       name = thePath.substr(idx + 1);
448 //       SCRUTE(basePath);
449     }
450
451 //   SCRUTE(name);
452   CORBA::Object_ptr obj = CORBA::Object::_nil();
453
454   bool isOk = false;
455   if (basePath.empty())
456     isOk =true;
457   else
458     isOk = Change_Directory(basePath.c_str());
459   
460   if (isOk)
461     {
462       vector<string> listElem = list_directory();
463       vector<string>::iterator its = listElem.begin();
464       
465       while (its != listElem.end())
466         {
467           MESSAGE(*its);
468           
469           if ((*its).find(name) == 0)
470             {
471               return Resolve((*its).c_str());
472             }
473           
474           its++;
475         }
476     }
477
478   return obj;
479 }
480
481 // ============================================================================
482 /*! \brief find a component instance from hostname, containername,
483  *         componentName and number of processors.
484  *
485  *  find a component instance from hostname, containername, componentName and
486  *  number of processors.
487  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
488  * \param hostname      name of the machine on which the component is searched.
489  * \param containername name of the container in which the component is
490                         instanciated.
491  * \param componentname name of the component we are looking for an existing 
492                         instance.
493  * \param nbproc        in case of multi processor machine, container name is
494  *                      suffixed with _nbproc.
495  * \return the object reference
496  */ 
497 // ============================================================================
498
499 CORBA::Object_ptr
500 SALOME_NamingService::ResolveComponent(const char* hostname,
501                                        const char* containerName,
502                                        const char* componentName,
503                                        const int nbproc)
504   throw(ServiceUnreachable)
505 {
506 //   MESSAGE("ResolveComponent");
507
508   Utils_Locker lock (&_myMutex);
509
510   string name = "/Containers/";
511
512   name += hostname;
513
514   if ( strlen(containerName) != 0 )
515     {
516       name += "/";
517
518       if ( nbproc >= 1 )
519         {
520           char *newContainerName = new char[strlen(containerName) + 8];
521           sprintf(newContainerName, "%s_%d", containerName, nbproc);
522           name += newContainerName;
523           delete [] newContainerName;
524         }
525
526       else
527         name += containerName;
528
529       name += "/";
530
531       name += componentName;
532
533       return ResolveFirst(name.c_str());
534     }
535
536   else
537     {
538       SCRUTE(name);
539       if (Change_Directory(name.c_str()))
540         {
541           vector<string> contList = list_subdirs();
542
543           for (unsigned int ind = 0; ind < contList.size(); ind++)
544             {
545               name = contList[ind].c_str();
546
547               if ( nbproc >= 1 )
548                 {
549                   char *str_nbproc = new char[8];
550                   sprintf(str_nbproc, "_%d", nbproc);
551                   if( strstr(name.c_str(),str_nbproc) == NULL)
552                     continue; // check only containers with _%d in name
553                   delete [] str_nbproc;
554                 }
555
556               name += "/";
557               name += componentName;
558               SCRUTE(name);
559               CORBA::Object_ptr obj = ResolveFirst(name.c_str());
560
561               if ( !CORBA::is_nil(obj) )
562                 return obj;
563             }
564         }
565
566       return CORBA::Object::_nil();
567     }
568 }
569
570 // ============================================================================
571 /*! \brief provide a default container name if empty.
572  *
573  *  the given container name is returned unchanged, unless it is empty.
574  * \param  containerName
575  * \return container name, where empty input is replaced by "FactoryServer",
576  *         without the path.
577  * \sa BuildContainerNameForNS(const char *containerName, const char *hostname)
578  */
579 // ============================================================================
580
581 string SALOME_NamingService::ContainerName(const char *containerName)
582 {
583   string ret;
584
585   if (strlen(containerName) == 0)
586     ret = "FactoryServer";
587   else
588     ret = containerName;
589
590   return ret;
591 }
592
593 // ============================================================================
594 /*! \brief build a container name, given a MachineParameters struct.
595  *
596  *  Build a container name with a MachineParameters struct. In case of multi
597  *  processor machine, container name is suffixed with _nbproc. nproc equals
598  *  (number of nodes)*(number of processor per nodes).
599  * \param params struct from which we get container name (may be
600  *               empty),  number of nodes and number of processor
601  *               per node.
602  * \return a container name without the path.
603  * \sa BuildContainerNameForNS(const Engines::MachineParameters& params,
604  *                             const char *hostname)
605  */
606 // ============================================================================
607
608 string 
609 SALOME_NamingService::ContainerName(const Engines::MachineParameters& params)
610 {
611   int nbproc;
612
613   if ( !params.isMPI )
614     nbproc = 0;
615   else if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
616     nbproc = 1;
617   else if ( params.nb_node == 0 )
618     nbproc = params.nb_proc_per_node;
619   else if ( params.nb_proc_per_node == 0 )
620     nbproc = params.nb_node;
621   else
622     nbproc = params.nb_node * params.nb_proc_per_node;
623
624   string ret = ContainerName(params.container_name);
625
626   if ( nbproc >= 1 )
627     {
628       char *suffix = new char[8];
629       sprintf(suffix, "_%d", nbproc);
630       ret += suffix;
631     }
632
633   return ret;
634 }
635
636 // ============================================================================
637 /*! \brief build a string representing a container in Naming Service.
638  *
639  *  Build a string representing the absolute pathname of a container in
640  *  SALOME_NamingService. This form gives a suffixed containerName in case of
641  *  multi processor machine.
642  * \param params struct from which we get container name (may be
643  *               empty),  number of nodes and number of processor
644  *               per node.
645  * \param hostname name of the host of the container, without domain names.
646  * \return the path under the form /Containers/hostname/containerName
647  * \sa ContainerName(const Engines::MachineParameters& params)
648  */
649 // ============================================================================
650
651 string SALOME_NamingService::BuildContainerNameForNS(const char *containerName,
652                                                      const char *hostname)
653 {
654   string ret = "/Containers/";
655   ret += hostname;
656   ret += "/";
657   ret += ContainerName(containerName);
658
659   return ret;
660 }
661
662 // ============================================================================
663 /*! \brief build a string representing a container in Naming Service.
664  *
665  *  Build a string representing the absolute pathname of a container in
666  *  SALOME_NamingService.
667  * \param params used as it is, or replaced by FactoryServer if empty.
668  * \param hostname name of the host of the container, without domain names.
669  * \return the path under the form /Containers/hostname/containerName
670  * \sa ContainerName(const char *containerName)
671  */
672 // ============================================================================
673
674 string
675 SALOME_NamingService::
676 BuildContainerNameForNS(const Engines::MachineParameters& params,
677                         const char *hostname)
678 {
679   string ret = "/Containers/";
680   ret += hostname;
681   ret += "/";
682   ret += ContainerName(params);
683
684   return ret;
685 }
686
687 // ============================================================================
688 /*! \brief search a name in current directory.
689  *
690  *  Search a name in the current directory. after call, the current directory
691  *  is changed to the directory containing the last occurence of name found.
692  *  If no occurence found (see return value), current directory remains
693  *  unchanged.
694  *
695  * \param  name the name to search.
696  * \return number of occurences found.
697  * \sa Change_Directory(const char* Path)
698  */ 
699 // ============================================================================
700
701 int SALOME_NamingService::Find(const char* name)
702 throw(ServiceUnreachable)
703 {
704   MESSAGE("BEGIN OF Find " << name);
705
706   Utils_Locker lock (&_myMutex);
707
708   CORBA::Long occurence_number = 0 ;
709
710   try
711     {
712       _Find(name, occurence_number);
713     }
714
715   catch (CORBA::SystemException&)
716     {
717       INFOS("!!!Find() : CORBA::SystemException : unable to contact"
718             << " the naming service");
719       throw ServiceUnreachable();
720     }
721
722   return occurence_number;
723 }
724
725 // ============================================================================
726 /*! \brief Creates a directory (context_name)
727  *
728  *  Creates a directory (context_name) relative to the current directory 
729  *  (current context) or relative to the root directory (root context), if
730  *  the path given begins with a '/'.
731  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
732  * \param Path   A relative or absolute pathname to store the object reference.
733  *               If the pathname begins with a '/', pathname is taken
734  *               as an absolute pathname. Else, pathname is taken as a relative
735  *               path, to current context. Prefer absolute pathname, relative
736  *               pathname are not safe, when SALOME_NamingService object is
737  *               shared or use in multithreaded context.   
738  *  \return true if successfull
739  *          (creation not strictly garanteed if true, because Register may
740  *           catch some specific unlikely exception without throw anything
741  *           --- to be corrected ---)
742  *  \sa RegisterCORBA::Object_ptr ObjRef, const char* Path)
743  */ 
744 // ============================================================================
745
746 bool SALOME_NamingService::Create_Directory(const char* Path)
747 throw(ServiceUnreachable)
748 {
749   MESSAGE("BEGIN OF Create_Directory");
750
751   Utils_Locker lock (&_myMutex);
752
753   string path(Path);
754
755   // --- if path empty, nothing to create, no context change
756
757   if (path.empty())
758     return false;
759
760   // --- if path ='/', nothing to create, only change to root_context
761
762   if (path == "/")
763     {
764       MESSAGE("Create Directory '/', just change to root_context");
765       _current_context = _root_context;
766       return true;
767     }
768
769   // --- path must end with '/'
770   
771   if (path[path.length()-1] != '/') path += '/';
772
773   Register(CORBA::Object::_nil(), path.c_str());
774   return true;
775 }
776
777 // ============================================================================
778 /*! \brief change current directory to the given path
779  *
780  *  change the current directory to the given path in parameter.
781  *  Warning: avoid use when the SALOME_NamingService instance is shared by
782  *  several threads (current context may be modified by another thread).
783  *  If the path is empty, nothing done return OK.
784  *  If Path ="/", the current directory changes to the root directory.
785  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
786  * \param  Path the new current directory
787  * \return true if the change succeeded
788  */ 
789 // ============================================================================
790
791 bool SALOME_NamingService::Change_Directory(const char* Path)
792 throw(ServiceUnreachable)
793 {
794 //   MESSAGE("BEGIN OF Change_Directory " << Path);
795   Utils_Locker lock (&_myMutex);
796
797   string path(Path);
798
799   // --- if path empty, nothing to do
800
801   if (path.empty())
802     return true;
803
804   // --- if path ='/', nothing to resolve, only change to root_context
805
806   if (path == "/")
807     {
808 //       MESSAGE("Change_Directory is called to go to the root_context");
809       _current_context = _root_context;
810       return true;
811     }
812
813   CosNaming::NamingContext_var current_context = _current_context;
814   bool changeOK = false;
815
816   // --- replace _current_context with _root_context if Path begins whith '/'
817
818   if (path[0] == '/')
819     current_context = _root_context;
820
821   // --- need to resolve directory path
822
823   ASSERT(!CORBA::is_nil(current_context));
824   
825   if (path[path.length()-1] != '/') path += '/';
826 //   SCRUTE(path);
827   CosNaming::Name context_name;
828   vector<string> splitPath;
829   int dimension_resultat = _createContextNameDir(path.c_str(),
830                                                  context_name,
831                                                  splitPath,
832                                                  true);
833   
834   // --- Context creation
835   
836   try
837     {
838       CORBA::Object_var obj = current_context->resolve(context_name);
839       current_context = CosNaming::NamingContext::_narrow(obj);
840       ASSERT(!CORBA::is_nil(current_context));
841       _current_context = current_context;
842       changeOK = true;
843     }
844   
845   catch (CosNaming::NamingContext::NotFound& ex)
846     {
847       CosNaming::Name n = ex.rest_of_name;
848       
849       if (ex.why == CosNaming::NamingContext::missing_node)
850         INFOS( "Change_Directory() : " << (char *) n[0].id
851                << " (" << (char *) n[0].kind << ") not found");
852       if (ex.why == CosNaming::NamingContext::not_context)
853         INFOS("Change_Directory() : " << (char *) n[0].id
854               << " (" << (char *) n[0].kind
855                   << ") is not a context" );
856       if (ex.why == CosNaming::NamingContext::not_object)
857         INFOS( "Change_Directory() : " << (char *) n[0].id
858                << " (" << (char *) n[0].kind
859                << ") is not an object" );
860     }
861   
862   catch (CosNaming::NamingContext::CannotProceed&)
863     {
864       INFOS("Change_Directory(): CosNaming::NamingContext::CannotProceed");
865     }
866   
867   catch (CosNaming::NamingContext::InvalidName&)
868     {
869       INFOS("Change_Directory(): CosNaming::NamingContext::InvalidName");
870     }
871   
872   catch (CORBA::SystemException&)
873     {
874       INFOS("Change_Directory():CORBA::SystemException : unable to contact"
875             << "the naming service");
876       throw ServiceUnreachable();
877     }
878
879   return changeOK;
880 }
881
882 // ============================================================================
883 /*! \brief get the current directory path
884  *
885  *  Get the current directory path.
886  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
887  * \return the path of the current_context
888  * \sa  _current_directory
889  */ 
890 // ============================================================================
891
892 char* SALOME_NamingService::Current_Directory()
893 throw(ServiceUnreachable)
894 {
895   MESSAGE("BEGIN OF Current_Directory");
896
897   Utils_Locker lock (&_myMutex);
898
899   CosNaming::NamingContext_var ref_context = _current_context;
900
901   vector<string> splitPath;
902   splitPath.resize(0);
903   int lengthPath = 0;
904   bool notFound = true ;
905
906   // --- start search from root context
907
908   _current_context = _root_context ;
909
910   try
911     {
912       _current_directory(splitPath, lengthPath, ref_context, notFound );
913     }
914
915   catch (CORBA::SystemException&)
916     {
917       INFOS("Current_Directory(): CORBA::SystemException: unable to contact"
918             << " the naming service" )
919       throw ServiceUnreachable();
920     }
921
922   string path;
923   lengthPath = splitPath.size();
924   for (int k = 0 ; k < lengthPath ;k++)
925     {
926       path += "/";
927       path += splitPath[k];
928     }
929
930   SCRUTE(path)
931   _current_context = ref_context ;
932
933   return strdup(path.c_str());
934 }
935
936 // ============================================================================
937 /*! \brief list recursively all objects in the current context
938  *
939  *  List and print via trace all directories and objects in the current
940  *  context. Trace must be activated: compile option _DEBUG_
941  *  If the NamingService is out, the exception ServiceUnreachable is thrown
942  */ 
943 // ============================================================================
944
945 void SALOME_NamingService::list()
946 throw(ServiceUnreachable)
947 {
948   MESSAGE("Begin of list");
949
950   Utils_Locker lock (&_myMutex)
951
952     ;
953   CosNaming::BindingList_var binding_list;
954   CosNaming::BindingIterator_var binding_iterator;
955   CosNaming::Binding_var binding ;
956
957   unsigned long nb = 0 ; // --- only for the BindingIterator use,
958                          //     to access the bindings
959
960   CosNaming::NamingContext_var ref_context = _current_context;
961
962   _current_context->list(nb, binding_list, binding_iterator) ;
963
964   if (! CORBA::is_nil(binding_iterator))
965     {
966       while (binding_iterator->next_one(binding))
967         {
968           CosNaming::Name bindingName = binding->binding_name;
969
970           if (binding->binding_type == CosNaming::ncontext)
971             {
972               MESSAGE( "Context : " << bindingName[0].id );
973
974               try
975                 {
976                   Change_Directory(bindingName[0].id);
977                 }
978
979               catch (ServiceUnreachable&)
980                 {
981                   INFOS( "list(): ServiceUnreachable" )
982                     throw ServiceUnreachable();
983                 }
984
985               list();
986               _current_context = ref_context ;
987             }
988
989           else if (binding->binding_type == CosNaming::nobject)
990             {
991               MESSAGE( "Object : " << bindingName[0].id );
992             }
993         }
994
995       binding_iterator->destroy();
996     }
997 }
998
999 // ============================================================================
1000 /*! \brief list all the objects in the current directory.
1001  *
1002  *  get a list of all the objects in the current directory, without recursion
1003  *  on the subdirectories. Only the objects are listed, not the directories.
1004  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
1005  * \return list of strings with objects found.
1006  * \sa vector<string> list_directory_recurs()
1007  */ 
1008 // ============================================================================
1009
1010 vector<string> SALOME_NamingService::list_directory()
1011 throw(ServiceUnreachable)
1012 {
1013 //   MESSAGE("list_directory");
1014   vector<string> dirList ;
1015   dirList.resize(0);
1016
1017   CosNaming::BindingList_var binding_list;
1018   CosNaming::BindingIterator_var binding_iterator;
1019   CosNaming::Binding_var binding ;
1020
1021   unsigned long nb = 0 ; // --- only for the BindingIterator use,
1022                          //     to access the bindings
1023
1024   CosNaming::NamingContext_var ref_context = _current_context;
1025
1026   _current_context->list(nb, binding_list, binding_iterator);
1027
1028   if (binding_iterator->_is_nil())
1029     return dirList;
1030
1031   while (binding_iterator->next_one(binding))
1032     {
1033       CosNaming::Name bindingName = binding->binding_name;
1034
1035       if (binding->binding_type == CosNaming::nobject)
1036         {
1037           dirList.push_back(CORBA::string_dup(bindingName[0].id));
1038         }
1039     }
1040
1041 //   for (unsigned int ind = 0; ind < dirList.size(); ind++)
1042 //     MESSAGE("list_directory : Object : " << dirList[ind]);
1043
1044   binding_iterator->destroy();
1045
1046   return dirList;
1047 }
1048
1049
1050 // ============================================================================
1051 /*! \brief list all the subdirectories in the current directory.
1052  *
1053  *  get a list of all the subdirectories in the current directory,
1054  *  without recursion on the subdirectories.
1055  *  Only the subdirectories are listed, not the objects.
1056  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
1057  * \return list of strings with directories found.
1058  * \sa vector<string> list_directory()
1059  */ 
1060 // ============================================================================
1061
1062 vector<string> SALOME_NamingService::list_subdirs()
1063 throw(ServiceUnreachable)
1064 {
1065   MESSAGE("list_subdirs");
1066   vector<string> dirList ;
1067   dirList.resize(0);
1068
1069   CosNaming::BindingList_var binding_list;
1070   CosNaming::BindingIterator_var binding_iterator;
1071   CosNaming::Binding_var binding ;
1072
1073   unsigned long nb = 0 ; // --- only for the BindingIterator use,
1074                          //     to access the bindings
1075
1076   CosNaming::NamingContext_var ref_context = _current_context;
1077
1078   _current_context->list(nb, binding_list, binding_iterator) ;
1079
1080   if (binding_iterator->_is_nil())
1081     return dirList;
1082
1083   while (binding_iterator->next_one(binding))
1084     {
1085       CosNaming::Name bindingName = binding->binding_name;
1086
1087       if (binding->binding_type == CosNaming::ncontext)
1088         {
1089           dirList.push_back(CORBA::string_dup(bindingName[0].id));
1090         }
1091     }
1092
1093   for (unsigned int ind = 0; ind < dirList.size(); ind++)
1094     MESSAGE("list_directory : Object : " << dirList[ind]);
1095
1096   binding_iterator->destroy();
1097
1098   return dirList;
1099 }
1100
1101 // ============================================================================
1102 /*! \brief  list all the objects in the current directory and subdirectories.
1103  *
1104  *  get a list of all the objects in the current directory, with recursion
1105  *  on the subdirectories. Only the objects are listed, not the directories.
1106  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
1107  * \return list of strings with objects found.
1108  * \sa vector<string> list_directory()
1109  */ 
1110 // ============================================================================
1111
1112 vector<string> SALOME_NamingService::list_directory_recurs()
1113 throw(ServiceUnreachable)
1114 {
1115   MESSAGE("list_directory_recurs");
1116
1117   Utils_Locker lock (&_myMutex);
1118
1119   vector<string> dirList ;
1120
1121   string currentDir = Current_Directory();
1122
1123   _list_directory_recurs(dirList, "", currentDir);
1124
1125   return dirList;
1126 }
1127
1128 // ============================================================================
1129 /*! \brief destroy an entry in naming service.
1130  *
1131  *  Destroy an association Path - Object Reference.
1132  *  If the NamingService is out, the exception ServiceUnreachable is thrown 
1133  * \param Path object path
1134  */ 
1135 // ============================================================================
1136
1137 void SALOME_NamingService::Destroy_Name(const char* Path)
1138 throw(ServiceUnreachable)
1139 {
1140   MESSAGE("BEGIN OF Destroy_Name " << Path);
1141
1142   Utils_Locker lock (&_myMutex);
1143
1144   string path(Path);
1145
1146   // --- if path empty, nothing to do
1147
1148   if (path.empty())
1149     return;
1150
1151   // --- if path = '/' not applicable, nothing to do
1152
1153   if (path == "/")
1154     return;
1155
1156   // --- if path begins with '/', set current directory to root context
1157
1158   if (path[0] == '/')
1159     _current_context = _root_context;
1160
1161   // --- context of the directory containing the object
1162
1163   CosNaming::Name context_name;
1164   vector<string> splitPath;
1165   int dimension_resultat = _createContextNameDir(path.c_str(),
1166                                                  context_name,
1167                                                  splitPath,
1168                                                  true);
1169
1170   bool exist = false;
1171
1172   if (dimension_resultat > 0)
1173     {
1174       // --- path contains a directory, not only an object name
1175       //     switch to the new directory (or return if directory not found)
1176
1177       try
1178         {
1179           CORBA::Object_var obj = _current_context->resolve(context_name);
1180           _current_context = CosNaming::NamingContext::_narrow(obj);
1181           exist = true;
1182         }
1183
1184       catch (CosNaming::NamingContext::NotFound &ex)
1185         {
1186           // --- failed to resolve
1187           exist = false;
1188
1189           CosNaming::Name n = ex.rest_of_name;
1190
1191           if (ex.why == CosNaming::NamingContext::missing_node)
1192             INFOS( "Destroy_Name(): " << (char *) n[0].id
1193                    << " (" << (char *) n[0].kind << ") not found" );
1194           if (ex.why == CosNaming::NamingContext::not_context)
1195             INFOS( "Destroy_Name() : " << (char *) n[0].id
1196                    << " (" << (char *) n[0].kind
1197                    << ") is not a context" );
1198           if (ex.why == CosNaming::NamingContext::not_object)
1199             INFOS( "Destroy_Name() : " << (char *) n[0].id
1200                    << " (" << (char *) n[0].kind
1201                    << ") is not an object" );
1202         }
1203
1204       catch (CosNaming::NamingContext::InvalidName &)
1205         {
1206           INFOS("Destroy_Name: CosNaming::NamingContext::InvalidName");
1207         }
1208
1209       catch (CosNaming::NamingContext::CannotProceed &)
1210         {
1211           INFOS("Destroy_Name: CosNaming::NamingContext::CannotProceed");
1212         }
1213
1214       catch (CORBA::SystemException&)
1215         {
1216           INFOS("Destroy_Name : CORBA::SystemException: "
1217                 << "unable to contact the naming service");
1218           throw ServiceUnreachable();
1219         }
1220
1221       if (! exist) return;
1222     }
1223
1224   ASSERT(!CORBA::is_nil(_current_context));
1225
1226   // --- The current directory is now the directory where the object should
1227   //     be destroyed
1228
1229   int sizePath = splitPath.size();
1230   if (sizePath > dimension_resultat)
1231     {
1232       ASSERT(sizePath == dimension_resultat+1);
1233       context_name.length(1);
1234
1235       try
1236         {
1237           // --- the last element is an object and not a directory
1238
1239           context_name[0].id =
1240             CORBA::string_dup(splitPath[dimension_resultat].c_str());
1241           context_name[0].kind = CORBA::string_dup("object");
1242           SCRUTE(context_name[0].id);
1243  
1244           _current_context->unbind(context_name);
1245           MESSAGE("The object " << context_name[0].id << " has been deleted");
1246         }
1247
1248       catch (CosNaming::NamingContext::NotFound& ex)
1249         {
1250           CosNaming::Name n = ex.rest_of_name;
1251
1252           if (ex.why == CosNaming::NamingContext::missing_node)
1253             INFOS( "Destroy_Name() : " << (char *) n[0].id
1254                    << " (" << (char *) n[0].kind << ") not found" );
1255           if (ex.why == CosNaming::NamingContext::not_context)
1256             INFOS( "Destroy_Name() : " << (char *) n[0].id
1257                    << " (" << (char *) n[0].kind
1258                    << ") is not a context" );
1259           if (ex.why == CosNaming::NamingContext::not_object)
1260             INFOS( "Destroy_Name() : " << (char *) n[0].id
1261                    << " (" << (char *) n[0].kind
1262                    << ") is not an object" );
1263           }
1264
1265       catch (CosNaming::NamingContext::CannotProceed&)
1266         {
1267           INFOS( "Destroy_Name(): CosNaming::NamingContext::CannotProceed");
1268         }
1269
1270       catch (CosNaming::NamingContext::InvalidName&)
1271         {
1272           INFOS( "Destroy_Name(): CosNaming::NamingContext::InvalidName");
1273         }
1274
1275       catch (CORBA::SystemException&)
1276         {
1277           INFOS( "Destroy_Name(): CORBA::SystemException: unable to contact"
1278                  << " the naming service");
1279           throw ServiceUnreachable();
1280         }
1281     }
1282 }
1283
1284 // ============================================================================
1285 /*! \brief Destroy an empty directory
1286  *
1287  *  Destroy an empty directory in Naming Service.
1288  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
1289  * \param Path directory path
1290  */ 
1291 // ============================================================================
1292
1293 void SALOME_NamingService::Destroy_Directory(const char* Path)
1294 throw(ServiceUnreachable)
1295 {
1296   MESSAGE("BEGIN OF Destroy_Directory " << Path);
1297
1298   Utils_Locker lock (&_myMutex);
1299
1300   string path(Path);
1301
1302   // --- if path empty, nothing to do
1303
1304   if (path.empty())
1305     return;
1306
1307   // --- if path begins with '/', set current directory to root context
1308
1309   if (path[0] == '/')
1310     _current_context = _root_context;
1311
1312   CosNaming::NamingContext_var ref_context = _current_context;
1313
1314   // --- path must ends with '/' for a directory
1315
1316   if (path[path.size() -1] != '/')
1317     path += '/';
1318
1319   // --- context of the directory
1320
1321   CosNaming::Name context_name;
1322   vector<string> splitPath;
1323   int dimension_resultat = _createContextNameDir(path.c_str(),
1324                                                  context_name,
1325                                                  splitPath,
1326                                                  true);
1327   bool exist = false;
1328
1329   if (dimension_resultat > 0)
1330     {
1331       // --- path contains a directory, not only an object name
1332       //     switch to the new directory (or return if directory not found)
1333
1334       try
1335         {
1336           CORBA::Object_var obj = _current_context->resolve(context_name);
1337           _current_context = CosNaming::NamingContext::_narrow(obj);
1338           exist = true;
1339         }
1340
1341       catch (CosNaming::NamingContext::NotFound &ex)
1342         {
1343           // --- failed to resolve
1344           exist = false;
1345
1346           CosNaming::Name n = ex.rest_of_name;
1347
1348           if (ex.why == CosNaming::NamingContext::missing_node)
1349             INFOS( "Destroy_Directory(): " << (char *) n[0].id
1350                    << " (" << (char *) n[0].kind << ") not found" );
1351           if (ex.why == CosNaming::NamingContext::not_context)
1352             INFOS( "Destroy_Directory() : " << (char *) n[0].id
1353                    << " (" << (char *) n[0].kind
1354                    << ") is not a context" );
1355           if (ex.why == CosNaming::NamingContext::not_object)
1356             INFOS( "Destroy_Directory() : " << (char *) n[0].id
1357                    << " (" << (char *) n[0].kind
1358                    << ") is not an object" );
1359         }
1360
1361       catch (CosNaming::NamingContext::InvalidName &)
1362         {
1363           INFOS("Destroy_Directory: CosNaming::NamingContext::InvalidName");
1364         }
1365
1366       catch (CosNaming::NamingContext::CannotProceed &)
1367         {
1368           INFOS("Destroy_Directory: CosNaming::NamingContext::CannotProceed");
1369         }
1370
1371       catch (CORBA::SystemException&)
1372         {
1373           INFOS("Destroy_Directory : CORBA::SystemException: "
1374                 << "unable to contact the naming service");
1375           throw ServiceUnreachable();
1376         }
1377
1378       if (! exist) return;
1379     }
1380
1381   ASSERT(!CORBA::is_nil(_current_context));
1382
1383   // --- Context Destruction
1384
1385   bool isContextDestroyed = false;
1386   try
1387     {
1388       _current_context->destroy();
1389       MESSAGE( "The context " << path << " has been deleted" );
1390       isContextDestroyed = true;
1391     }
1392
1393   catch (CosNaming::NamingContext::NotEmpty&)
1394     {
1395       INFOS( "Destroy_Directory(): CosNaming::NamingContext::NoEmpty "
1396              << path << " is not empty" );
1397     }
1398
1399   catch (CORBA::SystemException&)
1400     {
1401       INFOS( "Destroy_Directory():CORBA::SystemException : "
1402              << "unable to contact the naming service");
1403       throw ServiceUnreachable();
1404     }
1405
1406   // --- go to the reference directory
1407
1408   _current_context = ref_context ;
1409
1410   ASSERT(!CORBA::is_nil(_current_context));
1411
1412   if (isContextDestroyed)
1413     {
1414       try
1415         {
1416           _current_context->unbind(context_name);
1417           MESSAGE( "The bind to the context "
1418                    << context_name[0].id
1419                    << " has been deleted" );
1420         }
1421
1422       catch (CosNaming::NamingContext::NotFound& ex)
1423         {
1424           CosNaming::Name n = ex.rest_of_name;
1425
1426           if (ex.why == CosNaming::NamingContext::missing_node)
1427             INFOS( "Destroy_Directory() : " << (char *) n[0].id
1428                    << " (" << (char *) n[0].kind << ") not found" );
1429           if (ex.why == CosNaming::NamingContext::not_context)
1430             INFOS( "Destroy_Directory() : " << (char *) n[0].id
1431                    << " (" << (char *) n[0].kind
1432                    << ") is not a context" );
1433           if (ex.why == CosNaming::NamingContext::not_object)
1434             INFOS( "Destroy_Directory() : " << (char *) n[0].id
1435                    << " (" << (char *) n[0].kind
1436                    << ") is not an object" );
1437         }
1438
1439       catch (CosNaming::NamingContext::CannotProceed&)
1440         {
1441           INFOS("Destroy_Directory: CosNaming::NamingContext::CannotProceed");
1442         }
1443
1444       catch (CosNaming::NamingContext::InvalidName&)
1445         {
1446           INFOS("Destroy_Directory: CosNaming::NamingContext::InvalidName");
1447             }
1448
1449       catch (CORBA::SystemException&)
1450         {
1451           INFOS("Destroy_Directory:CORBA::SystemException : unable to contact"
1452                  << " the naming service");
1453           throw ServiceUnreachable();
1454         }
1455     }
1456 }
1457
1458 // ============================================================================
1459 /*! \brief Destroy a directory with its contents.
1460  *
1461  *  Destroy the objects associations in a directory, and the directory itself,
1462  *  if there is no subdirectories. 
1463  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
1464  * \param Path the directory path.
1465  */ 
1466 // ============================================================================
1467
1468 void SALOME_NamingService::Destroy_FullDirectory(const char* Path)
1469 throw(ServiceUnreachable)
1470 {
1471   MESSAGE("begin of Destroy_FullDirectory " << Path);
1472   if( Change_Directory(Path) )
1473     {
1474       vector<string> contList = list_directory();
1475
1476       for (unsigned int ind = 0; ind < contList.size(); ind++)
1477         Destroy_Name(contList[ind].c_str());
1478       
1479       Destroy_Directory(Path);
1480     }
1481 }
1482
1483 // ============================================================================
1484 /*! \brief  initialize root context (root directory)
1485  *
1486  * the root context initialisation must be done when the SALOME_NamingService
1487  * instance is created and before any othe call. See constructors.
1488  */ 
1489 // ============================================================================
1490
1491 void SALOME_NamingService::_initialize_root_context()
1492 {
1493   //MESSAGE("Get the root context");
1494
1495   try
1496     {
1497       CORBA::Object_var obj = _orb->resolve_initial_references("NameService");
1498       _root_context = CosNaming::NamingContext::_narrow(obj);
1499       _current_context = _root_context ;
1500       ASSERT(!CORBA::is_nil(_root_context));
1501     }
1502
1503   catch (CORBA::SystemException&)
1504     {
1505       INFOS("CORBA::SystemException: unable to contact the naming service");
1506       throw ServiceUnreachable();
1507     }
1508
1509   catch (...)
1510     {
1511       INFOS("Unknown Exception: unable to contact the naming service");
1512       throw ServiceUnreachable();
1513     }
1514 }
1515
1516 // ============================================================================
1517 /*! \brief transform a string path in CosNaming structure.
1518  *
1519  *  Transform a path given as a string in a CosNaming structure.
1520  *  \param path         a relative or absolute path, with or without an object.
1521  *                      An absolute path begins with '/'.
1522  *                      A path without an object ends with '/'.
1523  *  \param context_name CosNaming structure to put the path.
1524  *  \param splitPath    a vector of string with subdirectories and final
1525  *                      object, if any.
1526  *  \param onlyDir      if true, final object (if any) is ommited
1527  *                      in context_name.
1528  *                      if false, final object (if any) is included in
1529  *                      context_name.
1530  *  \return             dimension of context_name
1531  */ 
1532 // ============================================================================
1533
1534 int
1535 SALOME_NamingService::_createContextNameDir(string path,
1536                                             CosNaming::Name& context_name,
1537                                             vector<string>& splitPath,
1538                                             bool onlyDir)
1539 {
1540   if (path.empty())
1541     return 0;
1542
1543   string::size_type begIdx, endIdx;
1544   const string delims("/");
1545   splitPath.resize(0);
1546   bool endWithDelim = false;
1547
1548   begIdx = path.find_first_not_of(delims);
1549   while (begIdx != string::npos)
1550     {
1551       endIdx = path.find_first_of(delims, begIdx);
1552       if (endIdx == path.length()-1)
1553         endWithDelim = true;
1554       if (endIdx == string::npos)
1555         endIdx = path.length();
1556       int lsub = endIdx - begIdx;
1557       if (lsub >= 1)
1558         splitPath.push_back(path.substr(begIdx, lsub));
1559       begIdx = path.find_first_not_of(delims, endIdx);
1560     }
1561
1562   int dim;
1563   if (onlyDir)                  // only directory part
1564     {
1565       dim = splitPath.size()-1; // omit final object
1566       if (endWithDelim)         // unless the path ends with a delimiter 
1567         dim++;
1568       endWithDelim = true;
1569     }
1570   else
1571     dim = splitPath.size();     // directories and final object
1572
1573   context_name.length(dim);
1574   for (int i=0; i<dim; i++)
1575     {
1576 //       SCRUTE(splitPath[i]);
1577       context_name[i].id = CORBA::string_dup(splitPath[i].c_str());
1578       if (!endWithDelim && (i == dim-1)) // here, the last string is an object
1579         {
1580           context_name[i].kind = CORBA::string_dup("object");
1581 //        MESSAGE("--- " <<splitPath[i] <<".object");
1582         }
1583       else
1584         {
1585           context_name[i].kind = CORBA::string_dup("dir");
1586 //        MESSAGE("--- " <<splitPath[i] <<".dir");
1587         }
1588     }
1589   return dim;
1590 }
1591
1592 // ============================================================================
1593 /*! \brief search a name in current directory.
1594  *
1595  *  Search a name in the current directory. after call, the current directory
1596  *  is changed to the directory containing the last occurence of name found.
1597  *  If no occurence found (see return value), current directory remains
1598  *  unchanged. The call is recursive.
1599  *
1600  * \param  name the name to search.
1601  * \param  occurence_number number of occurence already found (incremented)
1602  */ 
1603 // ============================================================================
1604
1605 void SALOME_NamingService::_Find(const char* name,
1606                                  CORBA::Long& occurence_number)
1607 {
1608   MESSAGE("BEGIN OF _Find "<<  occurence_number << " " << name);
1609
1610   CosNaming::BindingList_var binding_list;
1611   CosNaming::BindingIterator_var binding_iterator;
1612   CosNaming::Binding_var binding;
1613
1614   unsigned long nb = 0 ; // --- only for the use of the BindingIterator,
1615                          //     to access the bindings
1616
1617   CosNaming::NamingContext_var ref_context = _current_context;
1618   CosNaming::NamingContext_var found_context = _current_context;
1619
1620   _current_context->list(nb, binding_list, binding_iterator) ;
1621
1622   if (! CORBA::is_nil(binding_iterator))
1623     {
1624       while (binding_iterator->next_one(binding))
1625         {
1626           CosNaming::Name bindingName = binding->binding_name;
1627           
1628           if (binding->binding_type == CosNaming::ncontext)
1629             {
1630               // --- We work on a directory,
1631               //     the search should be done in this directory
1632               
1633               Change_Directory(bindingName[0].id);
1634               _Find(name, occurence_number);
1635               
1636               // --- We'll go back to the initial context
1637               
1638               _current_context = ref_context ;
1639             }
1640           
1641           else if (binding->binding_type == CosNaming::nobject)
1642             {
1643               // --- We work on an object...
1644               
1645               if (!strcmp( bindingName[0].id, name))
1646                 {
1647                   //MESSAGE("One occurence was found");
1648                   occurence_number++;
1649                   
1650                   // --- We keep in memory the directory where
1651                   //     one occurence was found
1652                   
1653                   found_context = _current_context ;
1654                 }
1655             }
1656         }
1657       
1658       binding_iterator->destroy();
1659     }
1660   // --- We go to the last directory where an occurence was found
1661
1662   _current_context = found_context;
1663
1664   SCRUTE(occurence_number);
1665 }
1666
1667 // ============================================================================
1668 /*! \brief find the current directory path.
1669  * 
1670  *  Parse the naming service tree to find the current context and give the
1671  *  associated directory path (relative to root context).
1672  * \param splitPath 
1673  * \param lengthResult
1674  * \param contextToFind
1675  * \param _notFound
1676  */ 
1677 // ============================================================================
1678
1679 void
1680 SALOME_NamingService::
1681 _current_directory(vector<string>& splitPath,
1682                    int& lengthResult,
1683                    CosNaming::NamingContext_var contextToFind,
1684                    bool& notFound)
1685 {
1686   MESSAGE("BEGIN OF _current_Directory");
1687
1688   CosNaming::BindingList_var binding_list;
1689   CosNaming::BindingIterator_var binding_iterator;
1690   CosNaming::Binding_var binding;
1691
1692   unsigned long nb = 0 ; // --- only for the BindingIterator use,
1693                          //     to access the bindings
1694
1695   CosNaming::NamingContext_var ref_context = _current_context;
1696   CosNaming::NamingContext_var temp_context = _current_context;
1697
1698   _current_context->list(nb, binding_list, binding_iterator);
1699
1700   if ( !binding_iterator->_is_nil() )
1701     {
1702       while ((binding_iterator->next_one(binding)) && notFound)
1703         {
1704           CosNaming::Name bindingName = binding->binding_name;
1705
1706           if (binding->binding_type == CosNaming::ncontext)
1707             {
1708               // --- directory, search in it
1709
1710               splitPath.push_back(CORBA::string_dup(bindingName[0].id));
1711               lengthResult++;
1712
1713               CORBA::Object_var obj = _current_context->resolve(bindingName);
1714               temp_context = CosNaming::NamingContext::_narrow(obj);
1715
1716               if (temp_context->_is_equivalent(contextToFind))
1717                 {
1718                   MESSAGE("The context is found, we stop the search");
1719                   notFound = false;
1720                   SCRUTE(notFound);
1721                 }
1722
1723               if (notFound)
1724                 {
1725                   SCRUTE(bindingName[0].id);
1726                   Change_Directory(bindingName[0].id);
1727                   _current_directory(splitPath,
1728                                      lengthResult,
1729                                      contextToFind,
1730                                      notFound);
1731
1732                   if (notFound)
1733                     {
1734                       // --- go back to the initial context
1735
1736                       _current_context = ref_context;
1737
1738                       MESSAGE("Just before the delete of "
1739                               << splitPath[lengthResult-1]);
1740                       splitPath.pop_back();
1741                       lengthResult--;
1742                     }
1743                 }
1744             }
1745         }
1746
1747       binding_iterator->destroy();
1748     }
1749
1750   // --- return to the last directory where an occurence was found
1751
1752   _current_context = ref_context ;
1753 }
1754
1755
1756 // ============================================================================
1757 /*! \brief list recursively all objects in the given directory and subdirs.
1758  *
1759  *  get a list of all the objects in the current directory, with recursion
1760  *  on the subdirectories. Only the objects are listed, not the directories.
1761  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
1762  *  _current_context must refer to absCurDirectory.
1763  *
1764  *  \param myList          The list that will be filled.
1765  *  \param relativeSubDir  The directory relative to absCurDirectory in which
1766  *                         the objects are found.
1767  *  \param absCurDirectory The current directory, absolute path
1768  */ 
1769 // ============================================================================
1770
1771 void SALOME_NamingService::_list_directory_recurs(vector<string>& myList,
1772                                                   string relativeSubDir,
1773                                                   string absCurDirectory)
1774 {
1775   CosNaming::BindingList_var binding_list;
1776   CosNaming::BindingIterator_var binding_iterator;
1777   CosNaming::Binding_var binding ;
1778
1779   unsigned long nb = 0 ; // --- only for thethe use of BindingIterator
1780                          //     to access the bindings
1781
1782   string absDir;
1783
1784   CosNaming::NamingContext_var ref_context = _current_context;
1785
1786   if (! relativeSubDir.empty())
1787     {
1788       Change_Directory(relativeSubDir.c_str());
1789       absDir = absCurDirectory + "/" + relativeSubDir;
1790     }
1791
1792   else
1793     absDir = absCurDirectory;
1794
1795   SCRUTE(absDir);
1796   _current_context->list(nb, binding_list, binding_iterator) ;
1797
1798   if (! CORBA::is_nil(binding_iterator))
1799     {
1800       while (binding_iterator->next_one(binding))
1801         {
1802           CosNaming::Name bindingName = binding->binding_name;
1803
1804           if (binding->binding_type == CosNaming::ncontext)
1805             {
1806               string relativeSdir(bindingName[0].id);
1807               _list_directory_recurs(myList, relativeSdir, absDir);
1808             }
1809
1810           else if (binding->binding_type == CosNaming::nobject)
1811             {
1812               string objName(bindingName[0].id);
1813               string elt = absDir + "/" + objName;
1814               SCRUTE(elt);
1815               myList.push_back(elt);
1816             }
1817         }
1818
1819       binding_iterator->destroy();
1820     }
1821   if (! relativeSubDir.empty())
1822     {
1823       _current_context = ref_context;
1824     }
1825 }
1826
1827 // ============================================================================
1828 /*! \brief return a stringified reference of root context
1829  *
1830  * \return a stringified reference of root context
1831  */
1832 // ============================================================================
1833
1834 char * SALOME_NamingService::getIORaddr()
1835 {
1836   return _orb->object_to_string(_root_context);
1837 }
1838