Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / runtime / SalomeComponent.cxx
1 //  Copyright (C) 2006-2008  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "RuntimeSALOME.hxx"
20 #include "SalomeComponent.hxx"
21 #include "SalomeContainer.hxx"
22 #include "CORBANode.hxx"
23
24 #ifdef SALOME_KERNEL
25 #include "SALOME_NamingService.hxx"
26 #include "SALOME_LifeCycleCORBA.hxx"
27 #endif
28
29 #include <omniORB4/CORBA.h>
30 #include <iostream>
31 #include <sstream>
32
33 using namespace YACS::ENGINE;
34 using namespace std;
35
36 const char SalomeComponent::KIND[]="Salome";
37
38 //! SalomeComponent constructor
39 SalomeComponent::SalomeComponent(const std::string& name): ComponentInstance(name)
40 {
41   _objComponent=CORBA::Object::_nil();
42 }
43
44 //! SalomeComponent copy constructor
45 SalomeComponent::SalomeComponent(const SalomeComponent& other):ComponentInstance(other)
46 {
47   _objComponent=CORBA::Object::_nil();
48 }
49
50 SalomeComponent::~SalomeComponent()
51 {
52 }
53
54 std::string SalomeComponent::getKind() const
55 {
56   return KIND;
57 }
58
59 //! Unload the component 
60 void SalomeComponent::unload()
61 {
62   //Not implemented
63   std::cerr << "SalomeComponent::unload : not implemented " << std::endl;
64 }
65
66 //! Is the component instance already loaded ?
67 bool SalomeComponent::isLoaded()
68 {
69   if(CORBA::is_nil(_objComponent))
70     return false;
71   else
72     return true;
73 }
74
75 #ifdef SALOME_KERNEL
76 //! Load the component 
77 void SalomeComponent::load()
78 {
79   if(_container)
80     {
81       SalomeContainer *containerC=(SalomeContainer *)_container;
82       containerC->lock();//To be sure
83       if(!_container->isAlreadyStarted())
84         {
85           try
86             {
87               _container->start();
88             }
89           catch(Exception& e)
90             {
91               containerC->unLock();
92               throw e;
93             }
94         }
95       containerC->unLock();
96       containerC->lock();//To be sure
97       const char* componentName=_compoName.c_str();
98       //char *val2=CORBA::string_dup("");
99       // does not work with python components
100       // does not make a strict load but a find or load component
101       //   _objComponent=containerC->_trueCont->load_impl(componentName,val2);
102       bool isLoadable = containerC->_trueCont->load_component_Library(componentName);
103       if (isLoadable) 
104         if (containerC->isAPaCOContainer()) {
105           std::string compo_paco_name(componentName);
106           compo_paco_name = compo_paco_name + "@PARALLEL@";
107           char * c_paco_name = CORBA::string_dup(compo_paco_name.c_str());
108           _objComponent=containerC->_trueCont->create_component_instance(c_paco_name, 0);
109         }
110         else
111           _objComponent=containerC->_trueCont->create_component_instance(componentName, 0);
112
113       if(CORBA::is_nil(_objComponent))
114         {
115           containerC->unLock();
116           std::string text="Error while trying to create a new component: component '"+ _compoName;
117           text=text+"' is not installed or it's a wrong name";
118           throw Exception(text);
119         }
120       containerC->unLock();
121       return;
122     }
123   //throw Exception("SalomeComponent::load : no container specified !!! To be implemented in executor to allocate default a Container in case of presenceOfDefaultContainer.");
124   //This component has no specified container : use default container policy
125   SALOME_NamingService ns(getSALOMERuntime()->getOrb());
126   SALOME_LifeCycleCORBA LCC(&ns);
127   Engines::MachineParameters params;
128   LCC.preSet(params);
129   params.hostname="localhost";
130   params.container_name ="FactoryServer";
131   _objComponent=LCC.LoadComponent(params,_compoName.c_str());
132 }
133 #else
134 void SalomeComponent::load()
135 {
136   throw Exception("YACS has been built without SALOME support");
137 }
138 #endif
139
140 //! Create a ServiceNode with this component instance and no input or output port
141 /*!
142  *   \param name : node name
143  *   \return       a new SalomeNode node
144  */
145 ServiceNode* SalomeComponent::createNode(const std::string& name)
146 {
147    SalomeNode* node=new SalomeNode(name);
148    node->setComponent(this);
149    return node;
150 }
151
152 //! Clone the component instance
153 ComponentInstance* SalomeComponent::clone() const
154 {
155   if(_isAttachedOnCloning)
156     {
157       incrRef();
158       return (ComponentInstance*) (this);
159     }
160   else
161     return new SalomeComponent(*this);
162 }
163
164 std::string SalomeComponent::getFileRepr() const
165 {
166   ostringstream stream;
167   stream << "<component>" << getCompoName() << "</component>";
168   return stream.str();
169 }
170
171 void SalomeComponent::setContainer(Container *cont)
172 {
173   if (cont == _container) return;
174
175   if(cont)
176     cont->checkCapabilityToDealWith(this);
177
178   if(_container)
179     _container->decrRef();
180   _container=cont;
181   if(_container)
182   {
183     _container->incrRef();
184     ((SalomeContainer*)_container)->addComponentName(_compoName);
185   }
186 }
187