Salome HOME
[EDF29150] : Base mechanism to keep track of CPU and Mem info
[modules/kernel.git] / src / KernelHelpers / SALOME_KernelServices.cxx
index 481c1ac13c3ddd45573527ed097984f152bddba3..b0f8776601d45230afb5b56a0702af0688e61d0e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 // Author: Guillaume Boulant (EDF/R&D) 
 
 #include "SALOME_KernelServices.hxx"
+#include "SALOME_Fake_NamingService.hxx"
+#include "Utils_SALOME_Exception.hxx"
+#include "KernelBasis.hxx"
+
+#include <map>
+#include <memory>
+
+std::map<std::string,CORBA::Object_var> _compo_map;
+
+std::unique_ptr<SALOME_NamingService_Abstract> _naming_service;
 
 namespace KERNEL {
   
-  /**
-   * This function returns a static reference to the orb. The orb can
-   * be used for example to initialize CORBA variables or to serialize
-   * and unserialize the CORBA objet to/from an IOR string.
-   */
-  CORBA::ORB_ptr getORB() {
-    static CORBA::ORB_ptr orb;
-    if(CORBA::is_nil(orb)){
-      int argc=0;
-      orb = CORBA::ORB_init(argc,0);
+  void assignNamingServiceSL()
+  {
+    if ( !_naming_service.get() )
+    {
+      _naming_service.reset( new SALOME_Fake_NamingService );
+    }
+    else
+    {
+      THROW_SALOME_EXCEPTION("assignNamingServiceSL : NS SALOME Wrapper is already set !");
     }
-    return orb;
   }
-  
+
   /**
    * This function returns a static reference to the SALOME naming service.
    */
-  SALOME_NamingService * getNamingService() {
-    static SALOME_NamingService * namingService;
-    if ( namingService == NULL ) {
-      namingService = new SALOME_NamingService(getORB());
+  SALOME_NamingService_Abstract *getNamingService()
+  {
+    if ( !_naming_service.get() )
+    {
+      if(getSSLMode())
+        _naming_service.reset( new SALOME_Fake_NamingService );
+      else
+        _naming_service.reset( new SALOME_NamingService(getORB()) );
     }
-    return namingService;
+    return _naming_service.get();
   }
   
   /**
@@ -53,7 +66,7 @@ namespace KERNEL {
   SALOME_LifeCycleCORBA * getLifeCycleCORBA() {
     static SALOME_LifeCycleCORBA * lifeCycleCORBA;
     if ( lifeCycleCORBA == NULL ) {
-      SALOME_NamingService *aNamingService = getNamingService();
+      SALOME_NamingService_Abstract *aNamingService = getNamingService();
       lifeCycleCORBA = new SALOME_LifeCycleCORBA(aNamingService);
     }
     return lifeCycleCORBA;
@@ -61,18 +74,18 @@ namespace KERNEL {
 
 
   /**
-   * This returns a static reference to the SALOME study manager. The
-   * study manager can be used to retrieve a study or to get
-   * informations about a study.
+   * This returns a static reference to the SALOME study. The
+   * study can be used to get informations about it.
+   * \sa getStudyServantSA
    */
-  SALOMEDS::StudyManager_ptr getStudyManager() {
-    static SALOMEDS::StudyManager_ptr aStudyManager;
-    if(CORBA::is_nil(aStudyManager)){
-      SALOME_NamingService *aNamingService = getNamingService();
-      CORBA::Object_ptr anObject = aNamingService->Resolve("/myStudyManager");
-      aStudyManager = SALOMEDS::StudyManager::_narrow(anObject);
+  SALOMEDS::Study_ptr getStudyServant() {
+    static SALOMEDS::Study_ptr aStudy;
+    if(CORBA::is_nil(aStudy)){
+      SALOME_NamingService_Abstract *aNamingService = getNamingService();
+      CORBA::Object_ptr anObject = aNamingService->Resolve("/Study");
+      aStudy = SALOMEDS::Study::_narrow(anObject);
     }
-    return aStudyManager;
+    return SALOMEDS::Study::_duplicate(aStudy); // we duplicate it here to allow using it with the var client instances.
   }
 
   /**
@@ -83,7 +96,7 @@ namespace KERNEL {
   SALOME::Session_ptr getSalomeSession() {
     static SALOME::Session_ptr salomeSession;
     if(CORBA::is_nil(salomeSession)){
-      SALOME_NamingService *aNamingService = getNamingService();
+      SALOME_NamingService_Abstract *aNamingService = getNamingService();
       CORBA::Object_ptr obj = aNamingService->Resolve("/Kernel/Session");
       salomeSession = SALOME::Session::_narrow(obj);
     }
@@ -100,7 +113,7 @@ namespace KERNEL {
     static Engines::SalomeLauncher_ptr salomeLauncher;
     if(CORBA::is_nil(salomeLauncher)){
       //LOG("KERNEL_services::getSalomeLauncher(): creating the static instance");
-      SALOME_NamingService *aNamingService = getNamingService();
+      SALOME_NamingService_Abstract *aNamingService = getNamingService();
       CORBA::Object_ptr obj = aNamingService->Resolve("/SalomeLauncher");
       salomeLauncher = Engines::SalomeLauncher::_narrow(obj);
     }
@@ -110,34 +123,13 @@ namespace KERNEL {
   Engines::ResourcesManager_ptr getResourcesManager() {
     static Engines::ResourcesManager_ptr resourcesManager;
     if(CORBA::is_nil(resourcesManager)){
-      SALOME_NamingService *aNamingService = getNamingService();
+      SALOME_NamingService_Abstract *aNamingService = getNamingService();
       CORBA::Object_ptr obj = aNamingService->Resolve("/ResourcesManager");
       resourcesManager = Engines::ResourcesManager::_narrow(obj);
     }
     return resourcesManager;
   }
 
-  /**
-   * This returns the study with the specified id if it's defined in
-   * the SALOME study manager. Returns null otherwise.
-   * Please not that it is just a shortcut, and you may prefer use
-   * directly the study manager:
-   *    KERNEL::getStudyManager()->GetStudyByID(aStudyId)
-   */
-  SALOMEDS::Study_ptr getStudyById(int aStudyId) {
-    if ( aStudyId < 0 ) {
-      INFOS("ERR: trying to get a study with ID<0");
-      return SALOMEDS::Study::_nil();
-    }
-    return getStudyManager()->GetStudyByID(aStudyId);
-  }
-
-  int getStudyId(SALOMEDS::Study_ptr study) {
-    if( CORBA::is_nil(study) ) return -1;
-    return study->StudyId();
-  }
-
-
   /**
    * This function retrieve the CORBA object reference from the study
    * object wrapping it.
@@ -157,7 +149,7 @@ namespace KERNEL {
          anObject = anORB->string_to_object(aValue);
       }
     }catch(...){
-      INFOS("SObjectToObject - Unknown exception was occured!!!");
+      INFOS("SObjectToObject - Unknown exception has occurred!!!");
     }
     return anObject._retn();
   }
@@ -181,5 +173,26 @@ namespace KERNEL {
     es.text = CORBA::string_dup(text);
     return SALOME::SALOME_Exception(es);
   }
-
+  
+  void RegisterCompo(const std::string& compoName, CORBA::Object_var compoPtr)
+  {
+    _compo_map[compoName] = compoPtr;
+  }
+  
+  CORBA::Object_var RetrieveCompo(const std::string& compoName)
+  {
+    CORBA::Object_var ret;
+    auto it = _compo_map.find(compoName);
+    if( it != _compo_map.end() )
+    {
+      ret = (*it).second;
+    }
+    else
+    {
+      Engines::EngineComponent_var compo = getLifeCycleCORBA()->FindOrLoad_Component( "FactoryServer", compoName.c_str() );
+      ret = CORBA::Object::_narrow(compo);
+      _compo_map[compoName] = ret;
+    }
+    return ret._retn();
+  }
 }