1 // Copyright (C) 2007-2023 CEA, EDF, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // Author: Guillaume Boulant (EDF/R&D)
22 #include "SALOME_KernelServices.hxx"
23 #include "SALOME_Fake_NamingService.hxx"
24 #include "Utils_SALOME_Exception.hxx"
25 #include "KernelBasis.hxx"
30 std::map<std::string,CORBA::Object_var> _compo_map;
32 std::unique_ptr<SALOME_NamingService_Abstract> _naming_service;
36 void assignNamingServiceSL()
38 if ( !_naming_service.get() )
40 _naming_service.reset( new SALOME_Fake_NamingService );
44 THROW_SALOME_EXCEPTION("assignNamingServiceSL : NS SALOME Wrapper is already set !");
49 * This function returns a static reference to the SALOME naming service.
51 SALOME_NamingService_Abstract *getNamingService()
53 if ( !_naming_service.get() )
56 _naming_service.reset( new SALOME_Fake_NamingService );
58 _naming_service.reset( new SALOME_NamingService(getORB()) );
60 return _naming_service.get();
64 * This function returns a static reference to the SALOME life cycle CORBA.
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);
72 return lifeCycleCORBA;
77 * This returns a static reference to the SALOME study. The
78 * study can be used to get informations about it.
79 * \sa getStudyServantSA
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);
88 return SALOMEDS::Study::_duplicate(aStudy); // we duplicate it here to allow using it with the var client instances.
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.
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);
103 return salomeSession;
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).
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);
120 return salomeLauncher;
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);
130 return resourcesManager;
134 * This function retrieve the CORBA object reference from the study
135 * object wrapping it.
137 CORBA::Object_ptr SObjectToObject(SALOMEDS::SObject_ptr theSObject) {
139 SALOMEDS::GenericAttribute_var anAttr;
140 CORBA::Object_var anObject;
141 if(CORBA::is_nil(theSObject))
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);
152 INFOS("SObjectToObject - Unknown exception has occurred!!!");
154 return anObject._retn();
158 * This function provides a CORBA pointer to a servant from its IOR
159 * given as a string of characters.
161 CORBA::Object_ptr IORToObject(char * IOR) {
162 return getORB()->string_to_object(IOR);
166 // __GBO__ See the file ./src/SMESHGUI/SMESHGUI_Utils.h of SMESH_SRC
167 // for other helper functions
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);
177 void RegisterCompo(const std::string& compoName, CORBA::Object_var compoPtr)
179 _compo_map[compoName] = compoPtr;
182 CORBA::Object_var RetrieveCompo(const std::string& compoName)
184 CORBA::Object_var ret;
185 auto it = _compo_map.find(compoName);
186 if( it != _compo_map.end() )
192 Engines::EngineComponent_var compo = getLifeCycleCORBA()->FindOrLoad_Component( "FactoryServer", compoName.c_str() );
193 ret = CORBA::Object::_narrow(compo);
194 _compo_map[compoName] = ret;