1 // Copyright (C) 2007-2010 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
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)
114 SALOMEDS::Locker lock;
116 SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
118 MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
119 return SALOMEDS::Study::_nil();
122 MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
124 SALOMEDS_Study_i *Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
125 PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa registered in _mapOfPOA
126 SALOMEDS::Study_var Study = Study_servant->_this();
128 // Register study in the naming service
129 // Path to acces the study
130 if(!_name_service->Change_Directory("/Study"))
131 MESSAGE( "Unable to access the study directory" )
133 _name_service->Register(Study, study_name);
135 // Assign the value of the IOR in the study->root
136 CORBA::String_var IORStudy = _orb->object_to_string(Study);
138 aStudyImpl->SetTransientReference((char*)IORStudy.in());
140 _mapOfPOA[Study->StudyId()] = _poa;
142 return Study._retn();
145 //============================================================================
147 * Purpose : Open a Study from it's persistent reference
149 //============================================================================
150 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::Open(const char* aUrl)
151 throw(SALOME::SALOME_Exception)
153 SALOMEDS::Locker lock;
155 Unexpect aCatch(SalomeException);
156 MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
158 SALOMEDSImpl_Study* aStudyImpl = _impl->Open(std::string(aUrl));
161 THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
163 MESSAGE("Open : Creating the CORBA servant holding it... ");
165 // Temporary aStudyUrl in place of study name
166 SALOMEDS_Study_i * Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
167 PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa register in _mapOfPOA
168 SALOMEDS::Study_var Study = Study_servant->_this();
170 // Assign the value of the IOR in the study->root
171 CORBA::String_var IORStudy = _orb->object_to_string(Study);
172 aStudyImpl->SetTransientReference((char*)IORStudy.in());
174 _mapOfPOA[Study->StudyId()] = _poa;
176 // Register study in the naming service
177 // Path to acces the study
178 if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
179 else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().c_str()));
181 return Study._retn();
186 //============================================================================
188 * Purpose : Close a study.
189 * If the study hasn't been saved, ask the user to confirm the
190 * close action without saving
192 //============================================================================
193 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
195 SALOMEDS::Locker lock;
197 if(aStudy->_is_nil()) return;
199 // Destroy study name in the naming service
200 if(_name_service->Change_Directory("/Study")){
201 CORBA::String_var aString(aStudy->Name());
202 _name_service->Destroy_Name(aString.in());
209 //remove study servant
210 PortableServer::POA_ptr poa=GetPOA(aStudy);
211 PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy);
212 PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant);
213 poa->deactivate_object(anObjectId.in());
214 aservant->_remove_ref(); // decrement for the call to reference_to_servant
215 aservant->_remove_ref(); // to delete the object
218 //============================================================================
220 * Purpose : Save a Study to it's persistent reference
222 //============================================================================
223 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
225 SALOMEDS::Locker lock;
227 if(aStudy->_is_nil()) {
228 MESSAGE("Save error: Study is null");
232 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
233 return _impl->Save(aStudyImpl, _factory, theMultiFile);
236 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
238 SALOMEDS::Locker lock;
240 if(aStudy->_is_nil()) {
241 MESSAGE("SaveASCII error: Study is null");
245 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
246 return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
249 //=============================================================================
250 /*! Function : SaveAs
251 * Purpose : Save a study to the persistent reference aUrl
253 //============================================================================
254 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
256 SALOMEDS::Locker lock;
258 if(aStudy->_is_nil()) {
259 MESSAGE("SaveASCII error: Study is null");
263 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
264 return _impl->SaveAs(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
267 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
269 SALOMEDS::Locker lock;
271 if(aStudy->_is_nil()) {
272 MESSAGE("SaveASCII error: Study is null");
276 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
277 return _impl->SaveAsASCII(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
280 //============================================================================
281 /*! Function : GetOpenStudies
282 * Purpose : Get name list of open studies in the session
284 //============================================================================
285 SALOMEDS::ListOfOpenStudies* SALOMEDS_StudyManager_i::GetOpenStudies()
287 SALOMEDS::Locker lock;
289 std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
290 int aLength = anOpened.size();
292 SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
293 _list_open_studies->length(aLength);
297 MESSAGE("No active study in this session");
301 for (unsigned int ind=0; ind < aLength; ind++)
303 _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
304 SCRUTE(_list_open_studies[ind]) ;
307 return _list_open_studies._retn();
310 //============================================================================
311 /*! Function : GetStudyByName
312 * Purpose : Get a study from its name
314 //============================================================================
315 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
317 SALOMEDS::Locker lock;
319 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(std::string(aStudyName));
323 MESSAGE(_impl->GetErrorCode().c_str());
324 return SALOMEDS::Study::_nil();
327 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
328 return aStudy_servant->_this();
331 //============================================================================
332 /*! Function : GetStudyByID
333 * Purpose : Get a study from its ID
335 //============================================================================
336 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
338 SALOMEDS::Locker lock;
340 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
344 MESSAGE(_impl->GetErrorCode().c_str());
345 return SALOMEDS::Study::_nil();
348 SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
349 return aStudy_servant->_this();
353 //============================================================================
354 /*! Function : CanCopy
357 //============================================================================
358 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
360 SALOMEDS::Locker lock;
362 SALOMEDS::Study_var aStudy = theObject->GetStudy();
363 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
364 CORBA::String_var anID = theObject->GetID();
365 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
367 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
368 bool ret = _impl->CanCopy(anObject, aDriver);
373 //============================================================================
377 //============================================================================
378 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
380 SALOMEDS::Locker lock;
382 SALOMEDS::Study_var aStudy = theObject->GetStudy();
383 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
384 CORBA::String_var anID = theObject->GetID();
385 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
387 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
388 bool ret = _impl->Copy(anObject, aDriver);
393 //============================================================================
394 /*! Function : CanPaste
397 //============================================================================
398 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
400 SALOMEDS::Locker lock;
402 SALOMEDS::Study_var aStudy = theObject->GetStudy();
403 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
404 CORBA::String_var anID = theObject->GetID();
405 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
407 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
408 bool ret = _impl->CanPaste(anObject, aDriver);
413 //============================================================================
417 //============================================================================
418 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
419 throw(SALOMEDS::StudyBuilder::LockProtection)
421 SALOMEDS::Locker lock;
423 Unexpect aCatch(LockProtection);
424 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());
429 SALOMEDSImpl_SObject aNewSO;
432 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
433 aNewSO = _impl->Paste(anObject, aDriver);
437 throw SALOMEDS::StudyBuilder::LockProtection();
440 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
445 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
447 SALOMEDS_Driver_i* driver = NULL;
449 SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
451 std::string IOREngine = aSCO.GetIOR();
452 if(!IOREngine.empty()) {
453 CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
454 SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
455 driver = new SALOMEDS_Driver_i(Engine, orb);
462 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
463 if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
464 return PortableServer::POA::_nil();
467 CORBA::Long SALOMEDS_StudyManager_i::getPID()
470 return (CORBA::Long)_getpid();
472 return (CORBA::Long)getpid();
476 void SALOMEDS_StudyManager_i::ShutdownWithExit()
478 exit( EXIT_SUCCESS );
481 //===========================================================================
483 //===========================================================================
484 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
487 long pid = (long)_getpid();
489 long pid = (long)getpid();
491 isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
492 return reinterpret_cast<CORBA::LongLong>(_impl);
495 //===========================================================================
498 PortableServer::ServantBase_var
499 GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
501 if(CORBA::is_nil(theObject))
504 return thePOA->reference_to_servant(theObject);
512 //===========================================================================