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