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
30 #include "SALOME_NamingService.hxx"
31 #include "ServiceUnreachable.hxx"
35 //----------------------------------------------------------------------
36 /*! Function : SALOME_NamingService
37 * Purpose : Constructor and Initialisation of _root_context
39 //----------------------------------------------------------------------
41 SALOME_NamingService::SALOME_NamingService()
43 MESSAGE("SALOME_NamingService default constructor");
44 _orb = CORBA::ORB::_nil();
47 //----------------------------------------------------------------------
48 /*! Function : SALOME_NamingService
49 * \param orb CORBA::ORB_ptr arguments
51 //----------------------------------------------------------------------
53 SALOME_NamingService::SALOME_NamingService(CORBA::ORB_ptr orb)
55 MESSAGE("SALOME_NamingService creation");
57 _initialize_root_context();
60 //----------------------------------------------------------------------
61 /*! Function : ~SALOME_NamingService
62 * Purpose : Destructor
64 //----------------------------------------------------------------------
66 SALOME_NamingService::~SALOME_NamingService()
68 MESSAGE("SALOME_NamingService destruction");
71 //----------------------------------------------------------------------
72 /*! Function : init_orb
73 * initialize ORB reference after default constructor
75 //----------------------------------------------------------------------
77 void SALOME_NamingService::init_orb(CORBA::ORB_ptr orb)
79 // MESSAGE("SALOME_NamingService initialisation");
81 _initialize_root_context();
85 //----------------------------------------------------------------------
86 /*! Function : Register
87 * Method to create an association in the NamingService between ObjRef
89 * If the NamingService is out, the exception ServiceUnreachable is thrown
90 * \param ObjRef CORBA::Object_ptr arguments
91 * \param Path const char* arguments
93 //----------------------------------------------------------------------
95 void SALOME_NamingService::Register(CORBA::Object_ptr ObjRef,
97 throw(ServiceUnreachable)
99 // MESSAGE("BEGIN OF Register: "<< Path);
100 int dimension_Path = strlen(Path) + 1;
101 char** resultat_resolve_Path = new char* [dimension_Path];
103 // _current_context is replaced to the _root_context
104 // if the Path begins whith '/'
106 _current_context = _root_context;
107 // MESSAGE("Gone to the _root_context");
110 //the resolution of the directory path has to be done
111 //to place the currect_context to the correct node
112 int dimension_resultat = 0;
113 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
115 CosNaming::Name _context_name;
116 CORBA::Boolean _not_exist = false ;
117 CosNaming::NamingContext_var _temp_context;
119 if(dimension_resultat>1)
121 // A directory is treated (not only an object name)
122 // We had to test if the directory where ObjRef should be recorded
124 // If not, the new context has to be created
126 // MESSAGE("A complet Path has to be treated, not only an object name");
127 _context_name.length(dimension_resultat-1);
129 _create_context_name_dir(resultat_resolve_Path,dimension_resultat-1,
134 CORBA::Object_var _obj = _current_context->resolve(_context_name);
135 _current_context = CosNaming::NamingContext::_narrow(_obj);
137 catch (CosNaming::NamingContext::NotFound &)
139 // failed to resolve, therefore assume cold start
142 catch (CosNaming::NamingContext::InvalidName &)
144 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
146 catch (CosNaming::NamingContext::CannotProceed &)
148 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
150 catch(CORBA::COMM_FAILURE&)
152 INFOS("!!!Register() : CORBA::COMM_FAILURE : unable to contact"
153 << " the naming service");
154 throw ServiceUnreachable();
160 _context_name.length(1);
161 // MESSAGE("The Path indicated is not yet created. It will soon be done");
162 for (int i = 0 ; i <dimension_resultat -1 ;i++)
164 _context_name[0].id =
165 CORBA::string_dup(resultat_resolve_Path[i]);
166 _context_name[0].kind = CORBA::string_dup("dir");
167 // SCRUTE(_context_name[0].id);
168 //The Path could be in part already created.
172 // this context is already created.
173 // Nothing to be done
174 CORBA::Object_var _obj =
175 _current_context->resolve(_context_name);
177 CosNaming::NamingContext::_narrow(_obj);
178 //MESSAGE("This context was already created");
180 catch (CosNaming::NamingContext::NotFound &)
182 // This context is not created. It will be done
184 _current_context->bind_new_context(_context_name);
185 _current_context = _temp_context;
186 //INFOS("This context was'nt created, it's now done");
190 catch (CosNaming::NamingContext::AlreadyBound&)
192 INFOS("!!!Register() : CosNaming::NamingContext::AlreadyBound");
194 catch(CosNaming::NamingContext::NotFound& ex)
196 CosNaming::Name n = ex.rest_of_name;
197 if (ex.why == CosNaming::NamingContext::missing_node)
198 INFOS("Register() : " << (char *) n[0].id
199 << " (" << (char *) n[0].kind << ") not found");
200 if (ex.why == CosNaming::NamingContext::not_context)
201 INFOS("Register() : " << (char *) n[0].id
202 << " (" << (char *) n[0].kind
203 << ") is not a context");
204 if (ex.why == CosNaming::NamingContext::not_object)
205 INFOS("Register() : " << (char *) n[0].id
206 << " (" << (char *) n[0].kind
207 << ") is not an object");
209 catch(CosNaming::NamingContext::CannotProceed&)
211 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
213 catch(CosNaming::NamingContext::InvalidName&)
215 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
217 catch(CORBA::COMM_FAILURE&)
219 INFOS("!!!Register() :CORBA::COMM_FAILURE : unable to contact"
220 << " the naming service");
221 throw ServiceUnreachable();
226 // The current directory is now the directory where the object should
228 _context_name.length(1);
231 // the last element is an object an not a directory
232 _context_name[0].id =
233 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
234 _context_name[0].kind = CORBA::string_dup("object");
235 //SCRUTE(_context_name[0].id);
237 _current_context->bind(_context_name, ObjRef);
238 // MESSAGE("A new element " << _context_name[0].id
239 // << " is recorded in the _current_context");
241 catch(CosNaming::NamingContext::NotFound& ex)
243 CosNaming::Name n = ex.rest_of_name;
244 if (ex.why == CosNaming::NamingContext::missing_node)
245 INFOS("Register() : " << (char *) n[0].id
246 << " (" << (char *) n[0].kind << ") not found");
247 if (ex.why == CosNaming::NamingContext::not_context)
248 INFOS("Register() : " << (char *) n[0].id
249 << " (" << (char *) n[0].kind
250 << ") is not a context");
251 if (ex.why == CosNaming::NamingContext::not_object)
252 INFOS("Register() : " << (char *) n[0].id
253 << " (" << (char *) n[0].kind
254 << ") is not an object");
256 catch(CosNaming::NamingContext::CannotProceed&)
258 INFOS("!!!Register() : CosNaming::NamingContext::CannotProceed");
260 catch(CosNaming::NamingContext::InvalidName&)
262 INFOS("!!!Register() : CosNaming::NamingContext::InvalidName");
264 catch(CosNaming::NamingContext::AlreadyBound&)
266 INFOS("!!!Register() : CosNaming::NamingContext::AlreadyBound, object will be rebind");
267 _current_context->rebind(_context_name, ObjRef);
269 catch(CORBA::COMM_FAILURE&)
271 INFOS("!!!Register() :CORBA::COMM_FAILURE : unable to contact"
272 << " the naming service");
273 throw ServiceUnreachable();
277 // Memory destruction
278 for (int i = 0 ; i <dimension_resultat ;i++)
280 delete [] resultat_resolve_Path[i];
282 delete[] resultat_resolve_Path ;
285 //----------------------------------------------------------------------
286 /*! Function : Resolve
287 * Purpose : method to get the ObjRef of a symbolic name
288 * If the NamingService is out, the exception ServiceUnreachable is thrown
289 * \param Path const char* arguments
290 * \return the object reference
292 //----------------------------------------------------------------------
294 CORBA::Object_ptr SALOME_NamingService::Resolve(const char* Path)
295 throw(ServiceUnreachable)
297 //MESSAGE("BEGIN OF Resolve: " << Path);
298 int dimension_Path = strlen(Path) + 1;
299 char** resultat_resolve_Path = new char* [dimension_Path];
301 // _current_context is replaced to the _root_context
302 // if the Path begins whith '/'
303 if (Path[0]=='/') _current_context = _root_context;
306 //the resolution of the directory path has to be done
307 //to place the currect_context to the correct node
308 int dimension_resultat = 0;
309 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
311 CosNaming::Name _context_name;
312 _context_name.length(dimension_resultat);
313 CORBA::Object_ptr _obj = NULL ;
315 _create_context_name_dir(resultat_resolve_Path,dimension_resultat-1,
317 // the last element is an object an not a directory
318 _context_name[dimension_resultat -1].id =
319 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
320 _context_name[dimension_resultat -1].kind = CORBA::string_dup("object");
321 // SCRUTE(_context_name[dimension_resultat -1].id);
322 ASSERT(!CORBA::is_nil(_current_context));
326 _obj =_current_context->resolve(_context_name);
328 catch(CosNaming::NamingContext::NotFound& ex)
330 CosNaming::Name n = ex.rest_of_name;
331 if (ex.why == CosNaming::NamingContext::missing_node)
332 INFOS("Resolve() : " << (char *) n[0].id
333 << " (" << (char *) n[0].kind << ") not found");
334 if (ex.why == CosNaming::NamingContext::not_context)
336 << (char *) n[0].id << " (" << (char *) n[0].kind
337 << ") is not a context");
338 if (ex.why == CosNaming::NamingContext::not_object)
339 INFOS("Resolve() : " << (char *) n[0].id
340 << " (" << (char *) n[0].kind
341 << ") is not an object");
343 catch(CosNaming::NamingContext::CannotProceed&)
345 INFOS("!!!Resolve() : CosNaming::NamingContext::CannotProceed");
347 catch(CosNaming::NamingContext::InvalidName&)
349 INFOS("!!!Resolve() : CosNaming::NamingContext::InvalidName");
351 catch(CORBA::COMM_FAILURE&)
353 INFOS("!!!Resolve() :CORBA::COMM_FAILURE : unable to contact"
354 << "the naming service");
355 throw ServiceUnreachable();
357 // Memory destruction
358 for (int i = 0 ; i <dimension_resultat ;i++)
360 delete [] resultat_resolve_Path[i];
362 delete[] resultat_resolve_Path ;
367 //----------------------------------------------------------------------
369 * Purpose : method to research a name from the current directory
370 * of the naming service.
371 * The naming service changes directory to go to the directory where
372 * the last occurence was found.
373 * If the NamingService is out, the exception ServiceUnreachable is thrown
374 * \param name const char* arguments
375 * \return the number of occurences found
378 //----------------------------------------------------------------------
380 int SALOME_NamingService::Find(const char* name)
381 throw(ServiceUnreachable)
383 // MESSAGE("BEGIN OF Find " << name);
384 CORBA::Long occurence_number = 0 ;
387 _Find(name,occurence_number);
389 catch(CORBA::COMM_FAILURE&)
391 INFOS("!!!Find() : CORBA::COMM_FAILURE : unable to contact"
392 << " the naming service");
393 throw ServiceUnreachable();
395 return occurence_number;
398 //----------------------------------------------------------------------
399 /*! Function : Create_Directory
400 * Purpose : method to create a directory from the current directory.
401 * If the NamingService is out, the exception ServiceUnreachable is thrown
402 * \param Path const char* arguments
403 * \return a boolean to indicate if the creation succeeded
405 //----------------------------------------------------------------------
407 bool SALOME_NamingService::Create_Directory(const char* Path)
408 throw(ServiceUnreachable)
410 //MESSAGE("BEGIN OF Create_Directory");
411 int dimension_Path = strlen(Path) + 1;
412 char** resultat_resolve_Path= new char* [dimension_Path];;
413 CORBA::Boolean _return_code = true ;
415 // _current_context is replaced to the _root_context
416 // if the Path begins whith '/'
417 if (Path[0]=='/') _current_context = _root_context;
419 int dimension_resultat = 0;
420 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
423 // We had to test if a part of the directory to treat
425 // If not, the new context has to be created
427 CosNaming::Name _context_name;
428 _context_name.length(1);
429 CosNaming::NamingContext_var _temp_context;
430 ASSERT(!CORBA::is_nil(_current_context));
435 for (int i = 0 ; i <dimension_resultat ;i++)
437 _context_name[0].id =
438 CORBA::string_dup(resultat_resolve_Path[i]);
439 _context_name[0].kind = CORBA::string_dup("dir");
440 // SCRUTE(_context_name[0].id);
441 //The Path could be in part already created.
445 // this context is already created.
446 // Nothing to be done
447 CORBA::Object_var _obj =
448 _current_context->resolve(_context_name);
450 CosNaming::NamingContext::_narrow(_obj);
451 MESSAGE("This context was already created");
453 catch (CosNaming::NamingContext::NotFound &)
455 // This context is not created. It will be done
457 _current_context->bind_new_context(_context_name);
458 _current_context = _temp_context;
459 INFOS("This context was'nt created, it's now done");
463 catch (CosNaming::NamingContext::AlreadyBound&)
465 INFOS("!!! Create_Directory() CosNaming::NamingContext::AlreadyBound");
466 _return_code = false;
468 catch(CosNaming::NamingContext::NotFound& ex)
470 _return_code = false;
471 CosNaming::Name n = ex.rest_of_name;
472 if (ex.why == CosNaming::NamingContext::missing_node)
473 INFOS("Create_Directory() : " << (char *) n[0].id
474 << " (" << (char *) n[0].kind << ") not found");
475 if (ex.why == CosNaming::NamingContext::not_context)
476 INFOS("Create_Directory() : " << (char *) n[0].id
477 << " (" << (char *) n[0].kind
478 << ") is not a context");
479 if (ex.why == CosNaming::NamingContext::not_object)
480 INFOS("Create_Directory() : " << (char *) n[0].id
481 << " (" << (char *) n[0].kind
482 << ") is not an object");
484 catch(CosNaming::NamingContext::CannotProceed&)
486 _return_code = false;
487 INFOS("!!!Create_Directory():CosNaming::NamingContext::CannotProceed");
489 catch(CosNaming::NamingContext::InvalidName&)
491 _return_code = false;
492 INFOS("!!!Create_Directory():CosNaming::NamingContext::InvalidName");
494 catch(CORBA::COMM_FAILURE&)
496 _return_code = false;
497 INFOS("!!!Register() :CORBA::COMM_FAILURE : unable to contact"
498 << " the naming service");
499 throw ServiceUnreachable();
501 // Memory destruction
502 for (int i = 0 ; i <dimension_resultat;i++)
504 delete [] resultat_resolve_Path[i];
506 delete[] resultat_resolve_Path ;
510 //----------------------------------------------------------------------
511 /*! Function : Change_Directory
512 * Purpose : method to change the current directory to the
513 * directory Path indicated in "in" Parameter.
514 * If Path ="/", the current directory changes to the root directory.
515 * If the NamingService is out, the exception ServiceUnreachable is thrown.
516 * \param Path const char* arguments
517 * \return a boolean to indicate if the change succeeded
519 //----------------------------------------------------------------------
521 bool SALOME_NamingService::Change_Directory(const char* Path)
522 throw(ServiceUnreachable)
524 //MESSAGE("BEGIN OF Change_Directory " << Path);
525 int dimension_Path = strlen(Path) + 1;
526 char** resultat_resolve_Path = new char* [dimension_Path];
527 CORBA::Boolean _return_code = true ;
529 // _current_context is replaced to the _root_context
530 // if the Path begins whith '/'
531 if (Path[0]=='/') _current_context = _root_context;
533 if ((Path[0]=='/') && (dimension_Path == 2))
535 MESSAGE("Change_Directory is called to go to the root_context");
537 //nothing to de done, the change_dur is called to go to the root_context
538 // no Path to resolve
540 //the resolution of the directory path has to be done
541 //to place the currect_context to the correct node
543 int dimension_resultat = 0;
544 _result_resolve_Path(Path,dimension_resultat,resultat_resolve_Path);
546 CosNaming::Name _context_name;
547 _context_name.length(dimension_resultat);
548 CORBA::Object_var _obj;
550 _create_context_name_dir(resultat_resolve_Path,dimension_resultat,
553 ASSERT(!CORBA::is_nil(_current_context));
557 _obj =_current_context->resolve(_context_name);
558 _current_context = CosNaming::NamingContext::_narrow(_obj);
559 ASSERT(!CORBA::is_nil(_current_context))
561 catch(CosNaming::NamingContext::NotFound& ex)
563 _return_code = false;
564 CosNaming::Name n = ex.rest_of_name;
565 if (ex.why == CosNaming::NamingContext::missing_node)
566 INFOS( "Change_Directory() : " << (char *) n[0].id
567 << " (" << (char *) n[0].kind << ") not found")
568 if (ex.why == CosNaming::NamingContext::not_context)
569 INFOS("Change_Directory() : " << (char *) n[0].id
570 << " (" << (char *) n[0].kind
571 << ") is not a context" )
572 if (ex.why == CosNaming::NamingContext::not_object)
573 INFOS( "Change_Directory() : " << (char *) n[0].id
574 << " (" << (char *) n[0].kind
575 << ") is not an object" )
577 catch(CosNaming::NamingContext::CannotProceed&)
579 _return_code = false;
580 INFOS( "!!!Change_Directory() : CosNaming::NamingContext::CannotProceed" )
582 catch(CosNaming::NamingContext::InvalidName&)
584 _return_code = false;
585 INFOS( "!!!Change_Directory() : CosNaming::NamingContext::InvalidName" )
587 catch(CORBA::COMM_FAILURE&)
589 _return_code = false;
590 INFOS( "!!!Change_Directory() :CORBA::COMM_FAILURE : unable to contact"
591 << "the naming service")
592 throw ServiceUnreachable();
594 // Memory destruction
595 for (int i = 0 ; i <dimension_resultat ;i++)
597 delete [] resultat_resolve_Path[i];
599 delete[] resultat_resolve_Path ;
604 //----------------------------------------------------------------------
605 /*! Function : Current_Directory
606 * Purpose : method to get the current directory.
607 * If the NamingService is out, the exception ServiceUnreachable is thrown
608 * \return the path of the current_context
609 * \sa _current_directory
611 //----------------------------------------------------------------------
613 char* SALOME_NamingService::Current_Directory()
614 throw(ServiceUnreachable)
616 //MESSAGE("BEGIN OF Current_Directory");
618 CosNaming::NamingContext_var _ref_context = _current_context;
622 char** result_path = new char*[50]; // 50 is it enough?
625 // We go to the root_context to begin the search from the root
626 _current_context = _root_context ;
627 CORBA::Boolean _continue = true ;
630 _current_directory(result_path,i,_ref_context,_continue );
632 catch(CORBA::COMM_FAILURE&)
634 INFOS("!!!Current_Directory(): CORBA::COMM_FAILURE : unable to contact"
635 << " the naming service" )
636 throw ServiceUnreachable();
638 for (int k = 0 ; k <i ;k++)
640 // We count the length of the char* + 1 for the '/' to separate
642 length_path = length_path + strlen(result_path[k]) + 1;
644 char* return_Path = new char[length_path +1];
645 return_Path[0] = '/' ;
646 return_Path[1] = '\0' ;
647 for (int k = 0 ; k <i ;k++)
649 //SCRUTE(result_path[k])
650 strcat(return_Path,result_path[k]);
651 strcat(return_Path,"/");
653 //SCRUTE(return_Path)
654 _current_context = _ref_context ;
659 //----------------------------------------------------------------------
661 * Purpose : method to list and print all the context contained from
662 * the current context
663 * If the NamingService is out, the exception ServiceUnreachable is thrown
665 //----------------------------------------------------------------------
667 void SALOME_NamingService::list()
668 throw(ServiceUnreachable)
670 MESSAGE("Begin of list");
671 CosNaming::BindingList_var _binding_list;
672 CosNaming::BindingIterator_var _binding_iterator;
673 unsigned long nb=0 ; // for using only the BindingIterator to access the bindings
674 CosNaming::Binding_var _binding ;
675 CosNaming::NamingContext_var _ref_context = _current_context;
676 _current_context->list(nb, _binding_list, _binding_iterator) ;
678 while (_binding_iterator->next_one(_binding)) {
679 CosNaming::Name _bindingName = _binding->binding_name;
680 if (_binding->binding_type == CosNaming::ncontext) {
681 MESSAGE( "Context : " << _bindingName[0].id );
684 Change_Directory(_bindingName[0].id);
686 catch (ServiceUnreachable&)
688 INFOS( "!!!list(): ServiceUnreachable" )
689 throw ServiceUnreachable();
693 _current_context = _ref_context ;
695 else if (_binding->binding_type == CosNaming::nobject) {
696 MESSAGE( "Object : " << _bindingName[0].id );
699 _binding_iterator->destroy();
702 //----------------------------------------------------------------------
703 /*! Function : list_directory
704 * Purpose : method to get all the contexts contained in the current
706 * Get only objects, isn't iterative
707 * If the NamingService is out, the exception ServiceUnreachable is thrown
709 //----------------------------------------------------------------------
710 vector<string> SALOME_NamingService::list_directory()
711 throw(ServiceUnreachable)
713 vector<string> _list ;
715 CosNaming::BindingList_var _binding_list;
716 CosNaming::BindingIterator_var _binding_iterator;
717 unsigned long nb=0 ; // for using only the BindingIterator to access the bindings
718 CosNaming::Binding_var _binding ;
719 CosNaming::NamingContext_var _ref_context = _current_context;
720 _current_context->list(nb, _binding_list, _binding_iterator) ;
721 if (_binding_iterator->_is_nil()) return _list;
723 while (_binding_iterator->next_one(_binding)) {
724 CosNaming::Name _bindingName = _binding->binding_name;
725 if (_binding->binding_type == CosNaming::nobject) {
726 _list.push_back(CORBA::string_dup(_bindingName[0].id));
729 //for (unsigned int ind = 0; ind < _list.size(); ind++)
730 // MESSAGE("list_directory : Object : " << _list[ind]);
732 _binding_iterator->destroy();
737 //----------------------------------------------------------------------
738 /*! Function : Destroy_Name
739 * Purpose : method to destroy an association Path-Object Reference.
740 * WARNING : The complete Path should be given.
741 * If the NamingService is out, the exception ServiceUnreachable is thrown
742 * \param Path const char* arguments
744 //----------------------------------------------------------------------
746 void SALOME_NamingService::Destroy_Name(const char* Path)
747 throw(ServiceUnreachable)
749 MESSAGE("BEGIN OF Destroy_Name");
750 int dimension_Path = strlen(Path) + 1;
751 char** resultat_resolve_Path = new char* [dimension_Path];
753 // _current_context is replaced to the _root_context
754 // if the Path begins whith '/'
755 if (Path[0]=='/') _current_context = _root_context;
758 //the resolution of the directory path has to be done
759 //to place the currect_context to the correct node
760 int dimension_resultat = 0;
761 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
763 CosNaming::Name _context_name;
764 if (dimension_resultat>1)
766 // We go in the directory where the object to destroy is
767 _context_name.length(dimension_resultat-1);
769 _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
773 CORBA::Object_var _obj = _current_context->resolve(_context_name);
774 _current_context = CosNaming::NamingContext::_narrow(_obj);
776 catch (CosNaming::NamingContext::NotFound& ex)
778 CosNaming::Name n = ex.rest_of_name;
779 if (ex.why == CosNaming::NamingContext::missing_node)
780 INFOS( "Destroy_Name() : " << (char *) n[0].id
781 << " (" << (char *) n[0].kind << ") not found" )
782 if (ex.why == CosNaming::NamingContext::not_context)
783 INFOS( "Destroy_Name() : " << (char *) n[0].id
784 << " (" << (char *) n[0].kind
785 << ") is not a context" )
786 if (ex.why == CosNaming::NamingContext::not_object)
787 INFOS( "Destroy_Name() : " << (char *) n[0].id
788 << " (" << (char *) n[0].kind
789 << ") is not an object" )
791 catch (CosNaming::NamingContext::InvalidName &)
793 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName" )
795 catch (CosNaming::NamingContext::CannotProceed &)
797 INFOS( "!!!Destroy_Name(): CosNaming::NamingContext::CannotProceed" )
799 catch(CORBA::COMM_FAILURE&)
801 INFOS( "!!!Destroy_Name() : CORBA::COMM_FAILURE : unable to contact"
802 << " the naming service")
803 throw ServiceUnreachable();
807 // the last element is the object to destroy
808 _context_name.length(1);
809 _context_name[0].id =
810 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
811 _context_name[0].kind = CORBA::string_dup("object");
812 SCRUTE(_context_name[0].id);
813 ASSERT(!CORBA::is_nil(_current_context));
814 // Object destruction
817 _current_context->unbind(_context_name);
818 MESSAGE( "The object " << _context_name[0].id << " has been deleted" )
820 catch(CosNaming::NamingContext::NotFound& ex)
822 CosNaming::Name n = ex.rest_of_name;
823 if (ex.why == CosNaming::NamingContext::missing_node)
824 INFOS( "Destroy_Name() : " << (char *) n[0].id
825 << " (" << (char *) n[0].kind << ") not found" )
826 if (ex.why == CosNaming::NamingContext::not_context)
827 INFOS( "Destroy_Name() : " << (char *) n[0].id
828 << " (" << (char *) n[0].kind
829 << ") is not a context" )
830 if (ex.why == CosNaming::NamingContext::not_object)
831 INFOS( "Destroy_Name() : " << (char *) n[0].id
832 << " (" << (char *) n[0].kind
833 << ") is not an object" )
835 catch(CosNaming::NamingContext::CannotProceed&)
837 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::CannotProceed")
839 catch(CosNaming::NamingContext::InvalidName&)
841 INFOS( "!!!Destroy_Name() : CosNaming::NamingContext::InvalidName")
843 catch(CORBA::COMM_FAILURE&)
845 INFOS( "!!!Destroy_Name() :CORBA::COMM_FAILURE : unable to contact"
846 << " the naming service")
847 throw ServiceUnreachable();
849 // Memory destruction
850 for (int i = 0 ; i <dimension_resultat ;i++)
852 delete [] resultat_resolve_Path[i];
854 delete[] resultat_resolve_Path ;
857 //----------------------------------------------------------------------
858 /*! Function : Destroy_Directory.
859 * Purpose : method to destroy a directory if it is empty.
860 * WARNING : The complete Path to the directory (from the root_context)
861 * to destroy should be given.
862 * If the NamingService is out, the exception ServiceUnreachable is thrown.
863 * \param Path const char* arguments
865 //----------------------------------------------------------------------
867 void SALOME_NamingService::Destroy_Directory(const char* Path)
868 throw(ServiceUnreachable)
870 MESSAGE("BEGIN OF Destroy_Directory");
871 int dimension_Path = strlen(Path) + 1;
872 char** resultat_resolve_Path = new char* [dimension_Path];
874 // _current_context is replaced to the _root_context
875 // if the Path begins whith '/'
876 if (Path[0]=='/') _current_context = _root_context;
878 CosNaming::NamingContext_var _ref_context = _current_context;
880 //the resolution of the directory path has to be done
881 //to place the currect_context to the correct node
882 int dimension_resultat = 0;
883 _result_resolve_Path (Path, dimension_resultat, resultat_resolve_Path);
885 CosNaming::Name _context_name;
886 if (dimension_resultat>1)
888 // We go in the directory where the context to destroy is
889 _context_name.length(dimension_resultat-1);
891 _create_context_name_dir(resultat_resolve_Path,dimension_resultat -1,
895 CORBA::Object_var _obj = _current_context->resolve(_context_name);
896 _current_context = CosNaming::NamingContext::_narrow(_obj);
897 _ref_context = _current_context ;
899 catch (CosNaming::NamingContext::NotFound& ex)
901 CosNaming::Name n = ex.rest_of_name;
902 if (ex.why == CosNaming::NamingContext::missing_node)
903 INFOS( "Destroy_Directory() : " << (char *) n[0].id
904 << " (" << (char *) n[0].kind << ") not found")
905 if (ex.why == CosNaming::NamingContext::not_context)
906 INFOS( "Destroy_Directory() : " << (char *) n[0].id
907 << " (" << (char *) n[0].kind
908 << ") is not a context" )
909 if (ex.why == CosNaming::NamingContext::not_object)
910 INFOS( "Destroy_Directory() : " << (char *) n[0].id
911 << " (" << (char *) n[0].kind
912 << ") is not an object" )
914 catch (CosNaming::NamingContext::InvalidName &)
916 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
918 catch (CosNaming::NamingContext::CannotProceed &)
920 INFOS("!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
922 catch(CORBA::COMM_FAILURE&)
924 INFOS( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
925 << " the naming service" )
926 throw ServiceUnreachable();
930 // the last element is the context to destroy
931 _context_name.length(1);
932 _context_name[0].id =
933 CORBA::string_dup(resultat_resolve_Path[dimension_resultat -1]);
934 _context_name[0].kind = CORBA::string_dup("dir");
935 SCRUTE(_context_name[0].id);
939 // We go in the context to destroy
940 CORBA::Object_var _obj = _current_context->resolve(_context_name);
941 _current_context = CosNaming::NamingContext::_narrow(_obj);
943 catch (CosNaming::NamingContext::NotFound& ex)
945 CosNaming::Name n = ex.rest_of_name;
946 if (ex.why == CosNaming::NamingContext::missing_node)
947 INFOS( "Destroy_Directory() : " << (char *) n[0].id
948 << " (" << (char *) n[0].kind << ") not found" )
949 if (ex.why == CosNaming::NamingContext::not_context)
950 INFOS( "Destroy_Directory() : " << (char *) n[0].id
951 << " (" << (char *) n[0].kind
952 << ") is not a context" )
953 if (ex.why == CosNaming::NamingContext::not_object)
954 INFOS( "Destroy_Directory() : " << (char *) n[0].id
955 << " (" << (char *) n[0].kind
956 << ") is not an object" )
958 catch (CosNaming::NamingContext::InvalidName &)
960 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName" )
962 catch (CosNaming::NamingContext::CannotProceed &)
964 INFOS( "!!!Destroy_Directory(): CosNaming::NamingContext::CannotProceed" )
966 catch(CORBA::COMM_FAILURE&)
968 INFOS( "!!!Destroy_Directory() : CORBA::COMM_FAILURE : unable to contact"
969 << " the naming service" )
970 throw ServiceUnreachable();
973 ASSERT(!CORBA::is_nil(_current_context));
974 // Context Destruction
977 _current_context->destroy();
978 MESSAGE( "The context " << _context_name[0].id << " has been deleted" )
980 catch(CosNaming::NamingContext::NotEmpty&)
982 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::NoEmpty "
983 << Path << " is not empty" )
985 catch(CORBA::COMM_FAILURE&)
987 INFOS( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : "
988 << "unable to contact the naming service")
989 throw ServiceUnreachable();
991 // We go to the directory just before the context to delete
992 _current_context = _ref_context ;
995 _current_context->unbind(_context_name);
996 MESSAGE( "The bind to the context " << _context_name[0].id << " has been deleted" )
998 catch(CosNaming::NamingContext::NotFound& ex)
1000 CosNaming::Name n = ex.rest_of_name;
1001 if (ex.why == CosNaming::NamingContext::missing_node)
1002 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1003 << " (" << (char *) n[0].kind << ") not found" )
1004 if (ex.why == CosNaming::NamingContext::not_context)
1005 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1006 << " (" << (char *) n[0].kind
1007 << ") is not a context" )
1008 if (ex.why == CosNaming::NamingContext::not_object)
1009 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1010 << " (" << (char *) n[0].kind
1011 << ") is not an object" )
1013 catch(CosNaming::NamingContext::CannotProceed&)
1015 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::CannotProceed")
1017 catch(CosNaming::NamingContext::InvalidName&)
1019 INFOS( "!!!Destroy_Directory() : CosNaming::NamingContext::InvalidName")
1021 catch(CORBA::COMM_FAILURE&)
1023 INFOS( "!!!Destroy_Directory() :CORBA::COMM_FAILURE : unable to contact"
1024 << " the naming service")
1025 throw ServiceUnreachable();
1027 // Memory destruction
1028 for (int i = 0 ; i <dimension_resultat ;i++)
1030 delete [] resultat_resolve_Path[i];
1032 delete[] resultat_resolve_Path ;
1035 //----------------------------------------------------------------------
1036 /*! Function : _initialize_root_context
1037 * Purpose : method called by constructor to initialize _root_context
1039 //----------------------------------------------------------------------
1041 void SALOME_NamingService::_initialize_root_context()
1043 //MESSAGE("Get the root context");
1046 CORBA::Object_var obj = _orb->resolve_initial_references("NameService");
1047 _root_context = CosNaming::NamingContext::_narrow(obj);
1048 _current_context = _root_context ;
1049 ASSERT(!CORBA::is_nil(_root_context));
1052 catch(CORBA::COMM_FAILURE&)
1054 INFOS("CORBA::COMM_FAILURE: unable to contact the naming service");
1055 throw ServiceUnreachable();
1059 INFOS("Unknown Exception: unable to contact the naming service");
1060 throw ServiceUnreachable();
1064 //----------------------------------------------------------------------
1065 /*! Function : _resolve_Path
1066 * Purpose : method to decompose a Path : /Kernel/Services/Sessions.
1068 * \return a char* containing the first char between '/' (in this case Kernel)
1070 //----------------------------------------------------------------------
1072 char* SALOME_NamingService::_resolve_Path(char* Path)
1075 int length = strlen(Path);
1078 if (length==0) return NULL;
1081 while ((i<length) && (Path[i]!='/'))
1083 resultat = new char[i+1];
1084 strncpy(resultat,Path,i);
1090 //----------------------------------------------------------------------
1091 /*! Function : _result_resolve_Path.
1092 * Purpose : method to decompose a Path : /Kernel/Services/Sessions.
1093 * Gives an array of char* containing Kernel, Services, Sessions.
1094 * \param Path const char* arguments, the Path to decompose
1095 * \param j int& arguments, the size of the array of char*
1096 * \param resultat_resolve_Path char** arguments
1098 //----------------------------------------------------------------------
1101 SALOME_NamingService::_result_resolve_Path(const char* Path,
1103 char ** resultat_resolve_Path)
1105 //MESSAGE("BEGIN OF _result_resolve_Path");
1106 int dimension_Path = strlen(Path) + 1;
1107 char** temp= new char* [dimension_Path];
1108 char** tempslash = new char* [dimension_Path];
1110 temp[j] = new char[dimension_Path];
1111 strcpy(temp[j],Path);
1113 while (strlen(temp[j])>0)
1115 // temp[j] contains the characters to be treated :
1116 // (Path - characters already treted)
1117 // tempslash[j] = temp[j] if the string temp[j] doesn't begin whith '/'
1118 // tempslash[j] = temp[j] without '/' if the string begins whith '/'
1119 int length_temp = strlen(temp[j]);
1120 if (temp[j][0]=='/')
1122 // the characters to be treated begin whith '/'
1123 // we don't have to take the '/'
1124 tempslash[j] = new char [length_temp] ;
1125 for (int k = 0; k < length_temp-1; k++)
1126 tempslash[j][k] = temp[j][k+1];
1127 tempslash[j][length_temp-1]='\0';
1131 //the characters to be trated don't begin with '/'
1132 // Nothing to be done on the char
1133 tempslash[j] = new char [length_temp+1] ;
1134 strcpy(tempslash[j],temp[j]);
1136 // decomposition of the Path
1137 resultat_resolve_Path[j]= _resolve_Path(tempslash[j]);
1138 //SCRUTE(resultat_resolve_Path[j]);
1140 int length_resultat = strlen(resultat_resolve_Path[j]) ;
1141 int dimension_temp = length_temp -length_resultat ;
1143 temp[j] = new char[dimension_temp +1];
1144 for (int i = 0 ; i <dimension_temp ;i++)
1146 temp[j][i] =tempslash[j-1][i+ length_resultat];
1148 temp[j][dimension_temp]= '\0';
1151 // Memory destruction
1152 for (int i = 0 ; i <j;i++)
1155 delete [] tempslash[i];
1158 delete [] tempslash ;
1161 //----------------------------------------------------------------------
1162 /*! Function : _Find.
1163 * Purpose : method to research a name from the current directory
1164 * of the naming service.
1165 * The naming service changes directory to go to the directory where
1166 * the last occurence was found.
1167 * \param name const char* arguments
1168 * \param occurence_number CORBA::LONG (by value)
1170 //----------------------------------------------------------------------
1172 void SALOME_NamingService::_Find(const char* name,
1173 CORBA::Long& occurence_number)
1175 //MESSAGE("BEGIN OF _Find") SCRUTE(name);
1176 CosNaming::BindingList_var _binding_list;
1177 CosNaming::BindingIterator_var _binding_iterator;
1178 unsigned long nb=0 ; //for using only the BindingIterator
1179 // to access the bindings
1180 CosNaming::Binding_var _binding ;
1181 CosNaming::NamingContext_var _ref_context = _current_context;
1182 CosNaming::NamingContext_var _found_context = _current_context;
1184 _current_context->list(nb, _binding_list, _binding_iterator) ;
1186 while (_binding_iterator->next_one(_binding)) {
1187 CosNaming::Name _bindingName = _binding->binding_name;
1188 if (_binding->binding_type == CosNaming::ncontext) {
1189 // We work on a directory, the search should be done in this directory
1190 Change_Directory(_bindingName[0].id);
1191 _Find(name,occurence_number);
1192 // We'll go back to the initial context
1193 _current_context = _ref_context ;
1195 else if (_binding->binding_type == CosNaming::nobject) {
1196 // We work on an object...
1197 if (!strcmp( _bindingName[0].id,name))
1199 //MESSAGE("One occurence was found");
1201 // We keep in memory the directory where one occurence was found
1202 _found_context = _current_context ;
1206 _binding_iterator->destroy();
1207 // We go to the last directory where an occurence was found
1208 _current_context = _found_context ;
1209 //SCRUTE(occurence_number);
1212 //----------------------------------------------------------------------
1213 /*! Function : _create_context_name_dir.
1214 * Purpose : method to create a Context_name from an array of char.
1215 * The number of elements to be copied are indicated
1218 * \param resultat_resolve_Path char** arguments
1219 * \param length_copy int arguments
1220 * \param _context_name CosNaming::Name arguments (by value)
1222 //----------------------------------------------------------------------
1225 SALOME_NamingService::_create_context_name_dir(char** resultat_resolve_Path
1227 CosNaming::Name& _context_name)
1229 //MESSAGE("BEGIN OF _create_context_name_dir");
1230 for (int i = 0 ; i < length_copy;i++)
1232 _context_name[i].id = CORBA::string_dup(resultat_resolve_Path[i]);
1233 _context_name[i].kind = CORBA::string_dup("dir");
1234 //SCRUTE(_context_name[i].id);
1238 //----------------------------------------------------------------------
1239 /*! Function : _current_directory.
1240 * Purpose : method to parse the naming service tree to find a context
1241 * and determine the path to go to this context from the
1243 * \param result_path char** arguments
1244 * \param length_result int arguments by value
1245 * \param context_to_found CosNaming::NamingContext_var arguments
1246 * \param _continue boolean arguments
1248 //----------------------------------------------------------------------
1251 SALOME_NamingService::_current_directory(char** result_path,
1253 CosNaming::NamingContext_var context_to_found,
1254 CORBA::Boolean& _continue)
1256 //MESSAGE("BEGIN OF _current_Directory");
1257 CosNaming::BindingList_var _binding_list;
1258 CosNaming::BindingIterator_var _binding_iterator;
1259 unsigned long nb=0 ; //for using only the BindingIterator
1260 // to access the bindings
1261 CosNaming::Binding_var _binding ;
1262 CosNaming::NamingContext_var _ref_context = _current_context;
1263 CosNaming::NamingContext_var _temp_context = _current_context;
1265 _current_context->list(nb, _binding_list, _binding_iterator) ;
1267 while ((_binding_iterator->next_one(_binding)) && _continue) {
1268 CosNaming::Name _bindingName = _binding->binding_name;
1269 if (_binding->binding_type == CosNaming::ncontext)
1271 // We work on a directory, the search should be done in this directory
1273 result_path[length_result] = new char(strlen(_bindingName[0].id) + 1);
1274 strcpy(result_path[length_result],_bindingName[0].id);
1275 //SCRUTE(result_path[length_result])
1278 CORBA::Object_var _obj =_current_context->resolve(_bindingName);
1279 _temp_context = CosNaming::NamingContext::_narrow(_obj);
1281 if (_temp_context->_is_equivalent(context_to_found))
1283 //MESSAGE("The context is found, we stop the search");
1285 //SCRUTE(_continue);
1289 //SCRUTE(_bindingName[0].id);
1290 Change_Directory(_bindingName[0].id);
1291 _current_directory(result_path,length_result,
1292 context_to_found, _continue );
1295 // We'll go back to the initial context
1296 _current_context = _ref_context ;
1297 //MESSAGE("Just before the delete of ")
1298 //SCRUTE(result_path[length_result-1]);
1299 delete result_path[length_result-1];
1305 _binding_iterator->destroy();
1306 // We go to the last directory where an occurence was found
1307 _current_context = _ref_context ;
1309 //----------------------------------------------------------------------