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