Salome HOME
Porting to Python 2.6 - add coding page specification for Python scripts
[modules/kernel.git] / src / NamingService / SALOME_NamingService.cxx
index 0c046c9ea8b3a9a31a8fe446bc1d1a027c67f85a..70d337008f190dc5fe79c3599db7a518e6c35ba4 100644 (file)
@@ -1,6 +1,6 @@
-//  SALOME NamingService : wrapping NamingService services
+//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
 //  This library is free software; you can redistribute it and/or
 //  License along with this library; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
-//
-//
+//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+//  SALOME NamingService : wrapping NamingService services
 //  File   : SALOME_NamingService.cxx
 //  Author : Estelle Deville
 //  Module : SALOME
 //  $Header$
-
+//
 #include "SALOME_NamingService.hxx"
 #include "ServiceUnreachable.hxx"
 
 
 using namespace std;
 
+/*! \class SALOME_NamingService
+    \brief A class to manage the SALOME naming service
+
+*/
+
 // ============================================================================
 /*! \brief Default Constructor without ORB reference.
  *
@@ -63,7 +67,7 @@ SALOME_NamingService::SALOME_NamingService()
 SALOME_NamingService::SALOME_NamingService(CORBA::ORB_ptr orb)
 {
   MESSAGE("SALOME_NamingService creation");
-  _orb = orb ;
+  _orb = CORBA::ORB::_duplicate(orb);
   _initialize_root_context();
 }
 
@@ -86,6 +90,7 @@ SALOME_NamingService::~SALOME_NamingService()
  * 
  *  Initializes ORB reference and naming service root context.
  *  For use after default constructor.
+ *  If param orb is null, the orb is initialized
  *  \param orb CORBA::ORB_ptr arguments
  */ 
 // ============================================================================
@@ -95,7 +100,13 @@ void SALOME_NamingService::init_orb(CORBA::ORB_ptr orb)
   MESSAGE("SALOME_NamingService initialisation");
 
   Utils_Locker lock (&_myMutex);
-  _orb = orb;
+  if(orb)
+    _orb = CORBA::ORB::_duplicate(orb);
+  else
+    {
+      int argc=0;
+      _orb = CORBA::ORB_init(argc, 0); // Here we make the assumption that the orb has already been initialized
+    }
 
   _initialize_root_context();
 }
