1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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
23 // File : SALOMEDS_StudyManager_i.cxx
24 // Author : Sergey RUIN
27 #include "utilities.h"
28 #include "SALOME_LifeCycleCORBA.hxx"
29 #include "SALOMEDS_StudyManager_i.hxx"
30 #include "SALOMEDS_Study_i.hxx"
31 #include "SALOMEDS_SComponent_i.hxx"
32 #include "SALOMEDS_Driver_i.hxx"
33 #include "SALOMEDS_SimanStudy_i.hxx"
34 #include "SALOMEDS.hxx"
36 #include "SALOMEDSImpl_Study.hxx"
37 #include "SALOMEDSImpl_SObject.hxx"
38 #include "SALOMEDSImpl_SComponent.hxx"
39 #include "SALOMEDSImpl_AttributeIOR.hxx"
41 #include "Utils_CorbaException.hxx"
42 #include "Utils_ExceptHandlers.hxx"
43 #include "Basics_Utils.hxx"
44 #include "SALOME_GenericObj_i.hh"
53 #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");
93 delete SALOMEDS_SimanStudy_i::GetSimanServant(_orb);
96 //============================================================================
97 /*! Function : register_name
98 * Purpose : Register the study Manager in the naming service under the
101 //============================================================================
102 void SALOMEDS_StudyManager_i::register_name(const char * name)
104 SALOMEDS::StudyManager_var aManager(_this());
105 _name_service->Register(aManager.in(), name);
109 //============================================================================
110 /*! Function : NewStudy
111 * Purpose : Create a New Study of name study_name
113 //============================================================================
114 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
116 SALOMEDS::Locker lock;
118 SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
120 MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
121 return SALOMEDS::Study::_nil();
124 MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
126 SALOMEDS_Study_i *Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
127 PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa registered in _mapOfPOA
128 SALOMEDS::Study_var Study = Study_servant->_this();
130 // Register study in the naming service
131 // Path to acces the study
132 if(!_name_service->Change_Directory("/Study"))
133 MESSAGE( "Unable to access the study directory" )
135 _name_service->Register(Study, study_name);
137 // Assign the value of the IOR in the study->root
138 CORBA::String_var IORStudy = _orb->object_to_string(Study);
140 aStudyImpl->SetTransientReference((char*)IORStudy.in());
142 _mapOfPOA[Study->StudyId()] = _poa;
144 return Study._retn();
147 //============================================================================
149 * Purpose : Open a Study from it's persistent reference
151 //============================================================================
152 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::Open(const char* aUrl)
153 throw(SALOME::SALOME_Exception)
155 SALOMEDS::Locker lock;
157 Unexpect aCatch(SalomeException);
158 MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
160 SALOMEDSImpl_Study* aStudyImpl = _impl->Open(std::string(aUrl));
163 THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
165 MESSAGE("Open : Creating the CORBA servant holding it... ");
167 // Temporary aStudyUrl in place of study name
168 SALOMEDS_Study_i * Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
169 PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa register in _mapOfPOA
170 SALOMEDS::Study_var Study = Study_servant->_this();
172 // Assign the value of the IOR in the study->root
173 CORBA::String_var IORStudy = _orb->object_to_string(Study);
174 aStudyImpl->SetTransientReference((char*)IORStudy.in());
176 _mapOfPOA[Study->StudyId()] = _poa;
178 // Register study in the naming service
179 // Path to acces the study
180 if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
181 else _name_service->Register(Study, aStudyImpl->Name().c_str());
183 return Study._retn();
188 //============================================================================
190 * Purpose : Close a study.
191 * If the study hasn't been saved, ask the user to confirm the
192 * close action without saving
194 //============================================================================
195 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
197 SALOMEDS::Locker lock;
199 if(aStudy->_is_nil()) return;
201 // Destroy study name in the naming service
202 if(_name_service->Change_Directory("/Study")){
203 CORBA::String_var aString(aStudy->Name());
204 _name_service->Destroy_Name(aString.in());
211 //remove study servant
212 PortableServer::POA_ptr poa=GetPOA(aStudy);
213 PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy);
214 PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant);
215 poa->deactivate_object(anObjectId.in());
216 aservant->_remove_ref(); // decrement for the call to reference_to_servant
217 aservant->_remove_ref(); // to delete the object
220 //============================================================================
222 * Purpose : Save a Study to it's persistent reference
224 //============================================================================
225 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
227 SALOMEDS::Locker lock;
229 if(aStudy->_is_nil()) {
230 MESSAGE("Save error: Study is null");
234 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
235 return _impl->Save(aStudyImpl, _factory, theMultiFile);
238 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
240 SALOMEDS::Locker lock;
242 if(aStudy->_is_nil()) {
243 MESSAGE("SaveASCII error: Study is null");
247 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
248 return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
251 //=============================================================================
252 /*! Function : SaveAs
253 * Purpose : Save a study to the persistent reference aUrl
255 //============================================================================
256 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
258 SALOMEDS::Locker lock;
260 if(aStudy->_is_nil()) {
261 MESSAGE("SaveASCII error: Study is null");
265 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
266 return _impl->SaveAs(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
269 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
271 SALOMEDS::Locker lock;
273 if(aStudy->_is_nil()) {
274 MESSAGE("SaveASCII error: Study is null");
278 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
279 return _impl->SaveAsASCII(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
282 //============================================================================
283 /*! Function : GetOpenStudies
284 * Purpose : Get name list of open studies in the session
286 //============================================================================
287 SALOMEDS::ListOfOpenStudies* SALOMEDS_StudyManager_i::GetOpenStudies()
289 SALOMEDS::Locker lock;
291 std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
292 int aLength = anOpened.size();
294 SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
295 _list_open_studies->length(aLength);
299 MESSAGE("No active study in this session");
303 for (unsigned int ind=0; ind < aLength; ind++)
305 _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
306 SCRUTE(_list_open_studies[ind]) ;
309 return _list_open_studies._retn();
312 //============================================================================
313 /*! Function : GetStudyByName
314 * Purpose : Get a study from its name
316 //============================================================================
317 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
319 SALOMEDS::Locker lock;
321 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(std::string(aStudyName));
325 MESSAGE(_impl->GetErrorCode().c_str());
326 return SALOMEDS::Study::_nil();
329 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
330 return aStudy_servant->_this();
333 //============================================================================
334 /*! Function : GetStudyByID
335 * Purpose : Get a study from its ID
337 //============================================================================
338 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
340 SALOMEDS::Locker lock;
342 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
346 MESSAGE(_impl->GetErrorCode().c_str());
347 return SALOMEDS::Study::_nil();
350 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
351 return aStudy_servant->_this();
355 //============================================================================
356 /*! Function : CanCopy
359 //============================================================================
360 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
362 SALOMEDS::Locker lock;
364 SALOMEDS::Study_var aStudy = theObject->GetStudy();
365 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
366 CORBA::String_var anID = theObject->GetID();
367 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
369 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
370 bool ret = _impl->CanCopy(anObject, aDriver);
375 //============================================================================
379 //============================================================================
380 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
382 SALOMEDS::Locker lock;
384 SALOMEDS::Study_var aStudy = theObject->GetStudy();
385 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
386 CORBA::String_var anID = theObject->GetID();
387 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
389 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
390 bool ret = _impl->Copy(anObject, aDriver);
395 //============================================================================
396 /*! Function : CanPaste
399 //============================================================================
400 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
402 SALOMEDS::Locker lock;
404 SALOMEDS::Study_var aStudy = theObject->GetStudy();
405 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
406 CORBA::String_var anID = theObject->GetID();
407 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
409 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
410 bool ret = _impl->CanPaste(anObject, aDriver);
415 //============================================================================
419 //============================================================================
420 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
421 throw(SALOMEDS::StudyBuilder::LockProtection)
423 SALOMEDS::Locker lock;
425 Unexpect aCatch(LockProtection);
426 SALOMEDS::Study_var aStudy = theObject->GetStudy();
428 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
429 CORBA::String_var anID = theObject->GetID();
430 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
431 SALOMEDSImpl_SObject aNewSO;
434 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
435 aNewSO = _impl->Paste(anObject, aDriver);
439 throw SALOMEDS::StudyBuilder::LockProtection();
442 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
447 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
449 SALOMEDS_Driver_i* driver = NULL;
451 SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
453 std::string IOREngine = aSCO.GetIOR();
454 if(!IOREngine.empty()) {
455 CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
456 Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
457 driver = new SALOMEDS_Driver_i(Engine, orb);
464 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
465 if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
466 return PortableServer::POA::_nil();
469 CORBA::Long SALOMEDS_StudyManager_i::getPID()
472 return (CORBA::Long)_getpid();
474 return (CORBA::Long)getpid();
478 void SALOMEDS_StudyManager_i::ShutdownWithExit()
480 exit( EXIT_SUCCESS );
483 //============================================================================
484 /*! Function : GetSimanStudy
485 * Purpose : Retruns the created or cashed SimanStudy
487 //============================================================================
488 SALOMEDS::SimanStudy_ptr SALOMEDS_StudyManager_i::GetSimanStudy()
490 SALOMEDS::Locker lock;
492 SALOMEDS_SimanStudy_i* aSiman_servant = SALOMEDS_SimanStudy_i::GetSimanServant(_orb);
493 return aSiman_servant->_this();
496 //===========================================================================
498 //===========================================================================
499 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
502 long pid = (long)_getpid();
504 long pid = (long)getpid();
506 isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
507 return reinterpret_cast<CORBA::LongLong>(_impl);
510 //===========================================================================
513 PortableServer::ServantBase_var
514 GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
516 if(CORBA::is_nil(theObject))
519 return thePOA->reference_to_servant(theObject);
527 //===========================================================================