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