1 // Copyright (C) 2007-2016 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.hxx"
35 #include "SALOMEDSImpl_Study.hxx"
36 #include "SALOMEDSImpl_SObject.hxx"
37 #include "SALOMEDSImpl_SComponent.hxx"
38 #include "SALOMEDSImpl_AttributeIOR.hxx"
40 #include "Utils_CorbaException.hxx"
41 #include "Utils_ExceptHandlers.hxx"
42 #include "Basics_Utils.hxx"
43 #include "SALOME_GenericObj_i.hh"
52 #include <sys/types.h>
56 UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
57 UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
59 static SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
61 static std::map<int, PortableServer::POA_ptr> _mapOfPOA;
63 //============================================================================
64 /*! Function : SALOMEDS_StudyManager_i
65 * Purpose : SALOMEDS_StudyManager_i constructor
67 //============================================================================
68 SALOMEDS_StudyManager_i::SALOMEDS_StudyManager_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr thePOA)
70 _orb = CORBA::ORB::_duplicate(orb);
71 _poa = PortableServer::POA::_duplicate(thePOA);
72 MESSAGE("thePOA, _poa="<<_poa);
73 if (_mapOfPOA.empty())
76 MESSAGE("_mapOfPOA[0] already contains: " << _mapOfPOA[0]);
77 _name_service = new SALOME_NamingService(_orb);
78 // Study directory creation in the naming service : to register all
79 // open studies in the session
80 _name_service->Create_Directory("/Study");
81 _impl = new SALOMEDSImpl_StudyManager;
82 _factory = new SALOMEDS_DriverFactory_i(_orb);
85 //============================================================================
86 /*! Function : ~SALOMEDS_StudyManager_i
87 * Purpose : SALOMEDS_StudyManager_i destructor
89 //============================================================================
90 SALOMEDS_StudyManager_i::~SALOMEDS_StudyManager_i()
92 // Destroy directory to register open studies
93 _name_service->Destroy_Directory("/Study");
99 //============================================================================
101 \brief Get default POA for the servant object.
103 This function is implicitly called from "_this()" function.
104 Default POA can be set via the constructor.
106 \return reference to the default POA for the servant
108 //============================================================================
109 PortableServer::POA_ptr SALOMEDS_StudyManager_i::_default_POA()
111 MESSAGE("SALOMEDS_StudyManager_i::_default_POA: " << _poa);
112 return PortableServer::POA::_duplicate(_poa);
115 //============================================================================
116 /*! Function : register_name
117 * Purpose : Register the study Manager in the naming service under the
120 //============================================================================
121 void SALOMEDS_StudyManager_i::register_name(const char * name)
123 SALOMEDS::StudyManager_var aManager(_this());
124 _name_service->Register(aManager.in(), name);
128 //============================================================================
129 /*! Function : NewStudy
130 * Purpose : Create a New Study of name study_name
132 //============================================================================
133 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
134 throw(SALOME::SALOME_Exception)
136 SALOMEDS::Locker lock;
138 #ifndef ALLOW_MULTI_STUDIES
139 std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
140 int aLength = anOpened.size();
144 MESSAGE("There is already an active study in this session. Launch a new session, or close the study");
145 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)
147 #endif // !ALLOW_MULTI_STUDIES
149 SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
151 MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
152 return SALOMEDS::Study::_nil();
155 MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
157 SALOMEDS_Study_i *Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
158 PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa registered in _mapOfPOA
159 SALOMEDS::Study_var Study = Study_servant->_this();
161 // Register study in the naming service
162 // Path to access the study
163 if(!_name_service->Change_Directory("/Study"))
164 MESSAGE( "Unable to access the study directory" )
166 _name_service->Register(Study, study_name);
168 // Assign the value of the IOR in the study->root
169 CORBA::String_var IORStudy = _orb->object_to_string(Study);
171 aStudyImpl->SetTransientReference((char*)IORStudy.in());
173 _mapOfPOA[Study->StudyId()] = _poa;
175 return Study._retn();
178 //============================================================================
180 * Purpose : Open a Study from it's persistent reference
182 //============================================================================
183 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::Open(const char* aUrl)
184 throw(SALOME::SALOME_Exception)
186 SALOMEDS::Locker lock;
188 Unexpect aCatch(SalomeException);
189 MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
191 #ifndef ALLOW_MULTI_STUDIES
192 std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
193 int aLength = anOpened.size();
197 MESSAGE("There is already an active study in this session. Launch a new session, or close the study.");
198 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)
200 #endif // ;ALLOW_MULTI_STUDIES
202 SALOMEDSImpl_Study* aStudyImpl = _impl->Open(std::string(aUrl));
205 THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
207 MESSAGE("Open : Creating the CORBA servant holding it... ");
209 // Temporary aStudyUrl in place of study name
210 SALOMEDS_Study_i * Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
211 PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa register in _mapOfPOA
212 SALOMEDS::Study_var Study = Study_servant->_this();
214 // Assign the value of the IOR in the study->root
215 CORBA::String_var IORStudy = _orb->object_to_string(Study);
216 aStudyImpl->SetTransientReference((char*)IORStudy.in());
218 _mapOfPOA[Study->StudyId()] = _poa;
220 // Register study in the naming service
221 // Path to access the study
222 if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
223 else _name_service->Register(Study, aStudyImpl->Name().c_str());
225 return Study._retn();
230 //============================================================================
232 * Purpose : Close a study.
233 * If the study hasn't been saved, ask the user to confirm the
234 * close action without saving
236 //============================================================================
237 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
239 SALOMEDS::Locker lock;
241 if(aStudy->_is_nil()) return;
243 // Destroy study name in the naming service
244 if(_name_service->Change_Directory("/Study")){
245 CORBA::String_var aString(aStudy->Name());
246 _name_service->Destroy_Name(aString.in());
253 //remove study servant
254 PortableServer::POA_ptr poa=GetPOA(aStudy);
255 PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy);
256 PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant);
257 poa->deactivate_object(anObjectId.in());
258 aservant->_remove_ref(); // decrement for the call to reference_to_servant
259 aservant->_remove_ref(); // to delete the object
262 //============================================================================
264 * Purpose : Save a Study to it's persistent reference
266 //============================================================================
267 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
269 SALOMEDS::Locker lock;
271 if(aStudy->_is_nil()) {
272 MESSAGE("Save error: Study is null");
276 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
277 return _impl->Save(aStudyImpl, _factory, theMultiFile);
280 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
282 SALOMEDS::Locker lock;
284 if(aStudy->_is_nil()) {
285 MESSAGE("SaveASCII error: Study is null");
289 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
290 return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
293 //=============================================================================
294 /*! Function : SaveAs
295 * Purpose : Save a study to the persistent reference aUrl
297 //============================================================================
298 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
300 SALOMEDS::Locker lock;
302 if(aStudy->_is_nil()) {
303 MESSAGE("SaveASCII error: Study is null");
307 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
308 return _impl->SaveAs(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
311 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
313 SALOMEDS::Locker lock;
315 if(aStudy->_is_nil()) {
316 MESSAGE("SaveASCII error: Study is null");
320 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
321 return _impl->SaveAsASCII(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
324 //============================================================================
325 /*! Function : GetOpenStudies
326 * Purpose : Get name list of open studies in the session
328 //============================================================================
329 SALOMEDS::ListOfOpenStudies* SALOMEDS_StudyManager_i::GetOpenStudies()
331 SALOMEDS::Locker lock;
333 std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
334 int aLength = anOpened.size();
336 SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
337 _list_open_studies->length(aLength);
341 MESSAGE("No active study in this session");
345 for (unsigned int ind=0; ind < aLength; ind++)
347 _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
348 SCRUTE(_list_open_studies[ind]) ;
351 return _list_open_studies._retn();
354 //============================================================================
355 /*! Function : GetStudyByName
356 * Purpose : Get a study from its name
358 //============================================================================
359 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
361 SALOMEDS::Locker lock;
363 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(std::string(aStudyName));
367 MESSAGE(_impl->GetErrorCode().c_str());
368 return SALOMEDS::Study::_nil();
371 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
372 return aStudy_servant->_this();
375 //============================================================================
376 /*! Function : GetStudyByID
377 * Purpose : Get a study from its ID
379 //============================================================================
380 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
382 SALOMEDS::Locker lock;
384 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
388 MESSAGE(_impl->GetErrorCode().c_str());
389 return SALOMEDS::Study::_nil();
392 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
393 return aStudy_servant->_this();
397 //============================================================================
398 /*! Function : CanCopy
401 //============================================================================
402 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
404 SALOMEDS::Locker lock;
406 SALOMEDS::Study_var aStudy = theObject->GetStudy();
407 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
408 CORBA::String_var anID = theObject->GetID();
409 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
411 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
412 bool ret = _impl->CanCopy(anObject, aDriver);
417 //============================================================================
421 //============================================================================
422 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
424 SALOMEDS::Locker lock;
426 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());
431 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
432 bool ret = _impl->Copy(anObject, aDriver);
437 //============================================================================
438 /*! Function : CanPaste
441 //============================================================================
442 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
444 SALOMEDS::Locker lock;
446 SALOMEDS::Study_var aStudy = theObject->GetStudy();
447 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
448 CORBA::String_var anID = theObject->GetID();
449 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
451 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
452 bool ret = _impl->CanPaste(anObject, aDriver);
457 //============================================================================
461 //============================================================================
462 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
463 throw(SALOMEDS::StudyBuilder::LockProtection)
465 SALOMEDS::Locker lock;
467 Unexpect aCatch(LockProtection);
468 SALOMEDS::Study_var aStudy = theObject->GetStudy();
470 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
471 CORBA::String_var anID = theObject->GetID();
472 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
473 SALOMEDSImpl_SObject aNewSO;
476 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
477 aNewSO = _impl->Paste(anObject, aDriver);
481 throw SALOMEDS::StudyBuilder::LockProtection();
484 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
489 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
491 SALOMEDS_Driver_i* driver = NULL;
493 SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
495 std::string IOREngine = aSCO.GetIOR();
496 if(!IOREngine.empty()) {
497 CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
498 Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
499 driver = new SALOMEDS_Driver_i(Engine, orb);
506 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
507 if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
508 return PortableServer::POA::_nil();
511 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetThePOA()
513 std::map<int, PortableServer::POA_ptr>::iterator iter = _mapOfPOA.begin();
514 if (iter != _mapOfPOA.end())
516 PortableServer::POA_ptr aPoa = iter->second;
517 //MESSAGE("GetThePOA(): "<< aPoa);
520 MESSAGE("GetThePOA(): _nil !");
521 return PortableServer::POA::_nil();
524 CORBA::Long SALOMEDS_StudyManager_i::getPID()
527 return (CORBA::Long)_getpid();
529 return (CORBA::Long)getpid();
533 void SALOMEDS_StudyManager_i::ShutdownWithExit()
535 exit( EXIT_SUCCESS );
538 //===========================================================================
540 //===========================================================================
541 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
544 long pid = (long)_getpid();
546 long pid = (long)getpid();
548 isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
549 return reinterpret_cast<CORBA::LongLong>(_impl);
552 //===========================================================================
555 PortableServer::ServantBase_var
556 GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
558 if(CORBA::is_nil(theObject))
561 return thePOA->reference_to_servant(theObject);
569 //===========================================================================