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");
85 _initialize_root_context();
89 //----------------------------------------------------------------------
90 /*! Function : Register
91 * Method to create an association in the NamingService between ObjRef
93 * If the NamingService is out, the exception ServiceUnreachable is thrown
94 * \param ObjRef CORBA::Object_ptr arguments
95 * \param Path const char* arguments
97 //----------------------------------------------------------------------
99 void SALOME_NamingService::Register(CORBA::Object_ptr ObjRef,
101 throw(ServiceUnreachable)
103 // MESSAGE("BEGIN OF Register: "<< Path);
104 int dimension_Path = strlen(Path) + 1;
105 char** resultat_resolve_Path = new char* [dimension_Path];
107 // _current_context is replaced to the _root_context
108 // if the Path begins whith '/'
110 _current_context = _root_context;
111 // MESSAGE("Gone to the _root_context");
114 //the resolution of the directory path has to be done
115 //to place the currect_context to the correct node
116 int dimension_resultat = 0;
117 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
119 CosNaming::Name _context_name;
120 CORBA::Boolean _not_exist = false ;
121 CosNaming::NamingContext_var _temp_context;
123 if(dimension_resultat>1)
125 // A directory is treated (not only an object name)
126 // We had to test if the directory where ObjRef should be recorded
128 // If not, the new context has to be created
130 // MESSAGE("A complet Path has to be treated, not only an object name");
131 _context_name.length(dimension_resultat-1);
133 _create_context_name_dir(resultat_resolve_Path,dimension_resultat-1,
138 CORBA::Object_var _obj = _current_context->resolve(_context_name);
139 _current_context = CosNaming::NamingContext::_narrow(_obj);
141 catch (CosNaming::NamingContext::NotFound &)
143 // failed to resolve, therefore assume cold start
146 catch (CosNaming::NamingContext::InvalidName &)
148 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
150 catch (CosNaming::NamingContext::CannotProceed &)
152 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
154 catch(CORBA::COMM_FAILURE&)
156 INFOS("!!!Register() : CORBA::COMM_FAILURE : unable to contact"
157 << " the naming service");
158 throw ServiceUnreachable();
164 _context_name.length(1);
165 // MESSAGE("The Path indicated is not yet created. It will soon be done");
166 for (int i = 0 ; i <dimension_resultat -1 ;i++)
168 _context_name[0].id =
169 CORBA::string_dup(resultat_resolve_Path[i]);
170 _context_name[0].kind = CORBA::string_dup("dir");
171 // SCRUTE(_context_name[0].id);
172 //The Path could be in part already created.
176 // this context is already created.
177 // Nothing to be done
178 CORBA::Object_var _obj =
179 _current_context->resolve(_context_name);
181 CosNaming::NamingContext::_narrow(_obj);
182 //MESSAGE("This context was already created");
184 catch (CosNaming::NamingContext::NotFound &)
186 // This context is not created. It will be done
188 _current_context->bind_new_context(_context_name);
189 _current_context = _temp_context;
190 //INFOS("This context was'nt created, it's now done");
194 catch (CosNaming::NamingContext::AlreadyBound&)
196 INFOS("!!!Register() : CosNaming::NamingContext::AlreadyBound");
198 catch(CosNaming::NamingContext::NotFound& ex)
200 CosNaming::Name n = ex.rest_of_name;
201 if (ex.why == CosNaming::NamingContext::missing_node)
202 INFOS("Register() : " << (char *) n[0].id
203 << " (" << (char *) n[0].kind << ") not found");
204 if (ex.why == CosNaming::NamingContext::not_context)
205 INFOS("Register() : " << (char *) n[0].id
206 << " (" << (char *) n[0].kind
207 << ") is not a context");
208 if (ex.why == CosNaming::NamingContext::not_object)
209 INFOS("Register() : " << (char *) n[0].id
210 << " (" << (char *) n[0].kind
211 << ") is not an object");
213 catch(CosNaming::NamingContext::CannotProceed&)
215 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
217 catch(CosNaming::NamingContext::InvalidName&)
219 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
221 catch(CORBA::COMM_FAILURE&)
223 INFOS("!!!Register() :CORBA::COMM_FAILURE : unable to contact"
224 << " the naming service");
225 throw ServiceUnreachable();
230 // The current directory is now the directory where the object should
232 _context_name.length(1);
235 // the last element is an object an not a directory
236 _context_name[0].id =
237 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
238 _context_name[0].kind = CORBA::string_dup("object");
239 //SCRUTE(_context_name[0].id);
241 _current_context->bind(_context_name, ObjRef);
242 // MESSAGE("A new element " << _context_name[0].id
243 // << " is recorded in the _current_context");
245 catch(CosNaming::NamingContext::NotFound& ex)
247 CosNaming::Name n = ex.rest_of_name;
248 if (ex.why == CosNaming::NamingContext::missing_node)
249 INFOS("Register() : " << (char *) n[0].id
250 << " (" << (char *) n[0].kind << ") not found");
251 if (ex.why == CosNaming::NamingContext::not_context)
252 INFOS("Register() : " << (char *) n[0].id
253 << " (" << (char *) n[0].kind
254 << ") is not a context");
255 if (ex.why == CosNaming::NamingContext::not_object)
256 INFOS("Register() : " << (char *) n[0].id
257 << " (" << (char *) n[0].kind
258 << ") is not an object");
260 catch(CosNaming::NamingContext::CannotProceed&)
262 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
264 catch(CosNaming::NamingContext::InvalidName&)
266 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
268 catch(CosNaming::NamingContext::AlreadyBound&)
270 INFOS("!!!Register() : CosNaming::NamingContext::AlreadyBound, object will be rebind");
271 _current_context->rebind(_context_name, ObjRef);
273 catch(CORBA::COMM_FAILURE&)
275 INFOS("!!!Register() :CORBA::COMM_FAILURE : unable to contact"
276 << " the naming service");
277 throw ServiceUnreachable();
281 // Memory destruction
282 for (int i = 0 ; i <dimension_resultat ;i++)
284 delete [] resultat_resolve_Path[i];
286 delete[] resultat_resolve_Path ;
289 //----------------------------------------------------------------------
290 /*! Function : Resolve
291 * Purpose : method to get the ObjRef of a symbolic name
292 * If the NamingService is out, the exception ServiceUnreachable is thrown
293 * \param Path const char* arguments
294 * \return the object reference
296 //----------------------------------------------------------------------
298 CORBA::Object_ptr SALOME_NamingService::Resolve(const char* Path)
299 throw(ServiceUnreachable)
301 //MESSAGE("BEGIN OF Resolve: " << Path);
302 int dimension_Path = strlen(Path) + 1;
303 char** resultat_resolve_Path = new char* [dimension_Path];
305 // _current_context is replaced to the _root_context
306 // if the Path begins whith '/'
307 if (Path[0]=='/') _current_context = _root_context;
310 //the resolution of the directory path has to be done
311 //to place the currect_context to the correct node
312 int dimension_resultat = 0;
313 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
315 CosNaming::Name _context_name;
316 _context_name.length(dimension_resultat);
317 CORBA::Object_ptr _obj = NULL ;
319 _create_context_name_dir(resultat_resolve_Path,dimension_resultat-1,
321 // the last element is an object an not a directory
322 _context_name[dimension_resultat -1].id =
323 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
324 _context_name[dimension_resultat -1].kind = CORBA::string_dup("object");
325 // SCRUTE(_context_name[dimension_resultat -1].id);
326 ASSERT(!CORBA::is_nil(_current_context));
330 _obj =_current_context->resolve(_context_name);
332 catch(CosNaming::NamingContext::NotFound& ex)
334 CosNaming::Name n = ex.rest_of_name;
335 if (ex.why == CosNaming::NamingContext::missing_node)
336 INFOS("Resolve() : " << (char *) n[0].id
337 << " (" << (char *) n[0].kind << ") not found");
338 if (ex.why == CosNaming::NamingContext::not_context)
340 << (char *) n[0].id << " (" << (char *) n[0].kind
341 << ") is not a context");
342 if (ex.why == CosNaming::NamingContext::not_object)
343 INFOS("Resolve() : " << (char *) n[0].id
344 << " (" << (char *) n[0].kind
345 << ") is not an object");
347 catch(CosNaming::NamingContext::CannotProceed&)
349 INFOS("!!!Resolve() : CosNaming::NamingContext::CannotProceed");
351 catch(CosNaming::NamingContext::InvalidName&)
353 INFOS("!!!Resolve() : CosNaming::NamingContext::InvalidName");
355 catch(CORBA::COMM_FAILURE&)
357 INFOS("!!!Resolve() :CORBA::COMM_FAILURE : unable to contact"
358 << "the naming service");
359 throw ServiceUnreachable();
361 // Memory destruction
362 for (int i = 0 ; i <dimension_resultat ;i++)
364 delete [] resultat_resolve_Path[i];
366 delete[] resultat_resolve_Path ;
371 //----------------------------------------------------------------------
373 * Purpose : method to research a name from the current directory
374 * of the naming service.
375 * The naming service changes directory to go to the directory where
376 * the last occurence was found.
377 * If the NamingService is out, the exception ServiceUnreachable is thrown
378 * \param name const char* arguments
379 * \return the number of occurences found
382 //----------------------------------------------------------------------
384 int SALOME_NamingService::Find(const char* name)
385 throw(ServiceUnreachable)
387 // MESSAGE("BEGIN OF Find " << name);
388 CORBA::Long occurence_number = 0 ;
391 _Find(name,occurence_number);
393 catch(CORBA::COMM_FAILURE&)
395 INFOS("!!!Find() : CORBA::COMM_FAILURE : unable to contact"
396 << " the naming service");
397 throw ServiceUnreachable();
399 return occurence_number;
402 //----------------------------------------------------------------------
403 /*! Function : Create_Directory
404 * Purpose : method to create a directory from the current directory.
405 * If the NamingService is out, the exception ServiceUnreachable is thrown
406 * \param Path const char* arguments
407 * \return a boolean to indicate if the creation succeeded
409 //----------------------------------------------------------------------
411 bool SALOME_NamingService::Create_Directory(const char* Path)
412 throw(ServiceUnreachable)
414 //MESSAGE("BEGIN OF Create_Directory");
415 int dimension_Path = strlen(Path) + 1;
416 char** resultat_resolve_Path= new char* [dimension_Path];;
417 CORBA::Boolean _return_code = true ;
419 // _current_context is replaced to the _root_context
420 // if the Path begins whith '/'
421 if (Path[0]=='/') _current_context = _root_context;
423 int dimension_resultat = 0;
424 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
427 // We had to test if a part of the directory to treat
429 // If not, the new context has to be created
431 CosNaming::Name _context_name;
432 _context_name.length(1);
433 CosNaming::NamingContext_var _temp_context;
434 ASSERT(!CORBA::is_nil(_current_context));
439 for (int i = 0 ; i <dimension_resultat ;i++)
441 _context_name[0].id =
442 CORBA::string_dup(resultat_resolve_Path[i]);
443 _context_name[0].kind = CORBA::string_dup("dir");
444 // SCRUTE(_context_name[0].id);
445 //The Path could be in part already created.
449 // this context is already created.
450 // Nothing to be done
451 CORBA::Object_var _obj =
452 _current_context->resolve(_context_name);
454 CosNaming::NamingContext::_narrow(_obj);
455 MESSAGE("This context was already created");
457 catch (CosNaming::NamingContext::NotFound &)
459 // This context is not created. It will be done
461 _current_context->bind_new_context(_context_name);
462 _current_context = _temp_context;
463 INFOS("This context was'nt created, it's now done");
467 catch (CosNaming::NamingContext::AlreadyBound&)
469 INFOS("!!! Create_Directory() CosNaming::NamingContext::AlreadyBound");
470 _return_code = false;
472 catch(CosNaming::NamingContext::NotFound& ex)
474 _return_code = false;
475 CosNaming::Name n = ex.rest_of_name;
476 if (ex.why == CosNaming::NamingContext::missing_node)
477 INFOS("Create_Directory() : " << (char *) n[0].id
478 << " (" << (char *) n[0].kind << ") not found");
479 if (ex.why == CosNaming::NamingContext::not_context)
480 INFOS("Create_Directory() : " << (char *) n[0].id
481 << " (" << (char *) n[0].kind
482 << ") is not a context");
483 if (ex.why == CosNaming::NamingContext::not_object)
484 INFOS("Create_Directory() : " << (char *) n[0].id
485 << " (" << (char *) n[0].kind
486 << ") is not an object");
488 catch(CosNaming::NamingContext::CannotProceed&)
490 _return_code = false;
491 INFOS("!!!Create_Directory():CosNaming::NamingContext::CannotProceed");
493 catch(CosNaming::NamingContext::InvalidName&)
495 _return_code = false;
496 INFOS("!!!Create_Directory():CosNaming::NamingContext::InvalidName");
498 catch(CORBA::COMM_FAILURE&)
500 _return_code = false;
501 INFOS("!!!Register() :CORBA::COMM_FAILURE : unable to contact"
502 << " the naming service");
503 throw ServiceUnreachable();
505 // Memory destruction
506 for (int i = 0 ; i <dimension_resultat;i++)
508 delete [] resultat_resolve_Path[i];
510 delete[] resultat_resolve_Path ;
514 //----------------------------------------------------------------------
515 /*! Function : Change_Directory
516 * Purpose : method to change the current directory to the
517 * directory Path indicated in "in" Parameter.
518 * If Path ="/", the current directory changes to the root directory.
519 * If the NamingService is out, the exception ServiceUnreachable is thrown.
520 * \param Path const char* arguments
521 * \return a boolean to indicate if the change succeeded
523 //----------------------------------------------------------------------
525 bool SALOME_NamingService::Change_Directory(const char* Path)
526 throw(ServiceUnreachable)
528 //MESSAGE("BEGIN OF Change_Directory " << Path);
529 int dimension_Path = strlen(Path) + 1;
530 char** resultat_resolve_Path = new char* [dimension_Path];
531 CORBA::Boolean _return_code = true ;
533 // _current_context is replaced to the _root_context
534 // if the Path begins whith '/'
535 if (Path[0]=='/') _current_context = _root_context;
537 if ((Path[0]=='/') && (dimension_Path == 2))
539 MESSAGE("Change_Directory is called to go to the root_context");
541 //nothing to de done, the change_dur is called to go to the root_context
542 // no Path to resolve
544 //the resolution of the directory path has to be done
545 //to place the currect_context to the correct node
547 int dimension_resultat = 0;
548 _result_resolve_Path(Path,dimension_resultat,resultat_resolve_Path);
550 CosNaming::Name _context_name;
551 _context_name.length(dimension_resultat);
552 CORBA::Object_var _obj;
554 _create_context_name_dir(resultat_resolve_Path,dimension_resultat,
557 ASSERT(!CORBA::is_nil(_current_context));
561 _obj =_current_context->resolve(_context_name);
562 _current_context = CosNaming::NamingContext::_narrow(_obj);
563 ASSERT(!CORBA::is_nil(_current_context))
565 catch(CosNaming::NamingContext::NotFound& ex)
567 _return_code = false;
568 CosNaming::Name n = ex.rest_of_name;
569 if (ex.why == CosNaming::NamingContext::missing_node)
570 INFOS( "Change_Directory() : " << (char *) n[0].id
571 << " (" << (char *) n[0].kind << ") not found")
572 if (ex.why == CosNaming::NamingContext::not_context)
573 INFOS("Change_Directory() : " << (char *) n[0].id
574 << " (" << (char *) n[0].kind
575 << ") is not a context" )
576 if (ex.why == CosNaming::NamingContext::not_object)
577 INFOS( "Change_Directory() : " << (char *) n[0].id
578 << " (" << (char *) n[0].kind
579 << ") is not an object" )
581 catch(CosNaming::NamingContext::CannotProceed&)
583 _return_code = false;
584 INFOS( "!!!Change_Directory() : CosNaming::NamingContext::CannotProceed" )
586 catch(CosNaming::NamingContext::InvalidName&)
588 _return_code = false;
589 INFOS( "!!!Change_Directory() : CosNaming::NamingContext::InvalidName" )
591 catch(CORBA::COMM_FAILURE&)
593 _return_code = false;
594 INFOS( "!!!Change_Directory() :CORBA::COMM_FAILURE : unable to contact"
595 << "the naming service")
596 throw ServiceUnreachable();
598 // Memory destruction
599 for (int i = 0 ; i <dimension_resultat ;i++)
601 delete [] resultat_resolve_Path[i];
603 delete[] resultat_resolve_Path ;
608 //----------------------------------------------------------------------
609 /*! Function : Current_Directory
610 * Purpose : method to get the current directory.
611 * If the NamingService is out, the exception ServiceUnreachable is thrown
612 * \return the path of the current_context
613 * \sa _current_directory
615 //----------------------------------------------------------------------
617 char* SALOME_NamingService::Current_Directory()
618 throw(ServiceUnreachable)
620 //MESSAGE("BEGIN OF Current_Directory");
622 CosNaming::NamingContext_var _ref_context = _current_context;
626 char** result_path = new char*[50]; // 50 is it enough?
629 // We go to the root_context to begin the search from the root
630 _current_context = _root_context ;
631 CORBA::Boolean _continue = true ;
634 _current_directory(result_path,i,_ref_context,_continue );
636 catch(CORBA::COMM_FAILURE&)
638 INFOS("!!!Current_Directory(): CORBA::COMM_FAILURE : unable to contact"
639 << " the naming service" )
640 throw ServiceUnreachable();
642 for (int k = 0 ; k <i ;k++)
644 // We count the length of the char* + 1 for the '/' to separate
646 length_path = length_path + strlen(result_path[k]) + 1;
648 char* return_Path = new char[length_path +2];
649 return_Path[0] = '/' ;
650 return_Path[1] = '\0' ;
651 for (int k = 0 ; k <i ;k++)
653 //SCRUTE(result_path[k])
654 strcat(return_Path,result_path[k]);
655 strcat(return_Path,"/");
657 //SCRUTE(return_Path)
658 _current_context = _ref_context ;
663 //----------------------------------------------------------------------
665 * Purpose : method to list and print all the context contained from
666 * the current context
667 * If the NamingService is out, the exception ServiceUnreachable is thrown
669 //----------------------------------------------------------------------
671 void SALOME_NamingService::list()
672 throw(ServiceUnreachable)
674 MESSAGE("Begin of list");
675 CosNaming::BindingList_var _binding_list;
676 CosNaming::BindingIterator_var _binding_iterator;
677 unsigned long nb=0 ; // for using only the BindingIterator to access the bindings
678 CosNaming::Binding_var _binding ;
679 CosNaming::NamingContext_var _ref_context = _current_context;
680 _current_context->list(nb, _binding_list, _binding_iterator) ;
682 while (_binding_iterator->next_one(_binding)) {
683 CosNaming::Name _bindingName = _binding->binding_name;
684 if (_binding->binding_type == CosNaming::ncontext) {
685 MESSAGE( "Context : " << _bindingName[0].id );
688 Change_Directory(_bindingName[0].id);
690 catch (ServiceUnreachable&)
692 INFOS( "!!!list(): ServiceUnreachable" )
693 throw ServiceUnreachable();
697 _current_context = _ref_context ;
699 else if (_binding->binding_type == CosNaming::nobject) {
700 MESSAGE( "Object : " << _bindingName[0].id );
703 _binding_iterator->destroy();
706 //----------------------------------------------------------------------
707 /*! Function : list_directory
708 * Purpose : method to get all the contexts contained in the current
710 * Get only objects, isn't iterative
711 * If the NamingService is out, the exception ServiceUnreachable is thrown
713 //----------------------------------------------------------------------
714 vector<string> SALOME_NamingService::list_directory()
715 throw(ServiceUnreachable)
717 vector<string> _list ;
719 CosNaming::BindingList_var _binding_list;
720 CosNaming::BindingIterator_var _binding_iterator;
721 unsigned long nb=0 ; // for using only the BindingIterator to access the bindings
722 CosNaming::Binding_var _binding ;
723 CosNaming::NamingContext_var _ref_context = _current_context;
724 _current_context->list(nb, _binding_list, _binding_iterator) ;
725 if (_binding_iterator->_is_nil()) return _list;
727 while (_binding_iterator->next_one(_binding)) {
728 CosNaming::Name _bindingName = _binding->binding_name;
729 if (_binding->binding_type == CosNaming::nobject) {
730 _list.push_back(CORBA::string_dup(_bindingName[0].id));
733 //for (unsigned int ind = 0; ind < _list.size(); ind++)
734 // MESSAGE("list_directory : Object : " << _list[ind]);
736 _binding_iterator->destroy();
740 //----------------------------------------------------------------------
741 /*! Function : list_directory_recurs
742 * Purpose : method to get all the contexts contained in the current
744 * Get only objects and is recursive
745 * If the NamingService is out, the exception ServiceUnreachable is thrown
747 //----------------------------------------------------------------------
748 vector<string> SALOME_NamingService::list_directory_recurs()
749 throw(ServiceUnreachable)
751 vector<string> _list ;
752 char *currentDir=Current_Directory();
753 _list_directory_recurs(_list,0,currentDir);
754 delete [] currentDir;
758 //----------------------------------------------------------------------
759 /*! Function : Destroy_Name
760 * Purpose : method to destroy an association Path-Object Reference.
761 * WARNING : The complete Path should be given.
762 * If the NamingService is out, the exception ServiceUnreachable is thrown
763 * \param Path const char* arguments
765 //----------------------------------------------------------------------
767 void SALOME_NamingService::Destroy_Name(const char* Path)
768 throw(ServiceUnreachable)
770 MESSAGE("BEGIN OF Destroy_Name");
771 int dimension_Path = strlen(Path) + 1;
772 char** resultat_resolve_Path = new char* [dimension_Path];
774 // _current_context is replaced to the _root_context
775 // if the Path begins whith '/'
776 if (Path[0]=='/') _current_context = _root_context;
779 //the resolution of the directory path has to be done
780 //to place the currect_context to the correct node
781 int dimension_resultat = 0;
782 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
784 CosNaming::Name _context_name;
785 if (dimension_resultat>1)
787 // We go in the directory where the object to destroy is
788 _context_name.length(dimension_resultat-1);
790 _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
794 CORBA::Object_var _obj = _current_context->resolve(_context_name);
795 _current_context = CosNaming::NamingContext::_narrow(_obj);
797 catch (CosNaming::NamingContext::NotFound& ex)
799 CosNaming::Name n = ex.rest_of_name;
800 if (ex.why == CosNaming::NamingContext::missing_node)
801 INFOS( "Destroy_Name() : " << (char *) n[0].id
802 << " (" << (char *) n[0].kind << ") not found" )
803 if (ex.why == CosNaming::NamingContext::not_context)
804 INFOS( "Destroy_Name() : " << (char *) n[0].id
805 << " (" << (char *) n[0].kind
806 << ") is not a context" )
807 if (ex.why == CosNaming::NamingContext::not_object)
808 INFOS( "Destroy_Name() : " << (char *) n[0].id
809 << " (" << (char *) n[0].kind
810 << ") is not an object" )
812 catch (CosNaming::NamingContext::InvalidName &)
814 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName" )
816 catch (CosNaming::NamingContext::CannotProceed &)
818 INFOS( "!!!Destroy_Name(): CosNaming::NamingContext::CannotProceed" )
820 catch(CORBA::COMM_FAILURE&)
822 INFOS( "!!!Destroy_Name() : CORBA::COMM_FAILURE : unable to contact"
823 << " the naming service")
824 throw ServiceUnreachable();
828 // the last element is the object to destroy
829 _context_name.length(1);
830 _context_name[0].id =
831 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
832 _context_name[0].kind = CORBA::string_dup("object");
833 SCRUTE(_context_name[0].id);
834 ASSERT(!CORBA::is_nil(_current_context));
835 // Object destruction
838 _current_context->unbind(_context_name);
839 MESSAGE( "The object " << _context_name[0].id << " has been deleted" )
841 catch(CosNaming::NamingContext::NotFound& ex)
843 CosNaming::Name n = ex.rest_of_name;
844 if (ex.why == CosNaming::NamingContext::missing_node)
845 INFOS( "Destroy_Name() : " << (char *) n[0].id
846 << " (" << (char *) n[0].kind << ") not found" )
847 if (ex.why == CosNaming::NamingContext::not_context)
848 INFOS( "Destroy_Name() : " << (char *) n[0].id
849 << " (" << (char *) n[0].kind
850 << ") is not a context" )
851 if (ex.why == CosNaming::NamingContext::not_object)
852 INFOS( "Destroy_Name() : " << (char *) n[0].id
853 << " (" << (char *) n[0].kind
854 << ") is not an object" )
856 catch(CosNaming::NamingContext::CannotProceed&)
858 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::CannotProceed")
860 catch(CosNaming::NamingContext::InvalidName&)
862 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName")
864 catch(CORBA::COMM_FAILURE&)
866 INFOS( "!!!Destroy_Name() :CORBA::COMM_FAILURE : unable to contact"
867 << " the naming service")
868 throw ServiceUnreachable();
870 // Memory destruction
871 for (int i = 0 ; i <dimension_resultat ;i++)
873 delete [] resultat_resolve_Path[i];
875 delete[] resultat_resolve_Path ;
878 //----------------------------------------------------------------------
879 /*! Function : Destroy_Directory.
880 * Purpose : method to destroy a directory if it is empty.
881 * WARNING : The complete Path to the directory (from the root_context)
882 * to destroy should be given.
883 * If the NamingService is out, the exception ServiceUnreachable is thrown.
884 * \param Path const char* arguments
886 //----------------------------------------------------------------------
888 void SALOME_NamingService::Destroy_Directory(const char* Path)
889 throw(ServiceUnreachable)
891 MESSAGE("BEGIN OF Destroy_Directory");
892 int dimension_Path = strlen(Path) + 1;
893 char** resultat_resolve_Path = new char* [dimension_Path];
895 // _current_context is replaced to the _root_context
896 // if the Path begins whith '/'
897 if (Path[0]=='/') _current_context = _root_context;
899 CosNaming::NamingContext_var _ref_context = _current_context;
901 //the resolution of the directory path has to be done
902 //to place the currect_context to the correct node
903 int dimension_resultat = 0;
904 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
906 CosNaming::Name _context_name;
907 if (dimension_resultat>1)
909 // We go in the directory where the context to destroy is
910 _context_name.length(dimension_resultat-1);
912 _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
916 CORBA::Object_var _obj = _current_context->resolve(_context_name);
917 _current_context = CosNaming::NamingContext::_narrow(_obj);
918 _ref_context = _current_context ;
920 catch (CosNaming::NamingContext::NotFound& ex)
922 CosNaming::Name n = ex.rest_of_name;
923 if (ex.why == CosNaming::NamingContext::missing_node)
924 INFOS( "Destroy_Directory() : " << (char *) n[0].id
925 << " (" << (char *) n[0].kind << ") not found")
926 if (ex.why == CosNaming::NamingContext::not_context)
927 INFOS( "Destroy_Directory() : " << (char *) n[0].id
928 << " (" << (char *) n[0].kind
929 << ") is not a context" )
930 if (ex.why == CosNaming::NamingContext::not_object)
931 INFOS( "Destroy_Directory() : " << (char *) n[0].id
932 << " (" << (char *) n[0].kind
933 << ") is not an object" )
935 catch (CosNaming::NamingContext::InvalidName &)
937 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
939 catch (CosNaming::NamingContext::CannotProceed &)
941 INFOS("!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
943 catch(CORBA::COMM_FAILURE&)
945 INFOS( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
946 << " the naming service" )
947 throw ServiceUnreachable();
951 // the last element is the context to destroy
952 _context_name.length(1);
953 _context_name[0].id =
954 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
955 _context_name[0].kind = CORBA::string_dup("dir");
956 SCRUTE(_context_name[0].id);
960 // We go in the context to destroy
961 CORBA::Object_var _obj = _current_context->resolve(_context_name);
962 _current_context = CosNaming::NamingContext::_narrow(_obj);
964 catch (CosNaming::NamingContext::NotFound& ex)
966 CosNaming::Name n = ex.rest_of_name;
967 if (ex.why == CosNaming::NamingContext::missing_node)
968 INFOS( "Destroy_Directory() : " << (char *) n[0].id
969 << " (" << (char *) n[0].kind << ") not found" )
970 if (ex.why == CosNaming::NamingContext::not_context)
971 INFOS( "Destroy_Directory() : " << (char *) n[0].id
972 << " (" << (char *) n[0].kind
973 << ") is not a context" )
974 if (ex.why == CosNaming::NamingContext::not_object)
975 INFOS( "Destroy_Directory() : " << (char *) n[0].id
976 << " (" << (char *) n[0].kind
977 << ") is not an object" )
979 catch (CosNaming::NamingContext::InvalidName &)
981 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
983 catch (CosNaming::NamingContext::CannotProceed &)
985 INFOS( "!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
987 catch(CORBA::COMM_FAILURE&)
989 INFOS( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
990 << " the naming service" )
991 throw ServiceUnreachable();
994 ASSERT(!CORBA::is_nil(_current_context));
995 // Context Destruction
998 _current_context->destroy();
999 MESSAGE( "The context " << _context_name[0].id << " has been deleted" )
1001 catch(CosNaming::NamingContext::NotEmpty&)
1003 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::NoEmpty "
1004 << Path << " is not empty" )
1006 catch(CORBA::COMM_FAILURE&)
1008 INFOS( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : "
1009 << "unable to contact the naming service")
1010 throw ServiceUnreachable();
1012 // We go to the directory just before the context to delete
1013 _current_context = _ref_context ;
1016 _current_context->unbind(_context_name);
1017 MESSAGE( "The bind to the context " << _context_name[0].id << " has been deleted" )
1019 catch(CosNaming::NamingContext::NotFound& ex)
1021 CosNaming::Name n = ex.rest_of_name;
1022 if (ex.why == CosNaming::NamingContext::missing_node)
1023 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1024 << " (" << (char *) n[0].kind << ") not found" )
1025 if (ex.why == CosNaming::NamingContext::not_context)
1026 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1027 << " (" << (char *) n[0].kind
1028 << ") is not a context" )
1029 if (ex.why == CosNaming::NamingContext::not_object)
1030 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1031 << " (" << (char *) n[0].kind
1032 << ") is not an object" )
1034 catch(CosNaming::NamingContext::CannotProceed&)
1036 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::CannotProceed")
1038 catch(CosNaming::NamingContext::InvalidName&)
1040 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName")
1042 catch(CORBA::COMM_FAILURE&)
1044 INFOS( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : unable to contact"
1045 << " the naming service")
1046 throw ServiceUnreachable();
1048 // Memory destruction
1049 for (int i = 0 ; i <dimension_resultat ;i++)
1051 delete [] resultat_resolve_Path[i];
1053 delete[] resultat_resolve_Path ;
1056 //----------------------------------------------------------------------
1057 /*! Function : _initialize_root_context
1058 * Purpose : method called by constructor to initialize _root_context
1060 //----------------------------------------------------------------------
1062 void SALOME_NamingService::_initialize_root_context()
1064 //MESSAGE("Get the root context");
1067 CORBA::Object_var obj = _orb->resolve_initial_references("NameService");
1068 _root_context = CosNaming::NamingContext::_narrow(obj);
1069 _current_context = _root_context ;
1070 ASSERT(!CORBA::is_nil(_root_context));
1073 catch(CORBA::COMM_FAILURE&)
1075 INFOS("CORBA::COMM_FAILURE: unable to contact the naming service");
1076 throw ServiceUnreachable();
1080 INFOS("Unknown Exception: unable to contact the naming service");
1081 throw ServiceUnreachable();
1085 //----------------------------------------------------------------------
1086 /*! Function : _resolve_Path
1087 * Purpose : method to decompose a Path : /Kernel/Services/Sessions.
1089 * \return a char* containing the first char between '/' (in this case Kernel)
1091 //----------------------------------------------------------------------
1093 char* SALOME_NamingService::_resolve_Path(char* Path)
1096 int length = strlen(Path);
1099 if (length==0) return NULL;
1102 while ((i<length) && (Path[i]!='/'))
1104 resultat = new char[i+1];
1105 strncpy(resultat,Path,i);
1111 //----------------------------------------------------------------------
1112 /*! Function : _result_resolve_Path.
1113 * Purpose : method to decompose a Path : /Kernel/Services/Sessions.
1114 * Gives an array of char* containing Kernel, Services, Sessions.
1115 * \param Path const char* arguments, the Path to decompose
1116 * \param j int& arguments, the size of the array of char*
1117 * \param resultat_resolve_Path char** arguments
1119 //----------------------------------------------------------------------
1122 SALOME_NamingService::_result_resolve_Path(const char* Path,
1124 char ** resultat_resolve_Path)
1126 //MESSAGE("BEGIN OF _result_resolve_Path");
1127 int dimension_Path = strlen(Path) + 1;
1128 char** temp= new char* [dimension_Path];
1129 char** tempslash = new char* [dimension_Path];
1131 temp[j] = new char[dimension_Path];
1132 strcpy(temp[j],Path);
1134 while (strlen(temp[j])>0)
1136 // temp[j] contains the characters to be treated :
1137 // (Path - characters already treted)
1138 // tempslash[j] = temp[j] if the string temp[j] doesn't begin whith '/'
1139 // tempslash[j] = temp[j] without '/' if the string begins whith '/'
1140 int length_temp = strlen(temp[j]);
1141 if (temp[j][0]=='/')
1143 // the characters to be treated begin whith '/'
1144 // we don't have to take the '/'
1145 tempslash[j] = new char [length_temp] ;
1146 for (int k = 0; k < length_temp-1; k++)
1147 tempslash[j][k] = temp[j][k+1];
1148 tempslash[j][length_temp-1]='\0';
1152 //the characters to be trated don't begin with '/'
1153 // Nothing to be done on the char
1154 tempslash[j] = new char [length_temp+1] ;
1155 strcpy(tempslash[j],temp[j]);
1157 // decomposition of the Path
1158 resultat_resolve_Path[j]= _resolve_Path(tempslash[j]);
1159 //SCRUTE(resultat_resolve_Path[j]);
1161 int length_resultat = strlen(resultat_resolve_Path[j]) ;
1162 int dimension_temp = length_temp -length_resultat ;
1164 temp[j] = new char[dimension_temp +1];
1165 for (int i = 0 ; i <dimension_temp ;i++)
1167 temp[j][i] =tempslash[j-1][i+ length_resultat];
1169 temp[j][dimension_temp]= '\0';
1172 // Memory destruction
1173 for (int i = 0 ; i <j;i++)
1176 delete [] tempslash[i];
1179 delete [] tempslash ;
1182 //----------------------------------------------------------------------
1183 /*! Function : _Find.
1184 * Purpose : method to research a name from the current directory
1185 * of the naming service.
1186 * The naming service changes directory to go to the directory where
1187 * the last occurence was found.
1188 * \param name const char* arguments
1189 * \param occurence_number CORBA::LONG (by value)
1191 //----------------------------------------------------------------------
1193 void SALOME_NamingService::_Find(const char* name,
1194 CORBA::Long& occurence_number)
1196 //MESSAGE("BEGIN OF _Find") SCRUTE(name);
1197 CosNaming::BindingList_var _binding_list;
1198 CosNaming::BindingIterator_var _binding_iterator;
1199 unsigned long nb=0 ; //for using only the BindingIterator
1200 // to access the bindings
1201 CosNaming::Binding_var _binding ;
1202 CosNaming::NamingContext_var _ref_context = _current_context;
1203 CosNaming::NamingContext_var _found_context = _current_context;
1205 _current_context->list(nb, _binding_list, _binding_iterator) ;
1207 while (_binding_iterator->next_one(_binding)) {
1208 CosNaming::Name _bindingName = _binding->binding_name;
1209 if (_binding->binding_type == CosNaming::ncontext) {
1210 // We work on a directory, the search should be done in this directory
1211 Change_Directory(_bindingName[0].id);
1212 _Find(name,occurence_number);
1213 // We'll go back to the initial context
1214 _current_context = _ref_context ;
1216 else if (_binding->binding_type == CosNaming::nobject) {
1217 // We work on an object...
1218 if (!strcmp( _bindingName[0].id,name))
1220 //MESSAGE("One occurence was found");
1222 // We keep in memory the directory where one occurence was found
1223 _found_context = _current_context ;
1227 _binding_iterator->destroy();
1228 // We go to the last directory where an occurence was found
1229 _current_context = _found_context ;
1230 //SCRUTE(occurence_number);
1233 //----------------------------------------------------------------------
1234 /*! Function : _create_context_name_dir.
1235 * Purpose : method to create a Context_name from an array of char.
1236 * The number of elements to be copied are indicated
1239 * \param resultat_resolve_Path char** arguments
1240 * \param length_copy int arguments
1241 * \param _context_name CosNaming::Name arguments (by value)
1243 //----------------------------------------------------------------------
1246 SALOME_NamingService::_create_context_name_dir(char** resultat_resolve_Path
1248 CosNaming::Name& _context_name)
1250 //MESSAGE("BEGIN OF _create_context_name_dir");
1251 for (int i = 0 ; i < length_copy;i++)
1253 _context_name[i].id = CORBA::string_dup(resultat_resolve_Path[i]);
1254 _context_name[i].kind = CORBA::string_dup("dir");
1255 //SCRUTE(_context_name[i].id);
1259 //----------------------------------------------------------------------
1260 /*! Function : _current_directory.
1261 * Purpose : method to parse the naming service tree to find a context
1262 * and determine the path to go to this context from the
1264 * \param result_path char** arguments
1265 * \param length_result int arguments by value
1266 * \param context_to_found CosNaming::NamingContext_var arguments
1267 * \param _continue boolean arguments
1269 //----------------------------------------------------------------------
1272 SALOME_NamingService::_current_directory(char** result_path,
1274 CosNaming::NamingContext_var context_to_found,
1275 CORBA::Boolean& _continue)
1277 //MESSAGE("BEGIN OF _current_Directory");
1278 CosNaming::BindingList_var _binding_list;
1279 CosNaming::BindingIterator_var _binding_iterator;
1280 unsigned long nb=0 ; //for using only the BindingIterator
1281 // to access the bindings
1282 CosNaming::Binding_var _binding ;
1283 CosNaming::NamingContext_var _ref_context = _current_context;
1284 CosNaming::NamingContext_var _temp_context = _current_context;
1286 _current_context->list(nb, _binding_list, _binding_iterator) ;
1288 while ((_binding_iterator->next_one(_binding)) && _continue) {
1289 CosNaming::Name _bindingName = _binding->binding_name;
1290 if (_binding->binding_type == CosNaming::ncontext)
1292 // We work on a directory, the search should be done in this directory
1294 result_path[length_result] = new char(strlen(_bindingName[0].id) + 1);
1295 strcpy(result_path[length_result],_bindingName[0].id);
1296 //SCRUTE(result_path[length_result])
1299 CORBA::Object_var _obj =_current_context->resolve(_bindingName);
1300 _temp_context = CosNaming::NamingContext::_narrow(_obj);
1302 if (_temp_context->_is_equivalent(context_to_found))
1304 //MESSAGE("The context is found, we stop the search");
1306 //SCRUTE(_continue);
1310 //SCRUTE(_bindingName[0].id);
1311 Change_Directory(_bindingName[0].id);
1312 _current_directory(result_path,length_result,
1313 context_to_found, _continue );
1316 // We'll go back to the initial context
1317 _current_context = _ref_context ;
1318 //MESSAGE("Just before the delete of ")
1319 //SCRUTE(result_path[length_result-1]);
1320 delete result_path[length_result-1];
1326 _binding_iterator->destroy();
1327 // We go to the last directory where an occurence was found
1328 _current_context = _ref_context ;
1332 //----------------------------------------------------------------------
1333 /*! Function :_list_directory_recurs.
1334 * Purpose : method to list recursively all the objects contained in the tree of absCurDirectory/relativeSubDir.
1335 * \param myList The list that will be filled.
1336 * \param relativeSubDir The directory from absCurDirectory in which the objects are found.
1337 * \param absCurDirectory The directory in ABSOLUTE form.
1338 * _current_context must refer to absCurDirectory.
1340 //----------------------------------------------------------------------
1341 void SALOME_NamingService::_list_directory_recurs(vector<string>& myList, const char *relativeSubDir,const char *absCurDirectory)
1343 CosNaming::BindingList_var _binding_list;
1344 CosNaming::BindingIterator_var _binding_iterator;
1345 unsigned long nb=0 ; // for using only the BindingIterator to access the bindings
1346 CosNaming::Binding_var _binding ;
1349 CosNaming::NamingContext_var _ref_context = _current_context;
1352 Change_Directory(relativeSubDir);
1353 absDir=new char[strlen(absCurDirectory)+2+strlen(relativeSubDir)];
1354 strcpy(absDir,absCurDirectory);
1355 strcat(absDir,relativeSubDir);
1359 absDir=(char *)absCurDirectory;
1360 _current_context->list(nb, _binding_list, _binding_iterator) ;
1362 while (_binding_iterator->next_one(_binding)) {
1363 CosNaming::Name _bindingName = _binding->binding_name;
1364 if (_binding->binding_type == CosNaming::ncontext) {
1365 _list_directory_recurs(myList,_bindingName[0].id,absDir);
1367 else if (_binding->binding_type == CosNaming::nobject) {
1368 char *elt=new char[strlen(absDir)+2+strlen(_bindingName[0].id)];
1370 strcat(elt,_bindingName[0].id);
1371 myList.push_back(elt);
1377 _current_context = _ref_context ;
1381 _binding_iterator->destroy();
1384 //----------------------------------------------------------------------