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