Salome HOME
Update copyrights 2014.
[modules/yacs.git] / src / runtime / SalomeComponent.cxx
1 // Copyright (C) 2006-2014  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, or (at your option) any later version.
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
20 #include "RuntimeSALOME.hxx"
21 #include "SalomeComponent.hxx"
22 #include "SalomeContainer.hxx"
23 #include "CORBANode.hxx"
24
25 #ifdef SALOME_KERNEL
26 #include "SALOME_NamingService.hxx"
27 #include "SALOME_LifeCycleCORBA.hxx"
28 #endif
29
30 #include <omniORB4/CORBA.h>
31 #include <iostream>
32 #include <sstream>
33
34 //#define _DEVDEBUG_
35 #include "YacsTrace.hxx"
36
37 using namespace YACS::ENGINE;
38 using namespace std;
39
40 const char SalomeComponent::KIND[]="Salome";
41
42 //! SalomeComponent constructor
43 SalomeComponent::SalomeComponent(const std::string& name): ComponentInstance(name)
44 {
45   _objComponent=CORBA::Object::_nil();
46 }
47
48 //! SalomeComponent copy constructor
49 SalomeComponent::SalomeComponent(const SalomeComponent& other):ComponentInstance(other)
50 {
51   _objComponent=CORBA::Object::_nil();
52 }
53
54 SalomeComponent::~SalomeComponent()
55 {
56 }
57
58 std::string SalomeComponent::getKind() const
59 {
60   return KIND;
61 }
62
63 //! Unload the component 
64 void SalomeComponent::unload()
65 {
66   //Not implemented
67   std::cerr << "SalomeComponent::unload : not implemented " << std::endl;
68 }
69
70 //! Is the component instance already loaded ?
71 bool SalomeComponent::isLoaded()
72 {
73   if(CORBA::is_nil(_objComponent))
74     return false;
75   else
76     return true;
77 }
78
79 #ifdef SALOME_KERNEL
80 //! Load the component 
81 void SalomeComponent::load()
82 {
83   if(_container)
84     {
85       _objComponent=((SalomeContainer*)_container)->loadComponent(this);
86       return;
87     }
88   //throw Exception("SalomeComponent::load : no container specified !!! To be implemented in executor to allocate default a Container in case of presenceOfDefaultContainer.");
89   //This component has no specified container : use default container policy
90   SALOME_NamingService ns(getSALOMERuntime()->getOrb());
91   SALOME_LifeCycleCORBA LCC(&ns);
92   Engines::ContainerParameters params;
93   LCC.preSet(params);
94   params.resource_params.name = "localhost";
95   params.container_name ="FactoryServer";
96   _objComponent=LCC.LoadComponent(params,_compoName.c_str());
97 }
98 #else
99 void SalomeComponent::load()
100 {
101   throw Exception("YACS has been built without SALOME support");
102 }
103 #endif
104
105 //! Create a ServiceNode with this component instance and no input or output port
106 /*!
107  *   \param name : node name
108  *   \return       a new SalomeNode node
109  */
110 ServiceNode* SalomeComponent::createNode(const std::string& name)
111 {
112    SalomeNode* node=new SalomeNode(name);
113    node->setComponent(this);
114    return node;
115 }
116
117 //! Clone the component instance
118 ComponentInstance* SalomeComponent::clone() const
119 {
120   if(_isAttachedOnCloning)
121     {
122       incrRef();
123       return (ComponentInstance*) (this);
124     }
125   else
126     return new SalomeComponent(*this);
127 }
128
129 std::string SalomeComponent::getFileRepr() const
130 {
131   ostringstream stream;
132   stream << "<component>" << getCompoName() << "</component>";
133   return stream.str();
134 }
135
136 void SalomeComponent::setContainer(Container *cont)
137 {
138   if (cont == _container) return;
139
140   if(cont)
141     cont->checkCapabilityToDealWith(this);
142
143   if(_container)
144     _container->decrRef();
145   _container=cont;
146   if(_container)
147   {
148     _container->incrRef();
149     ((SalomeContainer*)_container)->addComponentName(_compoName);
150   }
151 }
152
153 void SalomeComponent::shutdown(int level)
154 {
155   DEBTRACE("SalomeComponent::shutdown " << level);
156   if(_container)
157     _container->shutdown(level);
158 }