@@ -132,10 +143,9 @@ void SALOME_NamingService::Register(CORBA::Object_ptr ObjRef,
   // --- _current_context is replaced to the _root_context
   //     if the Path begins whith '/'
 
-  if (Path[0] == '/')
-    {
-      _current_context = _root_context;
-    }
+  if (Path[0] == '/'){
+    _current_context = _root_context;
+  }
 
   // --- the resolution of the directory path has to be done
   //      to place the current_context to the correct node
@@ -149,179 +159,158 @@ void SALOME_NamingService::Register(CORBA::Object_ptr ObjRef,
 
   CORBA::Boolean not_exist = false;
 
-  if (dimension_resultat > 0)
-    {
-      // A directory is treated (not only an object name)
-      // test if the directory where ObjRef should be recorded already exists
-      // If not, create the new context
-
-      try
-        {
-          CORBA::Object_var obj = _current_context->resolve(context_name);
-          _current_context = CosNaming::NamingContext::_narrow(obj);
-        }
-
-      catch (CosNaming::NamingContext::NotFound &)
-        {
-          // --- failed to resolve, therefore assume cold start
-          not_exist = true;
-        }
-
-      catch (CosNaming::NamingContext::InvalidName &)
-        {
-          INFOS("Register() : CosNaming::NamingContext::InvalidName");
-        }
-
-      catch (CosNaming::NamingContext::CannotProceed &)
-        {
-          INFOS("Register() : CosNaming::NamingContext::CannotProceed");
-        }
-
-      catch (CORBA::SystemException&)
-        {
-          INFOS("Register() : CORBA::SystemException: "
-                << "unable to contact the naming service");
-          throw ServiceUnreachable();
-        }
-
-      if (not_exist)
-        {
-          try
-            {
-              context_name.length(1);
-              for (int i = 0 ; i < dimension_resultat ;i++)
-                {
-                  context_name[0].id =
-                    CORBA::string_dup(splitPath[i].c_str());
-                  context_name[0].kind = CORBA::string_dup("dir");
-                  // SCRUTE(_context_name[0].id);
-                  // --- check if the path is created
-                  try
-                    {
-                      // --- if the context is already created, nothing to do
-                      CORBA::Object_var obj =
-                        _current_context->resolve(context_name);
-                      _current_context =
-                        CosNaming::NamingContext::_narrow(obj);
-                    }
-
-                  catch (CosNaming::NamingContext::NotFound &)
-                    {
-                      // --- the context must be created
-                      CosNaming::NamingContext_var temp_context =
-                        _current_context->bind_new_context(context_name);
-                      _current_context = temp_context;
-                    }
-                }
-            }
-
-          catch (CosNaming::NamingContext::AlreadyBound&)
-            {
-              INFOS("Register() : CosNaming::NamingContext::AlreadyBound");
-            }
-
-          catch (CosNaming::NamingContext::NotFound& ex)
-            {
-              CosNaming::Name n = ex.rest_of_name;
-
-              if (ex.why == CosNaming::NamingContext::missing_node)
-                INFOS("Register() : " << (char *) n[0].id
-                      << " (" << (char *) n[0].kind << ") not found");
+  if (dimension_resultat > 0){
+    // A directory is treated (not only an object name)
+    // test if the directory where ObjRef should be recorded already exists
+    // If not, create the new context
+    
+    try{
+      CORBA::Object_var obj = _current_context->resolve(context_name);
+      _current_context = CosNaming::NamingContext::_narrow(obj);
+    }
 
-              if (ex.why == CosNaming::NamingContext::not_context)
-                INFOS("Register() : " << (char *) n[0].id
-                      << " (" << (char *) n[0].kind
-                      << ") is not a context");
+    catch (CosNaming::NamingContext::NotFound &){
+      // --- failed to resolve, therefore assume cold start
+      not_exist = true;
+    }
 
-              if (ex.why == CosNaming::NamingContext::not_object)
-                INFOS("Register() : " << (char *) n[0].id
-                      << " (" << (char *) n[0].kind
-                      << ") is not an object");
-            }
+    catch (CosNaming::NamingContext::InvalidName &){
+      INFOS("Register() : CosNaming::NamingContext::InvalidName");
+    }
 
-          catch (CosNaming::NamingContext::CannotProceed&)
-            {
-              INFOS("Register(): CosNaming::NamingContext::CannotProceed");
-            }
+    catch (CosNaming::NamingContext::CannotProceed &){
+      INFOS("Register() : CosNaming::NamingContext::CannotProceed");
+    }
 
-          catch (CosNaming::NamingContext::InvalidName&)
-            {
-              INFOS("Register(): CosNaming::NamingContext::InvalidName");
-            }
+    catch (CORBA::SystemException&){
+      INFOS("Register() : CORBA::SystemException: "
+           << "unable to contact the naming service");
+      throw ServiceUnreachable();
+    }
 
-          catch (CORBA::SystemException&)
-            {
-              INFOS("Register():CORBA::SystemException: "
-                    << "unable to contact the naming service");
-              throw ServiceUnreachable();
-            }
-        }
+    if (not_exist){
+      try{
+       context_name.length(1);
+       for (int i = 0 ; i < dimension_resultat ;i++){
+         context_name[0].id = CORBA::string_dup(splitPath[i].c_str());
+         context_name[0].kind = CORBA::string_dup("dir");
+         // SCRUTE(_context_name[0].id);
+         // --- check if the path is created
+         try{
+           // --- if the context is already created, nothing to do
+           CORBA::Object_var obj = _current_context->resolve(context_name);
+           _current_context = CosNaming::NamingContext::_narrow(obj);
+         }
+
+         catch (CosNaming::NamingContext::NotFound &){
+           try{
+             // --- the context must be created
+             CosNaming::NamingContext_var temp_context =
+               _current_context->bind_new_context(context_name);
+             _current_context = temp_context;
+           }
+           catch (CosNaming::NamingContext::AlreadyBound&){
+             CORBA::Object_var obj = _current_context->resolve(context_name);
+             _current_context = CosNaming::NamingContext::_narrow(obj);
+           }
+         }
+       }
+      }
+
+      catch (CosNaming::NamingContext::AlreadyBound&){
+       INFOS("Register() : CosNaming::NamingContext::AlreadyBound");
+      }
+
+      catch (CosNaming::NamingContext::NotFound& ex){
+       CosNaming::Name n = ex.rest_of_name;
+
+       if (ex.why == CosNaming::NamingContext::missing_node)
+         INFOS("Register() : " << (char *) n[0].id
+               << " (" << (char *) n[0].kind << ") not found");
+
+       if (ex.why == CosNaming::NamingContext::not_context)
+         INFOS("Register() : " << (char *) n[0].id
+               << " (" << (char *) n[0].kind
+               << ") is not a context");
+
+       if (ex.why == CosNaming::NamingContext::not_object)
+         INFOS("Register() : " << (char *) n[0].id
+               << " (" << (char *) n[0].kind
+               << ") is not an object");
+      }
+
+      catch (CosNaming::NamingContext::CannotProceed&){
+       INFOS("Register(): CosNaming::NamingContext::CannotProceed");
+      }
+
+      catch (CosNaming::NamingContext::InvalidName&){
+       INFOS("Register(): CosNaming::NamingContext::InvalidName");
+      }
+
+      catch (CORBA::SystemException&){
+       INFOS("Register():CORBA::SystemException: "
+             << "unable to contact the naming service");
+       throw ServiceUnreachable();
+      }
     }
+  }
 
   // --- The current directory is now the directory where the object should
   //     be recorded
 
   int sizePath = splitPath.size();
-  if (sizePath > dimension_resultat)
-    {
-      ASSERT(sizePath == dimension_resultat+1);
-      context_name.length(1);
+  if (sizePath > dimension_resultat){
+    ASSERT(sizePath == dimension_resultat+1);
+    context_name.length(1);
 
-      try
-       {
-         // --- the last element is an object and not a directory
+    try{
+      // --- the last element is an object and not a directory
 
-         context_name[0].id =
-           CORBA::string_dup(splitPath[dimension_resultat].c_str());
-         context_name[0].kind = CORBA::string_dup("object");
-         //SCRUTE(context_name[0].id);
+      context_name[0].id = CORBA::string_dup(splitPath[dimension_resultat].c_str());
+      context_name[0].kind = CORBA::string_dup("object");
+      //SCRUTE(context_name[0].id);
 
-         _current_context->bind(context_name, ObjRef);
-       }
+      _current_context->bind(context_name, ObjRef);
+    }
 
-      catch (CosNaming::NamingContext::NotFound& ex)
-       {
-         CosNaming::Name n = ex.rest_of_name;
+    catch (CosNaming::NamingContext::NotFound& ex){
+      CosNaming::Name n = ex.rest_of_name;
 
-         if (ex.why == CosNaming::NamingContext::missing_node)
-           INFOS("Register() : " << (char *) n[0].id
-                 << " (" << (char *) n[0].kind << ") not found");
+      if (ex.why == CosNaming::NamingContext::missing_node)
+       INFOS("Register() : " << (char *) n[0].id
+             << " (" << (char *) n[0].kind << ") not found");
 
-         if (ex.why == CosNaming::NamingContext::not_context)
-           INFOS("Register() : " << (char *) n[0].id
-                 << " (" << (char *) n[0].kind
-                 << ") is not a context");
+      if (ex.why == CosNaming::NamingContext::not_context)
+       INFOS("Register() : " << (char *) n[0].id
+             << " (" << (char *) n[0].kind
+             << ") is not a context");
 
-         if (ex.why == CosNaming::NamingContext::not_object)
-           INFOS("Register() : " << (char *) n[0].id
-                 << " (" << (char *) n[0].kind
-                 << ") is not an object");
-       }
+      if (ex.why == CosNaming::NamingContext::not_object)
+       INFOS("Register() : " << (char *) n[0].id
+             << " (" << (char *) n[0].kind
+             << ") is not an object");
+    }
 
-      catch (CosNaming::NamingContext::CannotProceed&)
-       {
-         INFOS("Register(): CosNaming::NamingContext::CannotProceed");
-       }
+    catch (CosNaming::NamingContext::CannotProceed&){
+      INFOS("Register(): CosNaming::NamingContext::CannotProceed");
+    }
 
-      catch (CosNaming::NamingContext::InvalidName&)
-       {
-         INFOS("Register(): CosNaming::NamingContext::InvalidName");
-       }
+    catch (CosNaming::NamingContext::InvalidName&){
+      INFOS("Register(): CosNaming::NamingContext::InvalidName");
+    }
 
-      catch (CosNaming::NamingContext::AlreadyBound&)
-       {
-         INFOS("Register(): CosNaming::NamingContext::AlreadyBound, "
-               << "object will be rebind");
-         _current_context->rebind(context_name, ObjRef);
-       }
+    catch (CosNaming::NamingContext::AlreadyBound&){
+      INFOS("Register(): CosNaming::NamingContext::AlreadyBound, "
+           << "object will be rebind");
+      _current_context->rebind(context_name, ObjRef);
+    }
 
-      catch (CORBA::SystemException&)
-       {
-         INFOS("!!!Register(): CORBA::SystemException: "
-               << "unable to contact the naming service");
-         throw ServiceUnreachable();
-       }
+    catch (CORBA::SystemException&){
+      INFOS("!!!Register(): CORBA::SystemException: "
+           << "unable to contact the naming service");
+      throw ServiceUnreachable();
     }
+  }
 }
 
 // ============================================================================
@@ -361,14 +350,14 @@ CORBA::Object_ptr SALOME_NamingService::Resolve(const char* Path)
 
   CosNaming::Name context_name;
   vector<string> splitPath;
-  int dimension_resultat = _createContextNameDir(Path,
+  _createContextNameDir(Path,
                                                 context_name,
                                                 splitPath,
                                                 false);
 
   ASSERT(!CORBA::is_nil(_current_context));
 
-  CORBA::Object_ptr obj =  CORBA::Object::_nil();
+  CORBA::Object_var obj =  CORBA::Object::_nil();
 
   try
     {
@@ -380,7 +369,7 @@ CORBA::Object_ptr SALOME_NamingService::Resolve(const char* Path)
       CosNaming::Name n = ex.rest_of_name;
 
       if (ex.why == CosNaming::NamingContext::missing_node)
-        INFOS("Resolve() : " << (char *) n[0].id
+        MESSAGE("Resolve() : " << (char *) n[0].id
               << " (" << (char *) n[0].kind << ") not found");
 
       if (ex.why == CosNaming::NamingContext::not_context)
@@ -411,7 +400,7 @@ CORBA::Object_ptr SALOME_NamingService::Resolve(const char* Path)
       throw ServiceUnreachable();
     }
 
-  return obj;
+  return obj._retn();
 }
 
 // ============================================================================
@@ -449,7 +438,7 @@ CORBA::Object_ptr SALOME_NamingService::ResolveFirst(const char* Path)
     }
 
 //   SCRUTE(name);
-  CORBA::Object_ptr obj = CORBA::Object::_nil();
+  CORBA::Object_var obj = CORBA::Object::_nil();
 
   bool isOk = false;
   if (basePath.empty())
@@ -475,7 +464,7 @@ CORBA::Object_ptr SALOME_NamingService::ResolveFirst(const char* Path)
        }
     }
 
-  return obj;
+  return obj._retn();
 }
 
 // ============================================================================
