1 // Copyright (C) 2007-2015 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)
115 throw(SALOME::SALOME_Exception)
117 SALOMEDS::Locker lock;
119 #ifndef ALLOW_MULTI_STUDIES
120 std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
121 int aLength = anOpened.size();
125 MESSAGE("There is already an active study in this session. Launch a new session, or close the study");
126 THROW_SALOME_CORBA_EXCEPTION("Problem on New Study.\nThere is already an active study in this session.\nLaunch a new session, or close the study", SALOME::BAD_PARAM)
128 #endif // !ALLOW_MULTI_STUDIES
130 SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
132 MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
133 return SALOMEDS::Study::_nil();
136 MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
138 SALOMEDS_Study_i *Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
139 PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa registered in _mapOfPOA
140 SALOMEDS::Study_var Study = Study_servant->_this();
142 // Register study in the naming service
143 // Path to acces the study
144 if(!_name_service->Change_Directory("/Study"))
145 MESSAGE( "Unable to access the study directory" )
147 _name_service->Register(Study, study_name);
149 // Assign the value of the IOR in the study->root
150 CORBA::String_var IORStudy = _orb->object_to_string(Study);
152 aStudyImpl->SetTransientReference((char*)IORStudy.in());
154 _mapOfPOA[Study->StudyId()] = _poa;
156 return Study._retn();
159 //============================================================================
161 * Purpose : Open a Study from it's persistent reference
163 //============================================================================
164 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::Open(const char* aUrl)
165 throw(SALOME::SALOME_Exception)
167 SALOMEDS::Locker lock;
169 Unexpect aCatch(SalomeException);
170 MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
172 #ifndef ALLOW_MULTI_STUDIES
173 std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
174 int aLength = anOpened.size();
178 MESSAGE("There is already an active study in this session. Launch a new session, or close the study.");
179 THROW_SALOME_CORBA_EXCEPTION("Problem on Open Study.\nThere is already an active study in this session.\nLaunch a new session, or close the study.", SALOME::BAD_PARAM)
181 #endif // ;ALLOW_MULTI_STUDIES
183 SALOMEDSImpl_Study* aStudyImpl = _impl->Open(std::string(aUrl));
186 THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
188 MESSAGE("Open : Creating the CORBA servant holding it... ");
190 // Temporary aStudyUrl in place of study name
191 SALOMEDS_Study_i * Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
192 PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa register in _mapOfPOA
193 SALOMEDS::Study_var Study = Study_servant->_this();
195 // Assign the value of the IOR in the study->root
196 CORBA::String_var IORStudy = _orb->object_to_string(Study);
197 aStudyImpl->SetTransientReference((char*)IORStudy.in());
199 _mapOfPOA[Study->StudyId()] = _poa;
201 // Register study in the naming service
202 // Path to acces the study
203 if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
204 else _name_service->Register(Study, aStudyImpl->Name().c_str());
206 return Study._retn();
211 //============================================================================
213 * Purpose : Close a study.
214 * If the study hasn't been saved, ask the user to confirm the
215 * close action without saving
217 //============================================================================
218 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
220 SALOMEDS::Locker lock;
222 if(aStudy->_is_nil()) return;
224 // Destroy study name in the naming service
225 if(_name_service->Change_Directory("/Study")){
226 CORBA::String_var aString(aStudy->Name());
227 _name_service->Destroy_Name(aString.in());
234 //remove study servant
235 PortableServer::POA_ptr poa=GetPOA(aStudy);
236 PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy);
237 PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant);
238 poa->deactivate_object(anObjectId.in());
239 aservant->_remove_ref(); // decrement for the call to reference_to_servant
240 aservant->_remove_ref(); // to delete the object
243 //============================================================================
245 * Purpose : Save a Study to it's persistent reference
247 //============================================================================
248 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
250 SALOMEDS::Locker lock;
252 if(aStudy->_is_nil()) {
253 MESSAGE("Save error: Study is null");
257 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
258 return _impl->Save(aStudyImpl, _factory, theMultiFile);
261 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
263 SALOMEDS::Locker lock;
265 if(aStudy->_is_nil()) {
266 MESSAGE("SaveASCII error: Study is null");
270 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
271 return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
274 //=============================================================================
275 /*! Function : SaveAs
276 * Purpose : Save a study to the persistent reference aUrl
278 //============================================================================
279 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
281 SALOMEDS::Locker lock;
283 if(aStudy->_is_nil()) {
284 MESSAGE("SaveASCII error: Study is null");
288 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
289 return _impl->SaveAs(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
292 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
294 SALOMEDS::Locker lock;
296 if(aStudy->_is_nil()) {
297 MESSAGE("SaveASCII error: Study is null");
301 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
302 return _impl->SaveAsASCII(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
305 //============================================================================
306 /*! Function : GetOpenStudies
307 * Purpose : Get name list of open studies in the session
309 //============================================================================
310 SALOMEDS::ListOfOpenStudies* SALOMEDS_StudyManager_i::GetOpenStudies()
312 SALOMEDS::Locker lock;
314 std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
315 int aLength = anOpened.size();
317 SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
318 _list_open_studies->length(aLength);
322 MESSAGE("No active study in this session");
326 for (unsigned int ind=0; ind < aLength; ind++)
328 _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
329 SCRUTE(_list_open_studies[ind]) ;
332 return _list_open_studies._retn();
335 //============================================================================
336 /*! Function : GetStudyByName
337 * Purpose : Get a study from its name
339 //============================================================================
340 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
342 SALOMEDS::Locker lock;
344 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(std::string(aStudyName));
348 MESSAGE(_impl->GetErrorCode().c_str());
349 return SALOMEDS::Study::_nil();
352 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
353 return aStudy_servant->_this();
356 //============================================================================
357 /*! Function : GetStudyByID
358 * Purpose : Get a study from its ID
360 //============================================================================
361 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
363 SALOMEDS::Locker lock;
365 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
369 MESSAGE(_impl->GetErrorCode().c_str());
370 return SALOMEDS::Study::_nil();
373 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
374 return aStudy_servant->_this();
378 //============================================================================
379 /*! Function : CanCopy
382 //============================================================================
383 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
385 SALOMEDS::Locker lock;
387 SALOMEDS::Study_var aStudy = theObject->GetStudy();
388 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
389 CORBA::String_var anID = theObject->GetID();
390 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
392 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
393 bool ret = _impl->CanCopy(anObject, aDriver);
398 //============================================================================
402 //============================================================================
403 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
405 SALOMEDS::Locker lock;
407 SALOMEDS::Study_var aStudy = theObject->GetStudy();
408 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
409 CORBA::String_var anID = theObject->GetID();
410 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
412 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
413 bool ret = _impl->Copy(anObject, aDriver);
418 //============================================================================
419 /*! Function : CanPaste
422 //============================================================================
423 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
425 SALOMEDS::Locker lock;
427 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());
432 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
433 bool ret = _impl->CanPaste(anObject, aDriver);
438 //============================================================================
442 //============================================================================
443 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
444 throw(SALOMEDS::StudyBuilder::LockProtection)
446 SALOMEDS::Locker lock;
448 Unexpect aCatch(LockProtection);
449 SALOMEDS::Study_var aStudy = theObject->GetStudy();
451 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
452 CORBA::String_var anID = theObject->GetID();
453 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
454 SALOMEDSImpl_SObject aNewSO;
457 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
458 aNewSO = _impl->Paste(anObject, aDriver);
462 throw SALOMEDS::StudyBuilder::LockProtection();
465 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
470 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
472 SALOMEDS_Driver_i* driver = NULL;
474 SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
476 std::string IOREngine = aSCO.GetIOR();
477 if(!IOREngine.empty()) {
478 CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
479 Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
480 driver = new SALOMEDS_Driver_i(Engine, orb);
487 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
488 if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
489 return PortableServer::POA::_nil();
492 CORBA::Long SALOMEDS_StudyManager_i::getPID()
495 return (CORBA::Long)_getpid();
497 return (CORBA::Long)getpid();
501 void SALOMEDS_StudyManager_i::ShutdownWithExit()
503 exit( EXIT_SUCCESS );
506 //============================================================================
507 /*! Function : GetSimanStudy
508 * Purpose : Retruns the created or cashed SimanStudy
510 //============================================================================
511 SALOMEDS::SimanStudy_ptr SALOMEDS_StudyManager_i::GetSimanStudy()
513 SALOMEDS::Locker lock;
515 SALOMEDS_SimanStudy_i* aSiman_servant = SALOMEDS_SimanStudy_i::GetSimanServant(_orb);
516 return aSiman_servant->_this();
519 //===========================================================================
521 //===========================================================================
522 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
525 long pid = (long)_getpid();
527 long pid = (long)getpid();
529 isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
530 return reinterpret_cast<CORBA::LongLong>(_impl);
533 //===========================================================================
536 PortableServer::ServantBase_var
537 GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
539 if(CORBA::is_nil(theObject))
542 return thePOA->reference_to_servant(theObject);
550 //===========================================================================