1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, 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"
28 std::map<std::string,CORBA::Object_var> _compo_map;
30 std::unique_ptr<SALOME_NamingService_Abstract> _naming_service;
35 * This function returns a static reference to the orb. The orb can
36 * be used for example to initialize CORBA variables or to serialize
37 * and unserialize the CORBA objet to/from an IOR string.
39 CORBA::ORB_ptr getORB() {
40 static CORBA::ORB_ptr orb;
41 if(CORBA::is_nil(orb)){
43 orb = CORBA::ORB_init(argc,0);
48 void assignNamingServiceSL()
50 if ( !_naming_service.get() )
52 _naming_service.reset( new SALOME_Fake_NamingService );
56 THROW_SALOME_EXCEPTION("assignNamingServiceSL : NS SALOME Wrapper is already set !");
61 * This function returns a static reference to the SALOME naming service.
63 SALOME_NamingService_Abstract *getNamingService()
65 if ( !_naming_service.get() )
67 _naming_service.reset( new SALOME_NamingService(getORB()) );
69 return _naming_service.get();
73 * This function returns a static reference to the SALOME life cycle CORBA.
75 SALOME_LifeCycleCORBA * getLifeCycleCORBA() {
76 static SALOME_LifeCycleCORBA * lifeCycleCORBA;
77 if ( lifeCycleCORBA == NULL ) {
78 SALOME_NamingService_Abstract *aNamingService = getNamingService();
79 lifeCycleCORBA = new SALOME_LifeCycleCORBA(aNamingService);
81 return lifeCycleCORBA;
86 * This returns a static reference to the SALOME study. The
87 * study can be used to get informations about it.
88 * \sa getStudyServantSA
90 SALOMEDS::Study_ptr getStudyServant() {
91 static SALOMEDS::Study_ptr aStudy;
92 if(CORBA::is_nil(aStudy)){
93 SALOME_NamingService_Abstract *aNamingService = getNamingService();
94 CORBA::Object_ptr anObject = aNamingService->Resolve("/Study");
95 aStudy = SALOMEDS::Study::_narrow(anObject);
97 return SALOMEDS::Study::_duplicate(aStudy); // we duplicate it here to allow using it with the var client instances.
101 * This returns a static reference to the SALOME session. The
102 * SALOME session can be used to retrieve some objects of the
103 * current session, as the SALOME study.
105 SALOME::Session_ptr getSalomeSession() {
106 static SALOME::Session_ptr salomeSession;
107 if(CORBA::is_nil(salomeSession)){
108 SALOME_NamingService_Abstract *aNamingService = getNamingService();
109 CORBA::Object_ptr obj = aNamingService->Resolve("/Kernel/Session");
110 salomeSession = SALOME::Session::_narrow(obj);
112 return salomeSession;
116 * This returns a static reference to the SALOME launcher. The
117 * SALOME launcher can be used to schedule jobs, local or remote,
118 * using a batch system or not (see SALOME documentation).
120 Engines::SalomeLauncher_ptr getSalomeLauncher() {
121 //LOG("KERNEL_services::getSalomeLauncher()");
122 static Engines::SalomeLauncher_ptr salomeLauncher;
123 if(CORBA::is_nil(salomeLauncher)){
124 //LOG("KERNEL_services::getSalomeLauncher(): creating the static instance");
125 SALOME_NamingService_Abstract *aNamingService = getNamingService();
126 CORBA::Object_ptr obj = aNamingService->Resolve("/SalomeLauncher");
127 salomeLauncher = Engines::SalomeLauncher::_narrow(obj);
129 return salomeLauncher;
132 Engines::ResourcesManager_ptr getResourcesManager() {
133 static Engines::ResourcesManager_ptr resourcesManager;
134 if(CORBA::is_nil(resourcesManager)){
135 SALOME_NamingService_Abstract *aNamingService = getNamingService();
136 CORBA::Object_ptr obj = aNamingService->Resolve("/ResourcesManager");
137 resourcesManager = Engines::ResourcesManager::_narrow(obj);
139 return resourcesManager;
143 * This function retrieve the CORBA object reference from the study
144 * object wrapping it.
146 CORBA::Object_ptr SObjectToObject(SALOMEDS::SObject_ptr theSObject) {
148 SALOMEDS::GenericAttribute_var anAttr;
149 CORBA::Object_var anObject;
150 if(CORBA::is_nil(theSObject))
153 if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
154 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
155 CORBA::String_var aValue = anIOR->Value();
156 CORBA::ORB_ptr anORB = getORB();
157 if(strcmp(aValue,"") != 0)
158 anObject = anORB->string_to_object(aValue);
161 INFOS("SObjectToObject - Unknown exception has occurred!!!");
163 return anObject._retn();
167 * This function provides a CORBA pointer to a servant from its IOR
168 * given as a string of characters.
170 CORBA::Object_ptr IORToObject(char * IOR) {
171 return getORB()->string_to_object(IOR);
175 // __GBO__ See the file ./src/SMESHGUI/SMESHGUI_Utils.h of SMESH_SRC
176 // for other helper functions
179 SALOME::SALOME_Exception createSalomeException(const char * text) {
180 SALOME::ExceptionStruct es;
181 es.type = SALOME::INTERNAL_ERROR;
182 es.text = CORBA::string_dup(text);
183 return SALOME::SALOME_Exception(es);
186 void RegisterCompo(const std::string& compoName, CORBA::Object_var compoPtr)
188 _compo_map[compoName] = compoPtr;
191 CORBA::Object_var RetrieveCompo(const std::string& compoName)
193 auto it = _compo_map.find(compoName);
194 if( it != _compo_map.end() )
200 SALOME::SALOME_Exception ex(createSalomeException("RetrieveCompo : not implemented yet !"));
202 //GetLCC()->FindOrLoad_Component( "FactoryServer", compoName );