]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/ServiceNode.cxx
Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / engine / ServiceNode.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 "ServiceNode.hxx"
20 #include "Visitor.hxx"
21 #include "ComponentInstance.hxx"
22 #include "ComposedNode.hxx"
23 #include "Runtime.hxx"
24 #include <iostream>
25 #include <cassert>
26
27 //#define _DEVDEBUG_
28 #include "YacsTrace.hxx"
29
30 using namespace YACS::ENGINE;
31
32 const char ServiceNode::KIND[]="";
33
34 std::string ServiceNode::getKind() const
35 {
36   return KIND;
37 }
38
39 ServiceNode::ServiceNode(const std::string& name):ElementaryNode(name),_component(0)
40
41 }
42
43 ServiceNode::ServiceNode(const ServiceNode& other, ComposedNode *father)
44            :ElementaryNode(other,father),_method(other._method),_component(0)
45 {
46 }
47
48 void ServiceNode::performDuplicationOfPlacement(const Node& other)
49 {
50   const ServiceNode &otherC=*(dynamic_cast<const ServiceNode *>(&other));
51   //if other has no component don't clone: this will not have one
52   if(otherC._component)
53     _component=otherC._component->clone();
54 }
55
56 ServiceNode::~ServiceNode()
57 {
58   if(_component)
59     _component->decrRef();
60 }
61
62 //! Load the component associated to the node
63 void ServiceNode::load()
64 {
65   if(_component)
66     {
67       if(!_component->isLoaded())
68         {
69           try
70             {
71               _component->load();
72             }
73           catch(Exception& e)
74             {
75               _errorDetails=e.what();
76               throw e;
77             }
78         }
79     }
80   else
81     {
82       std::string what("ServiceNode::load : a load operation requested on ServiceNode called \"");
83       what+=_name; what+="\" with no component specified.";
84       _errorDetails=what;
85       throw Exception(what);
86     }
87 }
88
89 void ServiceNode::accept(Visitor *visitor)
90 {
91   visitor->visitServiceNode(this);
92 }
93
94 //! Return the associated component instance
95 ComponentInstance *ServiceNode::getComponent()
96 {
97   return _component;
98 }
99
100 //! By definition of ServiceNode class.
101 bool ServiceNode::isDeployable() const
102 {
103   return true;
104 }
105
106 //! Associate an existing component instance to this service node \b AND check the consistency regarding the deployment from root node point of view.
107 void ServiceNode::setComponent(ComponentInstance* compo) throw(Exception)
108 {
109   if(compo)
110     if(compo->getKind() != this->getKind())
111       {
112         //Not allowed
113         std::string what("ServiceNode::setComponent : component instance kind not allowed ");
114         throw Exception(what);
115       }
116   if(_component)
117     {
118       //The node is already associated with a component instance
119       _component->decrRef();
120       //Don't forget to unassociate
121     }
122   _component=compo;
123   if(_component)
124     {
125       if(_father)
126         try
127           {
128             getRootNode()->checkDeploymentTree(false);
129           }
130         catch(Exception& e)
131           {
132             _component=0;
133             throw e;
134           }
135       _component->incrRef();
136     }
137   //assert(_component);
138 }
139
140 //! Associate a new component instance to this service node
141 /*!
142  * A new component instance with type name ref is created (from runtime
143  * factory createComponentInstance) and associated to the node.
144  *
145  */
146 void ServiceNode::setRef(const std::string& ref)
147 {
148   _ref = ref;
149   if(_component)
150     {
151       //The node is already associated with a component instance
152       _component->decrRef();
153       //Don't forget to unassociate
154     }
155   _component= getRuntime()->createComponentInstance(ref,getKind());
156   assert(_component);
157 }
158
159 std::string ServiceNode::getRef()
160 {
161   return _ref;
162 }