1 // Copyright (C) 2007-2008 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
22 // File : SALOMEDS_StudyManager_i.cxx
23 // Author : Sergey RUIN
26 #include "utilities.h"
27 #include "SALOME_LifeCycleCORBA.hxx"
28 #include "SALOMEDS_StudyManager_i.hxx"
29 #include "SALOMEDS_Study_i.hxx"
30 #include "SALOMEDS_SComponent_i.hxx"
31 #include "SALOMEDS_Driver_i.hxx"
32 #include "SALOMEDS.hxx"
34 #include "SALOMEDSImpl_Study.hxx"
35 #include "SALOMEDSImpl_SObject.hxx"
36 #include "SALOMEDSImpl_SComponent.hxx"
37 #include "SALOMEDSImpl_AttributeIOR.hxx"
39 #include "Utils_CorbaException.hxx"
40 #include "Utils_ExceptHandlers.hxx"
41 #include "Basics_Utils.hxx"
42 #include "SALOME_GenericObj_i.hh"
51 #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");
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(const 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 = 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(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 = new SALOMEDS_Study_i(aStudyImpl, _orb);
167 SALOMEDS::Study_var Study = SALOMEDS::Study::_narrow(Study_servant->_this());
169 // Assign the value of the IOR in the study->root
170 CORBA::String_var IORStudy = _orb->object_to_string(Study);
171 aStudyImpl->SetTransientReference((char*)IORStudy.in());
173 // Register study in the naming service
174 // Path to acces the study
175 if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
176 else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().c_str()));
183 //============================================================================
185 * Purpose : Close a study.
186 * If the study hasn't been saved, ask the user to confirm the
187 * close action without saving
189 //============================================================================
190 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
192 SALOMEDS::Locker lock;
194 if(aStudy->_is_nil()) return;
196 // Destroy study name in the naming service
197 if(_name_service->Change_Directory("/Study")){
198 CORBA::String_var aString(aStudy->Name());
199 _name_service->Destroy_Name(aString.in());
207 //============================================================================
209 * Purpose : Save a Study to it's persistent reference
211 //============================================================================
212 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
214 SALOMEDS::Locker lock;
216 if(aStudy->_is_nil()) {
217 MESSAGE("Save error: Study is null");
221 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
222 return _impl->Save(aStudyImpl, _factory, theMultiFile);
225 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
227 SALOMEDS::Locker lock;
229 if(aStudy->_is_nil()) {
230 MESSAGE("SaveASCII error: Study is null");
234 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
235 return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
238 //=============================================================================
239 /*! Function : SaveAs
240 * Purpose : Save a study to the persistent reference aUrl
242 //============================================================================
243 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
245 SALOMEDS::Locker lock;
247 if(aStudy->_is_nil()) {
248 MESSAGE("SaveASCII error: Study is null");
252 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
253 return _impl->SaveAs(string(aUrl), aStudyImpl, _factory, theMultiFile);
256 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
258 SALOMEDS::Locker lock;
260 if(aStudy->_is_nil()) {
261 MESSAGE("SaveASCII error: Study is null");
265 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
266 return _impl->SaveAsASCII(string(aUrl), aStudyImpl, _factory, theMultiFile);
269 //============================================================================
270 /*! Function : GetOpenStudies
271 * Purpose : Get name list of open studies in the session
273 //============================================================================
274 SALOMEDS::ListOfOpenStudies* SALOMEDS_StudyManager_i::GetOpenStudies()
276 SALOMEDS::Locker lock;
278 vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
279 int aLength = anOpened.size();
281 SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
282 _list_open_studies->length(aLength);
286 MESSAGE("No active study in this session");
290 for (unsigned int ind=0; ind < aLength; ind++)
292 _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
293 SCRUTE(_list_open_studies[ind]) ;
296 return _list_open_studies._retn();
299 //============================================================================
300 /*! Function : GetStudyByName
301 * Purpose : Get a study from its name
303 //============================================================================
304 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
306 SALOMEDS::Locker lock;
308 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(string(aStudyName));
312 MESSAGE(_impl->GetErrorCode().c_str());
313 return SALOMEDS::Study::_nil();
316 SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
317 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(aStudy_servant->_this());
319 return aStudy._retn();
322 //============================================================================
323 /*! Function : GetStudyByID
324 * Purpose : Get a study from its ID
326 //============================================================================
327 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
329 SALOMEDS::Locker lock;
331 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
335 MESSAGE(_impl->GetErrorCode().c_str());
336 return SALOMEDS::Study::_nil();
339 SALOMEDS_Study_i* aStudy_servant = new SALOMEDS_Study_i(aStudyImpl, _orb);
340 CORBA::Object_var obj = aStudy_servant->_this();
341 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(obj);
343 return aStudy._retn();
347 //============================================================================
348 /*! Function : CanCopy
351 //============================================================================
352 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
354 SALOMEDS::Locker lock;
356 SALOMEDS::Study_var aStudy = theObject->GetStudy();
357 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
358 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
360 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
361 bool ret = _impl->CanCopy(anObject, aDriver);
366 //============================================================================
370 //============================================================================
371 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
373 SALOMEDS::Locker lock;
375 SALOMEDS::Study_var aStudy = theObject->GetStudy();
376 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
377 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
379 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
380 bool ret = _impl->Copy(anObject, aDriver);
385 //============================================================================
386 /*! Function : CanPaste
389 //============================================================================
390 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
392 SALOMEDS::Locker lock;
394 SALOMEDS::Study_var aStudy = theObject->GetStudy();
395 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
396 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
398 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
399 bool ret = _impl->CanPaste(anObject, aDriver);
404 //============================================================================
408 //============================================================================
409 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
410 throw(SALOMEDS::StudyBuilder::LockProtection)
412 SALOMEDS::Locker lock;
414 Unexpect aCatch(LockProtection);
415 SALOMEDS::Study_var aStudy = theObject->GetStudy();
417 SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
418 SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(theObject->GetID());
419 SALOMEDSImpl_SObject aNewSO;
422 SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
423 aNewSO = _impl->Paste(anObject, aDriver);
427 throw SALOMEDS::StudyBuilder::LockProtection();
430 SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
435 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
437 SALOMEDS_Driver_i* driver = NULL;
439 SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
441 string IOREngine = aSCO.GetIOR();
442 if(!IOREngine.empty()) {
443 CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
444 SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
445 driver = new SALOMEDS_Driver_i(Engine, orb);
452 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
453 if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
454 return PortableServer::POA::_nil();
457 CORBA::Long SALOMEDS_StudyManager_i::getPID()
460 return (CORBA::Long)_getpid();
462 return (CORBA::Long)getpid();
466 void SALOMEDS_StudyManager_i::ShutdownWithExit()
468 exit( EXIT_SUCCESS );
471 //===========================================================================
473 //===========================================================================
474 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
477 long pid = (long)_getpid();
479 long pid = (long)getpid();
481 isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
482 return reinterpret_cast<CORBA::LongLong>(_impl);
485 //===========================================================================
488 PortableServer::ServantBase_var
489 GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
491 if(CORBA::is_nil(theObject))
494 return thePOA->reference_to_servant(theObject);
502 //===========================================================================