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