2 // File: SALOME_NamingService.cxx
3 // Created: Tue June 12 2001
4 // Author: Estelle Deville
6 // Copyright : CEA/DEN/DMSS/LGLS
9 //----------------------------------------------------------------------
10 #include "SALOME_NamingService.hxx"
11 #include "ServiceUnreachable.hxx"
15 //----------------------------------------------------------------------
16 /*! Function : SALOME_NamingService
17 * Purpose : Constructor and Initialisation of _root_context
19 //----------------------------------------------------------------------
21 SALOME_NamingService::SALOME_NamingService()
23 MESSAGE("SALOME_NamingService default constructor");
24 _orb = CORBA::ORB::_nil();
27 //----------------------------------------------------------------------
28 /*! Function : SALOME_NamingService
29 * \param orb CORBA::ORB_ptr arguments
31 //----------------------------------------------------------------------
33 SALOME_NamingService::SALOME_NamingService(CORBA::ORB_ptr orb)
35 MESSAGE("SALOME_NamingService creation");
37 _initialize_root_context();
40 //----------------------------------------------------------------------
41 /*! Function : ~SALOME_NamingService
42 * Purpose : Destructor
44 //----------------------------------------------------------------------
46 SALOME_NamingService::~SALOME_NamingService()
48 MESSAGE("SALOME_NamingService destruction");
51 //----------------------------------------------------------------------
52 /*! Function : init_orb
53 * initialize ORB reference after default constructor
55 //----------------------------------------------------------------------
57 void SALOME_NamingService::init_orb(CORBA::ORB_ptr orb)
59 // MESSAGE("SALOME_NamingService initialisation");
61 _initialize_root_context();
65 //----------------------------------------------------------------------
66 /*! Function : Register
67 * Method to create an association in the NamingService between ObjRef
69 * If the NamingService is out, the exception ServiceUnreachable is thrown
70 * \param ObjRef CORBA::Object_ptr arguments
71 * \param Path const char* arguments
73 //----------------------------------------------------------------------
75 void SALOME_NamingService::Register(CORBA::Object_ptr ObjRef,
77 throw(ServiceUnreachable)
79 // MESSAGE("BEGIN OF Register: "<< Path);
80 int dimension_Path = strlen(Path) + 1;
81 char** resultat_resolve_Path = new char* [dimension_Path];
83 // _current_context is replaced to the _root_context
84 // if the Path begins whith '/'
86 _current_context = _root_context;
87 // MESSAGE("Gone to the _root_context");
90 //the resolution of the directory path has to be done
91 //to place the currect_context to the correct node
92 int dimension_resultat = 0;
93 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
95 CosNaming::Name _context_name;
96 CORBA::Boolean _not_exist = false ;
97 CosNaming::NamingContext_var _temp_context;
99 if(dimension_resultat>1)
101 // A directory is treated (not only an object name)
102 // We had to test if the directory where ObjRef should be recorded
104 // If not, the new context has to be created
106 // MESSAGE("A complet Path has to be treated, not only an object name");
107 _context_name.length(dimension_resultat-1);
109 _create_context_name_dir(resultat_resolve_Path,dimension_resultat-1,
114 CORBA::Object_var _obj = _current_context->resolve(_context_name);
115 _current_context = CosNaming::NamingContext::_narrow(_obj);
117 catch (CosNaming::NamingContext::NotFound &)
119 // failed to resolve, therefore assume cold start
122 catch (CosNaming::NamingContext::InvalidName &)
124 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
126 catch (CosNaming::NamingContext::CannotProceed &)
128 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
130 catch(CORBA::COMM_FAILURE&)
132 INFOS("!!!Register() : CORBA::COMM_FAILURE : unable to contact"
133 << " the naming service");
134 throw ServiceUnreachable();
140 _context_name.length(1);
141 // MESSAGE("The Path indicated is not yet created. It will soon be done");
142 for (int i = 0 ; i <dimension_resultat -1 ;i++)
144 _context_name[0].id =
145 CORBA::string_dup(resultat_resolve_Path[i]);
146 _context_name[0].kind = CORBA::string_dup("dir");
147 // SCRUTE(_context_name[0].id);
148 //The Path could be in part already created.
152 // this context is already created.
153 // Nothing to be done
154 CORBA::Object_var _obj =
155 _current_context->resolve(_context_name);
157 CosNaming::NamingContext::_narrow(_obj);
158 //MESSAGE("This context was already created");
160 catch (CosNaming::NamingContext::NotFound &)
162 // This context is not created. It will be done
164 _current_context->bind_new_context(_context_name);
165 _current_context = _temp_context;
166 //MESSAGE("This context was'nt created, it's now done");
170 catch (CosNaming::NamingContext::AlreadyBound&)
172 INFOS("!!!Register() : CosNaming::NamingContext::AlreadyBound");
174 catch(CosNaming::NamingContext::NotFound& ex)
176 CosNaming::Name n = ex.rest_of_name;
177 if (ex.why == CosNaming::NamingContext::missing_node)
178 INFOS("Register() : " << (char *) n[0].id
179 << " (" << (char *) n[0].kind << ") not found");
180 if (ex.why == CosNaming::NamingContext::not_context)
181 INFOS("Register() : " << (char *) n[0].id
182 << " (" << (char *) n[0].kind
183 << ") is not a context");
184 if (ex.why == CosNaming::NamingContext::not_object)
185 INFOS("Register() : " << (char *) n[0].id
186 << " (" << (char *) n[0].kind
187 << ") is not an object");
189 catch(CosNaming::NamingContext::CannotProceed&)
191 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
193 catch(CosNaming::NamingContext::InvalidName&)
195 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
197 catch(CORBA::COMM_FAILURE&)
199 INFOS("!!!Register() :CORBA::COMM_FAILURE : unable to contact"
200 << " the naming service");
201 throw ServiceUnreachable();
206 // The current directory is now the directory where the object should
208 _context_name.length(1);
211 // the last element is an object an not a directory
212 _context_name[0].id =
213 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
214 _context_name[0].kind = CORBA::string_dup("object");
215 //SCRUTE(_context_name[0].id);
217 _current_context->bind(_context_name, ObjRef);
218 // MESSAGE("A new element " << _context_name[0].id
219 // << " is recorded in the _current_context");
221 catch(CosNaming::NamingContext::NotFound& ex)
223 CosNaming::Name n = ex.rest_of_name;
224 if (ex.why == CosNaming::NamingContext::missing_node)
225 INFOS("Register() : " << (char *) n[0].id
226 << " (" << (char *) n[0].kind << ") not found");
227 if (ex.why == CosNaming::NamingContext::not_context)
228 INFOS("Register() : " << (char *) n[0].id
229 << " (" << (char *) n[0].kind
230 << ") is not a context");
231 if (ex.why == CosNaming::NamingContext::not_object)
232 INFOS("Register() : " << (char *) n[0].id
233 << " (" << (char *) n[0].kind
234 << ") is not an object");
236 catch(CosNaming::NamingContext::CannotProceed&)
238 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
240 catch(CosNaming::NamingContext::InvalidName&)
242 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
244 catch(CosNaming::NamingContext::AlreadyBound&)
246 INFOS("!!!Register() : CosNaming::NamingContext::AlreadyBound, object will be rebind");
247 _current_context->rebind(_context_name, ObjRef);
249 catch(CORBA::COMM_FAILURE&)
251 INFOS("!!!Register() :CORBA::COMM_FAILURE : unable to contact"
252 << " the naming service");
253 throw ServiceUnreachable();
257 // Memory destruction
258 for (int i = 0 ; i <dimension_resultat ;i++)
260 delete [] resultat_resolve_Path[i];
262 delete[] resultat_resolve_Path ;
265 //----------------------------------------------------------------------
266 /*! Function : Resolve
267 * Purpose : method to get the ObjRef of a symbolic name
268 * If the NamingService is out, the exception ServiceUnreachable is thrown
269 * \param Path const char* arguments
270 * \return the object reference
272 //----------------------------------------------------------------------
274 CORBA::Object_ptr SALOME_NamingService::Resolve(const char* Path)
275 throw(ServiceUnreachable)
277 //MESSAGE("BEGIN OF Resolve: " << Path);
278 int dimension_Path = strlen(Path) + 1;
279 char** resultat_resolve_Path = new char* [dimension_Path];
281 // _current_context is replaced to the _root_context
282 // if the Path begins whith '/'
283 if (Path[0]=='/') _current_context = _root_context;
286 //the resolution of the directory path has to be done
287 //to place the currect_context to the correct node
288 int dimension_resultat = 0;
289 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
291 CosNaming::Name _context_name;
292 _context_name.length(dimension_resultat);
293 CORBA::Object_ptr _obj = NULL ;
295 _create_context_name_dir(resultat_resolve_Path,dimension_resultat-1,
297 // the last element is an object an not a directory
298 _context_name[dimension_resultat -1].id =
299 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
300 _context_name[dimension_resultat -1].kind = CORBA::string_dup("object");
301 // SCRUTE(_context_name[dimension_resultat -1].id);
302 ASSERT(!CORBA::is_nil(_current_context));
306 _obj =_current_context->resolve(_context_name);
308 catch(CosNaming::NamingContext::NotFound& ex)
310 CosNaming::Name n = ex.rest_of_name;
311 if (ex.why == CosNaming::NamingContext::missing_node)
312 INFOS("Resolve() : " << (char *) n[0].id
313 << " (" << (char *) n[0].kind << ") not found");
314 if (ex.why == CosNaming::NamingContext::not_context)
316 << (char *) n[0].id << " (" << (char *) n[0].kind
317 << ") is not a context");
318 if (ex.why == CosNaming::NamingContext::not_object)
319 INFOS("Resolve() : " << (char *) n[0].id
320 << " (" << (char *) n[0].kind
321 << ") is not an object");
323 catch(CosNaming::NamingContext::CannotProceed&)
325 INFOS("!!!Resolve() : CosNaming::NamingContext::CannotProceed");
327 catch(CosNaming::NamingContext::InvalidName&)
329 INFOS("!!!Resolve() : CosNaming::NamingContext::InvalidName");
331 catch(CORBA::COMM_FAILURE&)
333 INFOS("!!!Resolve() :CORBA::COMM_FAILURE : unable to contact"
334 << "the naming service");
335 throw ServiceUnreachable();
337 // Memory destruction
338 for (int i = 0 ; i <dimension_resultat ;i++)
340 delete [] resultat_resolve_Path[i];
342 delete[] resultat_resolve_Path ;
347 //----------------------------------------------------------------------
349 * Purpose : method to research a name from the current directory
350 * of the naming service.
351 * The naming service changes directory to go to the directory where
352 * the last occurence was found.
353 * If the NamingService is out, the exception ServiceUnreachable is thrown
354 * \param name const char* arguments
355 * \return the number of occurences found
358 //----------------------------------------------------------------------
360 int SALOME_NamingService::Find(const char* name)
361 throw(ServiceUnreachable)
363 // MESSAGE("BEGIN OF Find " << name);
364 CORBA::Long occurence_number = 0 ;
367 _Find(name,occurence_number);
369 catch(CORBA::COMM_FAILURE&)
371 INFOS("!!!Find() : CORBA::COMM_FAILURE : unable to contact"
372 << " the naming service");
373 throw ServiceUnreachable();
375 return occurence_number;
378 //----------------------------------------------------------------------
379 /*! Function : Create_Directory
380 * Purpose : method to create a directory from the current directory.
381 * If the NamingService is out, the exception ServiceUnreachable is thrown
382 * \param Path const char* arguments
383 * \return a boolean to indicate if the creation succeeded
385 //----------------------------------------------------------------------
387 bool SALOME_NamingService::Create_Directory(const char* Path)
388 throw(ServiceUnreachable)
390 //MESSAGE("BEGIN OF Create_Directory");
391 int dimension_Path = strlen(Path) + 1;
392 char** resultat_resolve_Path= new char* [dimension_Path];;
393 CORBA::Boolean _return_code = true ;
395 // _current_context is replaced to the _root_context
396 // if the Path begins whith '/'
397 if (Path[0]=='/') _current_context = _root_context;
399 int dimension_resultat = 0;
400 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
403 // We had to test if a part of the directory to treat
405 // If not, the new context has to be created
407 CosNaming::Name _context_name;
408 _context_name.length(1);
409 CosNaming::NamingContext_var _temp_context;
410 ASSERT(!CORBA::is_nil(_current_context));
415 for (int i = 0 ; i <dimension_resultat ;i++)
417 _context_name[0].id =
418 CORBA::string_dup(resultat_resolve_Path[i]);
419 _context_name[0].kind = CORBA::string_dup("dir");
420 // SCRUTE(_context_name[0].id);
421 //The Path could be in part already created.
425 // this context is already created.
426 // Nothing to be done
427 CORBA::Object_var _obj =
428 _current_context->resolve(_context_name);
430 CosNaming::NamingContext::_narrow(_obj);
431 MESSAGE("This context was already created");
433 catch (CosNaming::NamingContext::NotFound &)
435 // This context is not created. It will be done
437 _current_context->bind_new_context(_context_name);
438 _current_context = _temp_context;
439 MESSAGE("This context was'nt created, it's now done");
443 catch (CosNaming::NamingContext::AlreadyBound&)
445 INFOS("!!! Create_Directory() CosNaming::NamingContext::AlreadyBound");
446 _return_code = false;
448 catch(CosNaming::NamingContext::NotFound& ex)
450 _return_code = false;
451 CosNaming::Name n = ex.rest_of_name;
452 if (ex.why == CosNaming::NamingContext::missing_node)
453 INFOS("Create_Directory() : " << (char *) n[0].id
454 << " (" << (char *) n[0].kind << ") not found");
455 if (ex.why == CosNaming::NamingContext::not_context)
456 INFOS("Create_Directory() : " << (char *) n[0].id
457 << " (" << (char *) n[0].kind
458 << ") is not a context");
459 if (ex.why == CosNaming::NamingContext::not_object)
460 INFOS("Create_Directory() : " << (char *) n[0].id
461 << " (" << (char *) n[0].kind
462 << ") is not an object");
464 catch(CosNaming::NamingContext::CannotProceed&)
466 _return_code = false;
467 INFOS("!!!Create_Directory():CosNaming::NamingContext::CannotProceed");
469 catch(CosNaming::NamingContext::InvalidName&)
471 _return_code = false;
472 INFOS("!!!Create_Directory():CosNaming::NamingContext::InvalidName");
474 catch(CORBA::COMM_FAILURE&)
476 _return_code = false;
477 INFOS("!!!Register() :CORBA::COMM_FAILURE : unable to contact"
478 << " the naming service");
479 throw ServiceUnreachable();
481 // Memory destruction
482 for (int i = 0 ; i <dimension_resultat;i++)
484 delete [] resultat_resolve_Path[i];
486 delete[] resultat_resolve_Path ;
490 //----------------------------------------------------------------------
491 /*! Function : Change_Directory
492 * Purpose : method to change the current directory to the
493 * directory Path indicated in "in" Parameter.
494 * If Path ="/", the current directory changes to the root directory.
495 * If the NamingService is out, the exception ServiceUnreachable is thrown.
496 * \param Path const char* arguments
497 * \return a boolean to indicate if the change succeeded
499 //----------------------------------------------------------------------
501 bool SALOME_NamingService::Change_Directory(const char* Path)
502 throw(ServiceUnreachable)
504 //MESSAGE("BEGIN OF Change_Directory " << Path);
505 int dimension_Path = strlen(Path) + 1;
506 char** resultat_resolve_Path = new char* [dimension_Path];
507 CORBA::Boolean _return_code = true ;
509 // _current_context is replaced to the _root_context
510 // if the Path begins whith '/'
511 if (Path[0]=='/') _current_context = _root_context;
513 if ((Path[0]=='/') && (dimension_Path == 2))
515 MESSAGE("Change_Directory is called to go to the root_context");
517 //nothing to de done, the change_dur is called to go to the root_context
518 // no Path to resolve
520 //the resolution of the directory path has to be done
521 //to place the currect_context to the correct node
523 int dimension_resultat = 0;
524 _result_resolve_Path(Path,dimension_resultat,resultat_resolve_Path);
526 CosNaming::Name _context_name;
527 _context_name.length(dimension_resultat);
528 CORBA::Object_var _obj;
530 _create_context_name_dir(resultat_resolve_Path,dimension_resultat,
533 ASSERT(!CORBA::is_nil(_current_context));
537 _obj =_current_context->resolve(_context_name);
538 _current_context = CosNaming::NamingContext::_narrow(_obj);
539 ASSERT(!CORBA::is_nil(_current_context))
541 catch(CosNaming::NamingContext::NotFound& ex)
543 _return_code = false;
544 CosNaming::Name n = ex.rest_of_name;
545 if (ex.why == CosNaming::NamingContext::missing_node)
546 MESSAGE( "Change_Directory() : " << (char *) n[0].id
547 << " (" << (char *) n[0].kind << ") not found")
548 if (ex.why == CosNaming::NamingContext::not_context)
549 MESSAGE("Change_Directory() : " << (char *) n[0].id
550 << " (" << (char *) n[0].kind
551 << ") is not a context" )
552 if (ex.why == CosNaming::NamingContext::not_object)
553 MESSAGE( "Change_Directory() : " << (char *) n[0].id
554 << " (" << (char *) n[0].kind
555 << ") is not an object" )
557 catch(CosNaming::NamingContext::CannotProceed&)
559 _return_code = false;
560 MESSAGE( "!!!Change_Directory() : CosNaming::NamingContext::CannotProceed" )
562 catch(CosNaming::NamingContext::InvalidName&)
564 _return_code = false;
565 MESSAGE( "!!!Change_Directory() : CosNaming::NamingContext::InvalidName" )
567 catch(CORBA::COMM_FAILURE&)
569 _return_code = false;
570 MESSAGE( "!!!Change_Directory() :CORBA::COMM_FAILURE : unable to contact"
571 << "the naming service")
572 throw ServiceUnreachable();
574 // Memory destruction
575 for (int i = 0 ; i <dimension_resultat ;i++)
577 delete [] resultat_resolve_Path[i];
579 delete[] resultat_resolve_Path ;
584 //----------------------------------------------------------------------
585 /*! Function : Current_Directory
586 * Purpose : method to get the current directory.
587 * If the NamingService is out, the exception ServiceUnreachable is thrown
588 * \return the path of the current_context
589 * \sa _current_directory
591 //----------------------------------------------------------------------
593 char* SALOME_NamingService::Current_Directory()
594 throw(ServiceUnreachable)
596 //MESSAGE("BEGIN OF Current_Directory");
598 CosNaming::NamingContext_var _ref_context = _current_context;
602 char** result_path = new char*[50]; // 50 is it enough?
605 // We go to the root_context to begin the search from the root
606 _current_context = _root_context ;
607 CORBA::Boolean _continue = true ;
610 _current_directory(result_path,i,_ref_context,_continue );
612 catch(CORBA::COMM_FAILURE&)
614 MESSAGE("!!!Current_Directory(): CORBA::COMM_FAILURE : unable to contact"
615 << " the naming service" )
616 throw ServiceUnreachable();
618 for (int k = 0 ; k <i ;k++)
620 // We count the length of the char* + 1 for the '/' to separate
622 length_path = length_path + strlen(result_path[k]) + 1;
624 char* return_Path = new char[length_path +1];
625 return_Path[0] = '/' ;
626 return_Path[1] = '\0' ;
627 for (int k = 0 ; k <i ;k++)
629 //SCRUTE(result_path[k])
630 strcat(return_Path,result_path[k]);
631 strcat(return_Path,"/");
633 //SCRUTE(return_Path)
634 _current_context = _ref_context ;
639 //----------------------------------------------------------------------
641 * Purpose : method to list and print all the context contained from
642 * the current context
643 * If the NamingService is out, the exception ServiceUnreachable is thrown
645 //----------------------------------------------------------------------
647 void SALOME_NamingService::list()
648 throw(ServiceUnreachable)
650 MESSAGE("Begin of list");
651 CosNaming::BindingList_var _binding_list;
652 CosNaming::BindingIterator_var _binding_iterator;
653 unsigned long nb=0 ; // for using only the BindingIterator to access the bindings
654 CosNaming::Binding_var _binding ;
655 CosNaming::NamingContext_var _ref_context = _current_context;
656 _current_context->list(nb, _binding_list, _binding_iterator) ;
658 while (_binding_iterator->next_one(_binding)) {
659 CosNaming::Name _bindingName = _binding->binding_name;
660 if (_binding->binding_type == CosNaming::ncontext) {
661 MESSAGE( "Context : " << _bindingName[0].id );
664 Change_Directory(_bindingName[0].id);
666 catch (ServiceUnreachable&)
668 MESSAGE( "!!!list(): ServiceUnreachable" )
669 throw ServiceUnreachable();
673 _current_context = _ref_context ;
675 else if (_binding->binding_type == CosNaming::nobject) {
676 MESSAGE( "Object : " << _bindingName[0].id );
679 _binding_iterator->destroy();
682 //----------------------------------------------------------------------
683 /*! Function : list_directory
684 * Purpose : method to get all the contexts contained in the current
686 * Get only objects, isn't iterative
687 * If the NamingService is out, the exception ServiceUnreachable is thrown
689 //----------------------------------------------------------------------
690 vector<string> SALOME_NamingService::list_directory()
691 throw(ServiceUnreachable)
693 vector<string> _list ;
695 CosNaming::BindingList_var _binding_list;
696 CosNaming::BindingIterator_var _binding_iterator;
697 unsigned long nb=0 ; // for using only the BindingIterator to access the bindings
698 CosNaming::Binding_var _binding ;
699 CosNaming::NamingContext_var _ref_context = _current_context;
700 _current_context->list(nb, _binding_list, _binding_iterator) ;
702 while (_binding_iterator->next_one(_binding)) {
703 CosNaming::Name _bindingName = _binding->binding_name;
704 if (_binding->binding_type == CosNaming::nobject) {
705 _list.push_back(CORBA::string_dup(_bindingName[0].id));
708 //for (unsigned int ind = 0; ind < _list.size(); ind++)
709 // MESSAGE("list_directory : Object : " << _list[ind]);
711 _binding_iterator->destroy();
716 //----------------------------------------------------------------------
717 /*! Function : Destroy_Name
718 * Purpose : method to destroy an association Path-Object Reference.
719 * WARNING : The complete Path should be given.
720 * If the NamingService is out, the exception ServiceUnreachable is thrown
721 * \param Path const char* arguments
723 //----------------------------------------------------------------------
725 void SALOME_NamingService::Destroy_Name(const char* Path)
726 throw(ServiceUnreachable)
728 MESSAGE("BEGIN OF Destroy_Name");
729 int dimension_Path = strlen(Path) + 1;
730 char** resultat_resolve_Path = new char* [dimension_Path];
732 // _current_context is replaced to the _root_context
733 // if the Path begins whith '/'
734 if (Path[0]=='/') _current_context = _root_context;
737 //the resolution of the directory path has to be done
738 //to place the currect_context to the correct node
739 int dimension_resultat = 0;
740 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
742 CosNaming::Name _context_name;
743 if (dimension_resultat>1)
745 // We go in the directory where the object to destroy is
746 _context_name.length(dimension_resultat-1);
748 _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
752 CORBA::Object_var _obj = _current_context->resolve(_context_name);
753 _current_context = CosNaming::NamingContext::_narrow(_obj);
755 catch (CosNaming::NamingContext::NotFound& ex)
757 CosNaming::Name n = ex.rest_of_name;
758 if (ex.why == CosNaming::NamingContext::missing_node)
759 MESSAGE( "Destroy_Name() : " << (char *) n[0].id
760 << " (" << (char *) n[0].kind << ") not found" )
761 if (ex.why == CosNaming::NamingContext::not_context)
762 MESSAGE( "Destroy_Name() : " << (char *) n[0].id
763 << " (" << (char *) n[0].kind
764 << ") is not a context" )
765 if (ex.why == CosNaming::NamingContext::not_object)
766 MESSAGE( "Destroy_Name() : " << (char *) n[0].id
767 << " (" << (char *) n[0].kind
768 << ") is not an object" )
770 catch (CosNaming::NamingContext::InvalidName &)
772 MESSAGE( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName" )
774 catch (CosNaming::NamingContext::CannotProceed &)
776 MESSAGE( "!!!Destroy_Name(): CosNaming::NamingContext::CannotProceed" )
778 catch(CORBA::COMM_FAILURE&)
780 MESSAGE( "!!!Destroy_Name() : CORBA::COMM_FAILURE : unable to contact"
781 << " the naming service")
782 throw ServiceUnreachable();
786 // the last element is the object to destroy
787 _context_name.length(1);
788 _context_name[0].id =
789 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
790 _context_name[0].kind = CORBA::string_dup("object");
791 SCRUTE(_context_name[0].id);
792 ASSERT(!CORBA::is_nil(_current_context));
793 // Object destruction
796 _current_context->unbind(_context_name);
797 MESSAGE( "The object " << _context_name[0].id << " has been deleted" )
799 catch(CosNaming::NamingContext::NotFound& ex)
801 CosNaming::Name n = ex.rest_of_name;
802 if (ex.why == CosNaming::NamingContext::missing_node)
803 MESSAGE( "Destroy_Name() : " << (char *) n[0].id
804 << " (" << (char *) n[0].kind << ") not found" )
805 if (ex.why == CosNaming::NamingContext::not_context)
806 MESSAGE( "Destroy_Name() : " << (char *) n[0].id
807 << " (" << (char *) n[0].kind
808 << ") is not a context" )
809 if (ex.why == CosNaming::NamingContext::not_object)
810 MESSAGE( "Destroy_Name() : " << (char *) n[0].id
811 << " (" << (char *) n[0].kind
812 << ") is not an object" )
814 catch(CosNaming::NamingContext::CannotProceed&)
816 MESSAGE( "!!!Destroy_Name() : CosNaming::NamingContext::CannotProceed")
818 catch(CosNaming::NamingContext::InvalidName&)
820 MESSAGE( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName")
822 catch(CORBA::COMM_FAILURE&)
824 MESSAGE( "!!!Destroy_Name() :CORBA::COMM_FAILURE : unable to contact"
825 << " the naming service")
826 throw ServiceUnreachable();
828 // Memory destruction
829 for (int i = 0 ; i <dimension_resultat ;i++)
831 delete [] resultat_resolve_Path[i];
833 delete[] resultat_resolve_Path ;
836 //----------------------------------------------------------------------
837 /*! Function : Destroy_Directory.
838 * Purpose : method to destroy a directory if it is empty.
839 * WARNING : The complete Path to the directory (from the root_context)
840 * to destroy should be given.
841 * If the NamingService is out, the exception ServiceUnreachable is thrown.
842 * \param Path const char* arguments
844 //----------------------------------------------------------------------
846 void SALOME_NamingService::Destroy_Directory(const char* Path)
847 throw(ServiceUnreachable)
849 MESSAGE("BEGIN OF Destroy_Directory");
850 int dimension_Path = strlen(Path) + 1;
851 char** resultat_resolve_Path = new char* [dimension_Path];
853 // _current_context is replaced to the _root_context
854 // if the Path begins whith '/'
855 if (Path[0]=='/') _current_context = _root_context;
857 CosNaming::NamingContext_var _ref_context = _current_context;
859 //the resolution of the directory path has to be done
860 //to place the currect_context to the correct node
861 int dimension_resultat = 0;
862 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
864 CosNaming::Name _context_name;
865 if (dimension_resultat>1)
867 // We go in the directory where the context to destroy is
868 _context_name.length(dimension_resultat-1);
870 _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
874 CORBA::Object_var _obj = _current_context->resolve(_context_name);
875 _current_context = CosNaming::NamingContext::_narrow(_obj);
876 _ref_context = _current_context ;
878 catch (CosNaming::NamingContext::NotFound& ex)
880 CosNaming::Name n = ex.rest_of_name;
881 if (ex.why == CosNaming::NamingContext::missing_node)
882 MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
883 << " (" << (char *) n[0].kind << ") not found")
884 if (ex.why == CosNaming::NamingContext::not_context)
885 MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
886 << " (" << (char *) n[0].kind
887 << ") is not a context" )
888 if (ex.why == CosNaming::NamingContext::not_object)
889 MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
890 << " (" << (char *) n[0].kind
891 << ") is not an object" )
893 catch (CosNaming::NamingContext::InvalidName &)
895 MESSAGE( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
897 catch (CosNaming::NamingContext::CannotProceed &)
899 MESSAGE("!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
901 catch(CORBA::COMM_FAILURE&)
903 MESSAGE( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
904 << " the naming service" )
905 throw ServiceUnreachable();
909 // the last element is the context to destroy
910 _context_name.length(1);
911 _context_name[0].id =
912 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
913 _context_name[0].kind = CORBA::string_dup("dir");
914 SCRUTE(_context_name[0].id);
918 // We go in the context to destroy
919 CORBA::Object_var _obj = _current_context->resolve(_context_name);
920 _current_context = CosNaming::NamingContext::_narrow(_obj);
922 catch (CosNaming::NamingContext::NotFound& ex)
924 CosNaming::Name n = ex.rest_of_name;
925 if (ex.why == CosNaming::NamingContext::missing_node)
926 MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
927 << " (" << (char *) n[0].kind << ") not found" )
928 if (ex.why == CosNaming::NamingContext::not_context)
929 MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
930 << " (" << (char *) n[0].kind
931 << ") is not a context" )
932 if (ex.why == CosNaming::NamingContext::not_object)
933 MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
934 << " (" << (char *) n[0].kind
935 << ") is not an object" )
937 catch (CosNaming::NamingContext::InvalidName &)
939 MESSAGE( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
941 catch (CosNaming::NamingContext::CannotProceed &)
943 MESSAGE( "!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
945 catch(CORBA::COMM_FAILURE&)
947 MESSAGE( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
948 << " the naming service" )
949 throw ServiceUnreachable();
952 ASSERT(!CORBA::is_nil(_current_context));
953 // Context Destruction
956 _current_context->destroy();
957 MESSAGE( "The context " << _context_name[0].id << " has been deleted" )
959 catch(CosNaming::NamingContext::NotEmpty&)
961 MESSAGE( "!!!Destroy_Directory() : CosNaming::NamingContext::NoEmpty "
962 << Path << " is not empty" )
964 catch(CORBA::COMM_FAILURE&)
966 MESSAGE( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : "
967 << "unable to contact the naming service")
968 throw ServiceUnreachable();
970 // We go to the directory just before the context to delete
971 _current_context = _ref_context ;
974 _current_context->unbind(_context_name);
975 MESSAGE( "The bind to the context " << _context_name[0].id << " has been deleted" )
977 catch(CosNaming::NamingContext::NotFound& ex)
979 CosNaming::Name n = ex.rest_of_name;
980 if (ex.why == CosNaming::NamingContext::missing_node)
981 MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
982 << " (" << (char *) n[0].kind << ") not found" )
983 if (ex.why == CosNaming::NamingContext::not_context)
984 MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
985 << " (" << (char *) n[0].kind
986 << ") is not a context" )
987 if (ex.why == CosNaming::NamingContext::not_object)
988 MESSAGE( "Destroy_Directory() : " << (char *) n[0].id
989 << " (" << (char *) n[0].kind
990 << ") is not an object" )
992 catch(CosNaming::NamingContext::CannotProceed&)
994 MESSAGE( "!!!Destroy_Directory() : CosNaming::NamingContext::CannotProceed")
996 catch(CosNaming::NamingContext::InvalidName&)
998 MESSAGE( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName")
1000 catch(CORBA::COMM_FAILURE&)
1002 MESSAGE( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : unable to contact"
1003 << " the naming service")
1004 throw ServiceUnreachable();
1006 // Memory destruction
1007 for (int i = 0 ; i <dimension_resultat ;i++)
1009 delete [] resultat_resolve_Path[i];
1011 delete[] resultat_resolve_Path ;
1014 //----------------------------------------------------------------------
1015 /*! Function : _initialize_root_context
1016 * Purpose : method called by constructor to initialize _root_context
1018 //----------------------------------------------------------------------
1020 void SALOME_NamingService::_initialize_root_context()
1022 //MESSAGE("Get the root context");
1025 CORBA::Object_var obj = _orb->resolve_initial_references("NameService");
1026 _root_context = CosNaming::NamingContext::_narrow(obj);
1027 _current_context = _root_context ;
1028 ASSERT(!CORBA::is_nil(_root_context));
1031 catch(CORBA::COMM_FAILURE&)
1033 INFOS("CORBA::COMM_FAILURE: unable to contact the naming service");
1034 throw ServiceUnreachable();
1038 INFOS("Unknown Exception: unable to contact the naming service");
1039 throw ServiceUnreachable();
1043 //----------------------------------------------------------------------
1044 /*! Function : _resolve_Path
1045 * Purpose : method to decompose a Path : /Kernel/Services/Sessions.
1047 * \return a char* containing the first char between '/' (in this case Kernel)
1049 //----------------------------------------------------------------------
1051 char* SALOME_NamingService::_resolve_Path(char* Path)
1054 int length = strlen(Path);
1057 if (length==0) return NULL;
1060 while ((i<length) && (Path[i]!='/'))
1062 resultat = new char[i+1];
1063 strncpy(resultat,Path,i);
1069 //----------------------------------------------------------------------
1070 /*! Function : _result_resolve_Path.
1071 * Purpose : method to decompose a Path : /Kernel/Services/Sessions.
1072 * Gives an array of char* containing Kernel, Services, Sessions.
1073 * \param Path const char* arguments, the Path to decompose
1074 * \param j int& arguments, the size of the array of char*
1075 * \param resultat_resolve_Path char** arguments
1077 //----------------------------------------------------------------------
1080 SALOME_NamingService::_result_resolve_Path(const char* Path,
1082 char ** resultat_resolve_Path)
1084 //MESSAGE("BEGIN OF _result_resolve_Path");
1085 int dimension_Path = strlen(Path) + 1;
1086 char** temp= new char* [dimension_Path];
1087 char** tempslash = new char* [dimension_Path];
1089 temp[j] = new char[dimension_Path];
1090 strcpy(temp[j],Path);
1092 while (strlen(temp[j])>0)
1094 // temp[j] contains the characters to be treated :
1095 // (Path - characters already treted)
1096 // tempslash[j] = temp[j] if the string temp[j] doesn't begin whith '/'
1097 // tempslash[j] = temp[j] without '/' if the string begins whith '/'
1098 int length_temp = strlen(temp[j]);
1099 if (temp[j][0]=='/')
1101 // the characters to be treated begin whith '/'
1102 // we don't have to take the '/'
1103 tempslash[j] = new char [length_temp] ;
1104 for (int k = 0; k < length_temp-1; k++)
1105 tempslash[j][k] = temp[j][k+1];
1106 tempslash[j][length_temp-1]='\0';
1110 //the characters to be trated don't begin with '/'
1111 // Nothing to be done on the char
1112 tempslash[j] = new char [length_temp+1] ;
1113 strcpy(tempslash[j],temp[j]);
1115 // decomposition of the Path
1116 resultat_resolve_Path[j]= _resolve_Path(tempslash[j]);
1117 //SCRUTE(resultat_resolve_Path[j]);
1119 int length_resultat = strlen(resultat_resolve_Path[j]) ;
1120 int dimension_temp = length_temp -length_resultat ;
1122 temp[j] = new char[dimension_temp +1];
1123 for (int i = 0 ; i <dimension_temp ;i++)
1125 temp[j][i] =tempslash[j-1][i+ length_resultat];
1127 temp[j][dimension_temp]= '\0';
1130 // Memory destruction
1131 for (int i = 0 ; i <j;i++)
1134 delete [] tempslash[i];
1137 delete [] tempslash ;
1140 //----------------------------------------------------------------------
1141 /*! Function : _Find.
1142 * Purpose : method to research a name from the current directory
1143 * of the naming service.
1144 * The naming service changes directory to go to the directory where
1145 * the last occurence was found.
1146 * \param name const char* arguments
1147 * \param occurence_number CORBA::LONG (by value)
1149 //----------------------------------------------------------------------
1151 void SALOME_NamingService::_Find(const char* name,
1152 CORBA::Long& occurence_number)
1154 //MESSAGE("BEGIN OF _Find") SCRUTE(name);
1155 CosNaming::BindingList_var _binding_list;
1156 CosNaming::BindingIterator_var _binding_iterator;
1157 unsigned long nb=0 ; //for using only the BindingIterator
1158 // to access the bindings
1159 CosNaming::Binding_var _binding ;
1160 CosNaming::NamingContext_var _ref_context = _current_context;
1161 CosNaming::NamingContext_var _found_context = _current_context;
1163 _current_context->list(nb, _binding_list, _binding_iterator) ;
1165 while (_binding_iterator->next_one(_binding)) {
1166 CosNaming::Name _bindingName = _binding->binding_name;
1167 if (_binding->binding_type == CosNaming::ncontext) {
1168 // We work on a directory, the search should be done in this directory
1169 Change_Directory(_bindingName[0].id);
1170 _Find(name,occurence_number);
1171 // We'll go back to the initial context
1172 _current_context = _ref_context ;
1174 else if (_binding->binding_type == CosNaming::nobject) {
1175 // We work on an object...
1176 if (!strcmp( _bindingName[0].id,name))
1178 //MESSAGE("One occurence was found");
1180 // We keep in memory the directory where one occurence was found
1181 _found_context = _current_context ;
1185 _binding_iterator->destroy();
1186 // We go to the last directory where an occurence was found
1187 _current_context = _found_context ;
1188 //SCRUTE(occurence_number);
1191 //----------------------------------------------------------------------
1192 /*! Function : _create_context_name_dir.
1193 * Purpose : method to create a Context_name from an array of char.
1194 * The number of elements to be copied are indicated
1197 * \param resultat_resolve_Path char** arguments
1198 * \param length_copy int arguments
1199 * \param _context_name CosNaming::Name arguments (by value)
1201 //----------------------------------------------------------------------
1204 SALOME_NamingService::_create_context_name_dir(char** resultat_resolve_Path
1206 CosNaming::Name& _context_name)
1208 //MESSAGE("BEGIN OF _create_context_name_dir");
1209 for (int i = 0 ; i < length_copy;i++)
1211 _context_name[i].id = CORBA::string_dup(resultat_resolve_Path[i]);
1212 _context_name[i].kind = CORBA::string_dup("dir");
1213 //SCRUTE(_context_name[i].id);
1217 //----------------------------------------------------------------------
1218 /*! Function : _current_directory.
1219 * Purpose : method to parse the naming service tree to find a context
1220 * and determine the path to go to this context from the
1222 * \param result_path char** arguments
1223 * \param length_result int arguments by value
1224 * \param context_to_found CosNaming::NamingContext_var arguments
1225 * \param _continue boolean arguments
1227 //----------------------------------------------------------------------
1230 SALOME_NamingService::_current_directory(char** result_path,
1232 CosNaming::NamingContext_var context_to_found,
1233 CORBA::Boolean& _continue)
1235 //MESSAGE("BEGIN OF _current_Directory");
1236 CosNaming::BindingList_var _binding_list;
1237 CosNaming::BindingIterator_var _binding_iterator;
1238 unsigned long nb=0 ; //for using only the BindingIterator
1239 // to access the bindings
1240 CosNaming::Binding_var _binding ;
1241 CosNaming::NamingContext_var _ref_context = _current_context;
1242 CosNaming::NamingContext_var _temp_context = _current_context;
1244 _current_context->list(nb, _binding_list, _binding_iterator) ;
1246 while ((_binding_iterator->next_one(_binding)) && _continue) {
1247 CosNaming::Name _bindingName = _binding->binding_name;
1248 if (_binding->binding_type == CosNaming::ncontext)
1250 // We work on a directory, the search should be done in this directory
1252 result_path[length_result] = new char(strlen(_bindingName[0].id) + 1);
1253 strcpy(result_path[length_result],_bindingName[0].id);
1254 //SCRUTE(result_path[length_result])
1257 CORBA::Object_var _obj =_current_context->resolve(_bindingName);
1258 _temp_context = CosNaming::NamingContext::_narrow(_obj);
1260 if (_temp_context->_is_equivalent(context_to_found))
1262 //MESSAGE("The context is found, we stop the search");
1264 //SCRUTE(_continue);
1268 //SCRUTE(_bindingName[0].id);
1269 Change_Directory(_bindingName[0].id);
1270 _current_directory(result_path,length_result,
1271 context_to_found, _continue );
1274 // We'll go back to the initial context
1275 _current_context = _ref_context ;
1276 //MESSAGE("Just before the delete of ")
1277 //SCRUTE(result_path[length_result-1]);
1278 delete result_path[length_result-1];
1284 _binding_iterator->destroy();
1285 // We go to the last directory where an occurence was found
1286 _current_context = _ref_context ;
1288 //----------------------------------------------------------------------