Back to index

salome-kernel  6.5.0
SALOMEDS_StudyManager_i.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : SALOMEDS_StudyManager_i.cxx
00024 //  Author : Sergey RUIN
00025 //  Module : SALOME
00026 //
00027 #include "utilities.h"
00028 #include "SALOME_LifeCycleCORBA.hxx"
00029 #include "SALOMEDS_StudyManager_i.hxx"
00030 #include "SALOMEDS_Study_i.hxx"
00031 #include "SALOMEDS_SComponent_i.hxx"
00032 #include "SALOMEDS_Driver_i.hxx"
00033 #include "SALOMEDS.hxx"
00034 
00035 #include "SALOMEDSImpl_Study.hxx"
00036 #include "SALOMEDSImpl_SObject.hxx"
00037 #include "SALOMEDSImpl_SComponent.hxx"
00038 #include "SALOMEDSImpl_AttributeIOR.hxx"
00039 
00040 #include "Utils_CorbaException.hxx"
00041 #include "Utils_ExceptHandlers.hxx"
00042 #include "Basics_Utils.hxx"
00043 #include "SALOME_GenericObj_i.hh"
00044 
00045 #include <sstream>
00046 #include <vector>
00047 #include <map>
00048 
00049 #ifdef WIN32
00050 #include <process.h>
00051 #else
00052 #include <sys/types.h>
00053 #include <unistd.h>
00054 #endif
00055 
00056 UNEXPECT_CATCH(SalomeException,SALOME::SALOME_Exception);
00057 UNEXPECT_CATCH(LockProtection, SALOMEDS::StudyBuilder::LockProtection);
00058 
00059 static SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
00060 
00061 static std::map<int, PortableServer::POA_ptr> _mapOfPOA;
00062 
00063 //============================================================================
00067 //============================================================================
00068 SALOMEDS_StudyManager_i::SALOMEDS_StudyManager_i(CORBA::ORB_ptr orb, PortableServer::POA_ptr thePOA)
00069 {
00070   _orb = CORBA::ORB::_duplicate(orb);
00071   _poa = PortableServer::POA::_duplicate(thePOA);
00072   _name_service = new SALOME_NamingService(_orb);
00073   // Study directory creation in the naming service : to register all
00074   // open studies in the session
00075   _name_service->Create_Directory("/Study");
00076   _impl = new SALOMEDSImpl_StudyManager;
00077   _factory = new SALOMEDS_DriverFactory_i(_orb);
00078 }
00079 
00080 //============================================================================
00084 //============================================================================
00085 SALOMEDS_StudyManager_i::~SALOMEDS_StudyManager_i()
00086 {
00087   // Destroy directory to register open studies
00088   _name_service->Destroy_Directory("/Study");
00089   delete _name_service;
00090   delete _factory;
00091   delete _impl;
00092 }
00093 
00094 //============================================================================
00099 //============================================================================
00100 void SALOMEDS_StudyManager_i::register_name(const char * name)
00101 {
00102   SALOMEDS::StudyManager_var aManager(_this());
00103   _name_service->Register(aManager.in(), name);
00104 }
00105 
00106 
00107 //============================================================================
00111 //============================================================================
00112 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::NewStudy(const char* study_name)
00113 {
00114   SALOMEDS::Locker lock;
00115 
00116   SALOMEDSImpl_Study* aStudyImpl = _impl->NewStudy(study_name);
00117   if(!aStudyImpl) {
00118     MESSAGE("NewStudy : Error : " << _impl->GetErrorCode());
00119     return SALOMEDS::Study::_nil();
00120   }
00121 
00122   MESSAGE("NewStudy : Creating the CORBA servant holding it... ");
00123 
00124   SALOMEDS_Study_i *Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
00125   PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa registered in _mapOfPOA
00126   SALOMEDS::Study_var Study = Study_servant->_this();
00127 
00128   // Register study in the naming service
00129   // Path to acces the study
00130   if(!_name_service->Change_Directory("/Study"))
00131       MESSAGE( "Unable to access the study directory" )
00132   else
00133       _name_service->Register(Study, study_name);
00134 
00135   // Assign the value of the IOR in the study->root
00136   CORBA::String_var IORStudy = _orb->object_to_string(Study);
00137 
00138   aStudyImpl->SetTransientReference((char*)IORStudy.in());
00139 
00140   _mapOfPOA[Study->StudyId()] = _poa;
00141 
00142   return Study._retn();
00143 }
00144 
00145 //============================================================================
00149 //============================================================================
00150 SALOMEDS::Study_ptr  SALOMEDS_StudyManager_i::Open(const char* aUrl)
00151      throw(SALOME::SALOME_Exception)
00152 {
00153   SALOMEDS::Locker lock;
00154 
00155   Unexpect aCatch(SalomeException);
00156   MESSAGE("Begin of SALOMEDS_StudyManager_i::Open");
00157 
00158   SALOMEDSImpl_Study* aStudyImpl = _impl->Open(std::string(aUrl));
00159 
00160   if ( !aStudyImpl )
00161     THROW_SALOME_CORBA_EXCEPTION("Impossible to Open study from file", SALOME::BAD_PARAM)
00162 
00163   MESSAGE("Open : Creating the CORBA servant holding it... ");
00164 
00165   // Temporary aStudyUrl in place of study name
00166   SALOMEDS_Study_i * Study_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
00167   PortableServer::ObjectId_var servantid = _poa->activate_object(Study_servant); // to use poa register in _mapOfPOA
00168   SALOMEDS::Study_var Study = Study_servant->_this();
00169 
00170   // Assign the value of the IOR in the study->root
00171   CORBA::String_var IORStudy = _orb->object_to_string(Study);
00172   aStudyImpl->SetTransientReference((char*)IORStudy.in());
00173 
00174   _mapOfPOA[Study->StudyId()] = _poa;
00175 
00176   // Register study in the naming service
00177   // Path to acces the study
00178   if(!_name_service->Change_Directory("/Study")) MESSAGE( "Unable to access the study directory" )
00179   else _name_service->Register(Study, CORBA::string_dup(aStudyImpl->Name().c_str()));
00180 
00181   return Study._retn();
00182 }
00183 
00184 
00185 
00186 //============================================================================
00192 //============================================================================
00193 void SALOMEDS_StudyManager_i::Close(SALOMEDS::Study_ptr aStudy)
00194 {
00195   SALOMEDS::Locker lock;
00196 
00197   if(aStudy->_is_nil()) return;
00198 
00199   // Destroy study name in the naming service
00200   if(_name_service->Change_Directory("/Study")){
00201     CORBA::String_var aString(aStudy->Name());
00202     _name_service->Destroy_Name(aString.in());
00203   }
00204 
00205   SALOMEDS::unlock();
00206   aStudy->Close();
00207   SALOMEDS::lock();
00208 
00209   //remove study servant
00210   PortableServer::POA_ptr poa=GetPOA(aStudy);
00211   PortableServer::ServantBase* aservant=poa->reference_to_servant(aStudy);
00212   PortableServer::ObjectId_var anObjectId = poa->servant_to_id(aservant);
00213   poa->deactivate_object(anObjectId.in());
00214   aservant->_remove_ref(); // decrement for the call to reference_to_servant
00215   aservant->_remove_ref(); // to delete the object
00216 }
00217 
00218 //============================================================================
00222 //============================================================================
00223 CORBA::Boolean SALOMEDS_StudyManager_i::Save(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
00224 {
00225   SALOMEDS::Locker lock;
00226 
00227   if(aStudy->_is_nil()) {
00228     MESSAGE("Save error: Study is null");
00229     return false;
00230   }
00231 
00232   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
00233   return _impl->Save(aStudyImpl, _factory, theMultiFile);
00234 }
00235 
00236 CORBA::Boolean SALOMEDS_StudyManager_i::SaveASCII(SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
00237 {
00238   SALOMEDS::Locker lock;
00239 
00240   if(aStudy->_is_nil()) {
00241     MESSAGE("SaveASCII error: Study is null");
00242     return false;
00243   }
00244 
00245   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
00246   return _impl->SaveASCII(aStudyImpl, _factory, theMultiFile);
00247 }
00248 
00249 //=============================================================================
00253 //============================================================================
00254 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAs(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
00255 {
00256   SALOMEDS::Locker lock;
00257 
00258   if(aStudy->_is_nil()) {
00259     MESSAGE("SaveASCII error: Study is null");
00260     return false;
00261   }
00262 
00263   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
00264   return _impl->SaveAs(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
00265 }
00266 
00267 CORBA::Boolean SALOMEDS_StudyManager_i::SaveAsASCII(const char* aUrl, SALOMEDS::Study_ptr aStudy, CORBA::Boolean theMultiFile)
00268 {
00269   SALOMEDS::Locker lock;
00270 
00271   if(aStudy->_is_nil()) {
00272     MESSAGE("SaveASCII error: Study is null");
00273     return false;
00274   }
00275 
00276   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
00277   return _impl->SaveAsASCII(std::string(aUrl), aStudyImpl, _factory, theMultiFile);
00278 }
00279 
00280 //============================================================================
00284 //============================================================================
00285 SALOMEDS::ListOfOpenStudies*  SALOMEDS_StudyManager_i::GetOpenStudies()
00286 {
00287   SALOMEDS::Locker lock;
00288 
00289   std::vector<SALOMEDSImpl_Study*> anOpened = _impl->GetOpenStudies();
00290   int aLength = anOpened.size();
00291 
00292   SALOMEDS::ListOfOpenStudies_var _list_open_studies = new SALOMEDS::ListOfOpenStudies;
00293   _list_open_studies->length(aLength);
00294 
00295   if(!aLength)
00296     {
00297       MESSAGE("No active study in this session");
00298     }
00299   else
00300     {
00301       for (unsigned int ind=0; ind < aLength; ind++)
00302         {
00303           _list_open_studies[ind] = CORBA::string_dup(anOpened[ind]->Name().c_str());
00304           SCRUTE(_list_open_studies[ind]) ;
00305         }
00306     }
00307   return _list_open_studies._retn();
00308 }
00309 
00310 //============================================================================
00314 //============================================================================
00315 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByName(const char* aStudyName)
00316 {
00317   SALOMEDS::Locker lock;
00318 
00319   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByName(std::string(aStudyName));
00320 
00321   if (!aStudyImpl)
00322   {
00323     MESSAGE(_impl->GetErrorCode().c_str());
00324     return SALOMEDS::Study::_nil();
00325   }
00326 
00327   SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
00328   return aStudy_servant->_this();
00329 }
00330 
00331 //============================================================================
00335 //============================================================================
00336 SALOMEDS::Study_ptr SALOMEDS_StudyManager_i::GetStudyByID(CORBA::Short aStudyID)
00337 {
00338   SALOMEDS::Locker lock;
00339 
00340   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudyID);
00341 
00342   if (!aStudyImpl)
00343   {
00344     MESSAGE(_impl->GetErrorCode().c_str());
00345     return SALOMEDS::Study::_nil();
00346   }
00347 
00348   SALOMEDS_Study_i* aStudy_servant = SALOMEDS_Study_i::GetStudyServant(aStudyImpl, _orb);
00349   return aStudy_servant->_this();
00350 }
00351 
00352 
00353 //============================================================================
00357 //============================================================================
00358 CORBA::Boolean SALOMEDS_StudyManager_i::CanCopy(SALOMEDS::SObject_ptr theObject)
00359 {
00360   SALOMEDS::Locker lock;
00361 
00362   SALOMEDS::Study_var aStudy = theObject->GetStudy();
00363   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
00364   CORBA::String_var anID = theObject->GetID();
00365   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
00366 
00367   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
00368   bool ret = _impl->CanCopy(anObject, aDriver);
00369   delete aDriver;
00370   return ret;
00371 }
00372 
00373 //============================================================================
00377 //============================================================================
00378 CORBA::Boolean SALOMEDS_StudyManager_i::Copy(SALOMEDS::SObject_ptr theObject)
00379 {
00380   SALOMEDS::Locker lock;
00381 
00382   SALOMEDS::Study_var aStudy = theObject->GetStudy();
00383   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
00384   CORBA::String_var anID = theObject->GetID();
00385   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
00386 
00387   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
00388   bool ret = _impl->Copy(anObject, aDriver);
00389   delete aDriver;
00390   return ret;
00391 }
00392 
00393 //============================================================================
00397 //============================================================================
00398 CORBA::Boolean SALOMEDS_StudyManager_i::CanPaste(SALOMEDS::SObject_ptr theObject)
00399 {
00400   SALOMEDS::Locker lock;
00401 
00402   SALOMEDS::Study_var aStudy = theObject->GetStudy();
00403   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
00404   CORBA::String_var anID = theObject->GetID();
00405   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
00406 
00407   SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
00408   bool ret = _impl->CanPaste(anObject, aDriver);
00409   delete aDriver;
00410   return ret;
00411 }
00412 
00413 //============================================================================
00417 //============================================================================
00418 SALOMEDS::SObject_ptr SALOMEDS_StudyManager_i::Paste(SALOMEDS::SObject_ptr theObject)
00419      throw(SALOMEDS::StudyBuilder::LockProtection)
00420 {
00421   SALOMEDS::Locker lock;
00422 
00423   Unexpect aCatch(LockProtection);
00424   SALOMEDS::Study_var aStudy = theObject->GetStudy();
00425 
00426   SALOMEDSImpl_Study* aStudyImpl = _impl->GetStudyByID(aStudy->StudyId());
00427   CORBA::String_var anID = theObject->GetID();
00428   SALOMEDSImpl_SObject anObject = aStudyImpl->GetSObject(anID.in());
00429   SALOMEDSImpl_SObject aNewSO;
00430 
00431   try {
00432     SALOMEDS_Driver_i* aDriver = GetDriver(anObject, _orb);
00433     aNewSO =  _impl->Paste(anObject, aDriver);
00434     delete aDriver;
00435   }
00436   catch (...) {
00437     throw SALOMEDS::StudyBuilder::LockProtection();
00438   }
00439 
00440   SALOMEDS::SObject_var so = SALOMEDS_SObject_i::New (aNewSO, _orb);
00441   return so._retn();
00442 }
00443 
00444 
00445 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
00446 {
00447   SALOMEDS_Driver_i* driver = NULL;
00448 
00449   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
00450   if(!aSCO.IsNull()) {
00451     std::string IOREngine = aSCO.GetIOR();
00452     if(!IOREngine.empty()) {
00453       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
00454       SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
00455       driver = new SALOMEDS_Driver_i(Engine, orb);
00456     }
00457   }
00458 
00459   return driver;
00460 }
00461 
00462 PortableServer::POA_ptr SALOMEDS_StudyManager_i::GetPOA(const SALOMEDS::Study_ptr theStudy) {
00463   if (_mapOfPOA.find(theStudy->StudyId()) != _mapOfPOA.end()) return _mapOfPOA[theStudy->StudyId()];
00464   return PortableServer::POA::_nil();
00465 }
00466 
00467 CORBA::Long SALOMEDS_StudyManager_i::getPID()
00468 { 
00469 #ifdef WIN32
00470   return (CORBA::Long)_getpid();
00471 #else
00472   return (CORBA::Long)getpid();
00473 #endif
00474 }
00475 
00476 void SALOMEDS_StudyManager_i::ShutdownWithExit()
00477 {
00478   exit( EXIT_SUCCESS );
00479 }
00480 
00481 //===========================================================================
00482 //   PRIVATE FUNCTIONS
00483 //===========================================================================
00484 CORBA::LongLong SALOMEDS_StudyManager_i::GetLocalImpl(const char* theHostname, CORBA::Long thePID, CORBA::Boolean& isLocal)
00485 {
00486 #ifdef WIN32
00487   long pid = (long)_getpid();
00488 #else
00489   long pid = (long)getpid();
00490 #endif
00491   isLocal = (strcmp(theHostname, Kernel_Utils::GetHostname().c_str()) == 0 && pid == thePID)?1:0;
00492   return reinterpret_cast<CORBA::LongLong>(_impl);
00493 }
00494 
00495 //===========================================================================
00496 namespace SALOMEDS
00497 {
00498   PortableServer::ServantBase_var
00499   GetServant(CORBA::Object_ptr theObject, PortableServer::POA_ptr thePOA)
00500   {
00501     if(CORBA::is_nil(theObject))
00502       return NULL;
00503     try{
00504       return thePOA->reference_to_servant(theObject);
00505     }catch(...){
00506       return NULL;
00507     }
00508   }
00509 
00510 }
00511 
00512 //===========================================================================