Salome HOME
Add the KernelHelpers package that contains helper functions to deal with KERNEL...
[modules/kernel.git] / src / KernelHelpers / SALOME_KernelServices.cxx
1 // Copyright (C) 2007-2011  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.
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 // Author: Guillaume Boulant (EDF/R&D) 
20
21 #include "SALOME_KernelServices.hxx"
22
23 namespace KERNEL {
24   
25   /**
26    * This function returns a static reference to the orb. The orb can
27    * be used for example to initialize CORBA variables or to serialize
28    * and unserialize the CORBA objet to/from an IOR string.
29    */
30   CORBA::ORB_ptr getORB() {
31     static CORBA::ORB_ptr orb;
32     if(CORBA::is_nil(orb)){
33       int argc=0;
34       orb = CORBA::ORB_init(argc,0);
35     }
36     return orb;
37   }
38   
39   /**
40    * This function returns a static reference to the SALOME naming service.
41    */
42   SALOME_NamingService * getNamingService() {
43     static SALOME_NamingService * namingService;
44     if ( namingService == NULL ) {
45       namingService = new SALOME_NamingService(getORB());
46     }
47     return namingService;
48   }
49   
50   /**
51    * This function returns a static reference to the SALOME life cycle CORBA.
52    */
53   SALOME_LifeCycleCORBA * getLifeCycleCORBA() {
54     static SALOME_LifeCycleCORBA * lifeCycleCORBA;
55     if ( lifeCycleCORBA == NULL ) {
56       SALOME_NamingService *aNamingService = getNamingService();
57       lifeCycleCORBA = new SALOME_LifeCycleCORBA(aNamingService);
58     }
59     return lifeCycleCORBA;
60   }
61
62
63   /**
64    * This returns a static reference to the SALOME study manager. The
65    * study manager can be used to retrieve a study or to get
66    * informations about a study.
67    */
68   SALOMEDS::StudyManager_ptr getStudyManager() {
69     static SALOMEDS::StudyManager_ptr aStudyManager;
70     if(CORBA::is_nil(aStudyManager)){
71       SALOME_NamingService *aNamingService = getNamingService();
72       CORBA::Object_ptr anObject = aNamingService->Resolve("/myStudyManager");
73       aStudyManager = SALOMEDS::StudyManager::_narrow(anObject);
74     }
75     return aStudyManager;
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 returns the study with the specified id if it's defined in
122    * the SALOME study manager. Returns null otherwise.
123    * Please not that it is just a shortcut, and you may prefer use
124    * directly the study manager:
125    *    KERNEL::getStudyManager()->GetStudyByID(aStudyId)
126    */
127   SALOMEDS::Study_ptr getStudyById(int aStudyId) {
128     if ( aStudyId < 0 ) {
129       INFOS("ERR: trying to get a study with ID<0");
130       return SALOMEDS::Study::_nil();
131     }
132     return getStudyManager()->GetStudyByID(aStudyId);
133   }
134
135   int getStudyId(SALOMEDS::Study_ptr study) {
136     if( CORBA::is_nil(study) ) return -1;
137     return study->StudyId();
138   }
139
140
141   /**
142    * This function retrieve the CORBA object reference from the study
143    * object wrapping it.
144    */
145   CORBA::Object_ptr SObjectToObject(SALOMEDS::SObject_ptr theSObject) {
146
147     SALOMEDS::GenericAttribute_var anAttr;
148     CORBA::Object_var anObject;
149     if(CORBA::is_nil(theSObject))
150       return anObject;
151     try{
152       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
153         SALOMEDS::AttributeIOR_var anIOR  = SALOMEDS::AttributeIOR::_narrow(anAttr);
154         CORBA::String_var aValue = anIOR->Value();
155         CORBA::ORB_ptr anORB = getORB();
156         if(strcmp(aValue,"") != 0)
157           anObject = anORB->string_to_object(aValue);
158       }
159     }catch(...){
160       INFOS("SObjectToObject - Unknown exception was occured!!!");
161     }
162     return anObject._retn();
163   }
164
165   /*!
166    * This function provides a CORBA pointer to a servant from its IOR
167    * given as a string of characters.
168    */
169   CORBA::Object_ptr IORToObject(char * IOR) {
170     return getORB()->string_to_object(IOR);
171   }
172
173   //
174   // __GBO__ See the file ./src/SMESHGUI/SMESHGUI_Utils.h of SMESH_SRC
175   // for other helper functions
176   //
177
178   SALOME::SALOME_Exception createSalomeException(const char * text) {
179     SALOME::ExceptionStruct es;
180     es.type = SALOME::INTERNAL_ERROR;
181     es.text = CORBA::string_dup(text);
182     return SALOME::SALOME_Exception(es);
183   }
184
185 }