Salome HOME
ecee2c7f424d03394e6fbb84896ce6c9a318a2fa
[modules/kernel.git] / src / KernelHelpers / SALOME_KernelServices.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // Author: Guillaume Boulant (EDF/R&D) 
21
22 #include "SALOME_KernelServices.hxx"
23 #include "SALOME_Fake_NamingService.hxx"
24 #include "Utils_SALOME_Exception.hxx"
25 #include "KernelBasis.hxx"
26
27 #include <map>
28 #include <memory>
29
30 std::map<std::string,CORBA::Object_var> _compo_map;
31
32 std::unique_ptr<SALOME_NamingService_Abstract> _naming_service;
33
34 namespace KERNEL {
35   
36   void assignNamingServiceSL()
37   {
38     if ( !_naming_service.get() )
39     {
40       _naming_service.reset( new SALOME_Fake_NamingService );
41     }
42     else
43     {
44       THROW_SALOME_EXCEPTION("assignNamingServiceSL : NS SALOME Wrapper is already set !");
45     }
46   }
47
48   /**
49    * This function returns a static reference to the SALOME naming service.
50    */
51   SALOME_NamingService_Abstract *getNamingService()
52   {
53     if ( !_naming_service.get() )
54     {
55       if(getSSLMode())
56         _naming_service.reset( new SALOME_Fake_NamingService );
57       else
58         _naming_service.reset( new SALOME_NamingService(getORB()) );
59     }
60     return _naming_service.get();
61   }
62   
63   /**
64    * This function returns a static reference to the SALOME life cycle CORBA.
65    */
66   SALOME_LifeCycleCORBA * getLifeCycleCORBA() {
67     static SALOME_LifeCycleCORBA * lifeCycleCORBA;
68     if ( lifeCycleCORBA == NULL ) {
69       SALOME_NamingService_Abstract *aNamingService = getNamingService();
70       lifeCycleCORBA = new SALOME_LifeCycleCORBA(aNamingService);
71     }
72     return lifeCycleCORBA;
73   }
74
75
76   /**
77    * This returns a static reference to the SALOME study. The
78    * study can be used to get informations about it.
79    * \sa getStudyServantSA
80    */
81   SALOMEDS::Study_ptr getStudyServant() {
82     static SALOMEDS::Study_ptr aStudy;
83     if(CORBA::is_nil(aStudy)){
84       SALOME_NamingService_Abstract *aNamingService = getNamingService();
85       CORBA::Object_ptr anObject = aNamingService->Resolve("/Study");
86       aStudy = SALOMEDS::Study::_narrow(anObject);
87     }
88     return SALOMEDS::Study::_duplicate(aStudy); // we duplicate it here to allow using it with the var client instances.
89   }
90
91   /**
92    * This returns a static reference to the SALOME session. The
93    * SALOME session can be used to retrieve some objects of the
94    * current session, as the SALOME study.
95    */
96   SALOME::Session_ptr getSalomeSession() {
97     static SALOME::Session_ptr salomeSession;
98     if(CORBA::is_nil(salomeSession)){
99       SALOME_NamingService_Abstract *aNamingService = getNamingService();
100       CORBA::Object_ptr obj = aNamingService->Resolve("/Kernel/Session");
101       salomeSession = SALOME::Session::_narrow(obj);
102     }
103     return salomeSession;
104   }
105
106   /**
107    * This returns a static reference to the SALOME launcher. The
108    * SALOME launcher can be used to schedule jobs, local or remote,
109    * using a batch system or not (see SALOME documentation).
110    */
111   Engines::SalomeLauncher_ptr getSalomeLauncher() {
112     //LOG("KERNEL_services::getSalomeLauncher()");
113     static Engines::SalomeLauncher_ptr salomeLauncher;
114     if(CORBA::is_nil(salomeLauncher)){
115       //LOG("KERNEL_services::getSalomeLauncher(): creating the static instance");
116       SALOME_NamingService_Abstract *aNamingService = getNamingService();
117       CORBA::Object_ptr obj = aNamingService->Resolve("/SalomeLauncher");
118       salomeLauncher = Engines::SalomeLauncher::_narrow(obj);
119     }
120     return salomeLauncher;
121   }
122
123   Engines::ResourcesManager_ptr getResourcesManager() {
124     static Engines::ResourcesManager_ptr resourcesManager;
125     if(CORBA::is_nil(resourcesManager)){
126       SALOME_NamingService_Abstract *aNamingService = getNamingService();
127       CORBA::Object_ptr obj = aNamingService->Resolve("/ResourcesManager");
128       resourcesManager = Engines::ResourcesManager::_narrow(obj);
129     }
130     return resourcesManager;
131   }
132
133   /**
134    * This function retrieve the CORBA object reference from the study
135    * object wrapping it.
136    */
137   CORBA::Object_ptr SObjectToObject(SALOMEDS::SObject_ptr theSObject) {
138
139     SALOMEDS::GenericAttribute_var anAttr;
140     CORBA::Object_var anObject;
141     if(CORBA::is_nil(theSObject))
142       return anObject;
143     try{
144       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
145         SALOMEDS::AttributeIOR_var anIOR  = SALOMEDS::AttributeIOR::_narrow(anAttr);
146         CORBA::String_var aValue = anIOR->Value();
147         CORBA::ORB_ptr anORB = getORB();
148         if(strcmp(aValue,"") != 0)
149           anObject = anORB->string_to_object(aValue);
150       }
151     }catch(...){
152       INFOS("SObjectToObject - Unknown exception has occurred!!!");
153     }
154     return anObject._retn();
155   }
156
157   /*!
158    * This function provides a CORBA pointer to a servant from its IOR
159    * given as a string of characters.
160    */
161   CORBA::Object_ptr IORToObject(char * IOR) {
162     return getORB()->string_to_object(IOR);
163   }
164
165   //
166   // __GBO__ See the file ./src/SMESHGUI/SMESHGUI_Utils.h of SMESH_SRC
167   // for other helper functions
168   //
169
170   SALOME::SALOME_Exception createSalomeException(const char * text) {
171     SALOME::ExceptionStruct es;
172     es.type = SALOME::INTERNAL_ERROR;
173     es.text = CORBA::string_dup(text);
174     return SALOME::SALOME_Exception(es);
175   }
176   
177   void RegisterCompo(const std::string& compoName, CORBA::Object_var compoPtr)
178   {
179     _compo_map[compoName] = compoPtr;
180   }
181   
182   CORBA::Object_var RetrieveCompo(const std::string& compoName)
183   {
184     CORBA::Object_var ret;
185     auto it = _compo_map.find(compoName);
186     if( it != _compo_map.end() )
187     {
188       ret = (*it).second;
189     }
190     else
191     {
192       Engines::EngineComponent_var compo = getLifeCycleCORBA()->FindOrLoad_Component( "FactoryServer", compoName.c_str() );
193       ret = CORBA::Object::_narrow(compo);
194       _compo_map[compoName] = ret;
195     }
196     return ret._retn();
197   }
198 }