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 +1];
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();
741 //----------------------------------------------------------------------
742 /*! Function : Destroy_Name
743 * Purpose : method to destroy an association Path-Object Reference.
744 * WARNING : The complete Path should be given.
745 * If the NamingService is out, the exception ServiceUnreachable is thrown
746 * \param Path const char* arguments
748 //----------------------------------------------------------------------
750 void SALOME_NamingService::Destroy_Name(const char* Path)
751 throw(ServiceUnreachable)
753 MESSAGE("BEGIN OF Destroy_Name");
754 int dimension_Path = strlen(Path) + 1;
755 char** resultat_resolve_Path = new char* [dimension_Path];
757 // _current_context is replaced to the _root_context
758 // if the Path begins whith '/'
759 if (Path[0]=='/') _current_context = _root_context;
762 //the resolution of the directory path has to be done
763 //to place the currect_context to the correct node
764 int dimension_resultat = 0;
765 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
767 CosNaming::Name _context_name;
768 if (dimension_resultat>1)
770 // We go in the directory where the object to destroy is
771 _context_name.length(dimension_resultat-1);
773 _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
777 CORBA::Object_var _obj = _current_context->resolve(_context_name);
778 _current_context = CosNaming::NamingContext::_narrow(_obj);
780 catch (CosNaming::NamingContext::NotFound& ex)
782 CosNaming::Name n = ex.rest_of_name;
783 if (ex.why == CosNaming::NamingContext::missing_node)
784 INFOS( "Destroy_Name() : " << (char *) n[0].id
785 << " (" << (char *) n[0].kind << ") not found" )
786 if (ex.why == CosNaming::NamingContext::not_context)
787 INFOS( "Destroy_Name() : " << (char *) n[0].id
788 << " (" << (char *) n[0].kind
789 << ") is not a context" )
790 if (ex.why == CosNaming::NamingContext::not_object)
791 INFOS( "Destroy_Name() : " << (char *) n[0].id
792 << " (" << (char *) n[0].kind
793 << ") is not an object" )
795 catch (CosNaming::NamingContext::InvalidName &)
797 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName" )
799 catch (CosNaming::NamingContext::CannotProceed &)
801 INFOS( "!!!Destroy_Name(): CosNaming::NamingContext::CannotProceed" )
803 catch(CORBA::COMM_FAILURE&)
805 INFOS( "!!!Destroy_Name() : CORBA::COMM_FAILURE : unable to contact"
806 << " the naming service")
807 throw ServiceUnreachable();
811 // the last element is the object to destroy
812 _context_name.length(1);
813 _context_name[0].id =
814 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
815 _context_name[0].kind = CORBA::string_dup("object");
816 SCRUTE(_context_name[0].id);
817 ASSERT(!CORBA::is_nil(_current_context));
818 // Object destruction
821 _current_context->unbind(_context_name);
822 MESSAGE( "The object " << _context_name[0].id << " has been deleted" )
824 catch(CosNaming::NamingContext::NotFound& ex)
826 CosNaming::Name n = ex.rest_of_name;
827 if (ex.why == CosNaming::NamingContext::missing_node)
828 INFOS( "Destroy_Name() : " << (char *) n[0].id
829 << " (" << (char *) n[0].kind << ") not found" )
830 if (ex.why == CosNaming::NamingContext::not_context)
831 INFOS( "Destroy_Name() : " << (char *) n[0].id
832 << " (" << (char *) n[0].kind
833 << ") is not a context" )
834 if (ex.why == CosNaming::NamingContext::not_object)
835 INFOS( "Destroy_Name() : " << (char *) n[0].id
836 << " (" << (char *) n[0].kind
837 << ") is not an object" )
839 catch(CosNaming::NamingContext::CannotProceed&)
841 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::CannotProceed")
843 catch(CosNaming::NamingContext::InvalidName&)
845 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName")
847 catch(CORBA::COMM_FAILURE&)
849 INFOS( "!!!Destroy_Name() :CORBA::COMM_FAILURE : unable to contact"
850 << " the naming service")
851 throw ServiceUnreachable();
853 // Memory destruction
854 for (int i = 0 ; i <dimension_resultat ;i++)
856 delete [] resultat_resolve_Path[i];
858 delete[] resultat_resolve_Path ;
861 //----------------------------------------------------------------------
862 /*! Function : Destroy_Directory.
863 * Purpose : method to destroy a directory if it is empty.
864 * WARNING : The complete Path to the directory (from the root_context)
865 * to destroy should be given.
866 * If the NamingService is out, the exception ServiceUnreachable is thrown.
867 * \param Path const char* arguments
869 //----------------------------------------------------------------------
871 void SALOME_NamingService::Destroy_Directory(const char* Path)
872 throw(ServiceUnreachable)
874 MESSAGE("BEGIN OF Destroy_Directory");
875 int dimension_Path = strlen(Path) + 1;
876 char** resultat_resolve_Path = new char* [dimension_Path];
878 // _current_context is replaced to the _root_context
879 // if the Path begins whith '/'
880 if (Path[0]=='/') _current_context = _root_context;
882 CosNaming::NamingContext_var _ref_context = _current_context;
884 //the resolution of the directory path has to be done
885 //to place the currect_context to the correct node
886 int dimension_resultat = 0;
887 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
889 CosNaming::Name _context_name;
890 if (dimension_resultat>1)
892 // We go in the directory where the context to destroy is
893 _context_name.length(dimension_resultat-1);
895 _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
899 CORBA::Object_var _obj = _current_context->resolve(_context_name);
900 _current_context = CosNaming::NamingContext::_narrow(_obj);
901 _ref_context = _current_context ;
903 catch (CosNaming::NamingContext::NotFound& ex)
905 CosNaming::Name n = ex.rest_of_name;
906 if (ex.why == CosNaming::NamingContext::missing_node)
907 INFOS( "Destroy_Directory() : " << (char *) n[0].id
908 << " (" << (char *) n[0].kind << ") not found")
909 if (ex.why == CosNaming::NamingContext::not_context)
910 INFOS( "Destroy_Directory() : " << (char *) n[0].id
911 << " (" << (char *) n[0].kind
912 << ") is not a context" )
913 if (ex.why == CosNaming::NamingContext::not_object)
914 INFOS( "Destroy_Directory() : " << (char *) n[0].id
915 << " (" << (char *) n[0].kind
916 << ") is not an object" )
918 catch (CosNaming::NamingContext::InvalidName &)
920 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
922 catch (CosNaming::NamingContext::CannotProceed &)
924 INFOS("!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
926 catch(CORBA::COMM_FAILURE&)
928 INFOS( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
929 << " the naming service" )
930 throw ServiceUnreachable();
934 // the last element is the context to destroy
935 _context_name.length(1);
936 _context_name[0].id =
937 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
938 _context_name[0].kind = CORBA::string_dup("dir");
939 SCRUTE(_context_name[0].id);
943 // We go in the context to destroy
944 CORBA::Object_var _obj = _current_context->resolve(_context_name);
945 _current_context = CosNaming::NamingContext::_narrow(_obj);
947 catch (CosNaming::NamingContext::NotFound& ex)
949 CosNaming::Name n = ex.rest_of_name;
950 if (ex.why == CosNaming::NamingContext::missing_node)
951 INFOS( "Destroy_Directory() : " << (char *) n[0].id
952 << " (" << (char *) n[0].kind << ") not found" )
953 if (ex.why == CosNaming::NamingContext::not_context)
954 INFOS( "Destroy_Directory() : " << (char *) n[0].id
955 << " (" << (char *) n[0].kind
956 << ") is not a context" )
957 if (ex.why == CosNaming::NamingContext::not_object)
958 INFOS( "Destroy_Directory() : " << (char *) n[0].id
959 << " (" << (char *) n[0].kind
960 << ") is not an object" )
962 catch (CosNaming::NamingContext::InvalidName &)
964 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
966 catch (CosNaming::NamingContext::CannotProceed &)
968 INFOS( "!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
970 catch(CORBA::COMM_FAILURE&)
972 INFOS( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
973 << " the naming service" )
974 throw ServiceUnreachable();
977 ASSERT(!CORBA::is_nil(_current_context));
978 // Context Destruction
981 _current_context->destroy();
982 MESSAGE( "The context " << _context_name[0].id << " has been deleted" )
984 catch(CosNaming::NamingContext::NotEmpty&)
986 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::NoEmpty "
987 << Path << " is not empty" )
989 catch(CORBA::COMM_FAILURE&)
991 INFOS( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : "
992 << "unable to contact the naming service")
993 throw ServiceUnreachable();
995 // We go to the directory just before the context to delete
996 _current_context = _ref_context ;
999 _current_context->unbind(_context_name);
1000 MESSAGE( "The bind to the context " << _context_name[0].id << " has been deleted" )
1002 catch(CosNaming::NamingContext::NotFound& ex)
1004 CosNaming::Name n = ex.rest_of_name;
1005 if (ex.why == CosNaming::NamingContext::missing_node)
1006 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1007 << " (" << (char *) n[0].kind << ") not found" )
1008 if (ex.why == CosNaming::NamingContext::not_context)
1009 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1010 << " (" << (char *) n[0].kind
1011 << ") is not a context" )
1012 if (ex.why == CosNaming::NamingContext::not_object)
1013 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1014 << " (" << (char *) n[0].kind
1015 << ") is not an object" )
1017 catch(CosNaming::NamingContext::CannotProceed&)
1019 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::CannotProceed")
1021 catch(CosNaming::NamingContext::InvalidName&)
1023 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName")
1025 catch(CORBA::COMM_FAILURE&)
1027 INFOS( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : unable to contact"
1028 << " the naming service")
1029 throw ServiceUnreachable();
1031 // Memory destruction
1032 for (int i = 0 ; i <dimension_resultat ;i++)
1034 delete [] resultat_resolve_Path[i];
1036 delete[] resultat_resolve_Path ;
1039 //----------------------------------------------------------------------
1040 /*! Function : _initialize_root_context
1041 * Purpose : method called by constructor to initialize _root_context
1043 //----------------------------------------------------------------------
1045 void SALOME_NamingService::_initialize_root_context()
1047 //MESSAGE("Get the root context");
1050 CORBA::Object_var obj = _orb->resolve_initial_references("NameService");
1051 _root_context = CosNaming::NamingContext::_narrow(obj);
1052 _current_context = _root_context ;
1053 ASSERT(!CORBA::is_nil(_root_context));
1056 catch(CORBA::COMM_FAILURE&)
1058 INFOS("CORBA::COMM_FAILURE: unable to contact the naming service");
1059 throw ServiceUnreachable();
1063 INFOS("Unknown Exception: unable to contact the naming service");
1064 throw ServiceUnreachable();
1068 //----------------------------------------------------------------------
1069 /*! Function : _resolve_Path
1070 * Purpose : method to decompose a Path : /Kernel/Services/Sessions.
1072 * \return a char* containing the first char between '/' (in this case Kernel)
1074 //----------------------------------------------------------------------
1076 char* SALOME_NamingService::_resolve_Path(char* Path)
1079 int length = strlen(Path);
1082 if (length==0) return NULL;
1085 while ((i<length) && (Path[i]!='/'))
1087 resultat = new char[i+1];
1088 strncpy(resultat,Path,i);
1094 //----------------------------------------------------------------------
1095 /*! Function : _result_resolve_Path.
1096 * Purpose : method to decompose a Path : /Kernel/Services/Sessions.
1097 * Gives an array of char* containing Kernel, Services, Sessions.
1098 * \param Path const char* arguments, the Path to decompose
1099 * \param j int& arguments, the size of the array of char*
1100 * \param resultat_resolve_Path char** arguments
1102 //----------------------------------------------------------------------
1105 SALOME_NamingService::_result_resolve_Path(const char* Path,
1107 char ** resultat_resolve_Path)
1109 //MESSAGE("BEGIN OF _result_resolve_Path");
1110 int dimension_Path = strlen(Path) + 1;
1111 char** temp= new char* [dimension_Path];
1112 char** tempslash = new char* [dimension_Path];
1114 temp[j] = new char[dimension_Path];
1115 strcpy(temp[j],Path);
1117 while (strlen(temp[j])>0)
1119 // temp[j] contains the characters to be treated :
1120 // (Path - characters already treted)
1121 // tempslash[j] = temp[j] if the string temp[j] doesn't begin whith '/'
1122 // tempslash[j] = temp[j] without '/' if the string begins whith '/'
1123 int length_temp = strlen(temp[j]);
1124 if (temp[j][0]=='/')
1126 // the characters to be treated begin whith '/'
1127 // we don't have to take the '/'
1128 tempslash[j] = new char [length_temp] ;
1129 for (int k = 0; k < length_temp-1; k++)
1130 tempslash[j][k] = temp[j][k+1];
1131 tempslash[j][length_temp-1]='\0';
1135 //the characters to be trated don't begin with '/'
1136 // Nothing to be done on the char
1137 tempslash[j] = new char [length_temp+1] ;
1138 strcpy(tempslash[j],temp[j]);
1140 // decomposition of the Path
1141 resultat_resolve_Path[j]= _resolve_Path(tempslash[j]);
1142 //SCRUTE(resultat_resolve_Path[j]);
1144 int length_resultat = strlen(resultat_resolve_Path[j]) ;
1145 int dimension_temp = length_temp -length_resultat ;
1147 temp[j] = new char[dimension_temp +1];
1148 for (int i = 0 ; i <dimension_temp ;i++)
1150 temp[j][i] =tempslash[j-1][i+ length_resultat];
1152 temp[j][dimension_temp]= '\0';
1155 // Memory destruction
1156 for (int i = 0 ; i <j;i++)
1159 delete [] tempslash[i];
1162 delete [] tempslash ;
1165 //----------------------------------------------------------------------
1166 /*! Function : _Find.
1167 * Purpose : method to research a name from the current directory
1168 * of the naming service.
1169 * The naming service changes directory to go to the directory where
1170 * the last occurence was found.
1171 * \param name const char* arguments
1172 * \param occurence_number CORBA::LONG (by value)
1174 //----------------------------------------------------------------------
1176 void SALOME_NamingService::_Find(const char* name,
1177 CORBA::Long& occurence_number)
1179 //MESSAGE("BEGIN OF _Find") SCRUTE(name);
1180 CosNaming::BindingList_var _binding_list;
1181 CosNaming::BindingIterator_var _binding_iterator;
1182 unsigned long nb=0 ; //for using only the BindingIterator
1183 // to access the bindings
1184 CosNaming::Binding_var _binding ;
1185 CosNaming::NamingContext_var _ref_context = _current_context;
1186 CosNaming::NamingContext_var _found_context = _current_context;
1188 _current_context->list(nb, _binding_list, _binding_iterator) ;
1190 while (_binding_iterator->next_one(_binding)) {
1191 CosNaming::Name _bindingName = _binding->binding_name;
1192 if (_binding->binding_type == CosNaming::ncontext) {
1193 // We work on a directory, the search should be done in this directory
1194 Change_Directory(_bindingName[0].id);
1195 _Find(name,occurence_number);
1196 // We'll go back to the initial context
1197 _current_context = _ref_context ;
1199 else if (_binding->binding_type == CosNaming::nobject) {
1200 // We work on an object...
1201 if (!strcmp( _bindingName[0].id,name))
1203 //MESSAGE("One occurence was found");
1205 // We keep in memory the directory where one occurence was found
1206 _found_context = _current_context ;
1210 _binding_iterator->destroy();
1211 // We go to the last directory where an occurence was found
1212 _current_context = _found_context ;
1213 //SCRUTE(occurence_number);
1216 //----------------------------------------------------------------------
1217 /*! Function : _create_context_name_dir.
1218 * Purpose : method to create a Context_name from an array of char.
1219 * The number of elements to be copied are indicated
1222 * \param resultat_resolve_Path char** arguments
1223 * \param length_copy int arguments
1224 * \param _context_name CosNaming::Name arguments (by value)
1226 //----------------------------------------------------------------------
1229 SALOME_NamingService::_create_context_name_dir(char** resultat_resolve_Path
1231 CosNaming::Name& _context_name)
1233 //MESSAGE("BEGIN OF _create_context_name_dir");
1234 for (int i = 0 ; i < length_copy;i++)
1236 _context_name[i].id = CORBA::string_dup(resultat_resolve_Path[i]);
1237 _context_name[i].kind = CORBA::string_dup("dir");
1238 //SCRUTE(_context_name[i].id);
1242 //----------------------------------------------------------------------
1243 /*! Function : _current_directory.
1244 * Purpose : method to parse the naming service tree to find a context
1245 * and determine the path to go to this context from the
1247 * \param result_path char** arguments
1248 * \param length_result int arguments by value
1249 * \param context_to_found CosNaming::NamingContext_var arguments
1250 * \param _continue boolean arguments
1252 //----------------------------------------------------------------------
1255 SALOME_NamingService::_current_directory(char** result_path,
1257 CosNaming::NamingContext_var context_to_found,
1258 CORBA::Boolean& _continue)
1260 //MESSAGE("BEGIN OF _current_Directory");
1261 CosNaming::BindingList_var _binding_list;
1262 CosNaming::BindingIterator_var _binding_iterator;
1263 unsigned long nb=0 ; //for using only the BindingIterator
1264 // to access the bindings
1265 CosNaming::Binding_var _binding ;
1266 CosNaming::NamingContext_var _ref_context = _current_context;
1267 CosNaming::NamingContext_var _temp_context = _current_context;
1269 _current_context->list(nb, _binding_list, _binding_iterator) ;
1271 while ((_binding_iterator->next_one(_binding)) && _continue) {
1272 CosNaming::Name _bindingName = _binding->binding_name;
1273 if (_binding->binding_type == CosNaming::ncontext)
1275 // We work on a directory, the search should be done in this directory
1277 result_path[length_result] = new char(strlen(_bindingName[0].id) + 1);
1278 strcpy(result_path[length_result],_bindingName[0].id);
1279 //SCRUTE(result_path[length_result])
1282 CORBA::Object_var _obj =_current_context->resolve(_bindingName);
1283 _temp_context = CosNaming::NamingContext::_narrow(_obj);
1285 if (_temp_context->_is_equivalent(context_to_found))
1287 //MESSAGE("The context is found, we stop the search");
1289 //SCRUTE(_continue);
1293 //SCRUTE(_bindingName[0].id);
1294 Change_Directory(_bindingName[0].id);
1295 _current_directory(result_path,length_result,
1296 context_to_found, _continue );
1299 // We'll go back to the initial context
1300 _current_context = _ref_context ;
1301 //MESSAGE("Just before the delete of ")
1302 //SCRUTE(result_path[length_result-1]);
1303 delete result_path[length_result-1];
1309 _binding_iterator->destroy();
1310 // We go to the last directory where an occurence was found
1311 _current_context = _ref_context ;
1313 //----------------------------------------------------------------------