Salome HOME
Kernel evolution for salome gui without neither NS nor other servers.
[modules/kernel.git] / src / KernelHelpers / SALOME_KernelServices.cxx
1 // Copyright (C) 2007-2020  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   /**
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.
38    */
39   CORBA::ORB_ptr getORB() {
40     static CORBA::ORB_ptr orb;
41     if(CORBA::is_nil(orb)){
42       int argc=0;
43       orb = CORBA::ORB_init(argc,0);
44     }
45     return orb;
46   }
47   
48   void assignNamingServiceSL()
49   {
50     if ( !_naming_service.get() )
51     {
52       _naming_service.reset( new SALOME_Fake_NamingService );
53     }
54     else
55     {
56       THROW_SALOME_EXCEPTION("assignNamingServiceSL : NS SALOME Wrapper is already set !");
57     }
58   }
59
60   /**
61    * This function returns a static reference to the SALOME naming service.
62    */
63   SALOME_NamingService_Abstract *getNamingService()
64   {
65     if ( !_naming_service.get() )
66     {
67       _naming_service.reset( new SALOME_NamingService(getORB()) );
68     }
69     return _naming_service.get();
70   }
71   
72   /**
73    * This function returns a static reference to the SALOME life cycle CORBA.
74    */
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);
80     }
81     return lifeCycleCORBA;
82   }
83
84
85   /**
86    * This returns a static reference to the SALOME study. The
87    * study can be used to get informations about it.
88    * \sa getStudyServantSA
89    */
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);
96     }
97     return SALOMEDS::Study::_duplicate(aStudy); // we duplicate it here to allow using it with the var client instances.
98   }
99
100   /**
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.
104    */
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);
111     }
112     return salomeSession;
113   }
114
115   /**
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).
119    */
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);
128     }
129     return salomeLauncher;
130   }
131
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);
138     }
139     return resourcesManager;
140   }
141
142   /**
143    * This function retrieve the CORBA object reference from the study
144    * object wrapping it.
145    */
146   CORBA::Object_ptr SObjectToObject(SALOMEDS::SObject_ptr theSObject) {
147
148     SALOMEDS::GenericAttribute_var anAttr;
149     CORBA::Object_var anObject;
150     if(CORBA::is_nil(theSObject))
151       return anObject;
152     try{
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);
159       }
160     }catch(...){
161       INFOS("SObjectToObject - Unknown exception has occurred!!!");
162     }
163     return anObject._retn();
164   }
165
166   /*!
167    * This function provides a CORBA pointer to a servant from its IOR
168    * given as a string of characters.
169    */
170   CORBA::Object_ptr IORToObject(char * IOR) {
171     return getORB()->string_to_object(IOR);
172   }
173
174   //
175   // __GBO__ See the file ./src/SMESHGUI/SMESHGUI_Utils.h of SMESH_SRC
176   // for other helper functions
177   //
178
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);
184   }
185   
186   void RegisterCompo(const std::string& compoName, CORBA::Object_var compoPtr)
187   {
188     _compo_map[compoName] = compoPtr;
189   }
190   
191   CORBA::Object_var RetrieveCompo(const std::string& compoName)
192   {
193     auto it = _compo_map.find(compoName);
194     if( it != _compo_map.end() )
195     {
196       return (*it).second;
197     }
198     else
199     {
200       SALOME::SALOME_Exception ex(createSalomeException("RetrieveCompo : not implemented yet !"));
201       throw ex;
202     //GetLCC()->FindOrLoad_Component( "FactoryServer", compoName );
203     }
204   }
205 }