]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/ServiceNode.cxx
Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[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         {
51           try
52             {
53               _component->load();
54             }
55           catch(Exception& e)
56             {
57               _errorDetails=e.what();
58               throw e;
59             }
60         }
61     }
62   else
63     {
64       std::string what("ServiceNode::load : a load operation requested on ServiceNode called \"");
65       what+=_name; what+="\" with no component specified.";
66       _errorDetails=what;
67       throw Exception(what);
68     }
69 }
70
71 void ServiceNode::accept(Visitor *visitor)
72 {
73   visitor->visitServiceNode(this);
74 }
75
76 //! Return the associated component instance
77 ComponentInstance *ServiceNode::getComponent()
78 {
79   return _component;
80 }
81
82 //! By definition of ServiceNode class.
83 bool ServiceNode::isDeployable() const
84 {
85   return true;
86 }
87
88 //! Associate an existing component instance to this service node \b AND check the consistency regarding the deployment from root node point of view.
89 void ServiceNode::setComponent(ComponentInstance* compo) throw(Exception)
90 {
91   if(compo)
92     if(compo->getKind() != this->getKind())
93       {
94         //Not allowed
95         std::string what("ServiceNode::setComponent : component instance kind not allowed ");
96         throw Exception(what);
97       }
98   if(_component)
99     {
100       //The node is already associated with a component instance
101       _component->decrRef();
102       //Don't forget to unassociate
103     }
104   _component=compo;
105   if(_component)
106     {
107       if(_father)
108         try
109           {
110             getRootNode()->checkDeploymentTree(false);
111           }
112         catch(Exception& e)
113           {
114             _component=0;
115             throw e;
116           }
117       _component->incrRef();
118     }
119   //assert(_component);
120 }
121
122 //! Associate a new component instance to this service node
123 /*!
124  * A new component instance with type name ref is created (from runtime
125  * factory createComponentInstance) and associated to the node.
126  *
127  */
128 void ServiceNode::setRef(const std::string& ref)
129 {
130   _ref = ref;
131   if(_component)
132     {
133       //The node is already associated with a component instance
134       _component->decrRef();
135       //Don't forget to unassociate
136     }
137   _component= getRuntime()->createComponentInstance(ref,getKind());
138   assert(_component);
139 }
140
141 std::string ServiceNode::getRef()
142 {
143   return _ref;
144 }