1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SALOMEDS_StudyManager_i.cxx
23 // Author : Sergey RUIN
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"
34 #include "SALOMEDSImpl_Study.hxx"
35 #include "SALOMEDSImpl_SObject.hxx"
36 #include "SALOMEDSImpl_SComponent.hxx"
37 #include "SALOMEDSImpl_AttributeIOR.hxx"
39 #include "Utils_CorbaException.hxx"
40 #include "Utils_ExceptHandlers.hxx"
41 #include "Basics_Utils.hxx"
42 #include "SALOME_GenericObj_i.hh"
51 #include <sys/types.h>
57 UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
58 UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
60 static SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
62 static std::map<int, PortableServer::POA_ptr> _mapOfPOA;
64 //============================================================================
65 /*! Function : SALOMEDS_StudyManager_i
66 * Purpose : SALOMEDS_StudyManager_i constructor
68 //============================================================================
69 SALOMEDS_StudyManager_i::SALOMEDS_StudyManager_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr thePOA)
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);
81 //============================================================================
82 /*! Function : ~SALOMEDS_StudyManager_i
83 * Purpose : SALOMEDS_StudyManager_i destructor
85 //============================================================================
86 SALOMEDS_StudyManager_i::~SALOMEDS_StudyManager_i()
88 // Destroy directory to register open studies
89 _name_service->Destroy_Directory("/Study");
95 //============================================================================
96 /*! Function : register_name
97 * Purpose : Register the study Manager in the naming service under the
100 //============================================================================
101 void SALOMEDS_StudyManager_i::register_name(const char * name)
103 SALOMEDS::StudyManager_var aManager(_this());
104 _name_service->Register(aManager.in(), name);
108 //============================================================================
109 /*! Function : NewStudy
110 * Purpose : Create a New Study of name study_name
112 //============================================================================
113 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
115 SALOMEDS::Locker lock;
117 SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
119 MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
120 return SALOMEDS::Study::_nil();
123 MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
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();
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" )
134 _name_service->Register(Study, study_name);
136 // Assign the value of the IOR in the study->root
137 CORBA::String_var IORStudy = _orb->object_to_string(Study);
139 aStudyImpl->SetTransientReference((char*)IORStudy.in());
141 _mapOfPOA[Study->StudyId()] = _poa;
143 return Study._retn();
146 //============================================================================
148 * Purpose : Open a Study from it's persistent reference
150 //============================================================================
151 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::Open(const char* aUrl)
152 throw(SALOME::SALOME_Exception)
154 SALOMEDS::Locker lock;
156 Unexpect aCatch(SalomeException);
157 MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
159 SALOMEDSImpl_Study* aStudyImpl = _impl->Open(string(aUrl));
162 THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
164 MESSAGE("Open : Creating the CORBA servant holding it... ");
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();
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());
175 _mapOfPOA[Study->StudyId()] = _poa;
177 // Register study in the naming service
178 // Path to acces the study
179 if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
180 else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().c_str()));
182 return Study._retn();
187 //============================================================================
189 * Purpose : Close a study.
190 * If the study hasn't been saved, ask the user to confirm the
191 * close action without saving
193 //============================================================================
194 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
196 SALOMEDS::Locker lock;
198 if(aStudy->_is_nil()) return;
200 // Destroy study name in the naming service
201 if(_name_service->Change_Directory("/Study")){
202 CORBA::String_var aString(aStudy->Name());
203 _name_service->Destroy_Name(aString.in());
210 //remove study servant
211 PortableServer::POA_ptr poa=GetPOA(aStudy);
212 PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy);
213 PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant);
214 poa->deactivate_object(anObjectId.in());
215 aservant->_remove_ref(); // decrement for the call to reference_to_servant
216 aservant->_remove_ref(); // to delete the object
219 //============================================================================
221 * Purpose : Save a Study to it's persistent reference
223 //============================================================================
224 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
226 SALOMEDS::Locker lock;
228 if(aStudy->_is_nil()) {
229 MESSAGE("Save error: Study is null");
233 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
234 return _impl->Save(aStudyImpl, _factory, theMultiFile);
237 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
239 SALOMEDS::Locker lock;
241 if(aStudy->_is_nil()) {
242 MESSAGE("SaveASCII error: Study is null");
246 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
247 return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
250 //=============================================================================
251 /*! Function : SaveAs
252 * Purpose : Save a study to the persistent reference aUrl
254 //============================================================================
255 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
257 SALOMEDS::Locker lock;
259 if(aStudy->_is_nil()) {
260 MESSAGE("SaveASCII error: Study is null");
264 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
265 return _impl->SaveAs(string(aUrl), aStudyImpl, _factory, theMultiFile);
268 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
270 SALOMEDS::Locker lock;
272 if(aStudy->_is_nil()) {
273 MESSAGE("SaveASCII error: Study is null");
277 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
278 return _impl->SaveAsASCII(string(aUrl), aStudyImpl, _factory, theMultiFile);
281 //============================================================================
282 /*! Function : GetOpenStudies
283 * Purpose : Get name list of open studies in the session
285 //============================================================================
286 SALOMEDS::ListOfOpenStudies* SALOMEDS_StudyManager_i::GetOpenStudies()
288 SALOMEDS::Locker lock;
290 vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
291 int aLength = anOpened.size();
293 SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
294 _list_open_studies->length(aLength);
298 MESSAGE("No active study in this session");
302 for (unsigned int ind=0; ind < aLength; ind++)
304 _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
305 SCRUTE(_list_open_studies[ind]) ;
308 return _list_open_studies._retn();
311 //============================================================================
312 /*! Function : GetStudyByName
313 * Purpose : Get a study from its name
315 //============================================================================
316 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
318 SALOMEDS::Locker lock;
320 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(string(aStudyName));
324 MESSAGE(_impl->GetErrorCode().c_str());
325 return SALOMEDS::Study::_nil();
328 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
329 return aStudy_servant->_this();
332 //============================================================================
333 /*! Function : GetStudyByID
334 * Purpose : Get a study from its ID
336 //============================================================================
337 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
339 SALOMEDS::Locker lock;
341 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
345 MESSAGE(_impl->GetErrorCode().c_str());
346 return SALOMEDS::Study::_nil();
349 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
350 return aStudy_servant->_this();
354 //============================================================================
355 /*! Function : CanCopy
358 //============================================================================
359 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
361 SALOMEDS::Locker lock;
363 SALOMEDS::Study_var aStudy = theObject->GetStudy();
364 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
365 CORBA::String_var anID = theObject->GetID();
366 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
368 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
369 bool ret = _impl->CanCopy(anObject, aDriver);
374 //============================================================================
378 //============================================================================
379 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
381 SALOMEDS::Locker lock;
383 SALOMEDS::Study_var aStudy = theObject->GetStudy();
384 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
385 CORBA::String_var anID = theObject->GetID();
386 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
388 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
389 bool ret = _impl->Copy(anObject, aDriver);
394 //============================================================================
395 /*! Function : CanPaste
398 //============================================================================
399 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
401 SALOMEDS::Locker lock;
403 SALOMEDS::Study_var aStudy = theObject->GetStudy();
404 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
405 CORBA::String_var anID = theObject->GetID();
406 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
408 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
409 bool ret = _impl->CanPaste(anObject, aDriver);
414 //============================================================================
418 //============================================================================
419 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
420 throw(SALOMEDS::StudyBuilder::LockProtection)
422 SALOMEDS::Locker lock;
424 Unexpect aCatch(LockProtection);
425 SALOMEDS::Study_var aStudy = theObject->GetStudy();
427 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
428 CORBA::String_var anID = theObject->GetID();
429 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
430 SALOMEDSImpl_SObject aNewSO;
433 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
434 aNewSO = _impl->Paste(anObject, aDriver);
438 throw SALOMEDS::StudyBuilder::LockProtection();
441 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
446 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
448 SALOMEDS_Driver_i* driver = NULL;
450 SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
452 string IOREngine = aSCO.GetIOR();
453 if(!IOREngine.empty()) {
454 CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
455 SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
456 driver = new SALOMEDS_Driver_i(Engine, orb);
463 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
464 if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
465 return PortableServer::POA::_nil();
468 CORBA::Long SALOMEDS_StudyManager_i::getPID()
471 return (CORBA::Long)_getpid();
473 return (CORBA::Long)getpid();
477 void SALOMEDS_StudyManager_i::ShutdownWithExit()
479 exit( EXIT_SUCCESS );
482 //===========================================================================
484 //===========================================================================
485 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
488 long pid = (long)_getpid();
490 long pid = (long)getpid();
492 isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
493 return reinterpret_cast<CORBA::LongLong>(_impl);
496 //===========================================================================
499 PortableServer::ServantBase_var
500 GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
502 if(CORBA::is_nil(theObject))
505 return thePOA->reference_to_servant(theObject);
513 //===========================================================================