]> SALOME platform Git repositories - modules/kernel.git/blob - SALOME_KernelServices.cxx
Salome HOME
efc9bf85e2a708816763c10c3dd0be1a3e7780b9
[modules/kernel.git] / SALOME_KernelServices.cxx
1 // Copyright (C) 2007-2021  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
25 #include <map>
26 #include <memory>
27
28 std::map<std::string,CORBA::Object_var> _compo_map;
29
30 std::unique_ptr<SALOME_NamingService_Abstract> _naming_service;
31
32 namespace KERNEL {
33   
34   void assignNamingServiceSL()
35   {
36     if ( !_naming_service.get() )
37     {
38       _naming_service.reset( new SALOME_Fake_NamingService );
39     }
40     else
41     {
42       THROW_SALOME_EXCEPTION("assignNamingServiceSL : NS SALOME Wrapper is already set !");
43     }
44   }
45
46   /**
47    * This function returns a static reference to the SALOME naming service.
48    */
49   SALOME_NamingService_Abstract *getNamingService()
50   {
51     if ( !_naming_service.get() )
52     {
53       _naming_service.reset( new SALOME_NamingService(getORB()) );
54     }
55     return _naming_service.get();
56   }
57   
58   /**
59    * This function returns a static reference to the SALOME life cycle CORBA.
60    */
61   SALOME_LifeCycleCORBA * getLifeCycleCORBA() {
62     static SALOME_LifeCycleCORBA * lifeCycleCORBA;
63     if ( lifeCycleCORBA == NULL ) {
64       SALOME_NamingService_Abstract *aNamingService = getNamingService();
65       lifeCycleCORBA = new SALOME_LifeCycleCORBA(aNamingService);
66     }
67     return lifeCycleCORBA;
68   }
69
70
71   /**
72    * This returns a static reference to the SALOME study. The
73    * study can be used to get informations about it.
74    * \sa getStudyServantSA
75    */
76   SALOMEDS::Study_ptr getStudyServant() {
77     static SALOMEDS::Study_ptr aStudy;
78     if(CORBA::is_nil(aStudy)){
79       SALOME_NamingService_Abstract *aNamingService = getNamingService();
80       CORBA::Object_ptr anObject = aNamingService->Resolve("/Study");
81       aStudy = SALOMEDS::Study::_narrow(anObject);
82     }
83     return SALOMEDS::Study::_duplicate(aStudy); // we duplicate it here to allow using it with the var client instances.
84   }
85
86   /**
87    * This returns a static reference to the SALOME session. The
88    * SALOME session can be used to retrieve some objects of the
89    * current session, as the SALOME study.
90    */
91   SALOME::Session_ptr getSalomeSession() {
92     static SALOME::Session_ptr salomeSession;
93     if(CORBA::is_nil(salomeSession)){
94       SALOME_NamingService_Abstract *aNamingService = getNamingService();
95       CORBA::Object_ptr obj = aNamingService->Resolve("/Kernel/Session");
96       salomeSession = SALOME::Session::_narrow(obj);
97     }
98     return salomeSession;
99   }
100
101   /**
102    * This returns a static reference to the SALOME launcher. The
103    * SALOME launcher can be used to schedule jobs, local or remote,
104    * using a batch system or not (see SALOME documentation).
105    */
106   Engines::SalomeLauncher_ptr getSalomeLauncher() {
107     //LOG("KERNEL_services::getSalomeLauncher()");
108     static Engines::SalomeLauncher_ptr salomeLauncher;
109     if(CORBA::is_nil(salomeLauncher)){
110       //LOG("KERNEL_services::getSalomeLauncher(): creating the static instance");
111       SALOME_NamingService_Abstract *aNamingService = getNamingService();
112       CORBA::Object_ptr obj = aNamingService->Resolve("/SalomeLauncher");
113       salomeLauncher = Engines::SalomeLauncher::_narrow(obj);
114     }
115     return salomeLauncher;
116   }
117
118   Engines::ResourcesManager_ptr getResourcesManager() {
119     static Engines::ResourcesManager_ptr resourcesManager;
120     if(CORBA::is_nil(resourcesManager)){
121       SALOME_NamingService_Abstract *aNamingService = getNamingService();
122       CORBA::Object_ptr obj = aNamingService->Resolve("/ResourcesManager");
123       resourcesManager = Engines::ResourcesManager::_narrow(obj);
124     }
125     return resourcesManager;
126   }
127
128   /**
129    * This function retrieve the CORBA object reference from the study
130    * object wrapping it.
131    */
132   CORBA::Object_ptr SObjectToObject(SALOMEDS::SObject_ptr theSObject) {
133
134     SALOMEDS::GenericAttribute_var anAttr;
135     CORBA::Object_var anObject;
136     if(CORBA::is_nil(theSObject))
137       return anObject;
138     try{
139       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
140         SALOMEDS::AttributeIOR_var anIOR  = SALOMEDS::AttributeIOR::_narrow(anAttr);
141         CORBA::String_var aValue = anIOR->Value();
142         CORBA::ORB_ptr anORB = getORB();
143         if(strcmp(aValue,"") != 0)
144           anObject = anORB->string_to_object(aValue);
145       }
146     }catch(...){
147       INFOS("SObjectToObject - Unknown exception has occurred!!!");
148     }
149     return anObject._retn();
150   }
151
152   /*!
153    * This function provides a CORBA pointer to a servant from its IOR
154    * given as a string of characters.
155    */
156   CORBA::Object_ptr IORToObject(char * IOR) {
157     return getORB()->string_to_object(IOR);
158   }
159
160   //
161   // __GBO__ See the file ./src/SMESHGUI/SMESHGUI_Utils.h of SMESH_SRC
162   // for other helper functions
163   //
164
165   SALOME::SALOME_Exception createSalomeException(const char * text) {
166     SALOME::ExceptionStruct es;
167     es.type = SALOME::INTERNAL_ERROR;
168     es.text = CORBA::string_dup(text);
169     return SALOME::SALOME_Exception(es);
170   }
171   
172   void RegisterCompo(const std::string& compoName, CORBA::Object_var compoPtr)
173   {
174     _compo_map[compoName] = compoPtr;
175   }
176   
177   CORBA::Object_var RetrieveCompo(const std::string& compoName)
178   {
179     auto it = _compo_map.find(compoName);
180     if( it != _compo_map.end() )
181     {
182       return (*it).second;
183     }
184     else
185     {
186       SALOME::SALOME_Exception ex(createSalomeException("RetrieveCompo : not implemented yet !"));
187       throw ex;
188     //GetLCC()->FindOrLoad_Component( "FactoryServer", compoName );
189     }
190   }
191 }