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 // 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()));
180 return Study._retn();
185 //============================================================================
187 * Purpose : Close a study.
188 * If the study hasn't been saved, ask the user to confirm the
189 * close action without saving
191 //============================================================================
192 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
194 SALOMEDS::Locker lock;
196 if(aStudy->_is_nil()) return;
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());
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
217 //============================================================================
219 * Purpose : Save a Study to it's persistent reference
221 //============================================================================
222 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
224 SALOMEDS::Locker lock;
226 if(aStudy->_is_nil()) {
227 MESSAGE("Save error: Study is null");
231 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
232 return _impl->Save(aStudyImpl, _factory, theMultiFile);
235 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
237 SALOMEDS::Locker lock;
239 if(aStudy->_is_nil()) {
240 MESSAGE("SaveASCII error: Study is null");
244 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
245 return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
248 //=============================================================================
249 /*! Function : SaveAs
250 * Purpose : Save a study to the persistent reference aUrl
252 //============================================================================
253 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
255 SALOMEDS::Locker lock;
257 if(aStudy->_is_nil()) {
258 MESSAGE("SaveASCII error: Study is null");
262 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
263 return _impl->SaveAs(string(aUrl), aStudyImpl, _factory, theMultiFile);
266 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
268 SALOMEDS::Locker lock;
270 if(aStudy->_is_nil()) {
271 MESSAGE("SaveASCII error: Study is null");
275 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
276 return _impl->SaveAsASCII(string(aUrl), aStudyImpl, _factory, theMultiFile);
279 //============================================================================
280 /*! Function : GetOpenStudies
281 * Purpose : Get name list of open studies in the session
283 //============================================================================
284 SALOMEDS::ListOfOpenStudies* SALOMEDS_StudyManager_i::GetOpenStudies()
286 SALOMEDS::Locker lock;
288 vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
289 int aLength = anOpened.size();
291 SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
292 _list_open_studies->length(aLength);
296 MESSAGE("No active study in this session");
300 for (unsigned int ind=0; ind < aLength; ind++)
302 _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
303 SCRUTE(_list_open_studies[ind]) ;
306 return _list_open_studies._retn();
309 //============================================================================
310 /*! Function : GetStudyByName
311 * Purpose : Get a study from its name
313 //============================================================================
314 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
316 SALOMEDS::Locker lock;
318 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(string(aStudyName));
322 MESSAGE(_impl->GetErrorCode().c_str());
323 return SALOMEDS::Study::_nil();
326 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
327 return aStudy_servant->_this();
330 //============================================================================
331 /*! Function : GetStudyByID
332 * Purpose : Get a study from its ID
334 //============================================================================
335 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
337 SALOMEDS::Locker lock;
339 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
343 MESSAGE(_impl->GetErrorCode().c_str());
344 return SALOMEDS::Study::_nil();
347 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
348 return aStudy_servant->_this();
352 //============================================================================
353 /*! Function : CanCopy
356 //============================================================================
357 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
359 SALOMEDS::Locker lock;
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());
366 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
367 bool ret = _impl->CanCopy(anObject, aDriver);
372 //============================================================================
376 //============================================================================
377 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
379 SALOMEDS::Locker lock;
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());
386 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
387 bool ret = _impl->Copy(anObject, aDriver);
392 //============================================================================
393 /*! Function : CanPaste
396 //============================================================================
397 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
399 SALOMEDS::Locker lock;
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());
406 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
407 bool ret = _impl->CanPaste(anObject, aDriver);
412 //============================================================================
416 //============================================================================
417 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
418 throw(SALOMEDS::StudyBuilder::LockProtection)
420 SALOMEDS::Locker lock;
422 Unexpect aCatch(LockProtection);
423 SALOMEDS::Study_var aStudy = theObject->GetStudy();
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;
431 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
432 aNewSO = _impl->Paste(anObject, aDriver);
436 throw SALOMEDS::StudyBuilder::LockProtection();
439 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
444 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
446 SALOMEDS_Driver_i* driver = NULL;
448 SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
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);
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();
466 CORBA::Long SALOMEDS_StudyManager_i::getPID()
469 return (CORBA::Long)_getpid();
471 return (CORBA::Long)getpid();
475 void SALOMEDS_StudyManager_i::ShutdownWithExit()
477 exit( EXIT_SUCCESS );
480 //===========================================================================
482 //===========================================================================
483 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
486 long pid = (long)_getpid();
488 long pid = (long)getpid();
490 isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
491 return reinterpret_cast<CORBA::LongLong>(_impl);
494 //===========================================================================
497 PortableServer::ServantBase_var
498 GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
500 if(CORBA::is_nil(theObject))
503 return thePOA->reference_to_servant(theObject);
511 //===========================================================================