Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/yacs.git] / src / ModuleCatalog / SALOME_ModuleCatalog_Acomponent_impl.cxx
1 //  SALOME ModuleCatalog : implementation of ModuleCatalog server which parsers xml description of modules
2 //
3 //  Copyright (C) 2003  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. 
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 //
24 //  File   : SALOME_ModuleCatalog_Acomponent_impl.cxx
25 //  Author : Estelle Deville
26 //  Module : SALOME
27 //  $Header$
28
29 #include "SALOME_ModuleCatalog_Acomponent_impl.hxx"
30
31 #include "Utils_ExceptHandlers.hxx"
32 UNEXPECT_CATCH(MC_NotFound, SALOME_ModuleCatalog::NotFound);
33
34 #include "utilities.h"
35
36 using namespace std;
37
38 #ifdef _DEBUG_
39 static int MYDEBUG = 0;
40 #else
41 static int MYDEBUG = 0;
42 #endif
43
44 //----------------------------------------------------------------------
45 // Function : SALOME_ModuleCatalog_AcomponentImpl
46 // Purpose  : Constructor
47 //            Affect the component name, type,icone,  a bool to define 
48 //            if it's multistudy or not.
49 //            Affect too the constraint and the interfaces of the component
50 //            and the pathes prefixes for all computers
51 //----------------------------------------------------------------------  
52 SALOME_ModuleCatalog_AcomponentImpl::SALOME_ModuleCatalog_AcomponentImpl
53 (SALOME_ModuleCatalog::ComponentDef &C) : _Component(C)
54 {
55   if(MYDEBUG) BEGIN_OF("SALOME_ModuleCatalog_AcomponentImpl");
56
57   if(MYDEBUG) END_OF("SALOME_ModuleCatalog_AcomponentImpl");
58 }
59
60 //----------------------------------------------------------------------
61 // Function : ~SALOME_ModuleCatalog_AcomponentImpl
62 // Purpose  : Destructor 
63 //----------------------------------------------------------------------
64 SALOME_ModuleCatalog_AcomponentImpl::~SALOME_ModuleCatalog_AcomponentImpl()
65 {
66   if(MYDEBUG) BEGIN_OF("~SALOME_ModuleCatalog_AcomponentImpl");
67
68
69   if(MYDEBUG) END_OF("~SALOME_ModuleCatalog_AcomponentImpl");
70 }
71
72 //----------------------------------------------------------------------
73 // Function : GetInterfaceList
74 // Purpose  : get a list of the interfaces name of a component
75 //----------------------------------------------------------------------
76 SALOME_ModuleCatalog::ListOfInterfaces* 
77 SALOME_ModuleCatalog_AcomponentImpl::GetInterfaceList() 
78 {
79   if(MYDEBUG) BEGIN_OF("GetInterfaceList");
80
81   SALOME_ModuleCatalog::ListOfInterfaces_var _list 
82     = new SALOME_ModuleCatalog::ListOfInterfaces;
83
84   // All the interfaces are defined in _list_interfaces affected at the
85   // component creation
86   unsigned int _length_interfaces = _Component.interfaces.length();
87
88   _list->length(_length_interfaces);
89
90   // Parse all the interfaces to get their name
91   for (unsigned int ind = 0; ind < _length_interfaces; ind++)
92     {
93       _list[ind] = CORBA::string_dup(_Component.interfaces[ind].interfacename);
94       if(MYDEBUG) MESSAGE("The component " << _Component.name 
95                           << " contains " << _list[ind] << " as interface");
96     }
97   
98   if(MYDEBUG) END_OF("GetInterfaceList");
99   return _list._retn();
100 }
101
102 //----------------------------------------------------------------------
103 // Function : GetInterface
104 // Purpose  : get one interface of a component
105 //----------------------------------------------------------------------
106 SALOME_ModuleCatalog::DefinitionInterface*
107 SALOME_ModuleCatalog_AcomponentImpl::GetInterface(const char* interfacename)
108                                      throw(SALOME_ModuleCatalog::NotFound)
109 {
110   if(MYDEBUG) BEGIN_OF("GetInterface");
111   if(MYDEBUG) SCRUTE(interfacename);
112
113   SALOME_ModuleCatalog::DefinitionInterface *_interface =
114           new SALOME_ModuleCatalog::DefinitionInterface;
115
116   bool _find = false ;
117   
118   // looking for the specified interface
119   for (unsigned int ind = 0; ind < _Component.interfaces.length(); ind++)
120     {
121       SALOME_ModuleCatalog::DefinitionInterface &I
122         = _Component.interfaces[ind];
123
124       if (strcmp(interfacename, I.interfacename) == 0)
125         {
126           // wanted interface
127           _find = true ;
128           duplicate(*_interface, I);
129         }
130     }
131
132   if(MYDEBUG) SCRUTE(_find);
133   if (!_find)
134     {
135       // The interface was not found, the exception should be thrown
136       string message = "The interface";
137       message += interfacename;
138       message += " of the component ";
139       message += _Component.name;
140       message += " was not found"; 
141       if(MYDEBUG) MESSAGE(message);
142       throw SALOME_ModuleCatalog::NotFound(message.c_str());
143     }
144
145   if(MYDEBUG) END_OF("GetInterface");
146
147   return _interface;
148 }
149
150
151
152 //----------------------------------------------------------------------
153 // Function : GetServiceList
154 // Purpose  : get a list of the services name of an interface 
155 //            of a component
156 //----------------------------------------------------------------------
157 SALOME_ModuleCatalog::ListOfServices* 
158 SALOME_ModuleCatalog_AcomponentImpl::GetServiceList(const char* interfacename)
159                                      throw(SALOME_ModuleCatalog::NotFound)
160 {
161   if(MYDEBUG) BEGIN_OF("GetServiceList");
162   if(MYDEBUG) SCRUTE(interfacename);
163
164   SALOME_ModuleCatalog::ListOfServices_var _list 
165     = new SALOME_ModuleCatalog::ListOfServices;
166
167   // Variables initialisation
168   bool _find = false ;
169
170   // looking for the specified interface
171   for (unsigned int ind = 0; ind < _Component.interfaces.length(); ind++)
172     {
173       SALOME_ModuleCatalog::DefinitionInterface & I = _Component.interfaces[ind];
174
175       if (strcmp(interfacename, I.interfacename) == 0)
176         {
177           _find = true ;
178           // wanted interface
179           // Get the list of services name for this interface
180           unsigned int _length_services = I.interfaceservicelist.length();
181           _list->length(_length_services);
182           for (unsigned int ind1 = 0; ind1 < _length_services ; ind1++)
183             {
184               _list[ind1] = CORBA::string_dup(I.interfaceservicelist[ind1].ServiceName);
185               if(MYDEBUG) MESSAGE("The interface " << interfacename << " of the component " 
186                                   << _Component.name << " contains " << _list[ind1] << " as a service") 
187             }
188         }
189     }
190
191  if (!_find)
192     {
193       // The interface was not found, the exception should be thrown
194       string message = "The interface";
195       message += interfacename;
196       message += " of the component ";
197       message += _Component.name;
198       message += " was not found"; 
199       if(MYDEBUG) MESSAGE(message);
200       throw SALOME_ModuleCatalog::NotFound(message.c_str());
201     }
202
203   if(MYDEBUG) END_OF("GetServiceList");
204   return _list._retn();
205 }
206
207     
208 //----------------------------------------------------------------------
209 // Function : GetService
210 // Purpose  : get one service of an interface of a component
211 //----------------------------------------------------------------------
212 SALOME_ModuleCatalog::Service* 
213 SALOME_ModuleCatalog_AcomponentImpl::GetService(const char* interfacename, 
214                                                 const char* servicename) 
215                                      throw(SALOME_ModuleCatalog::NotFound)
216 {
217   if(MYDEBUG) BEGIN_OF("GetService");
218   if(MYDEBUG) SCRUTE(interfacename);
219   if(MYDEBUG) SCRUTE(servicename);
220
221   Unexpect aCatch( MC_NotFound );
222   SALOME_ModuleCatalog::Service *service = new SALOME_ModuleCatalog::Service;
223
224   // Variables initialization
225   bool _find = false ;
226   
227
228   // looking for the specified interface
229   for (unsigned int ind = 0; ind < _Component.interfaces.length(); ind++)
230     {
231       if(MYDEBUG) SCRUTE(ind);
232       if(MYDEBUG) SCRUTE(_Component.interfaces[ind].interfacename);
233
234       SALOME_ModuleCatalog::DefinitionInterface &I = _Component.interfaces[ind];
235       if (strcmp(interfacename, I.interfacename) == 0)
236         {
237           // wanted interface
238           // looking for the specified service
239           for (unsigned int ind1 = 0; ind1 <  I.interfaceservicelist.length() ; ind1++)
240             {
241               SALOME_ModuleCatalog::Service &S = I.interfaceservicelist[ind1];
242               if(MYDEBUG) SCRUTE(ind1);
243               if(MYDEBUG) SCRUTE(S.ServiceName);
244
245               if (strcmp(servicename, S.ServiceName) == 0)
246               {
247                 // Wanted Service
248                 // Affect the service to be returned
249                 _find = true ;
250                 duplicate(*service, S);
251               }
252             }
253
254         }
255     }
256   
257   if(MYDEBUG) SCRUTE(_find);
258   if (!_find)
259     {
260       // The interface was not found, the exception should be thrown
261       string message = "The service";
262       message += servicename;
263       message += " of the interface ";
264       message += interfacename;
265       message += " of the component ";
266       message += _Component.name;
267       message += " was not found"; 
268       if(MYDEBUG) MESSAGE(message);
269       throw SALOME_ModuleCatalog::NotFound(message.c_str());
270     }
271
272   if(MYDEBUG) END_OF("GetService");
273   return service;
274 }
275
276 //----------------------------------------------------------------------
277 // Function : GetDefaultService
278 // Purpose  : get the default service of the interface
279 //----------------------------------------------------------------------
280 SALOME_ModuleCatalog::Service* 
281 SALOME_ModuleCatalog_AcomponentImpl::GetDefaultService(const char* interfacename) 
282                                      throw(SALOME_ModuleCatalog::NotFound)
283 {
284   if(MYDEBUG) BEGIN_OF("GetDefaultService");
285   if(MYDEBUG) SCRUTE(interfacename);
286
287   Unexpect aCatch( MC_NotFound );
288   SALOME_ModuleCatalog::Service *_service = new  SALOME_ModuleCatalog::Service;
289
290   // Variables initialisation
291   bool _find = false ;
292
293   // looking for the specified interface
294   for (unsigned int ind = 0; ind < _Component.interfaces.length(); ind++)
295     {
296       if (strcmp(interfacename, _Component.interfaces[ind].interfacename) == 0)
297         {
298           // wanted interface
299           // looking for the defautl service of the wanted interface
300           for (unsigned int ind1 = 0; ind1 <  _Component.interfaces[ind].interfaceservicelist.length() ; ind1++)
301             {
302               if (_Component.interfaces[ind].interfaceservicelist[ind1].Servicebydefault)
303               {
304                 // Default Service
305                 // affect the service to be returned
306                 _find = true ;
307                 duplicate(*_service, _Component.interfaces[ind].interfaceservicelist[ind1]);
308               }
309             }
310
311         }
312     }
313
314   if (!_find)
315     {
316       // The service was not found, the exception should be thrown
317       string message = "The default service of the interface ";
318       message += interfacename;
319       message += " of the component ";
320       message += _Component.name;
321       message += " was not found";
322       if(MYDEBUG) MESSAGE(message);
323       throw SALOME_ModuleCatalog::NotFound(message.c_str());
324     }
325
326   if(MYDEBUG) END_OF("GetDefaultService");
327   return _service;
328 }
329
330 //----------------------------------------------------------------------
331 // Function : GetPathPrefix
332 // Purpose  : get the PathPrefix of a computer
333 //----------------------------------------------------------------------
334 char* 
335 SALOME_ModuleCatalog_AcomponentImpl::GetPathPrefix(const char* machinename) 
336                                      throw(SALOME_ModuleCatalog::NotFound)
337 {
338   if(MYDEBUG) BEGIN_OF("GetPathPrefix");
339   if(MYDEBUG) SCRUTE(machinename);
340   Unexpect aCatch( MC_NotFound );
341
342  // Variables initialisation
343   char* _path = NULL;
344   bool _find = false ;
345
346   // Parse all the path prefixes
347   // looking for the wanted computer
348   for (unsigned int ind = 0 ; ind < _Component.paths.length() ; ind++)
349     {
350       if (strcmp(machinename, _Component.paths[ind].machine) == 0)
351             {
352               _find = true ;
353               // Wanted computer
354               // affect the path to be returned
355                 const char* _temp = _Component.paths[ind].path ;
356                 _path = new char[strlen(_temp)+1];
357                 strcpy(_path,_temp);
358             }
359      }
360
361    if(MYDEBUG) SCRUTE(_find);
362    if (!_find)
363      {
364        // The computer was not found, the exception should be thrown
365        string message = "The computer ";
366        message += machinename;
367        message += " was not found in the catalog associated to the component ";
368        message += _Component.name;
369        if(MYDEBUG) MESSAGE(message);
370        throw SALOME_ModuleCatalog::NotFound(message.c_str());
371      }
372
373   if(MYDEBUG) END_OF("GetPathPrefix");
374   return _path;
375 }
376
377 //----------------------------------------------------------------------
378 // Function : constraint
379 // Purpose  : obtain the constraint affected to a component
380 //----------------------------------------------------------------------
381 char* SALOME_ModuleCatalog_AcomponentImpl::constraint() 
382 {
383   return CORBA::string_dup(_Component.constraint);
384 }
385
386 //----------------------------------------------------------------------
387 // Function : componentname
388 // Purpose  : obtain the componentname
389 //----------------------------------------------------------------------
390 char* SALOME_ModuleCatalog_AcomponentImpl::componentname()
391 {
392   return CORBA::string_dup(_Component.name);
393 }
394
395 //----------------------------------------------------------------------
396 // Function : componentusername
397 // Purpose  : obtain the componentusername
398 //----------------------------------------------------------------------
399 char* SALOME_ModuleCatalog_AcomponentImpl::componentusername()
400 {
401   return CORBA::string_dup(_Component.username);
402 }
403
404 //----------------------------------------------------------------------
405 // Function : multistudy
406 // Purpose  : define if a component can be multistudy or not
407 //----------------------------------------------------------------------
408 CORBA::Boolean SALOME_ModuleCatalog_AcomponentImpl::multistudy()
409 {
410   return _Component.multistudy ;
411 }
412
413
414 //----------------------------------------------------------------------
415 // Function : implementation type
416 // Purpose  : return the implementation type :  C++ (dyn lib), Python (module) or executable
417 //----------------------------------------------------------------------
418 SALOME_ModuleCatalog::ImplType SALOME_ModuleCatalog_AcomponentImpl::implementation_type()
419 {
420   return _Component.implementationType ;
421 }
422
423 //----------------------------------------------------------------------
424 // Function : implementation name
425 // Purpose  : return the implementation name to exec if the default one is not convenient
426 //----------------------------------------------------------------------
427 char* SALOME_ModuleCatalog_AcomponentImpl::implementation_name()
428 {
429   return _Component.implname ;
430 }
431
432 //----------------------------------------------------------------------
433 // Function : component_type
434 // Purpose  : define the type of the component
435 //----------------------------------------------------------------------
436 SALOME_ModuleCatalog::ComponentType 
437 SALOME_ModuleCatalog_AcomponentImpl::component_type() 
438 {
439   return _Component.type;
440 }
441
442 //----------------------------------------------------------------------
443 // Function : icone
444 // Purpose  : obtain the icone affected to a component (for IAPP)
445 //----------------------------------------------------------------------
446 char* SALOME_ModuleCatalog_AcomponentImpl::component_icone() 
447 {
448   return CORBA::string_dup(_Component.icon);
449 }
450
451
452 void SALOME_ModuleCatalog_AcomponentImpl::duplicate
453 (SALOME_ModuleCatalog::ServicesParameter & P_out,
454  const SALOME_ModuleCatalog::ServicesParameter &P_in)
455 {
456   // duplicate parameter name
457   P_out.Parametername = CORBA::string_dup(P_in.Parametername);
458   
459   // duplicate parameter type
460   P_out.Parametertype = CORBA::string_dup(P_in.Parametertype);
461 }
462
463
464 void SALOME_ModuleCatalog_AcomponentImpl::duplicate
465 (SALOME_ModuleCatalog::ServicesDataStreamParameter & P_out,
466  const SALOME_ModuleCatalog::ServicesDataStreamParameter &P_in)
467 {
468   // duplicate parameter name
469   P_out.Parametername = CORBA::string_dup(P_in.Parametername);
470   
471   // duplicate parameter type
472   P_out.Parametertype = P_in.Parametertype;
473
474   // duplicate parameter dependency
475   P_out.Parameterdependency = P_in.Parameterdependency;
476
477 }
478
479 void SALOME_ModuleCatalog_AcomponentImpl::duplicate
480 (SALOME_ModuleCatalog::Service & S_out,
481  const SALOME_ModuleCatalog::Service &S_in)
482 {
483   // type of node
484   S_out.TypeOfNode = S_in.TypeOfNode;
485
486     // duplicate service name
487   S_out.ServiceName = CORBA::string_dup(S_in.ServiceName);
488   
489   // duplicate service by default
490   S_out.Servicebydefault = S_in.Servicebydefault;
491
492   unsigned int _length;
493
494   // duplicate in Parameters
495   _length = S_in.ServiceinParameter.length();
496   S_out.ServiceinParameter.length(_length);
497
498   for (unsigned int ind2 = 0; ind2 < _length ; ind2 ++)
499     duplicate(S_out.ServiceinParameter[ind2],
500               S_in.ServiceinParameter[ind2]);
501   
502   // duplicate out Parameters
503   _length = S_in.ServiceoutParameter.length();
504   S_out.ServiceoutParameter.length(_length);
505
506 #ifndef WNT  
507   for (unsigned int ind2 = 0; ind2 < _length ; ind2 ++)
508 #else
509   for (ind2 = 0; ind2 < _length ; ind2 ++)
510 #endif
511     duplicate(S_out.ServiceoutParameter[ind2],
512               S_in.ServiceoutParameter[ind2]);
513   
514   // duplicate in DataStreamParameters
515   _length = S_in.ServiceinDataStreamParameter.length();
516   S_out.ServiceinDataStreamParameter.length(_length);
517
518 #ifndef WNT
519   for (unsigned int ind2 = 0; ind2 < _length ; ind2 ++)
520 #else
521   for (ind2 = 0; ind2 < _length ; ind2 ++)
522 #endif
523     duplicate(S_out.ServiceinDataStreamParameter[ind2],
524               S_in.ServiceinDataStreamParameter[ind2]);
525   
526   // duplicate out DataStreamParameters
527   _length = S_in.ServiceoutDataStreamParameter.length();
528   if(MYDEBUG) SCRUTE(_length);
529   S_out.ServiceoutDataStreamParameter.length(_length);
530   
531 #ifndef WNT
532   for (unsigned int ind2 = 0; ind2 < _length ; ind2 ++)
533 #else
534   for (ind2 = 0; ind2 < _length ; ind2 ++)
535 #endif
536     duplicate(S_out.ServiceoutDataStreamParameter[ind2],
537               S_in.ServiceoutDataStreamParameter[ind2]);
538 }
539
540
541 void SALOME_ModuleCatalog_AcomponentImpl::duplicate
542 (SALOME_ModuleCatalog::DefinitionInterface & I_out,
543  const SALOME_ModuleCatalog::DefinitionInterface & I_in)
544 {
545   //duplicate interface name
546   I_out.interfacename = CORBA::string_dup(I_in.interfacename);
547   
548   // duplicate service list
549   unsigned int _length = I_in.interfaceservicelist.length();
550   if(MYDEBUG) SCRUTE(_length);
551   I_out.interfaceservicelist.length(_length);
552   
553   for (unsigned int ind1 = 0; ind1 < _length ; ind1 ++)
554     duplicate(I_out.interfaceservicelist[ind1],
555               I_in.interfaceservicelist[ind1]);
556 }
557
558