@@ -486,9 +475,9 @@ CORBA::Object_ptr SALOME_NamingService::ResolveFirst(const char* Path)
  *  number of processors.
  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
  * \param hostname      name of the machine on which the component is searched.
- * \param containername name of the container in which the component is
+ * \param containerName name of the container in which the component is
                         instanciated.
- * \param componentname name of the component we are looking for an existing 
+ * \param componentName name of the component we are looking for an existing 
                         instance.
  * \param nbproc        in case of multi processor machine, container name is
  *                      suffixed with _nbproc.
@@ -536,7 +525,8 @@ SALOME_NamingService::ResolveComponent(const char* hostname,
   else
     {
       SCRUTE(name);
-      if (Change_Directory(name.c_str()))
+      string basename = name;
+      if (Change_Directory(basename.c_str()))
        {
          vector<string> contList = list_subdirs();
 
@@ -560,6 +550,8 @@ SALOME_NamingService::ResolveComponent(const char* hostname,
 
              if ( !CORBA::is_nil(obj) )
                return obj;
+              else
+                Change_Directory(basename.c_str());
            }
        }
 
@@ -639,12 +631,11 @@ SALOME_NamingService::ContainerName(const Engines::MachineParameters& params)
  *  Build a string representing the absolute pathname of a container in
  *  SALOME_NamingService. This form gives a suffixed containerName in case of
  *  multi processor machine.
- * \param params struct from which we get container name (may be
- *               empty),  number of nodes and number of processor
- *               per node.
- * /param hostname name of the host of the container, without domain names.
- * /return the path under the form /Containers/hostname/containerName
- * /sa ContainerName(const Engines::MachineParameters& params)
+ * \param containerName name of the container in which the component is
+                        instanciated.
+ * \param hostname name of the host of the container, without domain names.
+ * \return the path under the form /Containers/hostname/containerName
+ * \sa ContainerName(const Engines::MachineParameters& params)
  */
 // ============================================================================
 
@@ -664,10 +655,10 @@ string SALOME_NamingService::BuildContainerNameForNS(const char *containerName,
  *
  *  Build a string representing the absolute pathname of a container in
  *  SALOME_NamingService.
- * /param params used as it is, or replaced by FactoryServer if empty.
- * /param hostname name of the host of the container, without domain names.
- * /return the path under the form /Containers/hostname/containerName
- * /sa ContainerName(const char *containerName)
+ * \param params used as it is, or replaced by FactoryServer if empty.
+ * \param hostname name of the host of the container, without domain names.
+ * \return the path under the form /Containers/hostname/containerName
+ * \sa ContainerName(const char *containerName)
  */
 // ============================================================================
 
@@ -826,7 +817,7 @@ throw(ServiceUnreachable)
 //   SCRUTE(path);
   CosNaming::Name context_name;
   vector<string> splitPath;
-  int dimension_resultat = _createContextNameDir(path.c_str(),
+  _createContextNameDir(path.c_str(),
                                                 context_name,
                                                 splitPath,
                                                 true);
@@ -847,7 +838,7 @@ throw(ServiceUnreachable)
       CosNaming::Name n = ex.rest_of_name;
       
       if (ex.why == CosNaming::NamingContext::missing_node)
-       INFOS( "Change_Directory() : " << (char *) n[0].id
+       MESSAGE( "Change_Directory() : " << (char *) n[0].id
               << " (" << (char *) n[0].kind << ") not found");
       if (ex.why == CosNaming::NamingContext::not_context)
        INFOS("Change_Directory() : " << (char *) n[0].id
@@ -1034,7 +1025,9 @@ throw(ServiceUnreachable)
 
       if (binding->binding_type == CosNaming::nobject)
         {
-          dirList.push_back(CORBA::string_dup(bindingName[0].id));
+          // remove memory leak
+          // dirList.push_back(CORBA::string_dup(bindingName[0].id));
+          dirList.push_back(string(bindingName[0].id));
         }
     }
 
@@ -1086,7 +1079,7 @@ throw(ServiceUnreachable)
 
       if (binding->binding_type == CosNaming::ncontext)
         {
-          dirList.push_back(CORBA::string_dup(bindingName[0].id));
+          dirList.push_back(bindingName[0].id.in());
         }
     }
 
@@ -1118,10 +1111,12 @@ throw(ServiceUnreachable)
 
   vector<string> dirList ;
 
-  string currentDir = Current_Directory();
+  char* currentDir = Current_Directory();
 
   _list_directory_recurs(dirList, "", currentDir);
 
+  free(currentDir);
+
   return dirList;
 }
 
@@ -1672,7 +1667,7 @@ void SALOME_NamingService::_Find(const char* name,
  * \param splitPath 
  * \param lengthResult
  * \param contextToFind
- * \param _notFound
+ * \param notFound
  */ 
 // ============================================================================
 
@@ -1707,7 +1702,8 @@ _current_directory(vector<string>& splitPath,
             {
               // --- directory, search in it
 
-             splitPath.push_back(CORBA::string_dup(bindingName[0].id));
+              const char* bindingNameid=bindingName[0].id;
+             splitPath.push_back(bindingNameid);
               lengthResult++;
 
               CORBA::Object_var obj = _current_context->resolve(bindingName);
@@ -1759,14 +1755,12 @@ _current_directory(vector<string>& splitPath,
  *  get a list of all the objects in the current directory, with recursion
  *  on the subdirectories. Only the objects are listed, not the directories.
  *  If the NamingService is out, the exception ServiceUnreachable is thrown.
- * \param myList the list of objects
- * \
-Function :_list_directory_recurs.
- * Purpose  : method to list recursively all the objects contained in the tree of absCurDirectory/relativeSubDir.
- *  \param myList The list that will be filled.
- *  \param relativeSubDir The directory from absCurDirectory in which the objects are found.
- *  \param absCurDirectory The directory in ABSOLUTE form.
  *  _current_context must refer to absCurDirectory.
+ *
+ *  \param myList          The list that will be filled.
+ *  \param relativeSubDir  The directory relative to absCurDirectory in which
+ *                         the objects are found.
+ *  \param absCurDirectory The current directory, absolute path
  */ 
 // ============================================================================
 
@@ -1838,3 +1832,12 @@ char * SALOME_NamingService::getIORaddr()
   return _orb->object_to_string(_root_context);
 }
 
+/*! \brief get the orb used by the naming service
+ *
+ *  \return the orb
+ */
+CORBA::ORB_ptr SALOME_NamingService::orb()
+{
+  return _orb;
+}
+