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