Salome HOME
CCAR: remove more memory leaks in SALOMEDS
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyManager_i.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SALOMEDS_StudyManager_i.cxx
23 //  Author : Sergey RUIN
24 //  Module : SALOME
25 //
26 #include "utilities.h"
27 #include "SALOME_LifeCycleCORBA.hxx"
28 #include "SALOMEDS_StudyManager_i.hxx"
29 #include "SALOMEDS_Study_i.hxx"
30 #include "SALOMEDS_SComponent_i.hxx"
31 #include "SALOMEDS_Driver_i.hxx"
32 #include "SALOMEDS.hxx"
33
34 #include "SALOMEDSImpl_Study.hxx"
35 #include "SALOMEDSImpl_SObject.hxx"
36 #include "SALOMEDSImpl_SComponent.hxx"
37 #include "SALOMEDSImpl_AttributeIOR.hxx"
38
39 #include "Utils_CorbaException.hxx"
40 #include "Utils_ExceptHandlers.hxx"
41 #include "Basics_Utils.hxx"
42 #include "SALOME_GenericObj_i.hh"
43
44 #include <sstream>
45 #include <vector>
46 #include <map>
47
48 #ifdef WIN32
49 #include <process.h>
50 #else
51 #include <sys/types.h>
52 #include <unistd.h>
53 #endif
54
55 using namespace std;
56
57 UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
58 UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
59
60 static SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
61
62 static std::map<int, PortableServer::POA_ptr> _mapOfPOA;
63
64 //============================================================================
65 /*! Function : SALOMEDS_StudyManager_i
66  *  Purpose  : SALOMEDS_StudyManager_i constructor
67  */
68 //============================================================================
69 SALOMEDS_StudyManager_i::SALOMEDS_StudyManager_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr thePOA)
70 {
71   _orb = CORBA::ORB::_duplicate(orb);
72   _poa = PortableServer::POA::_duplicate(thePOA);
73   _name_service = new SALOME_NamingService(_orb);
74   // Study directory creation in the naming service : to register all
75   // open studies in the session
76   _name_service->Create_Directory("/Study");
77   _impl = new SALOMEDSImpl_StudyManager;
78   _factory = new SALOMEDS_DriverFactory_i(_orb);
79 }
80
81 //============================================================================
82 /*! Function : ~SALOMEDS_StudyManager_i
83  *  Purpose  : SALOMEDS_StudyManager_i destructor
84  */
85 //============================================================================
86 SALOMEDS_StudyManager_i::~SALOMEDS_StudyManager_i()
87 {
88   // Destroy directory to register open studies
89   _name_service->Destroy_Directory("/Study");
90   delete _name_service;
91   delete _factory;
92   delete _impl;
93 }
94
95 //============================================================================
96 /*! Function : register_name
97  *  Purpose  : Register the study Manager in the naming service under the
98  *             context name
99  */
100 //============================================================================
101 void SALOMEDS_StudyManager_i::register_name(const char * name)
102 {
103   SALOMEDS::StudyManager_var aManager(_this());
104   _name_service->Register(aManager.in(), name);
105 }
106
107
108 //============================================================================
109 /*! Function : NewStudy
110  *  Purpose  : Create a New Study of name study_name
111  */
112 //============================================================================
113 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
114 {
115   SALOMEDS::Locker lock;
116
117   SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
118   if(!aStudyImpl) {
119     MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
120     return SALOMEDS::Study::_nil();
121   }
122
123   MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
124
125   SALOMEDS_Study_i *Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
126   PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa registered in _mapOfPOA
127   SALOMEDS::Study_var Study = Study_servant->_this();
128
129   // Register study in the naming service
130   // Path to acces the study
131   if(!_name_service->Change_Directory("/Study"))
132       MESSAGE( "Unable to access the study directory" )
133   else
134       _name_service->Register(Study, study_name);
135
136   // Assign the value of the IOR in the study->root
137   CORBA::String_var IORStudy = _orb->object_to_string(Study);
138
139   aStudyImpl->SetTransientReference((char*)IORStudy.in());
140
141   _mapOfPOA[Study->StudyId()] = _poa;
142
143   return Study._retn();
144 }
145
146 //============================================================================
147 /*! Function : Open
148  *  Purpose  : Open a Study from it's persistent reference
149  */
150 //============================================================================
151 SALOMEDS::Study_ptr  SALOMEDS_StudyManager_i::Open(const char* aUrl)
152      throw(SALOME::SALOME_Exception)
153 {
154   SALOMEDS::Locker lock;
155
156   Unexpect aCatch(SalomeException);
157   MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
158
159   SALOMEDSImpl_Study* aStudyImpl = _impl->Open(string(aUrl));
160
161   if ( !aStudyImpl )
162     THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
163
164   MESSAGE("Open : Creating the CORBA servant holding it... ");
165
166   // Temporary aStudyUrl in place of study name
167   SALOMEDS_Study_i * Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
168   PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa register in _mapOfPOA
169   SALOMEDS::Study_var Study = Study_servant->_this();
170
171   // Assign the value of the IOR in the study->root
172   CORBA::String_var IORStudy = _orb->object_to_string(Study);
173   aStudyImpl->SetTransientReference((char*)IORStudy.in());
174
175   // Register study in the naming service
176   // Path to acces the study
177   if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
178   else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().c_str()));
179
180   return Study._retn();
181 }
182
183
184
185 //============================================================================
186 /*! Function : Close
187  *  Purpose  : Close a study.
188  *             If the study hasn't been saved, ask the user to confirm the
189  *             close action without saving
190  */
191 //============================================================================
192 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
193 {
194   SALOMEDS::Locker lock;
195
196   if(aStudy->_is_nil()) return;
197
198   // Destroy study name in the naming service
199   if(_name_service->Change_Directory("/Study")){
200     CORBA::String_var aString(aStudy->Name());
201     _name_service->Destroy_Name(aString.in());
202   }
203
204   SALOMEDS::unlock();
205   aStudy->Close();
206   SALOMEDS::lock();
207
208   //remove study servant
209   PortableServer::POA_ptr poa=GetPOA(aStudy);
210   PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy);
211   PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant);
212   poa->deactivate_object(anObjectId.in());
213   aservant->_remove_ref(); // decrement for the call to reference_to_servant
214   aservant->_remove_ref(); // to delete the object
215 }
216
217 //============================================================================
218 /*! Function : Save
219  *  Purpose  : Save a Study to it's persistent reference
220  */
221 //============================================================================
222 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
223 {
224   SALOMEDS::Locker lock;
225
226   if(aStudy->_is_nil()) {
227     MESSAGE("Save error: Study is null");
228     return false;
229   }
230
231   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
232   return _impl->Save(aStudyImpl, _factory, theMultiFile);
233 }
234
235 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
236 {
237   SALOMEDS::Locker lock;
238
239   if(aStudy->_is_nil()) {
240     MESSAGE("SaveASCII error: Study is null");
241     return false;
242   }
243
244   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
245   return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
246 }
247
248 //=============================================================================
249 /*! Function : SaveAs
250  *  Purpose  : Save a study to the persistent reference aUrl
251  */
252 //============================================================================
253 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
254 {
255   SALOMEDS::Locker lock;
256
257   if(aStudy->_is_nil()) {
258     MESSAGE("SaveASCII error: Study is null");
259     return false;
260   }
261
262   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
263   return _impl->SaveAs(string(aUrl), aStudyImpl, _factory, theMultiFile);
264 }
265
266 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
267 {
268   SALOMEDS::Locker lock;
269
270   if(aStudy->_is_nil()) {
271     MESSAGE("SaveASCII error: Study is null");
272     return false;
273   }
274
275   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
276   return _impl->SaveAsASCII(string(aUrl), aStudyImpl, _factory, theMultiFile);
277 }
278
279 //============================================================================
280 /*! Function : GetOpenStudies
281  *  Purpose  : Get name list of open studies in the session
282  */
283 //============================================================================
284 SALOMEDS::ListOfOpenStudies*  SALOMEDS_StudyManager_i::GetOpenStudies()
285 {
286   SALOMEDS::Locker lock;
287
288   vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
289   int aLength = anOpened.size();
290
291   SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
292   _list_open_studies->length(aLength);
293
294   if(!aLength)
295     {
296       MESSAGE("No active study in this session");
297     }
298   else
299     {
300       for (unsigned int ind=0; ind < aLength; ind++)
301         {
302           _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
303           SCRUTE(_list_open_studies[ind]) ;
304         }
305     }
306   return _list_open_studies._retn();
307 }
308
309 //============================================================================
310 /*! Function : GetStudyByName
311  *  Purpose  : Get a study from its name
312  */
313 //============================================================================
314 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
315 {
316   SALOMEDS::Locker lock;
317
318   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(string(aStudyName));
319
320   if (!aStudyImpl)
321   {
322     MESSAGE(_impl->GetErrorCode().c_str());
323     return SALOMEDS::Study::_nil();
324   }
325
326   SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
327   return aStudy_servant->_this();
328 }
329
330 //============================================================================
331 /*! Function : GetStudyByID
332  *  Purpose  : Get a study from its ID
333  */
334 //============================================================================
335 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
336 {
337   SALOMEDS::Locker lock;
338
339   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
340
341   if (!aStudyImpl)
342   {
343     MESSAGE(_impl->GetErrorCode().c_str());
344     return SALOMEDS::Study::_nil();
345   }
346
347   SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
348   return aStudy_servant->_this();
349 }
350
351
352 //============================================================================
353 /*! Function : CanCopy
354  *  Purpose  :
355  */
356 //============================================================================
357 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
358 {
359   SALOMEDS::Locker lock;
360
361   SALOMEDS::Study_var aStudy = theObject->GetStudy();
362   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
363   CORBA::String_var anID = theObject->GetID();
364   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
365
366   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
367   bool ret = _impl->CanCopy(anObject, aDriver);
368   delete aDriver;
369   return ret;
370 }
371
372 //============================================================================
373 /*! Function : Copy
374  *  Purpose  :
375  */
376 //============================================================================
377 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
378 {
379   SALOMEDS::Locker lock;
380
381   SALOMEDS::Study_var aStudy = theObject->GetStudy();
382   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
383   CORBA::String_var anID = theObject->GetID();
384   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
385
386   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
387   bool ret = _impl->Copy(anObject, aDriver);
388   delete aDriver;
389   return ret;
390 }
391
392 //============================================================================
393 /*! Function : CanPaste
394  *  Purpose  :
395  */
396 //============================================================================
397 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
398 {
399   SALOMEDS::Locker lock;
400
401   SALOMEDS::Study_var aStudy = theObject->GetStudy();
402   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
403   CORBA::String_var anID = theObject->GetID();
404   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
405
406   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
407   bool ret = _impl->CanPaste(anObject, aDriver);
408   delete aDriver;
409   return ret;
410 }
411
412 //============================================================================
413 /*! Function : Paste
414  *  Purpose  :
415  */
416 //============================================================================
417 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
418      throw(SALOMEDS::StudyBuilder::LockProtection)
419 {
420   SALOMEDS::Locker lock;
421
422   Unexpect aCatch(LockProtection);
423   SALOMEDS::Study_var aStudy = theObject->GetStudy();
424
425   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
426   CORBA::String_var anID = theObject->GetID();
427   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
428   SALOMEDSImpl_SObject aNewSO;
429
430   try {
431     SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
432     aNewSO =  _impl->Paste(anObject, aDriver);
433     delete aDriver;
434   }
435   catch (...) {
436     throw SALOMEDS::StudyBuilder::LockProtection();
437   }
438
439   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
440   return so._retn();
441 }
442
443
444 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
445 {
446   SALOMEDS_Driver_i* driver = NULL;
447
448   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
449   if(!aSCO.IsNull()) {
450     string IOREngine = aSCO.GetIOR();
451     if(!IOREngine.empty()) {
452       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
453       SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
454       driver = new SALOMEDS_Driver_i(Engine, orb);
455     }
456   }
457
458   return driver;
459 }
460
461 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
462   if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
463   return PortableServer::POA::_nil();
464 }
465
466 CORBA::Long SALOMEDS_StudyManager_i::getPID()
467
468 #ifdef WIN32
469   return (CORBA::Long)_getpid();
470 #else
471   return (CORBA::Long)getpid();
472 #endif
473 }
474
475 void SALOMEDS_StudyManager_i::ShutdownWithExit()
476 {
477   exit( EXIT_SUCCESS );
478 }
479
480 //===========================================================================
481 //   PRIVATE FUNCTIONS
482 //===========================================================================
483 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
484 {
485 #ifdef WIN32
486   long pid = (long)_getpid();
487 #else
488   long pid = (long)getpid();
489 #endif
490   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
491   return reinterpret_cast<CORBA::LongLong>(_impl);
492 }
493
494 //===========================================================================
495 namespace SALOMEDS
496 {
497   PortableServer::ServantBase_var
498   GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
499   {
500     if(CORBA::is_nil(theObject))
501       return NULL;
502     try{
503       return thePOA->reference_to_servant(theObject);
504     }catch(...){
505       return NULL;
506     }
507   }
508
509 }
510
511 //===========================================================================