1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDS_StudyManager_i.cxx
21 // Author : Sergey RUIN
24 #include "utilities.h"
25 #include "SALOME_LifeCycleCORBA.hxx"
26 #include "SALOMEDS_StudyManager_i.hxx"
27 #include "SALOMEDS_Study_i.hxx"
28 #include "SALOMEDS_SComponent_i.hxx"
29 #include "SALOMEDS_Driver_i.hxx"
30 #include "SALOMEDS.hxx"
32 #include "SALOMEDSImpl_Study.hxx"
33 #include "SALOMEDSImpl_SObject.hxx"
34 #include "SALOMEDSImpl_SComponent.hxx"
35 #include "SALOMEDSImpl_AttributeIOR.hxx"
37 #include "Utils_CorbaException.hxx"
47 #include <sys/types.h>
53 #include "SALOME_GenericObj_i.hh"
55 #include "Utils_ExceptHandlers.hxx"
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");
95 //============================================================================
96 /*! Function : register_name
97 * Purpose : Register the study Manager in the naming service under the
100 //============================================================================
101 void SALOMEDS_StudyManager_i::register_name(char * name)
103 SALOMEDS::StudyManager_var aManager(_this());
104 _name_service->Register(aManager.in(), name);
108 //============================================================================
109 /*! Function : NewStudy
110 * Purpose : Create a New Study of name study_name
112 //============================================================================
113 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
115 SALOMEDS::Locker lock;
117 SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
119 MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
120 return SALOMEDS::Study::_nil();
123 MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
125 SALOMEDS_Study_i *Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
126 SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(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;
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(string(aUrl));
160 MESSAGE("Open : Creating the CORBA servant holding it... ");
162 // Temporary aStudyUrl in place of study name
163 SALOMEDS_Study_i * Study_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
164 SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this());
166 // Assign the value of the IOR in the study->root
167 CORBA::String_var IORStudy = _orb->object_to_string(Study);
168 aStudyImpl->SetTransientReference((char*)IORStudy.in());
170 // Register study in the naming service
171 // Path to acces the study
172 if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
173 else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().c_str()));
180 //============================================================================
182 * Purpose : Close a study.
183 * If the study hasn't been saved, ask the user to confirm the
184 * close action without saving
186 //============================================================================
187 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
189 SALOMEDS::Locker lock;
191 if(aStudy->_is_nil()) return;
193 // Destroy study name in the naming service
194 if(_name_service->Change_Directory("/Study")){
195 CORBA::String_var aString(aStudy->Name());
196 _name_service->Destroy_Name(aString.in());
204 //============================================================================
206 * Purpose : Save a Study to it's persistent reference
208 //============================================================================
209 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
211 SALOMEDS::Locker lock;
213 if(aStudy->_is_nil()) {
214 MESSAGE("Save error: Study is null");
218 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
219 return _impl->Save(aStudyImpl, _factory, theMultiFile);
222 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
224 SALOMEDS::Locker lock;
226 if(aStudy->_is_nil()) {
227 MESSAGE("SaveASCII error: Study is null");
231 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
232 return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
235 //=============================================================================
236 /*! Function : SaveAs
237 * Purpose : Save a study to the persistent reference aUrl
239 //============================================================================
240 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
242 SALOMEDS::Locker lock;
244 if(aStudy->_is_nil()) {
245 MESSAGE("SaveASCII error: Study is null");
249 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
250 return _impl->SaveAs(string(aUrl), aStudyImpl, _factory, theMultiFile);
253 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
255 SALOMEDS::Locker lock;
257 if(aStudy->_is_nil()) {
258 MESSAGE("SaveASCII error: Study is null");
262 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
263 return _impl->SaveAsASCII(string(aUrl), aStudyImpl, _factory, theMultiFile);
266 //============================================================================
267 /*! Function : GetOpenStudies
268 * Purpose : Get name list of open studies in the session
270 //============================================================================
271 SALOMEDS::ListOfOpenStudies* SALOMEDS_StudyManager_i::GetOpenStudies()
273 SALOMEDS::Locker lock;
275 vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
276 int aLength = anOpened.size();
278 SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
279 _list_open_studies->length(aLength);
283 MESSAGE("No active study in this session");
287 for (unsigned int ind=0; ind < aLength; ind++)
289 _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
290 SCRUTE(_list_open_studies[ind]) ;
293 return _list_open_studies._retn();
296 //============================================================================
297 /*! Function : GetStudyByName
298 * Purpose : Get a study from its name
300 //============================================================================
301 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
303 SALOMEDS::Locker lock;
305 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(string(aStudyName));
309 MESSAGE(_impl->GetErrorCode().c_str());
310 return SALOMEDS::Study::_nil();
313 SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
314 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(aStudy_servant->_this());
316 return aStudy._retn();
319 //============================================================================
320 /*! Function : GetStudyByID
321 * Purpose : Get a study from its ID
323 //============================================================================
324 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
326 SALOMEDS::Locker lock;
328 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
332 MESSAGE(_impl->GetErrorCode().c_str());
333 return SALOMEDS::Study::_nil();
336 SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
337 CORBA::Object_var obj = aStudy_servant->_this();
338 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(obj);
340 return aStudy._retn();
344 //============================================================================
345 /*! Function : CanCopy
348 //============================================================================
349 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
351 SALOMEDS::Locker lock;
353 SALOMEDS::Study_var aStudy = theObject->GetStudy();
354 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
355 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
357 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
358 bool ret = _impl->CanCopy(anObject, aDriver);
363 //============================================================================
367 //============================================================================
368 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
370 SALOMEDS::Locker lock;
372 SALOMEDS::Study_var aStudy = theObject->GetStudy();
373 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
374 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
376 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
377 bool ret = _impl->Copy(anObject, aDriver);
382 //============================================================================
383 /*! Function : CanPaste
386 //============================================================================
387 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
389 SALOMEDS::Locker lock;
391 SALOMEDS::Study_var aStudy = theObject->GetStudy();
392 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
393 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
395 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
396 bool ret = _impl->CanPaste(anObject, aDriver);
401 //============================================================================
405 //============================================================================
406 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
407 throw(SALOMEDS::StudyBuilder::LockProtection)
409 SALOMEDS::Locker lock;
411 Unexpect aCatch(LockProtection);
412 SALOMEDS::Study_var aStudy = theObject->GetStudy();
414 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
415 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
416 SALOMEDSImpl_SObject aNewSO;
419 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
420 aNewSO = _impl->Paste(anObject, aDriver);
424 throw SALOMEDS::StudyBuilder::LockProtection();
427 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
432 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
434 SALOMEDS_Driver_i* driver = NULL;
436 SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
438 string IOREngine = aSCO.GetIOR();
439 if(!IOREngine.empty()) {
440 CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
441 SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
442 driver = new SALOMEDS_Driver_i(Engine, orb);
449 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
450 if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
451 return PortableServer::POA::_nil();
454 CORBA::Long SALOMEDS_StudyManager_i::getPID()
457 return (CORBA::Long)_getpid();
459 return (CORBA::Long)getpid();
463 void SALOMEDS_StudyManager_i::ShutdownWithExit()
465 exit( EXIT_SUCCESS );
468 //===========================================================================
470 //===========================================================================
471 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
474 long pid = (long)_getpid();
476 long pid = (long)getpid();
478 isLocal = (strcmp(theHostname, GetHostname().c_str()) == 0 && pid == thePID)?1:0;
479 return ((CORBA::LongLong)(void*)_impl);
482 //===========================================================================
485 PortableServer::ServantBase_var
486 GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
488 if(CORBA::is_nil(theObject))
491 return thePOA->reference_to_servant(theObject);
499 //===========================================================================