Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/yacs.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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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 /*! Function : SALOME_NamingService
42  *  Purpose  : Constructor and Initialisation of _root_context
43  */
44 //----------------------------------------------------------------------
45
46 SALOME_NamingService::SALOME_NamingService()
47 {
48   MESSAGE("SALOME_NamingService default constructor");
49   _orb = CORBA::ORB::_nil();
50 }
51
52 //----------------------------------------------------------------------
53 /*! Function : SALOME_NamingService
54  * \param orb CORBA::ORB_ptr arguments
55  */
56 //----------------------------------------------------------------------
57
58 SALOME_NamingService::SALOME_NamingService(CORBA::ORB_ptr orb)
59 {
60   MESSAGE("SALOME_NamingService creation");
61   _orb = orb ;
62   _initialize_root_context();
63 }
64
65 //----------------------------------------------------------------------
66 /*! Function : ~SALOME_NamingService
67  *  Purpose  : Destructor
68  */
69 //----------------------------------------------------------------------
70
71 SALOME_NamingService::~SALOME_NamingService()
72 {
73   // Problem MESSAGE with singleton: late destruction, after trace system destruction ?
74   //MESSAGE("SALOME_NamingService destruction");
75 }
76
77 //----------------------------------------------------------------------
78 /*! Function : init_orb
79  *  initialize ORB reference after default constructor
80  */
81 //----------------------------------------------------------------------
82
83 void SALOME_NamingService::init_orb(CORBA::ORB_ptr orb)
84 {
85   MESSAGE("SALOME_NamingService initialisation");
86   Utils_Locker lock(&_myMutex);
87   _orb = orb ;
88   _initialize_root_context();
89 }
90
91
92 //----------------------------------------------------------------------
93 /*! Function : Register
94  *  Method to create an association in the NamingService between ObjRef
95  *  and Path.
96  *  If the NamingService is out, the exception ServiceUnreachable is thrown
97  * \param ObjRef CORBA::Object_ptr arguments
98  * \param Path const char* arguments
99  */ 
100 //----------------------------------------------------------------------
101
102 void SALOME_NamingService::Register(CORBA::Object_ptr ObjRef,
103                                     const char* Path) 
104   throw(ServiceUnreachable)
105 {
106   MESSAGE("BEGIN OF Register: "<< Path);
107   Utils_Locker lock(&_myMutex);
108   int dimension_Path = strlen(Path) + 1;
109   char** resultat_resolve_Path = new char* [dimension_Path];
110
111   // _current_context is replaced to the _root_context 
112   // if the Path begins whith '/'
113   if (Path[0]=='/')  {
114     _current_context = _root_context;
115     //    MESSAGE("Gone to the _root_context");
116   }
117   
118   //the resolution of the directory path has to be done
119   //to place the currect_context to the correct node
120   int dimension_resultat = 0;
121   _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
122     
123   CosNaming::Name _context_name;
124   CORBA::Boolean _not_exist = false ;
125   CosNaming::NamingContext_var _temp_context;
126   
127   if(dimension_resultat>1)
128     {  
129       // A directory is treated (not only an object name)
130       // We had to test if the directory where ObjRef should be recorded 
131       // is already done
132       // If not, the new context has to be created
133
134       //      MESSAGE("A complet Path has to be treated, not only an object name");
135       _context_name.length(dimension_resultat-1);
136
137       _create_context_name_dir(resultat_resolve_Path,dimension_resultat-1,
138                                _context_name);  
139
140       try
141         {
142           CORBA::Object_var _obj = _current_context->resolve(_context_name);
143           _current_context = CosNaming::NamingContext::_narrow(_obj);
144         } 
145       catch (CosNaming::NamingContext::NotFound &) 
146         {
147           // failed to resolve, therefore assume cold start
148           _not_exist = true;
149         } 
150       catch (CosNaming::NamingContext::InvalidName &) 
151         {
152           INFOS("!!!Register() : CosNaming::NamingContext::InvalidName"); 
153         }
154       catch (CosNaming::NamingContext::CannotProceed &)
155         {
156           INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
157         }
158       catch(CORBA::SystemException&)
159         {
160           INFOS("!!!Register() : CORBA::SystemException : unable to contact"
161                << " the naming service"); 
162           throw ServiceUnreachable();
163         }
164       if(_not_exist)
165         {
166           try
167             {
168               _context_name.length(1);
169               // MESSAGE("The Path indicated is not yet created. It will soon be done");
170               for (int i = 0 ; i <dimension_resultat -1 ;i++)
171                 {
172                   _context_name[0].id =
173                     CORBA::string_dup(resultat_resolve_Path[i]);
174                   _context_name[0].kind = CORBA::string_dup("dir");
175                   // SCRUTE(_context_name[0].id);
176                   //The Path could be in part already created. 
177                   //We had to test it
178                   try 
179                     {
180                       // this context is already created. 
181                       // Nothing to be done
182                       CORBA::Object_var _obj = 
183                         _current_context->resolve(_context_name);
184                       _current_context = 
185                         CosNaming::NamingContext::_narrow(_obj);
186                       //MESSAGE("This context was already created");
187                     } 
188                   catch (CosNaming::NamingContext::NotFound &) 
189                     {
190                       // This context is not created. It will be done
191                       _temp_context =
192                         _current_context->bind_new_context(_context_name);
193                       _current_context = _temp_context;
194                       //INFOS("This context was'nt created, it's now done");
195                     }
196                 }
197             }
198           catch (CosNaming::NamingContext::AlreadyBound&)
199             {
200               INFOS("!!!Register() : CosNaming::NamingContext::AlreadyBound");  
201             }
202           catch(CosNaming::NamingContext::NotFound& ex)
203             {
204               CosNaming::Name n = ex.rest_of_name;
205               if (ex.why == CosNaming::NamingContext::missing_node)
206                 INFOS("Register() : " << (char *) n[0].id
207                       << " (" << (char *) n[0].kind << ") not found");
208               if (ex.why == CosNaming::NamingContext::not_context)
209                 INFOS("Register() : " << (char *) n[0].id
210                      << " (" << (char *) n[0].kind
211                      << ") is not a context");
212               if (ex.why == CosNaming::NamingContext::not_object)
213                 INFOS("Register() : " << (char *) n[0].id
214                      << " (" << (char *) n[0].kind
215                      << ") is not an object");
216             }
217           catch(CosNaming::NamingContext::CannotProceed&)
218             {
219               INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
220             } 
221           catch(CosNaming::NamingContext::InvalidName&) 
222             {
223               INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
224             }
225           catch(CORBA::SystemException&)
226             {
227               INFOS("!!!Register() :CORBA::SystemException : unable to contact"
228                    << " the naming service"); 
229               throw ServiceUnreachable();
230             }
231         }       
232     }
233
234   // The current directory is now the directory where the object should 
235   // be recorded
236   _context_name.length(1);
237   try 
238     {
239       // the last element is an object an not a directory
240       _context_name[0].id = 
241         CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
242       _context_name[0].kind = CORBA::string_dup("object");
243       //SCRUTE(_context_name[0].id);
244         
245       _current_context->bind(_context_name, ObjRef);
246       // MESSAGE("A new element " << _context_name[0].id 
247       //           << " is recorded in the _current_context");   
248     }
249   catch(CosNaming::NamingContext::NotFound& ex)
250     {
251       CosNaming::Name n = ex.rest_of_name;
252       if (ex.why == CosNaming::NamingContext::missing_node)
253         INFOS("Register() : "  << (char *) n[0].id
254               << " ("  << (char *) n[0].kind << ") not found");
255       if (ex.why == CosNaming::NamingContext::not_context)
256         INFOS("Register() : " << (char *) n[0].id
257              << " (" << (char *) n[0].kind
258              << ") is not a context");
259       if (ex.why == CosNaming::NamingContext::not_object)
260         INFOS("Register() : " << (char *) n[0].id
261              << " (" << (char *) n[0].kind
262              << ") is not an object");
263     }
264   catch(CosNaming::NamingContext::CannotProceed&)
265     {
266       INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed"); 
267     } 
268   catch(CosNaming::NamingContext::InvalidName&) 
269     {
270       INFOS("!!!Register() : CosNaming::NamingContext::InvalidName"); 
271     }
272   catch(CosNaming::NamingContext::AlreadyBound&) 
273     {
274       INFOS("!!!Register() : CosNaming::NamingContext::AlreadyBound, object will be rebind"); 
275       _current_context->rebind(_context_name, ObjRef);
276     }
277   catch(CORBA::SystemException&)
278     {
279       INFOS("!!!Register() :CORBA::SystemException : unable to contact"
280             << " the naming service");
281       throw ServiceUnreachable();
282     }
283   
284   
285   // Memory destruction
286   for (int i = 0 ; i <dimension_resultat ;i++) 
287     {
288       delete [] resultat_resolve_Path[i];     
289     }
290   delete[] resultat_resolve_Path ;
291 }
292
293 //----------------------------------------------------------------------
294 /*! Function : Resolve 
295  *  Purpose  : method to get the ObjRef of a symbolic name
296  *  If the NamingService is out, the exception ServiceUnreachable is thrown 
297  * \param Path const char* arguments
298  * \return the object reference
299  */
300 //----------------------------------------------------------------------
301
302 CORBA::Object_ptr SALOME_NamingService::Resolve(const char* Path)
303   throw(ServiceUnreachable)
304 {
305   MESSAGE("BEGIN OF Resolve: " << Path);
306   Utils_Locker lock(&_myMutex);
307   int dimension_Path = strlen(Path) + 1;
308   char** resultat_resolve_Path = new char* [dimension_Path];
309
310   // _current_context is replaced to the _root_context 
311   // if the Path begins whith '/'
312   if (Path[0]=='/') _current_context = _root_context;
313
314   
315   //the resolution of the directory path has to be done
316   //to place the currect_context to the correct node
317   int dimension_resultat = 0;
318   _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
319     
320   CosNaming::Name _context_name;
321   _context_name.length(dimension_resultat);
322   CORBA::Object_ptr _obj = NULL ;
323     
324   _create_context_name_dir(resultat_resolve_Path,dimension_resultat-1,
325                            _context_name);
326   // the last element is an object an not a directory
327   _context_name[dimension_resultat -1].id = 
328     CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
329   _context_name[dimension_resultat -1].kind = CORBA::string_dup("object");
330   // SCRUTE(_context_name[dimension_resultat -1].id);
331   ASSERT(!CORBA::is_nil(_current_context));
332   // Context creation
333   try 
334     {
335       _obj =_current_context->resolve(_context_name);
336     }
337   catch(CosNaming::NamingContext::NotFound& ex)
338     {
339       CosNaming::Name n = ex.rest_of_name;
340       if (ex.why == CosNaming::NamingContext::missing_node)
341         INFOS("Resolve() : " << (char *) n[0].id
342               << " (" << (char *) n[0].kind << ") not found");
343       if (ex.why == CosNaming::NamingContext::not_context)
344         INFOS("Resolve() : "
345              << (char *) n[0].id  << " (" << (char *) n[0].kind
346              << ") is not a context");
347       if (ex.why == CosNaming::NamingContext::not_object)
348         INFOS("Resolve() : " << (char *) n[0].id
349              << " (" << (char *) n[0].kind
350              << ") is not an object");
351     }
352   catch(CosNaming::NamingContext::CannotProceed&)
353     {
354       INFOS("!!!Resolve() : CosNaming::NamingContext::CannotProceed"); 
355     } 
356   catch(CosNaming::NamingContext::InvalidName&) 
357     {
358       INFOS("!!!Resolve() : CosNaming::NamingContext::InvalidName"); 
359     }
360   catch(CORBA::SystemException&)
361     {
362       INFOS("!!!Resolve() :CORBA::SystemException : unable to contact"
363             << "the naming service");
364       throw ServiceUnreachable();
365     }
366   // Memory destruction
367   for (int i = 0 ; i <dimension_resultat ;i++) 
368     {
369       delete [] resultat_resolve_Path[i];     
370     }
371   delete[] resultat_resolve_Path ;
372   
373   return _obj;
374 }
375
376 //----------------------------------------------------------------------
377 /*! Function : ResolveFirst 
378  *  Purpose  : method to get an ObjRef with a symbolic name
379  * \param Path const char* argument like "/path/name"
380  *  search the fist reference like "/path(.dir)/name*(.kind)"
381  *  If the NamingService is out, the exception ServiceUnreachable is thrown 
382
383
384
385
386  * \return the object reference
387  */
388 //----------------------------------------------------------------------
389
390 CORBA::Object_ptr SALOME_NamingService::ResolveFirst(const char* Path)
391   throw(ServiceUnreachable)
392 {
393   MESSAGE("ResolveFirst");
394   Utils_Locker lock(&_myMutex);
395   SCRUTE(Path);
396   string thePath =Path;
397   string basePath ="/";
398   string name = thePath;
399   string::size_type idx = thePath.rfind('/');
400   if (idx != string::npos) // at least one '/' found
401     {
402       basePath = thePath.substr(0,idx);
403       name = thePath.substr(idx+1);
404       SCRUTE(basePath);
405     }
406   SCRUTE(name);
407   CORBA::Object_ptr obj = CORBA::Object::_nil();
408   bool isOk = Change_Directory(basePath.c_str());
409   if (isOk)
410     {
411       vector<string> listElem = list_directory();
412       vector<string>::iterator its = listElem.begin();
413       while (its != listElem.end())
414         {
415           MESSAGE(*its);
416           if ((*its).find(name) == 0)
417             {
418               //string instance = basePath + "/" + *its;
419               return Resolve((*its).c_str());
420             }
421           its++;
422         }
423     }
424   return obj;
425 }
426
427 //----------------------------------------------------------------------
428 /*! Function : Resolve Component  from hostname, containername, componentName and number of prcoessors
429  *  Purpose  : method to get the ObjRef of a component
430  *  If the NamingService is out, the exception ServiceUnreachable is thrown 
431  * \param hostname const char* argument
432  * \param containername const char* argument
433  * \param componentname const char* argument
434  * \param nbproc const int argument
435  * \return the object reference
436  */
437 //----------------------------------------------------------------------
438
439 CORBA::Object_ptr SALOME_NamingService::ResolveComponent(const char* hostname, const char* containerName, const char* componentName, const int nbproc)
440 {
441   MESSAGE("ResolveComponent");
442   Utils_Locker lock(&_myMutex);
443
444   string name="/Containers/";
445   name += hostname;
446   if( strlen(containerName) != 0 ){
447     name += "/";
448     if( nbproc >=1 ){
449       char *newContainerName = new char[strlen(containerName)+8];
450       sprintf(newContainerName,"%s_%d",containerName,nbproc);
451       name += newContainerName;
452     }
453     else
454       name += containerName;
455     name += "/";
456     name += componentName;
457     return ResolveFirst(name.c_str());
458   }
459   else {
460     Change_Directory(name.c_str());
461     vector<string> contList = list_directory();
462     for(unsigned int ind = 0; ind < contList.size(); ind++){
463       name = contList[ind].c_str();
464       name += "/";
465       name += componentName;
466       CORBA::Object_ptr obj = ResolveFirst(name.c_str());
467       if( !CORBA::is_nil(obj) )
468         return obj;
469     }
470     return CORBA::Object::_nil();
471   }
472
473 }
474
475 string SALOME_NamingService::ContainerName(const char *containerName)
476 {
477   string ret;
478
479   if (strlen(containerName)== 0)
480     ret = "FactoryServer";
481   else
482     ret = containerName;
483
484   return ret;
485 }
486
487 string SALOME_NamingService::ContainerName(const Engines::MachineParameters& params)
488 {
489   int nbproc;
490   if( !params.isMPI )
491     nbproc = 0;
492   else if( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
493     nbproc = 1;
494   else if( params.nb_node == 0 )
495     nbproc = params.nb_proc_per_node;
496   else if( params.nb_proc_per_node == 0 )
497     nbproc = params.nb_node;
498   else
499     nbproc = params.nb_node * params.nb_proc_per_node;
500
501   string ret=ContainerName(params.container_name);
502
503   if( nbproc >=1 ){
504     char *suffix = new char[8];
505     sprintf(suffix,"_%d",nbproc);
506     ret += suffix;
507   }
508
509   return ret;
510 }
511
512 string SALOME_NamingService::BuildContainerNameForNS(const char *containerName, const char *hostname)
513 {
514   string ret="/Containers/";
515   ret += hostname;
516   ret+="/";
517   ret+=ContainerName(containerName);
518
519   return ret;
520 }
521
522 string SALOME_NamingService::BuildContainerNameForNS(const Engines::MachineParameters& params, const char *hostname)
523 {
524   string ret="/Containers/";
525   ret += hostname;
526   ret+="/";
527   ret+=ContainerName(params);
528
529   return ret;
530 }
531
532 //----------------------------------------------------------------------
533 /*!  Function : Find
534  *  Purpose  : method to research a name from the current directory 
535  *            of the naming service. 
536  *  The naming service changes directory to go to the directory where 
537  *  the last occurence was found.
538  *  If the NamingService is out, the exception ServiceUnreachable is thrown
539  *  \param name const char* arguments
540  *  \return the number of occurences found
541  *  \sa _Find
542  */   
543 //----------------------------------------------------------------------
544
545 int SALOME_NamingService::Find(const char* name)
546   throw(ServiceUnreachable)
547 {
548   MESSAGE("BEGIN OF Find " << name);
549   Utils_Locker lock(&_myMutex);
550   CORBA::Long occurence_number = 0 ; 
551   try
552     {
553       _Find(name,occurence_number);
554     }
555   catch(CORBA::SystemException&)
556     {
557       INFOS("!!!Find() : CORBA::SystemException : unable to contact"
558            << " the naming service"); 
559       throw ServiceUnreachable();
560     }
561   return occurence_number;
562 }
563
564 //----------------------------------------------------------------------
565 /*! Function : Create_Directory 
566  *  Purpose  : method to create a directory from the current directory.
567  * If the NamingService is out, the exception ServiceUnreachable is thrown
568  *  \param Path const char* arguments
569  *  \return a boolean to indicate if the creation succeeded
570  */
571 //----------------------------------------------------------------------
572
573 bool SALOME_NamingService::Create_Directory(const char* Path)
574   throw(ServiceUnreachable)
575 {
576   MESSAGE("BEGIN OF Create_Directory");
577   Utils_Locker lock(&_myMutex);
578   int dimension_Path = strlen(Path) + 1;
579   char** resultat_resolve_Path= new char* [dimension_Path];;
580   CORBA::Boolean _return_code = true ;
581
582   // _current_context is replaced to the _root_context 
583   // if the Path begins whith '/'
584   if (Path[0]=='/') _current_context = _root_context;
585
586   int dimension_resultat = 0;
587   _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
588
589  
590   // We had to test if a part of the directory to treat 
591   // is already done
592   // If not, the new context has to be created
593  
594   CosNaming::Name _context_name;
595   _context_name.length(1);
596   CosNaming::NamingContext_var _temp_context;
597   ASSERT(!CORBA::is_nil(_current_context));
598   // Context creation
599   try
600     {
601         
602       for (int i = 0 ; i <dimension_resultat ;i++)
603         {
604           _context_name[0].id =
605             CORBA::string_dup(resultat_resolve_Path[i]);
606           _context_name[0].kind = CORBA::string_dup("dir");
607           // SCRUTE(_context_name[0].id);
608           //The Path could be in part already created. 
609           //We had to test it
610           try 
611             {
612               // this context is already created. 
613               // Nothing to be done
614               CORBA::Object_var _obj = 
615                 _current_context->resolve(_context_name);
616               _current_context = 
617                 CosNaming::NamingContext::_narrow(_obj);
618               MESSAGE("This context was already created");
619                 } 
620           catch (CosNaming::NamingContext::NotFound &) 
621             {
622               // This context is not created. It will be done
623               _temp_context =
624                 _current_context->bind_new_context(_context_name);
625               _current_context = _temp_context;
626               INFOS("This context was'nt created, it's now done");
627                 }
628         }
629     }
630   catch (CosNaming::NamingContext::AlreadyBound&)
631     {
632       INFOS("!!! Create_Directory() CosNaming::NamingContext::AlreadyBound");
633       _return_code = false;     
634     }
635   catch(CosNaming::NamingContext::NotFound& ex)
636     {
637       _return_code = false;
638       CosNaming::Name n = ex.rest_of_name;
639       if (ex.why == CosNaming::NamingContext::missing_node)
640         INFOS("Create_Directory() : " << (char *) n[0].id
641               << " (" << (char *) n[0].kind << ") not found");
642       if (ex.why == CosNaming::NamingContext::not_context)
643         INFOS("Create_Directory() : " << (char *) n[0].id
644              << " (" << (char *) n[0].kind
645              << ") is not a context");
646       if (ex.why == CosNaming::NamingContext::not_object)
647         INFOS("Create_Directory() : " << (char *) n[0].id
648              << " (" << (char *) n[0].kind
649              << ") is not an object");
650     }
651   catch(CosNaming::NamingContext::CannotProceed&)
652     {
653       _return_code = false;
654       INFOS("!!!Create_Directory():CosNaming::NamingContext::CannotProceed"); 
655     } 
656   catch(CosNaming::NamingContext::InvalidName&) 
657     {
658       _return_code = false;
659       INFOS("!!!Create_Directory():CosNaming::NamingContext::InvalidName");
660     }
661   catch(CORBA::SystemException&)
662     {
663       _return_code = false;
664       INFOS("!!!Register() :CORBA::SystemException : unable to contact"
665            << " the naming service"); 
666       throw ServiceUnreachable();
667     }
668   // Memory destruction
669   for (int i = 0 ; i <dimension_resultat;i++) 
670     {
671       delete [] resultat_resolve_Path[i];     
672     }
673   delete[] resultat_resolve_Path ;
674   return _return_code;
675 }
676
677 //----------------------------------------------------------------------
678 /*! Function : Change_Directory 
679  *  Purpose  : method to change the current directory to the
680  *             directory Path indicated in "in" Parameter.
681  *  If Path ="/", the current directory changes to the root directory.
682  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
683  * \param Path const char* arguments
684  * \return a boolean to indicate if the change succeeded
685  */
686 //----------------------------------------------------------------------
687
688 bool SALOME_NamingService::Change_Directory(const char* Path)
689   throw(ServiceUnreachable)
690 {
691   MESSAGE("BEGIN OF Change_Directory " << Path);
692   Utils_Locker lock(&_myMutex);
693   int dimension_Path = strlen(Path) + 1;
694   char** resultat_resolve_Path = new char* [dimension_Path];
695   CORBA::Boolean _return_code = true ;
696
697   // _current_context is replaced to the _root_context 
698   // if the Path begins whith '/'
699   if (Path[0]=='/') _current_context = _root_context;
700
701   if ((Path[0]=='/') && (dimension_Path == 2))
702     {
703       MESSAGE("Change_Directory is called to go to the root_context");
704     }
705   //nothing to de done, the change_dur is called to go to the root_context
706   // no Path to resolve
707   else
708     //the resolution of the directory path has to be done
709     //to place the currect_context to the correct node
710     {
711       int dimension_resultat = 0;
712       _result_resolve_Path(Path,dimension_resultat,resultat_resolve_Path);
713           
714       CosNaming::Name _context_name;
715       _context_name.length(dimension_resultat);
716       CORBA::Object_var _obj;
717
718       _create_context_name_dir(resultat_resolve_Path,dimension_resultat,
719                                _context_name);
720
721       ASSERT(!CORBA::is_nil(_current_context));
722       // Context creation
723       try 
724         {
725           _obj =_current_context->resolve(_context_name);
726           _current_context = CosNaming::NamingContext::_narrow(_obj);
727           ASSERT(!CORBA::is_nil(_current_context))
728             }
729       catch(CosNaming::NamingContext::NotFound& ex)
730         {
731           _return_code = false;
732           CosNaming::Name n = ex.rest_of_name;
733           if (ex.why == CosNaming::NamingContext::missing_node)
734             INFOS( "Change_Directory() : " << (char *) n[0].id
735                   << " (" << (char *) n[0].kind << ") not found")
736           if (ex.why == CosNaming::NamingContext::not_context)
737             INFOS("Change_Directory() : " << (char *) n[0].id
738                  << " (" << (char *) n[0].kind
739                  << ") is not a context" )
740           if (ex.why == CosNaming::NamingContext::not_object)
741             INFOS( "Change_Directory() : " << (char *) n[0].id
742                  << " (" << (char *) n[0].kind
743                  << ") is not an object" )
744         }
745       catch(CosNaming::NamingContext::CannotProceed&)
746         {
747           _return_code = false;
748           INFOS( "!!!Change_Directory() : CosNaming::NamingContext::CannotProceed" )
749         } 
750       catch(CosNaming::NamingContext::InvalidName&) 
751         {
752           _return_code = false;
753           INFOS( "!!!Change_Directory() : CosNaming::NamingContext::InvalidName" )
754         }
755       catch(CORBA::SystemException&)
756         {
757           _return_code = false;
758           INFOS( "!!!Change_Directory() :CORBA::SystemException : unable to contact"
759                << "the naming service")
760           throw ServiceUnreachable();
761         }
762       // Memory destruction
763       for (int i = 0 ; i <dimension_resultat ;i++) 
764         {
765           delete [] resultat_resolve_Path[i];     
766         }
767       delete[] resultat_resolve_Path ;
768     }
769   return _return_code;
770 }
771
772 //----------------------------------------------------------------------
773 /*! Function : Current_Directory 
774  *  Purpose  : method to get the current directory.
775  *  If the NamingService is out, the exception ServiceUnreachable is thrown
776  * \return the path of the current_context
777  * \sa  _current_directory
778  */ 
779 //----------------------------------------------------------------------
780
781 char* SALOME_NamingService::Current_Directory()
782   throw(ServiceUnreachable)
783 {
784   MESSAGE("BEGIN OF Current_Directory");  
785   Utils_Locker lock(&_myMutex);
786
787   CosNaming::NamingContext_var _ref_context = _current_context;
788
789   int i = 0;
790   int length_path = 0;
791   char** result_path = new char*[50]; // 50 is it enough?
792
793  
794   // We go to the root_context to begin the search from the root
795   _current_context = _root_context ;
796   CORBA::Boolean _continue = true ;
797   try
798     {
799       _current_directory(result_path,i,_ref_context,_continue );
800     }
801   catch(CORBA::SystemException&)
802     {
803       INFOS("!!!Current_Directory(): CORBA::SystemException : unable to contact"
804            << " the naming service" )
805       throw ServiceUnreachable();
806     }
807   for (int k = 0 ; k <i ;k++) 
808     { 
809       // We count the length of the char* + 1 for the '/' to separate
810       // the directories
811       length_path = length_path + strlen(result_path[k]) + 1;
812     }
813   char* return_Path = new char[length_path +2];
814   return_Path[0] = '/' ;
815   return_Path[1] = '\0' ;
816 #ifndef WNT
817   for (int k = 0 ; k <i ;k++) 
818 #else
819   for (k = 0 ; k <i ;k++) 
820 #endif
821     { 
822       //SCRUTE(result_path[k])
823         strcat(return_Path,result_path[k]);
824       strcat(return_Path,"/");
825     }
826   //SCRUTE(return_Path)
827     _current_context = _ref_context ;
828  
829   return return_Path;
830 }
831
832 //----------------------------------------------------------------------
833 /*! Function : list
834  *  Purpose  : method to list and print all the context contained from
835  *            the current context
836  *  If the NamingService is out, the exception ServiceUnreachable is thrown
837  */ 
838 //----------------------------------------------------------------------
839
840 void SALOME_NamingService::list()
841   throw(ServiceUnreachable)
842 {
843   MESSAGE("Begin of list");
844   Utils_Locker lock(&_myMutex);
845   CosNaming::BindingList_var _binding_list;
846   CosNaming::BindingIterator_var _binding_iterator;
847   unsigned long nb=0 ; // for using only the BindingIterator to access the bindings
848   CosNaming::Binding_var _binding ;
849   CosNaming::NamingContext_var _ref_context = _current_context;
850   _current_context->list(nb, _binding_list, _binding_iterator) ;
851
852   while (_binding_iterator->next_one(_binding)) {
853     CosNaming::Name _bindingName = _binding->binding_name;
854     if (_binding->binding_type == CosNaming::ncontext) {
855       MESSAGE( "Context : " << _bindingName[0].id );
856       try
857         {
858           Change_Directory(_bindingName[0].id);
859         }
860       catch (ServiceUnreachable&)
861         {
862           INFOS( "!!!list(): ServiceUnreachable" )
863           throw ServiceUnreachable(); 
864         }
865
866       list();
867       _current_context = _ref_context ;
868     }
869     else if (_binding->binding_type == CosNaming::nobject) {
870       MESSAGE( "Object : " << _bindingName[0].id );
871     }
872   }
873   _binding_iterator->destroy();
874 }
875
876 //----------------------------------------------------------------------
877 /*! Function : list_directory
878  *  Purpose  : method to get all the contexts contained in the current 
879  *             directory
880  *             Get only objects, isn't iterative 
881  *  If the NamingService is out, the exception ServiceUnreachable is thrown
882  */ 
883 //----------------------------------------------------------------------
884 vector<string> SALOME_NamingService::list_directory()
885   throw(ServiceUnreachable)
886 {
887   vector<string> _list ;
888   _list.resize(0);
889   CosNaming::BindingList_var _binding_list;
890   CosNaming::BindingIterator_var _binding_iterator;
891   unsigned long nb=0 ; // for using only the BindingIterator to access the bindings
892   CosNaming::Binding_var _binding ;
893   CosNaming::NamingContext_var _ref_context = _current_context;
894   _current_context->list(nb, _binding_list, _binding_iterator) ;
895   if (_binding_iterator->_is_nil()) return _list;
896
897   while (_binding_iterator->next_one(_binding)) {
898     CosNaming::Name _bindingName = _binding->binding_name;
899     if (_binding->binding_type == CosNaming::nobject) {
900       _list.push_back(CORBA::string_dup(_bindingName[0].id));
901     }
902   }
903   //for (unsigned int ind = 0; ind < _list.size(); ind++)
904   //  MESSAGE("list_directory : Object : " << _list[ind]);
905
906   _binding_iterator->destroy();
907   return _list;
908 }
909
910 //----------------------------------------------------------------------
911 /*! Function : list_directory_recurs
912  *  Purpose  : method to get all the contexts contained in the current 
913  *             directory
914  *             Get only objects and is recursive 
915  *  If the NamingService is out, the exception ServiceUnreachable is thrown
916  */ 
917 //----------------------------------------------------------------------
918 vector<string> SALOME_NamingService::list_directory_recurs()
919     throw(ServiceUnreachable)
920 {
921   MESSAGE("list_directory_recurs");
922   Utils_Locker lock(&_myMutex);
923   vector<string> _list ;
924   char *currentDir=Current_Directory();
925   _list_directory_recurs(_list,0,currentDir);
926   delete [] currentDir;
927   return _list;
928 }
929
930 //----------------------------------------------------------------------
931 /*! Function : Destroy_Name 
932  *  Purpose  : method to destroy an association Path-Object Reference.
933  *             WARNING : The complete Path should be given.
934  *  If the NamingService is out, the exception ServiceUnreachable is thrown 
935  * \param Path const char* arguments
936  */
937 //----------------------------------------------------------------------
938
939 void SALOME_NamingService::Destroy_Name(const char* Path)
940   throw(ServiceUnreachable)
941 {
942   MESSAGE("BEGIN OF Destroy_Name");
943   Utils_Locker lock(&_myMutex);
944   int dimension_Path = strlen(Path) + 1;
945   char** resultat_resolve_Path = new char* [dimension_Path];
946
947   // _current_context is replaced to the _root_context 
948   // if the Path begins whith '/'
949   if (Path[0]=='/') _current_context = _root_context;
950
951   
952   //the resolution of the directory path has to be done
953   //to place the currect_context to the correct node
954   int dimension_resultat = 0;
955   _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
956     
957   CosNaming::Name _context_name;
958   if (dimension_resultat>1)
959     {
960       // We go in the directory where the object to destroy is
961       _context_name.length(dimension_resultat-1);
962   
963       _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
964                                _context_name); 
965       try
966         {
967           CORBA::Object_var _obj = _current_context->resolve(_context_name);
968           _current_context = CosNaming::NamingContext::_narrow(_obj);
969         } 
970       catch (CosNaming::NamingContext::NotFound& ex) 
971         {
972           CosNaming::Name n = ex.rest_of_name;
973           if (ex.why == CosNaming::NamingContext::missing_node)
974             INFOS(  "Destroy_Name() : " << (char *) n[0].id
975                   << " (" << (char *) n[0].kind << ") not found" )
976           if (ex.why == CosNaming::NamingContext::not_context)
977             INFOS( "Destroy_Name() : " << (char *) n[0].id
978                  << " (" << (char *) n[0].kind
979                  << ") is not a context" )
980           if (ex.why == CosNaming::NamingContext::not_object)
981             INFOS( "Destroy_Name() : " << (char *) n[0].id
982                  << " ("  << (char *) n[0].kind
983                  << ") is not an object" )
984         } 
985       catch (CosNaming::NamingContext::InvalidName &) 
986         {
987           INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName" )
988         }
989       catch (CosNaming::NamingContext::CannotProceed &)
990         {
991           INFOS( "!!!Destroy_Name(): CosNaming::NamingContext::CannotProceed" )
992         }
993       catch(CORBA::SystemException&)
994         {
995           INFOS( "!!!Destroy_Name() : CORBA::SystemException : unable to contact"
996                << " the naming service")
997           throw ServiceUnreachable();
998         }
999     }
1000
1001   // the last element is the object to destroy
1002   _context_name.length(1);  
1003   _context_name[0].id = 
1004     CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
1005   _context_name[0].kind = CORBA::string_dup("object");
1006   SCRUTE(_context_name[0].id);
1007   ASSERT(!CORBA::is_nil(_current_context));
1008   // Object destruction
1009   try 
1010     {
1011       _current_context->unbind(_context_name);
1012       MESSAGE( "The object " << _context_name[0].id  << " has been deleted" )
1013     }
1014   catch(CosNaming::NamingContext::NotFound& ex)
1015     {
1016       CosNaming::Name n = ex.rest_of_name;
1017       if (ex.why == CosNaming::NamingContext::missing_node)
1018         INFOS(  "Destroy_Name() : " << (char *) n[0].id
1019               << " (" << (char *) n[0].kind << ") not found" )
1020       if (ex.why == CosNaming::NamingContext::not_context)
1021         INFOS( "Destroy_Name() : " << (char *) n[0].id
1022              << " (" << (char *) n[0].kind
1023              << ") is not a context" )
1024       if (ex.why == CosNaming::NamingContext::not_object)
1025         INFOS( "Destroy_Name() : " << (char *) n[0].id
1026              << " ("  << (char *) n[0].kind
1027              << ") is not an object" )
1028     }
1029   catch(CosNaming::NamingContext::CannotProceed&)
1030     {
1031       INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::CannotProceed")
1032     } 
1033   catch(CosNaming::NamingContext::InvalidName&) 
1034     {
1035       INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName")
1036     }
1037   catch(CORBA::SystemException&)
1038     {
1039       INFOS( "!!!Destroy_Name() :CORBA::SystemException : unable to contact" 
1040            << " the naming service") 
1041       throw ServiceUnreachable();
1042     }
1043   // Memory destruction
1044   for (int i = 0 ; i <dimension_resultat ;i++) 
1045     {
1046       delete [] resultat_resolve_Path[i];     
1047     }
1048   delete[] resultat_resolve_Path ;
1049 }
1050
1051 //----------------------------------------------------------------------
1052 /*! Function : Destroy_Directory.
1053  *  Purpose  : method to destroy a directory if it is empty.
1054  *  WARNING : The complete Path  to the directory (from the root_context)
1055  *  to destroy should be given.
1056  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
1057  * \param Path const char* arguments
1058  */
1059 //----------------------------------------------------------------------
1060
1061 void SALOME_NamingService::Destroy_Directory(const char* Path)
1062   throw(ServiceUnreachable)
1063 {
1064   MESSAGE("BEGIN OF Destroy_Directory");
1065   Utils_Locker lock(&_myMutex);
1066   int dimension_Path = strlen(Path) + 1;
1067   char** resultat_resolve_Path = new char* [dimension_Path];
1068
1069   // _current_context is replaced to the _root_context 
1070   // if the Path begins whith '/'
1071   if (Path[0]=='/') _current_context = _root_context;
1072
1073   CosNaming::NamingContext_var _ref_context = _current_context;
1074  
1075   //the resolution of the directory path has to be done
1076   //to place the currect_context to the correct node
1077   int dimension_resultat = 0;
1078   _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
1079     
1080   CosNaming::Name _context_name;
1081   if (dimension_resultat>1)
1082     {
1083       // We go in the directory where the context to destroy is
1084       _context_name.length(dimension_resultat-1);
1085   
1086       _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
1087                                _context_name); 
1088       try
1089         {
1090           CORBA::Object_var _obj = _current_context->resolve(_context_name);
1091           _current_context = CosNaming::NamingContext::_narrow(_obj);
1092           _ref_context = _current_context ;
1093         } 
1094       catch (CosNaming::NamingContext::NotFound& ex) 
1095         {
1096           CosNaming::Name n = ex.rest_of_name;
1097           if (ex.why == CosNaming::NamingContext::missing_node)
1098             INFOS(  "Destroy_Directory() : " << (char *) n[0].id
1099                   << " (" << (char *) n[0].kind << ") not found")
1100           if (ex.why == CosNaming::NamingContext::not_context)
1101             INFOS( "Destroy_Directory() : " << (char *) n[0].id
1102                  << " (" << (char *) n[0].kind
1103                  << ") is not a context" )
1104           if (ex.why == CosNaming::NamingContext::not_object)
1105             INFOS( "Destroy_Directory() : " << (char *) n[0].id
1106                  << " ("  << (char *) n[0].kind
1107                  << ") is not an object" )
1108         } 
1109       catch (CosNaming::NamingContext::InvalidName &) 
1110         {
1111           INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
1112         }
1113       catch (CosNaming::NamingContext::CannotProceed &)
1114         {
1115           INFOS("!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
1116         }
1117       catch(CORBA::SystemException&)
1118         {
1119           INFOS( "!!!Destroy_Directory() : CORBA::SystemException : unable to contact"
1120                << " the naming service" )
1121           throw ServiceUnreachable();
1122         }
1123     }
1124
1125   // the last element is the context to destroy
1126   _context_name.length(1);  
1127   _context_name[0].id = 
1128     CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
1129   _context_name[0].kind = CORBA::string_dup("dir");
1130   SCRUTE(_context_name[0].id);
1131
1132   try
1133     {
1134       // We go in the context to destroy
1135       CORBA::Object_var _obj = _current_context->resolve(_context_name);
1136       _current_context = CosNaming::NamingContext::_narrow(_obj);
1137     } 
1138   catch (CosNaming::NamingContext::NotFound& ex) 
1139     {
1140       CosNaming::Name n = ex.rest_of_name;
1141       if (ex.why == CosNaming::NamingContext::missing_node)
1142         INFOS(  "Destroy_Directory() : " << (char *) n[0].id
1143               << " (" << (char *) n[0].kind << ") not found" )
1144       if (ex.why == CosNaming::NamingContext::not_context)
1145         INFOS( "Destroy_Directory() : " << (char *) n[0].id
1146              << " (" << (char *) n[0].kind
1147              << ") is not a context" )
1148       if (ex.why == CosNaming::NamingContext::not_object)
1149         INFOS( "Destroy_Directory() : " << (char *) n[0].id
1150              << " ("  << (char *) n[0].kind
1151              << ") is not an object" )
1152     } 
1153   catch (CosNaming::NamingContext::InvalidName &) 
1154     {
1155       INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
1156     }
1157   catch (CosNaming::NamingContext::CannotProceed &)
1158     {
1159       INFOS( "!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
1160     }
1161   catch(CORBA::SystemException&)
1162     {
1163       INFOS( "!!!Destroy_Directory() : CORBA::SystemException : unable to contact"
1164            << " the naming service" )
1165       throw ServiceUnreachable();
1166     }
1167
1168   ASSERT(!CORBA::is_nil(_current_context));
1169   // Context Destruction
1170   try 
1171     {
1172       _current_context->destroy();
1173       MESSAGE( "The context " << _context_name[0].id << " has been deleted" )
1174     }
1175   catch(CosNaming::NamingContext::NotEmpty&)
1176     {
1177       INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::NoEmpty "
1178            << Path << " is not empty" )
1179     } 
1180   catch(CORBA::SystemException&)
1181     {
1182       INFOS( "!!!Destroy_Directory() :CORBA::SystemException : "
1183            << "unable to contact the naming service") 
1184       throw ServiceUnreachable();
1185     }
1186   // We go to the directory just before the context to delete
1187   _current_context = _ref_context ; 
1188   try
1189     {
1190       _current_context->unbind(_context_name);
1191       MESSAGE( "The bind to the context " << _context_name[0].id  << " has been deleted" )
1192     }
1193   catch(CosNaming::NamingContext::NotFound& ex)
1194     {
1195       CosNaming::Name n = ex.rest_of_name;
1196       if (ex.why == CosNaming::NamingContext::missing_node)
1197         INFOS(  "Destroy_Directory() : " << (char *) n[0].id
1198               << " (" << (char *) n[0].kind << ") not found" )
1199       if (ex.why == CosNaming::NamingContext::not_context)
1200         INFOS( "Destroy_Directory() : " << (char *) n[0].id
1201              << " (" << (char *) n[0].kind
1202              << ") is not a context" )
1203       if (ex.why == CosNaming::NamingContext::not_object)
1204         INFOS( "Destroy_Directory() : " << (char *) n[0].id
1205              << " ("  << (char *) n[0].kind
1206              << ") is not an object" )
1207     }
1208   catch(CosNaming::NamingContext::CannotProceed&)
1209     {
1210       INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::CannotProceed")
1211     } 
1212   catch(CosNaming::NamingContext::InvalidName&) 
1213     {
1214       INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName")
1215     }
1216   catch(CORBA::SystemException&)
1217     {
1218       INFOS( "!!!Destroy_Directory() :CORBA::SystemException : unable to contact" 
1219            << " the naming service") 
1220       throw ServiceUnreachable();
1221     }
1222   // Memory destruction
1223   for (int i = 0 ; i <dimension_resultat ;i++) 
1224     {
1225       delete [] resultat_resolve_Path[i];     
1226     }
1227   delete[] resultat_resolve_Path ;
1228 }
1229
1230 //----------------------------------------------------------------------
1231 /*! Function : Destroy_Directory.
1232  *  Purpose  : method to destroy a directory if it is empty.
1233  *  WARNING : The complete Path  to the directory (from the root_context)
1234  *  to destroy should be given.
1235  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
1236  * \param Path const char* arguments
1237  */
1238 //----------------------------------------------------------------------
1239
1240 void SALOME_NamingService::Destroy_FullDirectory(const char* Path)
1241   throw(ServiceUnreachable)
1242 {
1243   Change_Directory(Path);
1244   vector<string> contList = list_directory();
1245   for(unsigned int ind = 0; ind < contList.size(); ind++)
1246     Destroy_Name(contList[ind].c_str());
1247   Destroy_Directory(Path);
1248   Destroy_Name(Path);
1249 }
1250
1251 //----------------------------------------------------------------------
1252 /*! Function : _initialize_root_context
1253  * Purpose  :  method called by constructor to initialize _root_context
1254  */
1255 //----------------------------------------------------------------------
1256
1257 void SALOME_NamingService::_initialize_root_context()
1258 {
1259   //MESSAGE("Get the root context");
1260   try
1261     {
1262       CORBA::Object_var obj = _orb->resolve_initial_references("NameService");
1263       _root_context = CosNaming::NamingContext::_narrow(obj);
1264       _current_context = _root_context ;
1265       ASSERT(!CORBA::is_nil(_root_context)); 
1266     }
1267
1268   catch(CORBA::SystemException&)
1269     {
1270       INFOS("CORBA::SystemException: unable to contact the naming service");
1271       throw ServiceUnreachable();
1272     }
1273   catch(...)
1274     {
1275       INFOS("Unknown Exception: unable to contact the naming service");
1276       throw ServiceUnreachable();
1277     }
1278 }
1279
1280 //----------------------------------------------------------------------
1281 /*! Function : _resolve_Path
1282  * Purpose  : method to decompose a Path : /Kernel/Services/Sessions.
1283  * 
1284  * \return a char* containing the first char between '/' (in this case Kernel)
1285  */
1286 //----------------------------------------------------------------------
1287
1288 char* SALOME_NamingService::_resolve_Path(char* Path)
1289 {
1290   int i = 0 ;
1291   int length = strlen(Path);
1292   char *resultat;
1293
1294   if (length==0) return NULL;
1295   else
1296     {
1297       while ((i<length) && (Path[i]!='/'))
1298         i++;
1299       resultat = new char[i+1];
1300       strncpy(resultat,Path,i);
1301       resultat[i]='\0';    
1302       return resultat;
1303     }
1304 }
1305
1306 //----------------------------------------------------------------------
1307 /*! Function : _result_resolve_Path.
1308  *  Purpose  : method to decompose a Path : /Kernel/Services/Sessions.
1309  *  Gives an array of char* containing Kernel, Services, Sessions.
1310  * \param  Path const char* arguments, the Path to decompose
1311  * \param j int& arguments, the size of the array of char*
1312  * \param resultat_resolve_Path char** arguments
1313  */
1314 //----------------------------------------------------------------------
1315
1316 void 
1317 SALOME_NamingService::_result_resolve_Path(const char* Path, 
1318                                            int& j, 
1319                                            char ** resultat_resolve_Path)
1320 {  
1321   //MESSAGE("BEGIN OF _result_resolve_Path");
1322   int dimension_Path = strlen(Path) + 1;
1323   char** temp= new char* [dimension_Path];
1324   char** tempslash = new char* [dimension_Path];
1325
1326   temp[j] = new char[dimension_Path];
1327   strcpy(temp[j],Path);
1328
1329   while (strlen(temp[j])>0)
1330     {
1331       // temp[j] contains the characters to be treated :
1332       //  (Path - characters already treted)
1333       // tempslash[j] = temp[j] if the string temp[j] doesn't begin whith '/'
1334       // tempslash[j] = temp[j] without '/' if the string begins whith '/'
1335       int length_temp = strlen(temp[j]);
1336       if (temp[j][0]=='/')
1337         {
1338           // the characters to be treated begin whith '/'
1339           // we don't have to take the '/'
1340           tempslash[j] = new char [length_temp] ;
1341           for (int k = 0; k < length_temp-1; k++) 
1342             tempslash[j][k] = temp[j][k+1];
1343           tempslash[j][length_temp-1]='\0';
1344         }
1345       else
1346         {
1347           //the characters to be trated don't begin with '/'
1348           // Nothing to be done on the char
1349           tempslash[j] = new char [length_temp+1] ;
1350           strcpy(tempslash[j],temp[j]);   
1351         }
1352       // decomposition of the Path 
1353       resultat_resolve_Path[j]= _resolve_Path(tempslash[j]);
1354       //SCRUTE(resultat_resolve_Path[j]);
1355
1356       int length_resultat = strlen(resultat_resolve_Path[j]) ;
1357       int dimension_temp = length_temp -length_resultat ;
1358       j++;
1359       temp[j] = new char[dimension_temp +1];
1360       for (int i = 0 ; i <dimension_temp  ;i++)
1361         {
1362           temp[j][i] =tempslash[j-1][i+ length_resultat];
1363         }
1364       temp[j][dimension_temp]= '\0';
1365       //SCRUTE(temp[j]);
1366     } 
1367   // Memory destruction
1368   for (int i = 0 ; i <j;i++) 
1369     {
1370       delete [] temp[i];
1371       delete [] tempslash[i];    
1372     }
1373   delete[] temp;
1374   delete [] tempslash ;
1375 }
1376
1377 //----------------------------------------------------------------------
1378 /*! Function : _Find.
1379  *  Purpose  : method to research a name from the current directory 
1380  *             of the naming service.   
1381  *  The naming service changes directory to go to the directory where 
1382  *  the last occurence was found.
1383  *  \param name const char* arguments
1384  *  \param occurence_number CORBA::LONG (by value)
1385  */   
1386 //----------------------------------------------------------------------
1387
1388 void SALOME_NamingService::_Find(const char* name, 
1389                                  CORBA::Long& occurence_number)
1390 {
1391   //MESSAGE("BEGIN OF _Find") SCRUTE(name); 
1392   CosNaming::BindingList_var _binding_list;
1393   CosNaming::BindingIterator_var _binding_iterator;
1394   unsigned long nb=0 ; //for using only the BindingIterator 
1395                        // to access the bindings
1396   CosNaming::Binding_var _binding ;
1397   CosNaming::NamingContext_var _ref_context = _current_context;
1398   CosNaming::NamingContext_var _found_context = _current_context;
1399
1400   _current_context->list(nb, _binding_list, _binding_iterator) ;
1401
1402   while (_binding_iterator->next_one(_binding)) {
1403     CosNaming::Name _bindingName = _binding->binding_name;
1404     if (_binding->binding_type == CosNaming::ncontext) {
1405       // We work on a directory, the search should be done in this directory
1406       Change_Directory(_bindingName[0].id);
1407       _Find(name,occurence_number);
1408       // We'll go back to the initial context
1409       _current_context = _ref_context ;
1410     }
1411     else if (_binding->binding_type == CosNaming::nobject) {
1412       // We work on an object...
1413       if (!strcmp( _bindingName[0].id,name))
1414         {
1415           //MESSAGE("One occurence was found");
1416           occurence_number++;
1417           // We keep in memory the directory where one occurence was found
1418           _found_context = _current_context ;
1419         }
1420     }
1421   }
1422   _binding_iterator->destroy();  
1423   // We go to the last directory where an occurence was found
1424   _current_context = _found_context ;
1425   //SCRUTE(occurence_number);
1426 }
1427
1428 //----------------------------------------------------------------------
1429 /*! Function : _create_context_name_dir.
1430  *  Purpose  : method to create a Context_name from an array of char.
1431  *             The number of elements to be copied are indicated 
1432  *             with lenth_copy.
1433  *
1434  * \param resultat_resolve_Path char** arguments
1435  * \param length_copy int arguments
1436  * \param _context_name CosNaming::Name arguments (by value)
1437  */
1438 //----------------------------------------------------------------------
1439
1440 void 
1441 SALOME_NamingService::_create_context_name_dir(char** resultat_resolve_Path
1442                                                ,int length_copy,
1443                                                CosNaming::Name& _context_name) 
1444 {
1445   //MESSAGE("BEGIN OF _create_context_name_dir");
1446   for (int i = 0 ; i < length_copy;i++)
1447     {
1448       _context_name[i].id = CORBA::string_dup(resultat_resolve_Path[i]);
1449       _context_name[i].kind = CORBA::string_dup("dir");
1450       //SCRUTE(_context_name[i].id);
1451     }
1452 }
1453
1454 //----------------------------------------------------------------------
1455 /*! Function : _current_directory.
1456  * Purpose  : method to parse the naming service tree to find a context
1457  *            and determine the path to go to this context from the 
1458  *            _root_context.
1459  *  \param result_path char** arguments
1460  *  \param length_result int arguments by value
1461  *  \param context_to_found CosNaming::NamingContext_var arguments
1462  *  \param _continue boolean arguments
1463  */
1464 //----------------------------------------------------------------------
1465
1466 void
1467 SALOME_NamingService::_current_directory(char** result_path,
1468                                          int& length_result,
1469                                          CosNaming::NamingContext_var context_to_found,
1470                                          CORBA::Boolean& _continue) 
1471 {
1472   //MESSAGE("BEGIN OF _current_Directory");  
1473   CosNaming::BindingList_var _binding_list;
1474   CosNaming::BindingIterator_var _binding_iterator;
1475   unsigned long nb=0 ; //for using only the BindingIterator 
1476                        // to access the bindings
1477   CosNaming::Binding_var _binding ;
1478   CosNaming::NamingContext_var _ref_context = _current_context;
1479   CosNaming::NamingContext_var _temp_context = _current_context;
1480  
1481   _current_context->list(nb, _binding_list, _binding_iterator) ;
1482   if ( !_binding_iterator->_is_nil() ) {
1483   while ((_binding_iterator->next_one(_binding)) && _continue) {
1484     CosNaming::Name _bindingName = _binding->binding_name;
1485     if (_binding->binding_type == CosNaming::ncontext)
1486       {
1487         // We work on a directory, the search should be done in this directory
1488  
1489         result_path[length_result] = new char(strlen(_bindingName[0].id) + 1);
1490         strcpy(result_path[length_result],_bindingName[0].id);
1491         //SCRUTE(result_path[length_result])
1492           length_result++;
1493
1494         CORBA::Object_var  _obj =_current_context->resolve(_bindingName);
1495         _temp_context = CosNaming::NamingContext::_narrow(_obj);
1496
1497         if (_temp_context->_is_equivalent(context_to_found)) 
1498           {
1499             //MESSAGE("The context is found, we stop the search");
1500             _continue = false; 
1501             //SCRUTE(_continue);
1502           }
1503         if(_continue)
1504           {
1505             //SCRUTE(_bindingName[0].id);
1506             Change_Directory(_bindingName[0].id);
1507             _current_directory(result_path,length_result,
1508                                context_to_found, _continue );
1509             if (_continue)
1510               {
1511                 // We'll go back to the initial context
1512                 _current_context = _ref_context ;
1513                 //MESSAGE("Just before the delete of ")
1514                 //SCRUTE(result_path[length_result-1]);
1515                 delete result_path[length_result-1];
1516                 length_result--;
1517               }
1518           }
1519       }
1520   }
1521   _binding_iterator->destroy();  
1522   }     
1523   // We go to the last directory where an occurence was found
1524   _current_context = _ref_context ; 
1525 }
1526
1527
1528 //----------------------------------------------------------------------
1529 /*! Function :_list_directory_recurs.
1530  * Purpose  : method to list recursively all the objects contained in the tree of absCurDirectory/relativeSubDir.
1531  *  \param myList The list that will be filled.
1532  *  \param relativeSubDir The directory from absCurDirectory in which the objects are found.
1533  *  \param absCurDirectory The directory in ABSOLUTE form.
1534  *  _current_context must refer to absCurDirectory.
1535  */
1536 //----------------------------------------------------------------------
1537 void SALOME_NamingService::_list_directory_recurs(vector<string>& myList, const char *relativeSubDir,const char *absCurDirectory)
1538 {
1539   CosNaming::BindingList_var _binding_list;
1540   CosNaming::BindingIterator_var _binding_iterator;
1541   unsigned long nb=0 ; // for using only the BindingIterator to access the bindings
1542   CosNaming::Binding_var _binding ;
1543   char *absDir;
1544
1545   CosNaming::NamingContext_var _ref_context = _current_context;
1546   if(relativeSubDir)
1547     {
1548       Change_Directory(relativeSubDir);
1549       absDir=new char[strlen(absCurDirectory)+2+strlen(relativeSubDir)];
1550       strcpy(absDir,absCurDirectory);
1551       strcat(absDir,relativeSubDir);
1552       strcat(absDir,"/");
1553     }
1554   else
1555     absDir=(char *)absCurDirectory;
1556   _current_context->list(nb, _binding_list, _binding_iterator) ;
1557
1558   while (_binding_iterator->next_one(_binding)) {
1559     CosNaming::Name _bindingName = _binding->binding_name;
1560     if (_binding->binding_type == CosNaming::ncontext) {
1561       _list_directory_recurs(myList,_bindingName[0].id,absDir);
1562     }
1563     else if (_binding->binding_type == CosNaming::nobject) {
1564       char *elt=new char[strlen(absDir)+2+strlen(_bindingName[0].id)];
1565       strcpy(elt,absDir);
1566       strcat(elt,_bindingName[0].id);
1567       myList.push_back(elt);
1568       delete [] elt;
1569     }
1570   }
1571   if(relativeSubDir)
1572     {
1573       _current_context = _ref_context ;
1574       delete [] absDir;
1575     }
1576
1577   _binding_iterator->destroy();
1578 }
1579
1580 //----------------------------------------------------------------------
1581
1582 char * SALOME_NamingService::getIORaddr()
1583 {
1584    return _orb->object_to_string(_root_context);
1585 }
1586