1 // SALOME NamingService : wrapping NamingService services
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOME_NamingService.cxx
25 // Author : Estelle Deville
29 #include "SALOME_NamingService.hxx"
30 #include "ServiceUnreachable.hxx"
32 #include "utilities.h"
39 //----------------------------------------------------------------------
40 /*! Function : SALOME_NamingService
41 * Purpose : Constructor and Initialisation of _root_context
43 //----------------------------------------------------------------------
45 SALOME_NamingService::SALOME_NamingService()
47 MESSAGE("SALOME_NamingService default constructor");
48 _orb = CORBA::ORB::_nil();
51 //----------------------------------------------------------------------
52 /*! Function : SALOME_NamingService
53 * \param orb CORBA::ORB_ptr arguments
55 //----------------------------------------------------------------------
57 SALOME_NamingService::SALOME_NamingService(CORBA::ORB_ptr orb)
59 MESSAGE("SALOME_NamingService creation");
61 _initialize_root_context();
64 //----------------------------------------------------------------------
65 /*! Function : ~SALOME_NamingService
66 * Purpose : Destructor
68 //----------------------------------------------------------------------
70 SALOME_NamingService::~SALOME_NamingService()
72 MESSAGE("SALOME_NamingService destruction");
75 //----------------------------------------------------------------------
76 /*! Function : init_orb
77 * initialize ORB reference after default constructor
79 //----------------------------------------------------------------------
81 void SALOME_NamingService::init_orb(CORBA::ORB_ptr orb)
83 MESSAGE("SALOME_NamingService initialisation");
84 Utils_Locker lock(&_myMutex);
86 _initialize_root_context();
90 //----------------------------------------------------------------------
91 /*! Function : Register
92 * Method to create an association in the NamingService between ObjRef
94 * If the NamingService is out, the exception ServiceUnreachable is thrown
95 * \param ObjRef CORBA::Object_ptr arguments
96 * \param Path const char* arguments
98 //----------------------------------------------------------------------
100 void SALOME_NamingService::Register(CORBA::Object_ptr ObjRef,
102 throw(ServiceUnreachable)
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];
109 // _current_context is replaced to the _root_context
110 // if the Path begins whith '/'
112 _current_context = _root_context;
113 // MESSAGE("Gone to the _root_context");
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);
121 CosNaming::Name _context_name;
122 CORBA::Boolean _not_exist = false ;
123 CosNaming::NamingContext_var _temp_context;
125 if(dimension_resultat>1)
127 // A directory is treated (not only an object name)
128 // We had to test if the directory where ObjRef should be recorded
130 // If not, the new context has to be created
132 // MESSAGE("A complet Path has to be treated, not only an object name");
133 _context_name.length(dimension_resultat-1);
135 _create_context_name_dir(resultat_resolve_Path,dimension_resultat-1,
140 CORBA::Object_var _obj = _current_context->resolve(_context_name);
141 _current_context = CosNaming::NamingContext::_narrow(_obj);
143 catch (CosNaming::NamingContext::NotFound &)
145 // failed to resolve, therefore assume cold start
148 catch (CosNaming::NamingContext::InvalidName &)
150 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
152 catch (CosNaming::NamingContext::CannotProceed &)
154 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
156 catch(CORBA::SystemException&)
158 INFOS("!!!Register() : CORBA::SystemException : unable to contact"
159 << " the naming service");
160 throw ServiceUnreachable();
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++)
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.
178 // this context is already created.
179 // Nothing to be done
180 CORBA::Object_var _obj =
181 _current_context->resolve(_context_name);
183 CosNaming::NamingContext::_narrow(_obj);
184 //MESSAGE("This context was already created");
186 catch (CosNaming::NamingContext::NotFound &)
188 // This context is not created. It will be done
190 _current_context->bind_new_context(_context_name);
191 _current_context = _temp_context;
192 //INFOS("This context was'nt created, it's now done");
196 catch (CosNaming::NamingContext::AlreadyBound&)
198 INFOS("!!!Register() : CosNaming::NamingContext::AlreadyBound");
200 catch(CosNaming::NamingContext::NotFound& ex)
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");
215 catch(CosNaming::NamingContext::CannotProceed&)
217 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
219 catch(CosNaming::NamingContext::InvalidName&)
221 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
223 catch(CORBA::SystemException&)
225 INFOS("!!!Register() :CORBA::SystemException : unable to contact"
226 << " the naming service");
227 throw ServiceUnreachable();
232 // The current directory is now the directory where the object should
234 _context_name.length(1);
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);
243 _current_context->bind(_context_name, ObjRef);
244 // MESSAGE("A new element " << _context_name[0].id
245 // << " is recorded in the _current_context");
247 catch(CosNaming::NamingContext::NotFound& ex)
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");
262 catch(CosNaming::NamingContext::CannotProceed&)
264 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
266 catch(CosNaming::NamingContext::InvalidName&)
268 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
270 catch(CosNaming::NamingContext::AlreadyBound&)
272 INFOS("!!!Register() : CosNaming::NamingContext::AlreadyBound, object will be rebind");
273 _current_context->rebind(_context_name, ObjRef);
275 catch(CORBA::SystemException&)
277 INFOS("!!!Register() :CORBA::SystemException : unable to contact"
278 << " the naming service");
279 throw ServiceUnreachable();
283 // Memory destruction
284 for (int i = 0 ; i <dimension_resultat ;i++)
286 delete [] resultat_resolve_Path[i];
288 delete[] resultat_resolve_Path ;
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
298 //----------------------------------------------------------------------
300 CORBA::Object_ptr SALOME_NamingService::Resolve(const char* Path)
301 throw(ServiceUnreachable)
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];
308 // _current_context is replaced to the _root_context
309 // if the Path begins whith '/'
310 if (Path[0]=='/') _current_context = _root_context;
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);
318 CosNaming::Name _context_name;
319 _context_name.length(dimension_resultat);
320 CORBA::Object_ptr _obj = NULL ;
322 _create_context_name_dir(resultat_resolve_Path,dimension_resultat-1,
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));
333 _obj =_current_context->resolve(_context_name);
335 catch(CosNaming::NamingContext::NotFound& ex)
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)
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");
350 catch(CosNaming::NamingContext::CannotProceed&)
352 INFOS("!!!Resolve() : CosNaming::NamingContext::CannotProceed");
354 catch(CosNaming::NamingContext::InvalidName&)
356 INFOS("!!!Resolve() : CosNaming::NamingContext::InvalidName");
358 catch(CORBA::SystemException&)
360 INFOS("!!!Resolve() :CORBA::SystemException : unable to contact"
361 << "the naming service");
362 throw ServiceUnreachable();
364 // Memory destruction
365 for (int i = 0 ; i <dimension_resultat ;i++)
367 delete [] resultat_resolve_Path[i];
369 delete[] resultat_resolve_Path ;
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
382 //----------------------------------------------------------------------
384 CORBA::Object_ptr SALOME_NamingService::ResolveFirst(const char* Path)
385 throw(ServiceUnreachable)
387 MESSAGE("ResolveFirst");
388 Utils_Locker lock(&_myMutex);
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
396 basePath = thePath.substr(0,idx);
397 name = thePath.substr(idx+1);
401 CORBA::Object_ptr obj = CORBA::Object::_nil();
402 bool isOk = Change_Directory(basePath.c_str());
405 vector<string> listElem = list_directory();
406 vector<string>::iterator its = listElem.begin();
407 while (its != listElem.end())
410 if ((*its).find(name) == 0)
412 //string instance = basePath + "/" + *its;
413 return Resolve((*its).c_str());
421 //----------------------------------------------------------------------
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
432 //----------------------------------------------------------------------
434 int SALOME_NamingService::Find(const char* name)
435 throw(ServiceUnreachable)
437 MESSAGE("BEGIN OF Find " << name);
438 Utils_Locker lock(&_myMutex);
439 CORBA::Long occurence_number = 0 ;
442 _Find(name,occurence_number);
444 catch(CORBA::SystemException&)
446 INFOS("!!!Find() : CORBA::SystemException : unable to contact"
447 << " the naming service");
448 throw ServiceUnreachable();
450 return occurence_number;
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
460 //----------------------------------------------------------------------
462 bool SALOME_NamingService::Create_Directory(const char* Path)
463 throw(ServiceUnreachable)
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 ;
471 // _current_context is replaced to the _root_context
472 // if the Path begins whith '/'
473 if (Path[0]=='/') _current_context = _root_context;
475 int dimension_resultat = 0;
476 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
479 // We had to test if a part of the directory to treat
481 // If not, the new context has to be created
483 CosNaming::Name _context_name;
484 _context_name.length(1);
485 CosNaming::NamingContext_var _temp_context;
486 ASSERT(!CORBA::is_nil(_current_context));
491 for (int i = 0 ; i <dimension_resultat ;i++)
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.
501 // this context is already created.
502 // Nothing to be done
503 CORBA::Object_var _obj =
504 _current_context->resolve(_context_name);
506 CosNaming::NamingContext::_narrow(_obj);
507 MESSAGE("This context was already created");
509 catch (CosNaming::NamingContext::NotFound &)
511 // This context is not created. It will be done
513 _current_context->bind_new_context(_context_name);
514 _current_context = _temp_context;
515 INFOS("This context was'nt created, it's now done");
519 catch (CosNaming::NamingContext::AlreadyBound&)
521 INFOS("!!! Create_Directory() CosNaming::NamingContext::AlreadyBound");
522 _return_code = false;
524 catch(CosNaming::NamingContext::NotFound& ex)
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");
540 catch(CosNaming::NamingContext::CannotProceed&)
542 _return_code = false;
543 INFOS("!!!Create_Directory():CosNaming::NamingContext::CannotProceed");
545 catch(CosNaming::NamingContext::InvalidName&)
547 _return_code = false;
548 INFOS("!!!Create_Directory():CosNaming::NamingContext::InvalidName");
550 catch(CORBA::SystemException&)
552 _return_code = false;
553 INFOS("!!!Register() :CORBA::SystemException : unable to contact"
554 << " the naming service");
555 throw ServiceUnreachable();
557 // Memory destruction
558 for (int i = 0 ; i <dimension_resultat;i++)
560 delete [] resultat_resolve_Path[i];
562 delete[] resultat_resolve_Path ;
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
575 //----------------------------------------------------------------------
577 bool SALOME_NamingService::Change_Directory(const char* Path)
578 throw(ServiceUnreachable)
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 ;
586 // _current_context is replaced to the _root_context
587 // if the Path begins whith '/'
588 if (Path[0]=='/') _current_context = _root_context;
590 if ((Path[0]=='/') && (dimension_Path == 2))
592 MESSAGE("Change_Directory is called to go to the root_context");
594 //nothing to de done, the change_dur is called to go to the root_context
595 // no Path to resolve
597 //the resolution of the directory path has to be done
598 //to place the currect_context to the correct node
600 int dimension_resultat = 0;
601 _result_resolve_Path(Path,dimension_resultat,resultat_resolve_Path);
603 CosNaming::Name _context_name;
604 _context_name.length(dimension_resultat);
605 CORBA::Object_var _obj;
607 _create_context_name_dir(resultat_resolve_Path,dimension_resultat,
610 ASSERT(!CORBA::is_nil(_current_context));
614 _obj =_current_context->resolve(_context_name);
615 _current_context = CosNaming::NamingContext::_narrow(_obj);
616 ASSERT(!CORBA::is_nil(_current_context))
618 catch(CosNaming::NamingContext::NotFound& ex)
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" )
634 catch(CosNaming::NamingContext::CannotProceed&)
636 _return_code = false;
637 INFOS( "!!!Change_Directory() : CosNaming::NamingContext::CannotProceed" )
639 catch(CosNaming::NamingContext::InvalidName&)
641 _return_code = false;
642 INFOS( "!!!Change_Directory() : CosNaming::NamingContext::InvalidName" )
644 catch(CORBA::SystemException&)
646 _return_code = false;
647 INFOS( "!!!Change_Directory() :CORBA::SystemException : unable to contact"
648 << "the naming service")
649 throw ServiceUnreachable();
651 // Memory destruction
652 for (int i = 0 ; i <dimension_resultat ;i++)
654 delete [] resultat_resolve_Path[i];
656 delete[] resultat_resolve_Path ;
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
668 //----------------------------------------------------------------------
670 char* SALOME_NamingService::Current_Directory()
671 throw(ServiceUnreachable)
673 MESSAGE("BEGIN OF Current_Directory");
674 Utils_Locker lock(&_myMutex);
676 CosNaming::NamingContext_var _ref_context = _current_context;
680 char** result_path = new char*[50]; // 50 is it enough?
683 // We go to the root_context to begin the search from the root
684 _current_context = _root_context ;
685 CORBA::Boolean _continue = true ;
688 _current_directory(result_path,i,_ref_context,_continue );
690 catch(CORBA::SystemException&)
692 INFOS("!!!Current_Directory(): CORBA::SystemException : unable to contact"
693 << " the naming service" )
694 throw ServiceUnreachable();
696 for (int k = 0 ; k <i ;k++)
698 // We count the length of the char* + 1 for the '/' to separate
700 length_path = length_path + strlen(result_path[k]) + 1;
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++)
707 //SCRUTE(result_path[k])
708 strcat(return_Path,result_path[k]);
709 strcat(return_Path,"/");
711 //SCRUTE(return_Path)
712 _current_context = _ref_context ;
717 //----------------------------------------------------------------------
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
723 //----------------------------------------------------------------------
725 void SALOME_NamingService::list()
726 throw(ServiceUnreachable)
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) ;
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 );
743 Change_Directory(_bindingName[0].id);
745 catch (ServiceUnreachable&)
747 INFOS( "!!!list(): ServiceUnreachable" )
748 throw ServiceUnreachable();
752 _current_context = _ref_context ;
754 else if (_binding->binding_type == CosNaming::nobject) {
755 MESSAGE( "Object : " << _bindingName[0].id );
758 _binding_iterator->destroy();
761 //----------------------------------------------------------------------
762 /*! Function : list_directory
763 * Purpose : method to get all the contexts contained in the current
765 * Get only objects, isn't iterative
766 * If the NamingService is out, the exception ServiceUnreachable is thrown
768 //----------------------------------------------------------------------
769 vector<string> SALOME_NamingService::list_directory()
770 throw(ServiceUnreachable)
772 vector<string> _list ;
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;
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));
788 //for (unsigned int ind = 0; ind < _list.size(); ind++)
789 // MESSAGE("list_directory : Object : " << _list[ind]);
791 _binding_iterator->destroy();
795 //----------------------------------------------------------------------
796 /*! Function : list_directory_recurs
797 * Purpose : method to get all the contexts contained in the current
799 * Get only objects and is recursive
800 * If the NamingService is out, the exception ServiceUnreachable is thrown
802 //----------------------------------------------------------------------
803 vector<string> SALOME_NamingService::list_directory_recurs()
804 throw(ServiceUnreachable)
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;
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
822 //----------------------------------------------------------------------
824 void SALOME_NamingService::Destroy_Name(const char* Path)
825 throw(ServiceUnreachable)
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];
832 // _current_context is replaced to the _root_context
833 // if the Path begins whith '/'
834 if (Path[0]=='/') _current_context = _root_context;
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);
842 CosNaming::Name _context_name;
843 if (dimension_resultat>1)
845 // We go in the directory where the object to destroy is
846 _context_name.length(dimension_resultat-1);
848 _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
852 CORBA::Object_var _obj = _current_context->resolve(_context_name);
853 _current_context = CosNaming::NamingContext::_narrow(_obj);
855 catch (CosNaming::NamingContext::NotFound& ex)
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" )
870 catch (CosNaming::NamingContext::InvalidName &)
872 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName" )
874 catch (CosNaming::NamingContext::CannotProceed &)
876 INFOS( "!!!Destroy_Name(): CosNaming::NamingContext::CannotProceed" )
878 catch(CORBA::SystemException&)
880 INFOS( "!!!Destroy_Name() : CORBA::SystemException : unable to contact"
881 << " the naming service")
882 throw ServiceUnreachable();
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
896 _current_context->unbind(_context_name);
897 MESSAGE( "The object " << _context_name[0].id << " has been deleted" )
899 catch(CosNaming::NamingContext::NotFound& ex)
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" )
914 catch(CosNaming::NamingContext::CannotProceed&)
916 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::CannotProceed")
918 catch(CosNaming::NamingContext::InvalidName&)
920 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName")
922 catch(CORBA::SystemException&)
924 INFOS( "!!!Destroy_Name() :CORBA::SystemException : unable to contact"
925 << " the naming service")
926 throw ServiceUnreachable();
928 // Memory destruction
929 for (int i = 0 ; i <dimension_resultat ;i++)
931 delete [] resultat_resolve_Path[i];
933 delete[] resultat_resolve_Path ;
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
944 //----------------------------------------------------------------------
946 void SALOME_NamingService::Destroy_Directory(const char* Path)
947 throw(ServiceUnreachable)
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];
954 // _current_context is replaced to the _root_context
955 // if the Path begins whith '/'
956 if (Path[0]=='/') _current_context = _root_context;
958 CosNaming::NamingContext_var _ref_context = _current_context;
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);
965 CosNaming::Name _context_name;
966 if (dimension_resultat>1)
968 // We go in the directory where the context to destroy is
969 _context_name.length(dimension_resultat-1);
971 _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
975 CORBA::Object_var _obj = _current_context->resolve(_context_name);
976 _current_context = CosNaming::NamingContext::_narrow(_obj);
977 _ref_context = _current_context ;
979 catch (CosNaming::NamingContext::NotFound& ex)
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" )
994 catch (CosNaming::NamingContext::InvalidName &)
996 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
998 catch (CosNaming::NamingContext::CannotProceed &)
1000 INFOS("!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
1002 catch(CORBA::SystemException&)
1004 INFOS( "!!!Destroy_Directory() : CORBA::SystemException : unable to contact"
1005 << " the naming service" )
1006 throw ServiceUnreachable();
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);
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);
1023 catch (CosNaming::NamingContext::NotFound& ex)
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" )
1038 catch (CosNaming::NamingContext::InvalidName &)
1040 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
1042 catch (CosNaming::NamingContext::CannotProceed &)
1044 INFOS( "!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
1046 catch(CORBA::SystemException&)
1048 INFOS( "!!!Destroy_Directory() : CORBA::SystemException : unable to contact"
1049 << " the naming service" )
1050 throw ServiceUnreachable();
1053 ASSERT(!CORBA::is_nil(_current_context));
1054 // Context Destruction
1057 _current_context->destroy();
1058 MESSAGE( "The context " << _context_name[0].id << " has been deleted" )
1060 catch(CosNaming::NamingContext::NotEmpty&)
1062 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::NoEmpty "
1063 << Path << " is not empty" )
1065 catch(CORBA::SystemException&)
1067 INFOS( "!!!Destroy_Directory() :CORBA::SystemException : "
1068 << "unable to contact the naming service")
1069 throw ServiceUnreachable();
1071 // We go to the directory just before the context to delete
1072 _current_context = _ref_context ;
1075 _current_context->unbind(_context_name);
1076 MESSAGE( "The bind to the context " << _context_name[0].id << " has been deleted" )
1078 catch(CosNaming::NamingContext::NotFound& ex)
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" )
1093 catch(CosNaming::NamingContext::CannotProceed&)
1095 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::CannotProceed")
1097 catch(CosNaming::NamingContext::InvalidName&)
1099 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName")
1101 catch(CORBA::SystemException&)
1103 INFOS( "!!!Destroy_Directory() :CORBA::SystemException : unable to contact"
1104 << " the naming service")
1105 throw ServiceUnreachable();
1107 // Memory destruction
1108 for (int i = 0 ; i <dimension_resultat ;i++)
1110 delete [] resultat_resolve_Path[i];
1112 delete[] resultat_resolve_Path ;
1115 //----------------------------------------------------------------------
1116 /*! Function : _initialize_root_context
1117 * Purpose : method called by constructor to initialize _root_context
1119 //----------------------------------------------------------------------
1121 void SALOME_NamingService::_initialize_root_context()
1123 //MESSAGE("Get the root context");
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));
1132 catch(CORBA::SystemException&)
1134 INFOS("CORBA::SystemException: unable to contact the naming service");
1135 throw ServiceUnreachable();
1139 INFOS("Unknown Exception: unable to contact the naming service");
1140 throw ServiceUnreachable();
1144 //----------------------------------------------------------------------
1145 /*! Function : _resolve_Path
1146 * Purpose : method to decompose a Path : /Kernel/Services/Sessions.
1148 * \return a char* containing the first char between '/' (in this case Kernel)
1150 //----------------------------------------------------------------------
1152 char* SALOME_NamingService::_resolve_Path(char* Path)
1155 int length = strlen(Path);
1158 if (length==0) return NULL;
1161 while ((i<length) && (Path[i]!='/'))
1163 resultat = new char[i+1];
1164 strncpy(resultat,Path,i);
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
1178 //----------------------------------------------------------------------
1181 SALOME_NamingService::_result_resolve_Path(const char* Path,
1183 char ** resultat_resolve_Path)
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];
1190 temp[j] = new char[dimension_Path];
1191 strcpy(temp[j],Path);
1193 while (strlen(temp[j])>0)
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]=='/')
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';
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]);
1216 // decomposition of the Path
1217 resultat_resolve_Path[j]= _resolve_Path(tempslash[j]);
1218 //SCRUTE(resultat_resolve_Path[j]);
1220 int length_resultat = strlen(resultat_resolve_Path[j]) ;
1221 int dimension_temp = length_temp -length_resultat ;
1223 temp[j] = new char[dimension_temp +1];
1224 for (int i = 0 ; i <dimension_temp ;i++)
1226 temp[j][i] =tempslash[j-1][i+ length_resultat];
1228 temp[j][dimension_temp]= '\0';
1231 // Memory destruction
1232 for (int i = 0 ; i <j;i++)
1235 delete [] tempslash[i];
1238 delete [] tempslash ;
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)
1250 //----------------------------------------------------------------------
1252 void SALOME_NamingService::_Find(const char* name,
1253 CORBA::Long& occurence_number)
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;
1264 _current_context->list(nb, _binding_list, _binding_iterator) ;
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 ;
1275 else if (_binding->binding_type == CosNaming::nobject) {
1276 // We work on an object...
1277 if (!strcmp( _bindingName[0].id,name))
1279 //MESSAGE("One occurence was found");
1281 // We keep in memory the directory where one occurence was found
1282 _found_context = _current_context ;
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);
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
1298 * \param resultat_resolve_Path char** arguments
1299 * \param length_copy int arguments
1300 * \param _context_name CosNaming::Name arguments (by value)
1302 //----------------------------------------------------------------------
1305 SALOME_NamingService::_create_context_name_dir(char** resultat_resolve_Path
1307 CosNaming::Name& _context_name)
1309 //MESSAGE("BEGIN OF _create_context_name_dir");
1310 for (int i = 0 ; i < length_copy;i++)
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);
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
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
1328 //----------------------------------------------------------------------
1331 SALOME_NamingService::_current_directory(char** result_path,
1333 CosNaming::NamingContext_var context_to_found,
1334 CORBA::Boolean& _continue)
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;
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)
1351 // We work on a directory, the search should be done in this directory
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])
1358 CORBA::Object_var _obj =_current_context->resolve(_bindingName);
1359 _temp_context = CosNaming::NamingContext::_narrow(_obj);
1361 if (_temp_context->_is_equivalent(context_to_found))
1363 //MESSAGE("The context is found, we stop the search");
1365 //SCRUTE(_continue);
1369 //SCRUTE(_bindingName[0].id);
1370 Change_Directory(_bindingName[0].id);
1371 _current_directory(result_path,length_result,
1372 context_to_found, _continue );
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];
1385 _binding_iterator->destroy();
1387 // We go to the last directory where an occurence was found
1388 _current_context = _ref_context ;
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.
1400 //----------------------------------------------------------------------
1401 void SALOME_NamingService::_list_directory_recurs(vector<string>& myList, const char *relativeSubDir,const char *absCurDirectory)
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 ;
1409 CosNaming::NamingContext_var _ref_context = _current_context;
1412 Change_Directory(relativeSubDir);
1413 absDir=new char[strlen(absCurDirectory)+2+strlen(relativeSubDir)];
1414 strcpy(absDir,absCurDirectory);
1415 strcat(absDir,relativeSubDir);
1419 absDir=(char *)absCurDirectory;
1420 _current_context->list(nb, _binding_list, _binding_iterator) ;
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);
1427 else if (_binding->binding_type == CosNaming::nobject) {
1428 char *elt=new char[strlen(absDir)+2+strlen(_bindingName[0].id)];
1430 strcat(elt,_bindingName[0].id);
1431 myList.push_back(elt);
1437 _current_context = _ref_context ;
1441 _binding_iterator->destroy();
1444 //----------------------------------------------------------------------
1446 char * SALOME_NamingService::getIORaddr()
1448 return _orb->object_to_string(_root_context);