Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMEDS / SALOMEDS.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
24 //  File   : SALOMEDS.cxx
25 //  Author : Sergey ANIKIN
26 //  Module : SALOME
27 //  $Header$
28 //
29 #include "SALOMEDS.hxx"
30 #include "SALOMEDS_Study.hxx"
31 #include "SALOMEDS_Study_i.hxx"
32 #include "SALOMEDS_StudyBuilder.hxx"
33 #include "SALOMEDS_SObject.hxx"
34 #include "SALOMEDS_SComponent.hxx"
35 #include "SALOMEDSClient.hxx"
36 #include "SALOMEDSClient_IParameters.hxx"
37 #include "SALOMEDS_IParameters.hxx"
38 #include "SALOME_Fake_NamingService.hxx"
39
40 #include "SALOMEDS_Defines.hxx"
41
42 #include <utilities.h>
43
44 // IDL headers
45 #include <SALOMEconfig.h>
46 #include CORBA_SERVER_HEADER(SALOMEDS)
47 #include <SALOME_NamingService.hxx>
48
49 // PAL8065: san -- Global recursive mutex for SALOMEDS methods
50 Utils_Mutex SALOMEDS::Locker::MutexDS;
51
52 // PAL8065: san -- Global SALOMEDS locker
53 SALOMEDS::Locker::Locker()
54 : Utils_Locker( &MutexDS )
55 {}
56
57 SALOMEDS::Locker::~Locker()
58 {}
59
60 void SALOMEDS::lock()
61 {
62   Locker::MutexDS.lock();
63 }
64
65 void SALOMEDS::unlock()
66 {
67   SALOMEDS::Locker::MutexDS.unlock();
68 }
69
70 // srn: Added new library methods that create basic SALOMEDS objects (Study, SComponent, SObject)
71
72 //=============================================================================
73 /*!
74  * C factory, accessible with dlsym, after dlopen
75  */
76 //=============================================================================
77
78
79 extern "C"
80 {
81   SALOMEDS_EXPORT
82   SALOMEDSClient_Study* StudyFactory(SALOMEDS::Study_ptr theStudy)
83   {
84     if(CORBA::is_nil(theStudy)) return NULL;
85     return new SALOMEDS_Study(theStudy);
86   }
87
88   SALOMEDS_EXPORT
89   SALOMEDSClient_SObject* SObjectFactory(SALOMEDS::SObject_ptr theSObject)
90   {
91     if(CORBA::is_nil(theSObject)) return NULL;
92     return new SALOMEDS_SObject(theSObject);
93   }
94
95   SALOMEDS_EXPORT
96   SALOMEDSClient_SComponent* SComponentFactory(SALOMEDS::SComponent_ptr theSComponent)
97   {
98     if(CORBA::is_nil(theSComponent)) return NULL;
99     return new SALOMEDS_SComponent(theSComponent);
100   }
101
102   SALOMEDS_EXPORT
103   SALOMEDSClient_StudyBuilder* BuilderFactory(SALOMEDS::StudyBuilder_ptr theBuilder)
104   {
105     if(CORBA::is_nil(theBuilder)) return NULL;
106     return new SALOMEDS_StudyBuilder(theBuilder);
107   }
108
109   void CreateStudyNSAbstract(CORBA::ORB_ptr orb, PortableServer::POA_ptr root_poa, SALOME_NamingService_Abstract& namingService)
110   {
111     CORBA::Object_var obj = namingService.Resolve( "/Study" );
112     SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow( obj );
113     if( CORBA::is_nil(aStudy) ) {
114       PortableServer::POAManager_var pman = root_poa->the_POAManager();
115       CORBA::PolicyList policies;
116       policies.length(2);
117       //PortableServer::ThreadPolicy_var threadPol(root_poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL));
118       PortableServer::ThreadPolicy_var threadPol(root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL));
119       PortableServer::ImplicitActivationPolicy_var implicitPol(root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION));
120       policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
121       policies[1] = PortableServer::ImplicitActivationPolicy::_duplicate(implicitPol);
122       PortableServer::POA_var poa = root_poa->create_POA("KERNELStudySingleThreadPOA",pman,policies);
123       MESSAGE("CreateStudy: KERNELStudySingleThreadPOA: "<< poa);
124       threadPol->destroy();
125
126       SALOMEDS_Study_i::SetThePOA(poa);
127       SALOMEDS_Study_i* aStudy_i = new SALOMEDS_Study_i(orb,&namingService);
128
129       // Activate the objects.  This tells the POA that the objects are ready to accept requests.
130       PortableServer::ObjectId_var aStudy_iid =  root_poa->activate_object(aStudy_i);
131       aStudy = aStudy_i->_this();
132       KERNEL::setStudyServantSA(aStudy,poa);
133       namingService.Register(aStudy, "/Study");
134       aStudy_i->GetImpl()->GetDocument()->SetModified(false);
135       aStudy_i->_remove_ref();
136     }
137   }
138
139   SALOMEDS_EXPORT
140   void CreateStudy(CORBA::ORB_ptr orb, PortableServer::POA_ptr root_poa)
141   {
142     SALOME_NamingService *namingService = new SALOME_NamingService(orb);
143     CreateStudyNSAbstract(orb,root_poa,*namingService);
144   }
145   
146   SALOMEDS_EXPORT
147   void CreateStudyWithoutNS(CORBA::ORB_ptr orb, PortableServer::POA_ptr root_poa)
148   {
149     SALOME_Fake_NamingService *namingService = new SALOME_Fake_NamingService(orb);
150     CreateStudyNSAbstract(orb,root_poa,*namingService);
151   }
152
153   SALOMEDS_EXPORT
154   SALOMEDSClient_IParameters* GetIParameters(const _PTR(AttributeParameter)& ap)
155   {
156     return new SALOMEDS_IParameters(ap);
157   }
158
159   SALOMEDS_EXPORT
160   SALOMEDS::SObject_ptr ConvertSObject(const _PTR(SObject)& theSObject)
161   {
162     SALOMEDS_SObject* so = _CAST(SObject, theSObject);
163     if ( !theSObject || !so )
164       return SALOMEDS::SObject::_nil();
165     return so->GetSObject();
166   }
167
168   SALOMEDS_EXPORT
169   SALOMEDS::StudyBuilder_ptr ConvertBuilder(const _PTR(StudyBuilder)& theBuilder)
170   {
171     SALOMEDS_StudyBuilder* builder = _CAST(StudyBuilder, theBuilder);
172     if ( !theBuilder || !builder )
173       return SALOMEDS::StudyBuilder::_nil();
174     return builder->GetBuilder();
175   }
176 }