1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SALOME NamingService : wrapping NamingService services
23 // File : SALOME_NamingService.cxx
24 // Author : Estelle Deville
28 #include "SALOME_NamingService.hxx"
29 #include "ServiceUnreachable.hxx"
31 #include "utilities.h"
39 /*! \class SALOME_NamingService
40 \brief A class to manage the SALOME naming service
44 // ============================================================================
45 /*! \brief Default Constructor without ORB reference.
47 * After Default Constructor, one needs to initialize ORB.
48 * \sa init_orb(CORBA::ORB_ptr orb), SALOME_NamingService(CORBA::ORB_ptr orb)
50 // ============================================================================
52 SALOME_NamingService::SALOME_NamingService()
54 MESSAGE("SALOME_NamingService default constructor");
55 _orb = CORBA::ORB::_nil();
56 _root_context = CosNaming::NamingContext::_nil();
59 // ============================================================================
60 /*! \brief Standard Constructor, with ORB reference.
62 * Initializes the naming service root context
63 * \param orb CORBA::ORB_ptr arguments
65 // ============================================================================
67 SALOME_NamingService::SALOME_NamingService(CORBA::ORB_ptr orb)
69 MESSAGE("SALOME_NamingService creation");
71 _initialize_root_context();
74 // ============================================================================
75 /*! \brief Standard destructor.
77 * The standard destructor does nothing special.
79 // ============================================================================
81 SALOME_NamingService::~SALOME_NamingService()
83 // Problem MESSAGE with singleton: late destruction,
84 // after trace system destruction ?
85 //MESSAGE("SALOME_NamingService destruction");
88 // ============================================================================
89 /*! \brief initializes ORB reference and naming service root context.
91 * Initializes ORB reference and naming service root context.
92 * For use after default constructor.
93 * \param orb CORBA::ORB_ptr arguments
95 // ============================================================================
97 void SALOME_NamingService::init_orb(CORBA::ORB_ptr orb)
99 MESSAGE("SALOME_NamingService initialisation");
101 Utils_Locker lock (&_myMutex);
104 _initialize_root_context();
107 // ============================================================================
108 /*! \brief Registers a CORBA object reference under a path.
110 * Registers a CORBA object reference under a path. If the path ends with '/',
111 * only a directory is created.
112 * If the NamingService is out, the exception ServiceUnreachable is thrown.
113 * \param ObjRef CORBA object reference to associate to the path. To create
114 * only a directory, give nil pointer.
115 * \param Path A relative or absolute pathname to store the object reference.
116 * If the pathname begins with a '/', pathname is taken
117 * as an absolute pathname. Else, pathname is taken as a relative
118 * path, to current context. Prefer absolute pathname, relative
119 * pathname are not safe, when SALOME_NamingService object is
120 * shared or use in multithreaded context.
121 * If the path ends with '/', only a directory is created.
122 * \sa Change_Directory(const char* Path),
123 * Create_Directory(const char* Path)
124 * CORBA::Object_ptr Resolve(const char* Path)
126 // ============================================================================
128 void SALOME_NamingService::Register(CORBA::Object_ptr ObjRef,
130 throw(ServiceUnreachable)
132 MESSAGE("BEGIN OF Register: " << Path);
134 Utils_Locker lock (&_myMutex);
136 // --- _current_context is replaced to the _root_context
137 // if the Path begins whith '/'
140 _current_context = _root_context;
143 // --- the resolution of the directory path has to be done
144 // to place the current_context to the correct node
146 CosNaming::Name context_name;
147 vector<string> splitPath;
148 int dimension_resultat = _createContextNameDir(Path,
153 CORBA::Boolean not_exist = false;
155 if (dimension_resultat > 0){
156 // A directory is treated (not only an object name)
157 // test if the directory where ObjRef should be recorded already exists
158 // If not, create the new context
161 CORBA::Object_var obj = _current_context->resolve(context_name);
162 _current_context = CosNaming::NamingContext::_narrow(obj);
165 catch (CosNaming::NamingContext::NotFound &){
166 // --- failed to resolve, therefore assume cold start
170 catch (CosNaming::NamingContext::InvalidName &){
171 INFOS("Register() : CosNaming::NamingContext::InvalidName");
174 catch (CosNaming::NamingContext::CannotProceed &){
175 INFOS("Register() : CosNaming::NamingContext::CannotProceed");
178 catch (CORBA::SystemException&){
179 INFOS("Register() : CORBA::SystemException: "
180 << "unable to contact the naming service");
181 throw ServiceUnreachable();
186 context_name.length(1);
187 for (int i = 0 ; i < dimension_resultat ;i++){
188 context_name[0].id = CORBA::string_dup(splitPath[i].c_str());
189 context_name[0].kind = CORBA::string_dup("dir");
190 // SCRUTE(_context_name[0].id);
191 // --- check if the path is created
193 // --- if the context is already created, nothing to do
194 CORBA::Object_var obj = _current_context->resolve(context_name);
195 _current_context = CosNaming::NamingContext::_narrow(obj);
198 catch (CosNaming::NamingContext::NotFound &){
200 // --- the context must be created
201 CosNaming::NamingContext_var temp_context =
202 _current_context->bind_new_context(context_name);
203 _current_context = temp_context;
205 catch (CosNaming::NamingContext::AlreadyBound&){
206 CORBA::Object_var obj = _current_context->resolve(context_name);
207 _current_context = CosNaming::NamingContext::_narrow(obj);
213 catch (CosNaming::NamingContext::AlreadyBound&){
214 INFOS("Register() : CosNaming::NamingContext::AlreadyBound");
217 catch (CosNaming::NamingContext::NotFound& ex){
218 CosNaming::Name n = ex.rest_of_name;
220 if (ex.why == CosNaming::NamingContext::missing_node)
221 INFOS("Register() : " << (char *) n[0].id
222 << " (" << (char *) n[0].kind << ") not found");
224 if (ex.why == CosNaming::NamingContext::not_context)
225 INFOS("Register() : " << (char *) n[0].id
226 << " (" << (char *) n[0].kind
227 << ") is not a context");
229 if (ex.why == CosNaming::NamingContext::not_object)
230 INFOS("Register() : " << (char *) n[0].id
231 << " (" << (char *) n[0].kind
232 << ") is not an object");
235 catch (CosNaming::NamingContext::CannotProceed&){
236 INFOS("Register(): CosNaming::NamingContext::CannotProceed");
239 catch (CosNaming::NamingContext::InvalidName&){
240 INFOS("Register(): CosNaming::NamingContext::InvalidName");
243 catch (CORBA::SystemException&){
244 INFOS("Register():CORBA::SystemException: "
245 << "unable to contact the naming service");
246 throw ServiceUnreachable();
251 // --- The current directory is now the directory where the object should
254 int sizePath = splitPath.size();
255 if (sizePath > dimension_resultat){
256 ASSERT(sizePath == dimension_resultat+1);
257 context_name.length(1);
260 // --- the last element is an object and not a directory
262 context_name[0].id = CORBA::string_dup(splitPath[dimension_resultat].c_str());
263 context_name[0].kind = CORBA::string_dup("object");
264 //SCRUTE(context_name[0].id);
266 _current_context->bind(context_name, ObjRef);
269 catch (CosNaming::NamingContext::NotFound& ex){
270 CosNaming::Name n = ex.rest_of_name;
272 if (ex.why == CosNaming::NamingContext::missing_node)
273 INFOS("Register() : " << (char *) n[0].id
274 << " (" << (char *) n[0].kind << ") not found");
276 if (ex.why == CosNaming::NamingContext::not_context)
277 INFOS("Register() : " << (char *) n[0].id
278 << " (" << (char *) n[0].kind
279 << ") is not a context");
281 if (ex.why == CosNaming::NamingContext::not_object)
282 INFOS("Register() : " << (char *) n[0].id
283 << " (" << (char *) n[0].kind
284 << ") is not an object");
287 catch (CosNaming::NamingContext::CannotProceed&){
288 INFOS("Register(): CosNaming::NamingContext::CannotProceed");
291 catch (CosNaming::NamingContext::InvalidName&){
292 INFOS("Register(): CosNaming::NamingContext::InvalidName");
295 catch (CosNaming::NamingContext::AlreadyBound&){
296 INFOS("Register(): CosNaming::NamingContext::AlreadyBound, "
297 << "object will be rebind");
298 _current_context->rebind(context_name, ObjRef);
301 catch (CORBA::SystemException&){
302 INFOS("!!!Register(): CORBA::SystemException: "
303 << "unable to contact the naming service");
304 throw ServiceUnreachable();
309 // ============================================================================
310 /*! \brief get the CORBA object reference associated to a name.
312 * get the CORBA object reference associated to a complete name with a path.
313 * If the NamingService is out, the exception ServiceUnreachable is thrown
314 * \param Path pathname. If the pathname begins with a '/', pathname is taken
315 * as an absolute pathname. Else, pathname is taken as a relative
316 * path, to current context. Prefer absolute pathname, relative
317 * pathname are not safe, when SALOME_NamingService object is
318 * shared or use in multithreaded context.
319 * \return the object reference if it exists under the pathname,
320 * or nil reference in other cases.
321 * \sa Register(CORBA::Object_ptr ObjRef, const char* Path),
322 * Change_Directory(const char* Path)
324 // ============================================================================
326 CORBA::Object_ptr SALOME_NamingService::Resolve(const char* Path)
327 throw(ServiceUnreachable)
329 // MESSAGE("BEGIN OF Resolve: " << Path);
331 Utils_Locker lock (&_myMutex);
333 // --- _current_context is replaced to the _root_context
334 // if the Path begins whith '/'
338 _current_context = _root_context;
341 // --- the resolution of the directory path has to be done
342 // to place the current_context to the correct node
344 CosNaming::Name context_name;
345 vector<string> splitPath;
346 _createContextNameDir(Path,
351 ASSERT(!CORBA::is_nil(_current_context));
353 CORBA::Object_var obj = CORBA::Object::_nil();
357 obj = _current_context->resolve(context_name);
360 catch (CosNaming::NamingContext::NotFound& ex)
362 CosNaming::Name n = ex.rest_of_name;
364 if (ex.why == CosNaming::NamingContext::missing_node)
365 MESSAGE("Resolve() : " << (char *) n[0].id
366 << " (" << (char *) n[0].kind << ") not found");
368 if (ex.why == CosNaming::NamingContext::not_context)
370 << (char *) n[0].id << " (" << (char *) n[0].kind
371 << ") is not a context");
373 if (ex.why == CosNaming::NamingContext::not_object)
374 INFOS("Resolve() : " << (char *) n[0].id
375 << " (" << (char *) n[0].kind
376 << ") is not an object");
379 catch (CosNaming::NamingContext::CannotProceed&)
381 INFOS("Resolve(): CosNaming::NamingContext::CannotProceed");
384 catch (CosNaming::NamingContext::InvalidName&)
386 INFOS("Resolve(): CosNaming::NamingContext::InvalidName");
389 catch (CORBA::SystemException&)
391 INFOS("Resolve():CORBA::SystemException : unable to contact"
392 << "the naming service");
393 throw ServiceUnreachable();
399 // ============================================================================
400 /*! \brief get the CORBA object reference associated to an uncomplete name.
402 * get the CORBA object reference associated to an uncomplete name with a
403 * path. Look for the first occurence of name*.
404 * If the NamingService is out, the exception ServiceUnreachable is thrown
405 * \param Path pathname under the form "/path/name" (Absolute reference !)
406 * search the fist reference like "/path(.dir)/name*(.kind)"
407 * \return the object reference if found, or nil reference.
408 * \sa Resolve(const char* Path)
410 // ============================================================================
412 CORBA::Object_ptr SALOME_NamingService::ResolveFirst(const char* Path)
413 throw(ServiceUnreachable)
415 // MESSAGE("ResolveFirst");
417 Utils_Locker lock (&_myMutex);
420 string thePath = Path;
421 string basePath = "";
422 string name = thePath;
424 string::size_type idx = thePath.rfind('/');
426 if (idx != string::npos) // at least one '/' found
428 basePath = thePath.substr(0, idx);
429 name = thePath.substr(idx + 1);
434 CORBA::Object_var obj = CORBA::Object::_nil();
437 if (basePath.empty())
440 isOk = Change_Directory(basePath.c_str());
444 vector<string> listElem = list_directory();
445 vector<string>::iterator its = listElem.begin();
447 while (its != listElem.end())
451 if ((*its).find(name) == 0)
453 return Resolve((*its).c_str());
463 // ============================================================================
464 /*! \brief find a component instance from hostname, containername,
465 * componentName and number of processors.
467 * find a component instance from hostname, containername, componentName and
468 * number of processors.
469 * If the NamingService is out, the exception ServiceUnreachable is thrown.
470 * \param hostname name of the machine on which the component is searched.
471 * \param containerName name of the container in which the component is
473 * \param componentName name of the component we are looking for an existing
475 * \param nbproc in case of multi processor machine, container name is
476 * suffixed with _nbproc.
477 * \return the object reference
479 // ============================================================================
482 SALOME_NamingService::ResolveComponent(const char* hostname,
483 const char* containerName,
484 const char* componentName,
486 throw(ServiceUnreachable)
488 // MESSAGE("ResolveComponent");
490 Utils_Locker lock (&_myMutex);
492 string name = "/Containers/";
496 if ( strlen(containerName) != 0 )
502 char *newContainerName = new char[strlen(containerName) + 8];
503 sprintf(newContainerName, "%s_%d", containerName, nbproc);
504 name += newContainerName;
505 delete [] newContainerName;
509 name += containerName;
513 name += componentName;
515 return ResolveFirst(name.c_str());
521 if (Change_Directory(name.c_str()))
523 vector<string> contList = list_subdirs();
525 for (unsigned int ind = 0; ind < contList.size(); ind++)
527 name = contList[ind].c_str();
531 char *str_nbproc = new char[8];
532 sprintf(str_nbproc, "_%d", nbproc);
533 if( strstr(name.c_str(),str_nbproc) == NULL)
534 continue; // check only containers with _%d in name
535 delete [] str_nbproc;
539 name += componentName;
541 CORBA::Object_ptr obj = ResolveFirst(name.c_str());
543 if ( !CORBA::is_nil(obj) )
548 return CORBA::Object::_nil();
552 // ============================================================================
553 /*! \brief provide a default container name if empty.
555 * the given container name is returned unchanged, unless it is empty.
556 * \param containerName
557 * \return container name, where empty input is replaced by "FactoryServer",
559 * \sa BuildContainerNameForNS(const char *containerName, const char *hostname)
561 // ============================================================================
563 string SALOME_NamingService::ContainerName(const char *containerName)
567 if (strlen(containerName) == 0)
568 ret = "FactoryServer";
575 // ============================================================================
576 /*! \brief build a container name, given a MachineParameters struct.
578 * Build a container name with a MachineParameters struct. In case of multi
579 * processor machine, container name is suffixed with _nbproc. nproc equals
580 * (number of nodes)*(number of processor per nodes).
581 * \param params struct from which we get container name (may be
582 * empty), number of nodes and number of processor
584 * \return a container name without the path.
585 * \sa BuildContainerNameForNS(const Engines::MachineParameters& params,
586 * const char *hostname)
588 // ============================================================================
591 SALOME_NamingService::ContainerName(const Engines::MachineParameters& params)
597 else if ( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
599 else if ( params.nb_node == 0 )
600 nbproc = params.nb_proc_per_node;
601 else if ( params.nb_proc_per_node == 0 )
602 nbproc = params.nb_node;
604 nbproc = params.nb_node * params.nb_proc_per_node;
606 string ret = ContainerName(params.container_name);
610 char *suffix = new char[8];
611 sprintf(suffix, "_%d", nbproc);
618 // ============================================================================
619 /*! \brief build a string representing a container in Naming Service.
621 * Build a string representing the absolute pathname of a container in
622 * SALOME_NamingService. This form gives a suffixed containerName in case of
623 * multi processor machine.
624 * \param containerName name of the container in which the component is
626 * \param hostname name of the host of the container, without domain names.
627 * \return the path under the form /Containers/hostname/containerName
628 * \sa ContainerName(const Engines::MachineParameters& params)
630 // ============================================================================
632 string SALOME_NamingService::BuildContainerNameForNS(const char *containerName,
633 const char *hostname)
635 string ret = "/Containers/";
638 ret += ContainerName(containerName);
643 // ============================================================================
644 /*! \brief build a string representing a container in Naming Service.
646 * Build a string representing the absolute pathname of a container in
647 * SALOME_NamingService.
648 * \param params used as it is, or replaced by FactoryServer if empty.
649 * \param hostname name of the host of the container, without domain names.
650 * \return the path under the form /Containers/hostname/containerName
651 * \sa ContainerName(const char *containerName)
653 // ============================================================================
656 SALOME_NamingService::
657 BuildContainerNameForNS(const Engines::MachineParameters& params,
658 const char *hostname)
660 string ret = "/Containers/";
663 ret += ContainerName(params);
668 // ============================================================================
669 /*! \brief search a name in current directory.
671 * Search a name in the current directory. after call, the current directory
672 * is changed to the directory containing the last occurence of name found.
673 * If no occurence found (see return value), current directory remains
676 * \param name the name to search.
677 * \return number of occurences found.
678 * \sa Change_Directory(const char* Path)
680 // ============================================================================
682 int SALOME_NamingService::Find(const char* name)
683 throw(ServiceUnreachable)
685 MESSAGE("BEGIN OF Find " << name);
687 Utils_Locker lock (&_myMutex);
689 CORBA::Long occurence_number = 0 ;
693 _Find(name, occurence_number);
696 catch (CORBA::SystemException&)
698 INFOS("!!!Find() : CORBA::SystemException : unable to contact"
699 << " the naming service");
700 throw ServiceUnreachable();
703 return occurence_number;
706 // ============================================================================
707 /*! \brief Creates a directory (context_name)
709 * Creates a directory (context_name) relative to the current directory
710 * (current context) or relative to the root directory (root context), if
711 * the path given begins with a '/'.
712 * If the NamingService is out, the exception ServiceUnreachable is thrown.
713 * \param Path A relative or absolute pathname to store the object reference.
714 * If the pathname begins with a '/', pathname is taken
715 * as an absolute pathname. Else, pathname is taken as a relative
716 * path, to current context. Prefer absolute pathname, relative
717 * pathname are not safe, when SALOME_NamingService object is
718 * shared or use in multithreaded context.
719 * \return true if successfull
720 * (creation not strictly garanteed if true, because Register may
721 * catch some specific unlikely exception without throw anything
722 * --- to be corrected ---)
723 * \sa RegisterCORBA::Object_ptr ObjRef, const char* Path)
725 // ============================================================================
727 bool SALOME_NamingService::Create_Directory(const char* Path)
728 throw(ServiceUnreachable)
730 MESSAGE("BEGIN OF Create_Directory");
732 Utils_Locker lock (&_myMutex);
736 // --- if path empty, nothing to create, no context change
741 // --- if path ='/', nothing to create, only change to root_context
745 MESSAGE("Create Directory '/', just change to root_context");
746 _current_context = _root_context;
750 // --- path must end with '/'
752 if (path[path.length()-1] != '/') path += '/';
754 Register(CORBA::Object::_nil(), path.c_str());
758 // ============================================================================
759 /*! \brief change current directory to the given path
761 * change the current directory to the given path in parameter.
762 * Warning: avoid use when the SALOME_NamingService instance is shared by
763 * several threads (current context may be modified by another thread).
764 * If the path is empty, nothing done return OK.
765 * If Path ="/", the current directory changes to the root directory.
766 * If the NamingService is out, the exception ServiceUnreachable is thrown.
767 * \param Path the new current directory
768 * \return true if the change succeeded
770 // ============================================================================
772 bool SALOME_NamingService::Change_Directory(const char* Path)
773 throw(ServiceUnreachable)
775 // MESSAGE("BEGIN OF Change_Directory " << Path);
776 Utils_Locker lock (&_myMutex);
780 // --- if path empty, nothing to do
785 // --- if path ='/', nothing to resolve, only change to root_context
789 // MESSAGE("Change_Directory is called to go to the root_context");
790 _current_context = _root_context;
794 CosNaming::NamingContext_var current_context = _current_context;
795 bool changeOK = false;
797 // --- replace _current_context with _root_context if Path begins whith '/'
800 current_context = _root_context;
802 // --- need to resolve directory path
804 ASSERT(!CORBA::is_nil(current_context));
806 if (path[path.length()-1] != '/') path += '/';
808 CosNaming::Name context_name;
809 vector<string> splitPath;
810 _createContextNameDir(path.c_str(),
815 // --- Context creation
819 CORBA::Object_var obj = current_context->resolve(context_name);
820 current_context = CosNaming::NamingContext::_narrow(obj);
821 ASSERT(!CORBA::is_nil(current_context));
822 _current_context = current_context;
826 catch (CosNaming::NamingContext::NotFound& ex)
828 CosNaming::Name n = ex.rest_of_name;
830 if (ex.why == CosNaming::NamingContext::missing_node)
831 MESSAGE( "Change_Directory() : " << (char *) n[0].id
832 << " (" << (char *) n[0].kind << ") not found");
833 if (ex.why == CosNaming::NamingContext::not_context)
834 INFOS("Change_Directory() : " << (char *) n[0].id
835 << " (" << (char *) n[0].kind
836 << ") is not a context" );
837 if (ex.why == CosNaming::NamingContext::not_object)
838 INFOS( "Change_Directory() : " << (char *) n[0].id
839 << " (" << (char *) n[0].kind
840 << ") is not an object" );
843 catch (CosNaming::NamingContext::CannotProceed&)
845 INFOS("Change_Directory(): CosNaming::NamingContext::CannotProceed");
848 catch (CosNaming::NamingContext::InvalidName&)
850 INFOS("Change_Directory(): CosNaming::NamingContext::InvalidName");
853 catch (CORBA::SystemException&)
855 INFOS("Change_Directory():CORBA::SystemException : unable to contact"
856 << "the naming service");
857 throw ServiceUnreachable();
863 // ============================================================================
864 /*! \brief get the current directory path
866 * Get the current directory path.
867 * If the NamingService is out, the exception ServiceUnreachable is thrown.
868 * \return the path of the current_context
869 * \sa _current_directory
871 // ============================================================================
873 char* SALOME_NamingService::Current_Directory()
874 throw(ServiceUnreachable)
876 MESSAGE("BEGIN OF Current_Directory");
878 Utils_Locker lock (&_myMutex);
880 CosNaming::NamingContext_var ref_context = _current_context;
882 vector<string> splitPath;
885 bool notFound = true ;
887 // --- start search from root context
889 _current_context = _root_context ;
893 _current_directory(splitPath, lengthPath, ref_context, notFound );
896 catch (CORBA::SystemException&)
898 INFOS("Current_Directory(): CORBA::SystemException: unable to contact"
899 << " the naming service" )
900 throw ServiceUnreachable();
904 lengthPath = splitPath.size();
905 for (int k = 0 ; k < lengthPath ;k++)
908 path += splitPath[k];
912 _current_context = ref_context ;
914 return strdup(path.c_str());
917 // ============================================================================
918 /*! \brief list recursively all objects in the current context
920 * List and print via trace all directories and objects in the current
921 * context. Trace must be activated: compile option _DEBUG_
922 * If the NamingService is out, the exception ServiceUnreachable is thrown
924 // ============================================================================
926 void SALOME_NamingService::list()
927 throw(ServiceUnreachable)
929 MESSAGE("Begin of list");
931 Utils_Locker lock (&_myMutex)
934 CosNaming::BindingList_var binding_list;
935 CosNaming::BindingIterator_var binding_iterator;
936 CosNaming::Binding_var binding ;
938 unsigned long nb = 0 ; // --- only for the BindingIterator use,
939 // to access the bindings
941 CosNaming::NamingContext_var ref_context = _current_context;
943 _current_context->list(nb, binding_list, binding_iterator) ;
945 if (! CORBA::is_nil(binding_iterator))
947 while (binding_iterator->next_one(binding))
949 CosNaming::Name bindingName = binding->binding_name;
951 if (binding->binding_type == CosNaming::ncontext)
953 MESSAGE( "Context : " << bindingName[0].id );
957 Change_Directory(bindingName[0].id);
960 catch (ServiceUnreachable&)
962 INFOS( "list(): ServiceUnreachable" )
963 throw ServiceUnreachable();
967 _current_context = ref_context ;
970 else if (binding->binding_type == CosNaming::nobject)
972 MESSAGE( "Object : " << bindingName[0].id );
976 binding_iterator->destroy();
980 // ============================================================================
981 /*! \brief list all the objects in the current directory.
983 * get a list of all the objects in the current directory, without recursion
984 * on the subdirectories. Only the objects are listed, not the directories.
985 * If the NamingService is out, the exception ServiceUnreachable is thrown.
986 * \return list of strings with objects found.
987 * \sa vector<string> list_directory_recurs()
989 // ============================================================================
991 vector<string> SALOME_NamingService::list_directory()
992 throw(ServiceUnreachable)
994 // MESSAGE("list_directory");
995 vector<string> dirList ;
998 CosNaming::BindingList_var binding_list;
999 CosNaming::BindingIterator_var binding_iterator;
1000 CosNaming::Binding_var binding ;
1002 unsigned long nb = 0 ; // --- only for the BindingIterator use,
1003 // to access the bindings
1005 CosNaming::NamingContext_var ref_context = _current_context;
1007 _current_context->list(nb, binding_list, binding_iterator);
1009 if (binding_iterator->_is_nil())
1012 while (binding_iterator->next_one(binding))
1014 CosNaming::Name bindingName = binding->binding_name;
1016 if (binding->binding_type == CosNaming::nobject)
1018 // remove memory leak
1019 // dirList.push_back(CORBA::string_dup(bindingName[0].id));
1020 dirList.push_back(string(bindingName[0].id));
1024 // for (unsigned int ind = 0; ind < dirList.size(); ind++)
1025 // MESSAGE("list_directory : Object : " << dirList[ind]);
1027 binding_iterator->destroy();
1033 // ============================================================================
1034 /*! \brief list all the subdirectories in the current directory.
1036 * get a list of all the subdirectories in the current directory,
1037 * without recursion on the subdirectories.
1038 * Only the subdirectories are listed, not the objects.
1039 * If the NamingService is out, the exception ServiceUnreachable is thrown.
1040 * \return list of strings with directories found.
1041 * \sa vector<string> list_directory()
1043 // ============================================================================
1045 vector<string> SALOME_NamingService::list_subdirs()
1046 throw(ServiceUnreachable)
1048 MESSAGE("list_subdirs");
1049 vector<string> dirList ;
1052 CosNaming::BindingList_var binding_list;
1053 CosNaming::BindingIterator_var binding_iterator;
1054 CosNaming::Binding_var binding ;
1056 unsigned long nb = 0 ; // --- only for the BindingIterator use,
1057 // to access the bindings
1059 CosNaming::NamingContext_var ref_context = _current_context;
1061 _current_context->list(nb, binding_list, binding_iterator) ;
1063 if (binding_iterator->_is_nil())
1066 while (binding_iterator->next_one(binding))
1068 CosNaming::Name bindingName = binding->binding_name;
1070 if (binding->binding_type == CosNaming::ncontext)
1072 dirList.push_back(bindingName[0].id.in());
1076 for (unsigned int ind = 0; ind < dirList.size(); ind++)
1077 MESSAGE("list_directory : Object : " << dirList[ind]);
1079 binding_iterator->destroy();
1084 // ============================================================================
1085 /*! \brief list all the objects in the current directory and subdirectories.
1087 * get a list of all the objects in the current directory, with recursion
1088 * on the subdirectories. Only the objects are listed, not the directories.
1089 * If the NamingService is out, the exception ServiceUnreachable is thrown.
1090 * \return list of strings with objects found.
1091 * \sa vector<string> list_directory()
1093 // ============================================================================
1095 vector<string> SALOME_NamingService::list_directory_recurs()
1096 throw(ServiceUnreachable)
1098 MESSAGE("list_directory_recurs");
1100 Utils_Locker lock (&_myMutex);
1102 vector<string> dirList ;
1104 char* currentDir = Current_Directory();
1106 _list_directory_recurs(dirList, "", currentDir);
1113 // ============================================================================
1114 /*! \brief destroy an entry in naming service.
1116 * Destroy an association Path - Object Reference.
1117 * If the NamingService is out, the exception ServiceUnreachable is thrown
1118 * \param Path object path
1120 // ============================================================================
1122 void SALOME_NamingService::Destroy_Name(const char* Path)
1123 throw(ServiceUnreachable)
1125 MESSAGE("BEGIN OF Destroy_Name " << Path);
1127 Utils_Locker lock (&_myMutex);
1131 // --- if path empty, nothing to do
1136 // --- if path = '/' not applicable, nothing to do
1141 // --- if path begins with '/', set current directory to root context
1144 _current_context = _root_context;
1146 // --- context of the directory containing the object
1148 CosNaming::Name context_name;
1149 vector<string> splitPath;
1150 int dimension_resultat = _createContextNameDir(path.c_str(),
1157 if (dimension_resultat > 0)
1159 // --- path contains a directory, not only an object name
1160 // switch to the new directory (or return if directory not found)
1164 CORBA::Object_var obj = _current_context->resolve(context_name);
1165 _current_context = CosNaming::NamingContext::_narrow(obj);
1169 catch (CosNaming::NamingContext::NotFound &ex)
1171 // --- failed to resolve
1174 CosNaming::Name n = ex.rest_of_name;
1176 if (ex.why == CosNaming::NamingContext::missing_node)
1177 INFOS( "Destroy_Name(): " << (char *) n[0].id
1178 << " (" << (char *) n[0].kind << ") not found" );
1179 if (ex.why == CosNaming::NamingContext::not_context)
1180 INFOS( "Destroy_Name() : " << (char *) n[0].id
1181 << " (" << (char *) n[0].kind
1182 << ") is not a context" );
1183 if (ex.why == CosNaming::NamingContext::not_object)
1184 INFOS( "Destroy_Name() : " << (char *) n[0].id
1185 << " (" << (char *) n[0].kind
1186 << ") is not an object" );
1189 catch (CosNaming::NamingContext::InvalidName &)
1191 INFOS("Destroy_Name: CosNaming::NamingContext::InvalidName");
1194 catch (CosNaming::NamingContext::CannotProceed &)
1196 INFOS("Destroy_Name: CosNaming::NamingContext::CannotProceed");
1199 catch (CORBA::SystemException&)
1201 INFOS("Destroy_Name : CORBA::SystemException: "
1202 << "unable to contact the naming service");
1203 throw ServiceUnreachable();
1206 if (! exist) return;
1209 ASSERT(!CORBA::is_nil(_current_context));
1211 // --- The current directory is now the directory where the object should
1214 int sizePath = splitPath.size();
1215 if (sizePath > dimension_resultat)
1217 ASSERT(sizePath == dimension_resultat+1);
1218 context_name.length(1);
1222 // --- the last element is an object and not a directory
1224 context_name[0].id =
1225 CORBA::string_dup(splitPath[dimension_resultat].c_str());
1226 context_name[0].kind = CORBA::string_dup("object");
1227 SCRUTE(context_name[0].id);
1229 _current_context->unbind(context_name);
1230 MESSAGE("The object " << context_name[0].id << " has been deleted");
1233 catch (CosNaming::NamingContext::NotFound& ex)
1235 CosNaming::Name n = ex.rest_of_name;
1237 if (ex.why == CosNaming::NamingContext::missing_node)
1238 INFOS( "Destroy_Name() : " << (char *) n[0].id
1239 << " (" << (char *) n[0].kind << ") not found" );
1240 if (ex.why == CosNaming::NamingContext::not_context)
1241 INFOS( "Destroy_Name() : " << (char *) n[0].id
1242 << " (" << (char *) n[0].kind
1243 << ") is not a context" );
1244 if (ex.why == CosNaming::NamingContext::not_object)
1245 INFOS( "Destroy_Name() : " << (char *) n[0].id
1246 << " (" << (char *) n[0].kind
1247 << ") is not an object" );
1250 catch (CosNaming::NamingContext::CannotProceed&)
1252 INFOS( "Destroy_Name(): CosNaming::NamingContext::CannotProceed");
1255 catch (CosNaming::NamingContext::InvalidName&)
1257 INFOS( "Destroy_Name(): CosNaming::NamingContext::InvalidName");
1260 catch (CORBA::SystemException&)
1262 INFOS( "Destroy_Name(): CORBA::SystemException: unable to contact"
1263 << " the naming service");
1264 throw ServiceUnreachable();
1269 // ============================================================================
1270 /*! \brief Destroy an empty directory
1272 * Destroy an empty directory in Naming Service.
1273 * If the NamingService is out, the exception ServiceUnreachable is thrown.
1274 * \param Path directory path
1276 // ============================================================================
1278 void SALOME_NamingService::Destroy_Directory(const char* Path)
1279 throw(ServiceUnreachable)
1281 MESSAGE("BEGIN OF Destroy_Directory " << Path);
1283 Utils_Locker lock (&_myMutex);
1287 // --- if path empty, nothing to do
1292 // --- if path begins with '/', set current directory to root context
1295 _current_context = _root_context;
1297 CosNaming::NamingContext_var ref_context = _current_context;
1299 // --- path must ends with '/' for a directory
1301 if (path[path.size() -1] != '/')
1304 // --- context of the directory
1306 CosNaming::Name context_name;
1307 vector<string> splitPath;
1308 int dimension_resultat = _createContextNameDir(path.c_str(),
1314 if (dimension_resultat > 0)
1316 // --- path contains a directory, not only an object name
1317 // switch to the new directory (or return if directory not found)
1321 CORBA::Object_var obj = _current_context->resolve(context_name);
1322 _current_context = CosNaming::NamingContext::_narrow(obj);
1326 catch (CosNaming::NamingContext::NotFound &ex)
1328 // --- failed to resolve
1331 CosNaming::Name n = ex.rest_of_name;
1333 if (ex.why == CosNaming::NamingContext::missing_node)
1334 INFOS( "Destroy_Directory(): " << (char *) n[0].id
1335 << " (" << (char *) n[0].kind << ") not found" );
1336 if (ex.why == CosNaming::NamingContext::not_context)
1337 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1338 << " (" << (char *) n[0].kind
1339 << ") is not a context" );
1340 if (ex.why == CosNaming::NamingContext::not_object)
1341 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1342 << " (" << (char *) n[0].kind
1343 << ") is not an object" );
1346 catch (CosNaming::NamingContext::InvalidName &)
1348 INFOS("Destroy_Directory: CosNaming::NamingContext::InvalidName");
1351 catch (CosNaming::NamingContext::CannotProceed &)
1353 INFOS("Destroy_Directory: CosNaming::NamingContext::CannotProceed");
1356 catch (CORBA::SystemException&)
1358 INFOS("Destroy_Directory : CORBA::SystemException: "
1359 << "unable to contact the naming service");
1360 throw ServiceUnreachable();
1363 if (! exist) return;
1366 ASSERT(!CORBA::is_nil(_current_context));
1368 // --- Context Destruction
1370 bool isContextDestroyed = false;
1373 _current_context->destroy();
1374 MESSAGE( "The context " << path << " has been deleted" );
1375 isContextDestroyed = true;
1378 catch (CosNaming::NamingContext::NotEmpty&)
1380 INFOS( "Destroy_Directory(): CosNaming::NamingContext::NoEmpty "
1381 << path << " is not empty" );
1384 catch (CORBA::SystemException&)
1386 INFOS( "Destroy_Directory():CORBA::SystemException : "
1387 << "unable to contact the naming service");
1388 throw ServiceUnreachable();
1391 // --- go to the reference directory
1393 _current_context = ref_context ;
1395 ASSERT(!CORBA::is_nil(_current_context));
1397 if (isContextDestroyed)
1401 _current_context->unbind(context_name);
1402 MESSAGE( "The bind to the context "
1403 << context_name[0].id
1404 << " has been deleted" );
1407 catch (CosNaming::NamingContext::NotFound& ex)
1409 CosNaming::Name n = ex.rest_of_name;
1411 if (ex.why == CosNaming::NamingContext::missing_node)
1412 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1413 << " (" << (char *) n[0].kind << ") not found" );
1414 if (ex.why == CosNaming::NamingContext::not_context)
1415 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1416 << " (" << (char *) n[0].kind
1417 << ") is not a context" );
1418 if (ex.why == CosNaming::NamingContext::not_object)
1419 INFOS( "Destroy_Directory() : " << (char *) n[0].id
1420 << " (" << (char *) n[0].kind
1421 << ") is not an object" );
1424 catch (CosNaming::NamingContext::CannotProceed&)
1426 INFOS("Destroy_Directory: CosNaming::NamingContext::CannotProceed");
1429 catch (CosNaming::NamingContext::InvalidName&)
1431 INFOS("Destroy_Directory: CosNaming::NamingContext::InvalidName");
1434 catch (CORBA::SystemException&)
1436 INFOS("Destroy_Directory:CORBA::SystemException : unable to contact"
1437 << " the naming service");
1438 throw ServiceUnreachable();
1443 // ============================================================================
1444 /*! \brief Destroy a directory with its contents.
1446 * Destroy the objects associations in a directory, and the directory itself,
1447 * if there is no subdirectories.
1448 * If the NamingService is out, the exception ServiceUnreachable is thrown.
1449 * \param Path the directory path.
1451 // ============================================================================
1453 void SALOME_NamingService::Destroy_FullDirectory(const char* Path)
1454 throw(ServiceUnreachable)
1456 MESSAGE("begin of Destroy_FullDirectory " << Path);
1457 if( Change_Directory(Path) )
1459 vector<string> contList = list_directory();
1461 for (unsigned int ind = 0; ind < contList.size(); ind++)
1462 Destroy_Name(contList[ind].c_str());
1464 Destroy_Directory(Path);
1468 // ============================================================================
1469 /*! \brief initialize root context (root directory)
1471 * the root context initialisation must be done when the SALOME_NamingService
1472 * instance is created and before any othe call. See constructors.
1474 // ============================================================================
1476 void SALOME_NamingService::_initialize_root_context()
1478 //MESSAGE("Get the root context");
1482 CORBA::Object_var obj = _orb->resolve_initial_references("NameService");
1483 _root_context = CosNaming::NamingContext::_narrow(obj);
1484 _current_context = _root_context ;
1485 ASSERT(!CORBA::is_nil(_root_context));
1488 catch (CORBA::SystemException&)
1490 INFOS("CORBA::SystemException: unable to contact the naming service");
1491 throw ServiceUnreachable();
1496 INFOS("Unknown Exception: unable to contact the naming service");
1497 throw ServiceUnreachable();
1501 // ============================================================================
1502 /*! \brief transform a string path in CosNaming structure.
1504 * Transform a path given as a string in a CosNaming structure.
1505 * \param path a relative or absolute path, with or without an object.
1506 * An absolute path begins with '/'.
1507 * A path without an object ends with '/'.
1508 * \param context_name CosNaming structure to put the path.
1509 * \param splitPath a vector of string with subdirectories and final
1511 * \param onlyDir if true, final object (if any) is ommited
1513 * if false, final object (if any) is included in
1515 * \return dimension of context_name
1517 // ============================================================================
1520 SALOME_NamingService::_createContextNameDir(string path,
1521 CosNaming::Name& context_name,
1522 vector<string>& splitPath,
1528 string::size_type begIdx, endIdx;
1529 const string delims("/");
1530 splitPath.resize(0);
1531 bool endWithDelim = false;
1533 begIdx = path.find_first_not_of(delims);
1534 while (begIdx != string::npos)
1536 endIdx = path.find_first_of(delims, begIdx);
1537 if (endIdx == path.length()-1)
1538 endWithDelim = true;
1539 if (endIdx == string::npos)
1540 endIdx = path.length();
1541 int lsub = endIdx - begIdx;
1543 splitPath.push_back(path.substr(begIdx, lsub));
1544 begIdx = path.find_first_not_of(delims, endIdx);
1548 if (onlyDir) // only directory part
1550 dim = splitPath.size()-1; // omit final object
1551 if (endWithDelim) // unless the path ends with a delimiter
1553 endWithDelim = true;
1556 dim = splitPath.size(); // directories and final object
1558 context_name.length(dim);
1559 for (int i=0; i<dim; i++)
1561 // SCRUTE(splitPath[i]);
1562 context_name[i].id = CORBA::string_dup(splitPath[i].c_str());
1563 if (!endWithDelim && (i == dim-1)) // here, the last string is an object
1565 context_name[i].kind = CORBA::string_dup("object");
1566 // MESSAGE("--- " <<splitPath[i] <<".object");
1570 context_name[i].kind = CORBA::string_dup("dir");
1571 // MESSAGE("--- " <<splitPath[i] <<".dir");
1577 // ============================================================================
1578 /*! \brief search a name in current directory.
1580 * Search a name in the current directory. after call, the current directory
1581 * is changed to the directory containing the last occurence of name found.
1582 * If no occurence found (see return value), current directory remains
1583 * unchanged. The call is recursive.
1585 * \param name the name to search.
1586 * \param occurence_number number of occurence already found (incremented)
1588 // ============================================================================
1590 void SALOME_NamingService::_Find(const char* name,
1591 CORBA::Long& occurence_number)
1593 MESSAGE("BEGIN OF _Find "<< occurence_number << " " << name);
1595 CosNaming::BindingList_var binding_list;
1596 CosNaming::BindingIterator_var binding_iterator;
1597 CosNaming::Binding_var binding;
1599 unsigned long nb = 0 ; // --- only for the use of the BindingIterator,
1600 // to access the bindings
1602 CosNaming::NamingContext_var ref_context = _current_context;
1603 CosNaming::NamingContext_var found_context = _current_context;
1605 _current_context->list(nb, binding_list, binding_iterator) ;
1607 if (! CORBA::is_nil(binding_iterator))
1609 while (binding_iterator->next_one(binding))
1611 CosNaming::Name bindingName = binding->binding_name;
1613 if (binding->binding_type == CosNaming::ncontext)
1615 // --- We work on a directory,
1616 // the search should be done in this directory
1618 Change_Directory(bindingName[0].id);
1619 _Find(name, occurence_number);
1621 // --- We'll go back to the initial context
1623 _current_context = ref_context ;
1626 else if (binding->binding_type == CosNaming::nobject)
1628 // --- We work on an object...
1630 if (!strcmp( bindingName[0].id, name))
1632 //MESSAGE("One occurence was found");
1635 // --- We keep in memory the directory where
1636 // one occurence was found
1638 found_context = _current_context ;
1643 binding_iterator->destroy();
1645 // --- We go to the last directory where an occurence was found
1647 _current_context = found_context;
1649 SCRUTE(occurence_number);
1652 // ============================================================================
1653 /*! \brief find the current directory path.
1655 * Parse the naming service tree to find the current context and give the
1656 * associated directory path (relative to root context).
1658 * \param lengthResult
1659 * \param contextToFind
1662 // ============================================================================
1665 SALOME_NamingService::
1666 _current_directory(vector<string>& splitPath,
1668 CosNaming::NamingContext_var contextToFind,
1671 MESSAGE("BEGIN OF _current_Directory");
1673 CosNaming::BindingList_var binding_list;
1674 CosNaming::BindingIterator_var binding_iterator;
1675 CosNaming::Binding_var binding;
1677 unsigned long nb = 0 ; // --- only for the BindingIterator use,
1678 // to access the bindings
1680 CosNaming::NamingContext_var ref_context = _current_context;
1681 CosNaming::NamingContext_var temp_context = _current_context;
1683 _current_context->list(nb, binding_list, binding_iterator);
1685 if ( !binding_iterator->_is_nil() )
1687 while ((binding_iterator->next_one(binding)) && notFound)
1689 CosNaming::Name bindingName = binding->binding_name;
1691 if (binding->binding_type == CosNaming::ncontext)
1693 // --- directory, search in it
1695 const char* bindingNameid=bindingName[0].id;
1696 splitPath.push_back(bindingNameid);
1699 CORBA::Object_var obj = _current_context->resolve(bindingName);
1700 temp_context = CosNaming::NamingContext::_narrow(obj);
1702 if (temp_context->_is_equivalent(contextToFind))
1704 MESSAGE("The context is found, we stop the search");
1711 SCRUTE(bindingName[0].id);
1712 Change_Directory(bindingName[0].id);
1713 _current_directory(splitPath,
1720 // --- go back to the initial context
1722 _current_context = ref_context;
1724 MESSAGE("Just before the delete of "
1725 << splitPath[lengthResult-1]);
1726 splitPath.pop_back();
1733 binding_iterator->destroy();
1736 // --- return to the last directory where an occurence was found
1738 _current_context = ref_context ;
1742 // ============================================================================
1743 /*! \brief list recursively all objects in the given directory and subdirs.
1745 * get a list of all the objects in the current directory, with recursion
1746 * on the subdirectories. Only the objects are listed, not the directories.
1747 * If the NamingService is out, the exception ServiceUnreachable is thrown.
1748 * _current_context must refer to absCurDirectory.
1750 * \param myList The list that will be filled.
1751 * \param relativeSubDir The directory relative to absCurDirectory in which
1752 * the objects are found.
1753 * \param absCurDirectory The current directory, absolute path
1755 // ============================================================================
1757 void SALOME_NamingService::_list_directory_recurs(vector<string>& myList,
1758 string relativeSubDir,
1759 string absCurDirectory)
1761 CosNaming::BindingList_var binding_list;
1762 CosNaming::BindingIterator_var binding_iterator;
1763 CosNaming::Binding_var binding ;
1765 unsigned long nb = 0 ; // --- only for thethe use of BindingIterator
1766 // to access the bindings
1770 CosNaming::NamingContext_var ref_context = _current_context;
1772 if (! relativeSubDir.empty())
1774 Change_Directory(relativeSubDir.c_str());
1775 absDir = absCurDirectory + "/" + relativeSubDir;
1779 absDir = absCurDirectory;
1782 _current_context->list(nb, binding_list, binding_iterator) ;
1784 if (! CORBA::is_nil(binding_iterator))
1786 while (binding_iterator->next_one(binding))
1788 CosNaming::Name bindingName = binding->binding_name;
1790 if (binding->binding_type == CosNaming::ncontext)
1792 string relativeSdir(bindingName[0].id);
1793 _list_directory_recurs(myList, relativeSdir, absDir);
1796 else if (binding->binding_type == CosNaming::nobject)
1798 string objName(bindingName[0].id);
1799 string elt = absDir + "/" + objName;
1801 myList.push_back(elt);
1805 binding_iterator->destroy();
1807 if (! relativeSubDir.empty())
1809 _current_context = ref_context;
1813 // ============================================================================
1814 /*! \brief return a stringified reference of root context
1816 * \return a stringified reference of root context
1818 // ============================================================================
1820 char * SALOME_NamingService::getIORaddr()
1822 return _orb->object_to_string(_root_context);