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 _name_service = new SALOME_NamingService(_orb);
73 // Study directory creation in the naming service : to register all
74 // open studies in the session
75 _name_service->Create_Directory("/Study");
76 _impl = new SALOMEDSImpl_StudyManager;
77 _factory = new SALOMEDS_DriverFactory_i(_orb);
80 //============================================================================
81 /*! Function : ~SALOMEDS_StudyManager_i
82 * Purpose : SALOMEDS_StudyManager_i destructor
84 //============================================================================
85 SALOMEDS_StudyManager_i::~SALOMEDS_StudyManager_i()
87 // Destroy directory to register open studies
88 _name_service->Destroy_Directory("/Study");
94 //============================================================================
95 /*! Function : register_name
96 * Purpose : Register the study Manager in the naming service under the
99 //============================================================================
100 void SALOMEDS_StudyManager_i::register_name(const char * name)
102 SALOMEDS::StudyManager_var aManager(_this());
103 _name_service->Register(aManager.in(), name);
107 //============================================================================
108 /*! Function : NewStudy
109 * Purpose : Create a New Study of name study_name
111 //============================================================================
112 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
113 throw(SALOME::SALOME_Exception)
115 SALOMEDS::Locker lock;
117 #ifndef ALLOW_MULTI_STUDIES
118 std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
119 int aLength = anOpened.size();
123 MESSAGE("There is already an active study in this session. Launch a new session, or close the study");
124 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)
126 #endif // !ALLOW_MULTI_STUDIES
128 SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
130 MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
131 return SALOMEDS::Study::_nil();
134 MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
136 SALOMEDS_Study_i *Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
137 PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa registered in _mapOfPOA
138 SALOMEDS::Study_var Study = Study_servant->_this();
140 // Register study in the naming service
141 // Path to acces the study
142 if(!_name_service->Change_Directory("/Study"))
143 MESSAGE( "Unable to access the study directory" )
145 _name_service->Register(Study, study_name);
147 // Assign the value of the IOR in the study->root
148 CORBA::String_var IORStudy = _orb->object_to_string(Study);
150 aStudyImpl->SetTransientReference((char*)IORStudy.in());
152 _mapOfPOA[Study->StudyId()] = _poa;
154 return Study._retn();
157 //============================================================================
159 * Purpose : Open a Study from it's persistent reference
161 //============================================================================
162 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::Open(const char* aUrl)
163 throw(SALOME::SALOME_Exception)
165 SALOMEDS::Locker lock;
167 Unexpect aCatch(SalomeException);
168 MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
170 #ifndef ALLOW_MULTI_STUDIES
171 std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
172 int aLength = anOpened.size();
176 MESSAGE("There is already an active study in this session. Launch a new session, or close the study.");
177 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)
179 #endif // ;ALLOW_MULTI_STUDIES
181 SALOMEDSImpl_Study* aStudyImpl = _impl->Open(std::string(aUrl));
184 THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
186 MESSAGE("Open : Creating the CORBA servant holding it... ");
188 // Temporary aStudyUrl in place of study name
189 SALOMEDS_Study_i * Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
190 PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa register in _mapOfPOA
191 SALOMEDS::Study_var Study = Study_servant->_this();
193 // Assign the value of the IOR in the study->root
194 CORBA::String_var IORStudy = _orb->object_to_string(Study);
195 aStudyImpl->SetTransientReference((char*)IORStudy.in());
197 _mapOfPOA[Study->StudyId()] = _poa;
199 // Register study in the naming service
200 // Path to acces the study
201 if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
202 else _name_service->Register(Study, aStudyImpl->Name().c_str());
204 return Study._retn();
209 //============================================================================
211 * Purpose : Close a study.
212 * If the study hasn't been saved, ask the user to confirm the
213 * close action without saving
215 //============================================================================
216 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
218 SALOMEDS::Locker lock;
220 if(aStudy->_is_nil()) return;
222 // Destroy study name in the naming service
223 if(_name_service->Change_Directory("/Study")){
224 CORBA::String_var aString(aStudy->Name());
225 _name_service->Destroy_Name(aString.in());
232 //remove study servant
233 PortableServer::POA_ptr poa=GetPOA(aStudy);
234 PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy);
235 PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant);
236 poa->deactivate_object(anObjectId.in());
237 aservant->_remove_ref(); // decrement for the call to reference_to_servant
238 aservant->_remove_ref(); // to delete the object
241 //============================================================================
243 * Purpose : Save a Study to it's persistent reference
245 //============================================================================
246 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
248 SALOMEDS::Locker lock;
250 if(aStudy->_is_nil()) {
251 MESSAGE("Save error: Study is null");
255 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
256 return _impl->Save(aStudyImpl, _factory, theMultiFile);
259 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
261 SALOMEDS::Locker lock;
263 if(aStudy->_is_nil()) {
264 MESSAGE("SaveASCII error: Study is null");
268 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
269 return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
272 //=============================================================================
273 /*! Function : SaveAs
274 * Purpose : Save a study to the persistent reference aUrl
276 //============================================================================
277 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
279 SALOMEDS::Locker lock;
281 if(aStudy->_is_nil()) {
282 MESSAGE("SaveASCII error: Study is null");
286 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
287 return _impl->SaveAs(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
290 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
292 SALOMEDS::Locker lock;
294 if(aStudy->_is_nil()) {
295 MESSAGE("SaveASCII error: Study is null");
299 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
300 return _impl->SaveAsASCII(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
303 //============================================================================
304 /*! Function : GetOpenStudies
305 * Purpose : Get name list of open studies in the session
307 //============================================================================
308 SALOMEDS::ListOfOpenStudies* SALOMEDS_StudyManager_i::GetOpenStudies()
310 SALOMEDS::Locker lock;
312 std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
313 int aLength = anOpened.size();
315 SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
316 _list_open_studies->length(aLength);
320 MESSAGE("No active study in this session");
324 for (unsigned int ind=0; ind < aLength; ind++)
326 _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
327 SCRUTE(_list_open_studies[ind]) ;
330 return _list_open_studies._retn();
333 //============================================================================
334 /*! Function : GetStudyByName
335 * Purpose : Get a study from its name
337 //============================================================================
338 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
340 SALOMEDS::Locker lock;
342 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(std::string(aStudyName));
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();
354 //============================================================================
355 /*! Function : GetStudyByID
356 * Purpose : Get a study from its ID
358 //============================================================================
359 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
361 SALOMEDS::Locker lock;
363 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
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();
376 //============================================================================
377 /*! Function : CanCopy
380 //============================================================================
381 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
383 SALOMEDS::Locker lock;
385 SALOMEDS::Study_var aStudy = theObject->GetStudy();
386 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
387 CORBA::String_var anID = theObject->GetID();
388 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
390 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
391 bool ret = _impl->CanCopy(anObject, aDriver);
396 //============================================================================
400 //============================================================================
401 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
403 SALOMEDS::Locker lock;
405 SALOMEDS::Study_var aStudy = theObject->GetStudy();
406 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
407 CORBA::String_var anID = theObject->GetID();
408 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
410 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
411 bool ret = _impl->Copy(anObject, aDriver);
416 //============================================================================
417 /*! Function : CanPaste
420 //============================================================================
421 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
423 SALOMEDS::Locker lock;
425 SALOMEDS::Study_var aStudy = theObject->GetStudy();
426 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
427 CORBA::String_var anID = theObject->GetID();
428 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
430 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
431 bool ret = _impl->CanPaste(anObject, aDriver);
436 //============================================================================
440 //============================================================================
441 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
442 throw(SALOMEDS::StudyBuilder::LockProtection)
444 SALOMEDS::Locker lock;
446 Unexpect aCatch(LockProtection);
447 SALOMEDS::Study_var aStudy = theObject->GetStudy();
449 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
450 CORBA::String_var anID = theObject->GetID();
451 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
452 SALOMEDSImpl_SObject aNewSO;
455 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
456 aNewSO = _impl->Paste(anObject, aDriver);
460 throw SALOMEDS::StudyBuilder::LockProtection();
463 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
468 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
470 SALOMEDS_Driver_i* driver = NULL;
472 SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
474 std::string IOREngine = aSCO.GetIOR();
475 if(!IOREngine.empty()) {
476 CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
477 Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
478 driver = new SALOMEDS_Driver_i(Engine, orb);
485 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
486 if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
487 return PortableServer::POA::_nil();
490 CORBA::Long SALOMEDS_StudyManager_i::getPID()
493 return (CORBA::Long)_getpid();
495 return (CORBA::Long)getpid();
499 void SALOMEDS_StudyManager_i::ShutdownWithExit()
501 exit( EXIT_SUCCESS );
504 //===========================================================================
506 //===========================================================================
507 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
510 long pid = (long)_getpid();
512 long pid = (long)getpid();
514 isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
515 return reinterpret_cast<CORBA::LongLong>(_impl);
518 //===========================================================================
521 PortableServer::ServantBase_var
522 GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
524 if(CORBA::is_nil(theObject))
527 return thePOA->reference_to_servant(theObject);
535 //===========================================================================