1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
11 #include <anna/core/tracing/Logger.hpp>
13 #include <anna/app/Component.hpp>
14 #include <anna/app/functions.hpp>
16 #include <anna/xml/Node.hpp>
17 #include <anna/xml/Attribute.hpp>
22 app::Component::Component(const char* className) :
23 anna::Component(className),
24 a_state(State::Stopped) {
25 Application& app(app::functions::getApp());
27 if(app.a_running == false) {
30 } catch(RuntimeException& ex) {
33 } else if(Logger::isActive(Logger::Warning) == true) {
34 string msg(asString());
35 msg += " | Application already running. You may initialize this component manually (lazy initialization)";
36 Logger::warning(msg, ANNA_FILE_LOCATION);
40 app::Component::~Component() {
42 functions::getApp().detach(this);
43 } catch(RuntimeException& ex) {
48 void app::Component::addPredecessor(const char* componentName)
50 const std::string name(componentName);
52 if((find(begin(), end(), name)) != end())
55 a_predecessors.push_back(name);
57 string msg("anna::app::Component::addPredecessor | ");
59 msg += " | Requires: ";
61 Logger::debug(msg, ANNA_FILE_LOCATION);
65 void app::Component::initialize()
66 throw(RuntimeException) {
67 if(a_state == State::Running)
70 a_state = State::Starting;
71 Application& app = functions::getApp();
72 Component* predecessor;
75 for(iterator ii = begin(), maxii = end(); ii != maxii; ii ++) {
76 const std::string& name = data(ii);
78 if((predecessor = app.find(name.c_str())) == NULL) {
79 string msg("anna::app::Component::initialize | ");
81 msg += " | Requires component '";
84 throw RuntimeException(msg, ANNA_FILE_LOCATION);
87 if(predecessor->a_state == State::Starting) {
88 string msg("anna::app::Component::initialize | ");
90 msg += " | Cyclic dependency with '";
93 throw RuntimeException(msg, ANNA_FILE_LOCATION);
96 predecessor->initialize();
100 a_state = State::Running;
101 } catch(RuntimeException&) {
102 a_state = State::Stopped;
107 void app::Component::attach()
108 throw(RuntimeException) {
109 app::functions::getApp().attach(this);
112 std::string app::Component::asString() const
114 return anna::Component::asString();
117 xml::Node* app::Component::asXML(xml::Node* parent) const
119 return anna::Component::asXML(parent);