diff --git a/base/application.cpp b/base/application.cpp index cee433e4f..c675a9e69 100644 --- a/base/application.cpp +++ b/base/application.cpp @@ -171,28 +171,12 @@ ConfigHive::RefType Application::GetConfigHive(void) return m_ConfigHive; } -Component::RefType Application::LoadComponent(string name) +Component::RefType Application::LoadComponent(string path, ConfigObject::RefType componentConfig) { Component::RefType component; Component *(*pCreateComponent)(); - component = GetComponent(name); - - if (component.get() != NULL) - return component; - - Log("Loading component '%s'", name.c_str()); - - ConfigObject::RefType componentConfig = m_ConfigHive->GetObject("component", name); - - if (componentConfig.get() == NULL) { - componentConfig = new_object(); - componentConfig->SetName(name); - componentConfig->SetType("component"); - m_ConfigHive->AddObject(componentConfig); - } - - string path = componentConfig->GetProperty("path", name); + Log("Loading component '%s'", path.c_str()); HMODULE hModule = LoadLibrary(path.c_str()); diff --git a/base/application.h b/base/application.h index a71e883fc..07eee7941 100644 --- a/base/application.h +++ b/base/application.h @@ -36,7 +36,7 @@ public: ConfigHive::RefType GetConfigHive(void); - shared_ptr LoadComponent(string name); + shared_ptr LoadComponent(string path, ConfigObject::RefType componentConfig); void UnloadComponent(string name); shared_ptr GetComponent(string name); }; diff --git a/base/configobject.cpp b/base/configobject.cpp index 838d19a9e..e2b564b6b 100644 --- a/base/configobject.cpp +++ b/base/configobject.cpp @@ -34,8 +34,6 @@ string ConfigObject::GetType(void) const void ConfigObject::SetProperty(const string& name, const string& value) { - string oldValue = GetProperty(name); - Properties[name] = value; ConfigHive::RefType hive = m_Hive.lock(); @@ -44,31 +42,38 @@ void ConfigObject::SetProperty(const string& name, const string& value) ea->Source = hive; ea->Object = static_pointer_cast(shared_from_this()); ea->Property = name; - ea->OldValue = oldValue; + + string oldValue; + if (GetProperty(name, &oldValue)) + ea->OldValue = oldValue; + hive->OnPropertyChanged(ea); } } -string ConfigObject::GetProperty(const string& name, const string& defaultValue) const +bool ConfigObject::GetProperty(const string& name, string *value) const { map::const_iterator vi = Properties.find(name); if (vi == Properties.end()) - return defaultValue; - return vi->second; + return false; + *value = vi->second; + return true; } -int ConfigObject::GetPropertyInteger(const string& name, int defaultValue) const +bool ConfigObject::GetPropertyInteger(const string& name, int *value) const { - string value = GetProperty(name); - if (value == string()) - return defaultValue; - return strtol(value.c_str(), NULL, 10); + string stringValue; + if (!GetProperty(name, &stringValue)) + return false; + *value = strtol(stringValue.c_str(), NULL, 10); + return true; } -double ConfigObject::GetPropertyDouble(const string& name, double defaultValue) const +bool ConfigObject::GetPropertyDouble(const string& name, double *value) const { - string value = GetProperty(name); - if (value == string()) - return defaultValue; - return strtod(value.c_str(), NULL); + string stringValue; + if (!GetProperty(name, &stringValue)) + return false; + *value = strtod(stringValue.c_str(), NULL); + return true; } diff --git a/base/configobject.h b/base/configobject.h index 78bfc8c31..2ee0f5a99 100644 --- a/base/configobject.h +++ b/base/configobject.h @@ -39,9 +39,9 @@ public: void SetPropertyInteger(const string& name, int value); void SetPropertyDouble(const string& name, double value); - string GetProperty(const string& name, const string& defaultValue = string()) const; - int GetPropertyInteger(const string& name, int defaultValue = 0) const; - double GetPropertyDouble(const string& name, double defaultValue = 0.0f) const; + bool GetProperty(const string& name, string *value) const; + bool GetPropertyInteger(const string& name, int *value) const; + bool GetPropertyDouble(const string& name, double *value) const; }; } diff --git a/configfilecomponent/configfilecomponent.cpp b/configfilecomponent/configfilecomponent.cpp index 4c10c0d25..34438e6fd 100644 --- a/configfilecomponent/configfilecomponent.cpp +++ b/configfilecomponent/configfilecomponent.cpp @@ -16,7 +16,11 @@ void ConfigFileComponent::Start(void) ifstream fp; FIFO::RefType fifo = new_object(); - fp.open(GetConfig()->GetProperty("filename").c_str(), ifstream::in); + string filename; + if (!GetConfig()->GetProperty("filename", &filename)) + throw exception(/*"Missing filename property"*/); + + fp.open(filename.c_str(), ifstream::in); if (fp.fail()) throw exception(/*"Could not open config file"*/); diff --git a/configrpccomponent/configrpccomponent.cpp b/configrpccomponent/configrpccomponent.cpp index e4be5255e..266eda1a4 100644 --- a/configrpccomponent/configrpccomponent.cpp +++ b/configrpccomponent/configrpccomponent.cpp @@ -23,7 +23,8 @@ void ConfigRpcComponent::Start(void) ConnectionManager::RefType connectionManager = icingaApp->GetConnectionManager(); ConfigHive::RefType configHive = icingaApp->GetConfigHive(); - if (GetConfig()->GetPropertyInteger("configSource") != 0) { + int configSource; + if (GetConfig()->GetPropertyInteger("configSource", &configSource) && configSource != 0) { connectionManager->RegisterMethod("config::FetchObjects", bind_weak(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this())); configHive->OnObjectCreated.bind(bind_weak(&ConfigRpcComponent::LocalObjectCreatedHandler, shared_from_this())); @@ -99,7 +100,8 @@ int ConfigRpcComponent::LocalPropertyChangedHandler(ConfigHiveEventArgs::RefType JsonRpcMessage::RefType msg = MakeObjectMessage(ea->Object, "config::ObjectRemoved", false); cJSON *params = msg->GetParams(); cJSON_AddStringToObject(params, "property", ea->Property.c_str()); - string value = ea->Object->GetProperty(ea->Property); + string value; + ea->Object->GetProperty(ea->Property, &value); cJSON_AddStringToObject(params, "value", value.c_str()); ConnectionManager::RefType connectionManager = GetIcingaApplication()->GetConnectionManager(); diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index 79e383e8b..ed68b8f73 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -36,7 +36,12 @@ ConnectionManager::RefType IcingaApplication::GetConnectionManager(void) int IcingaApplication::ConfigObjectCreatedHandler(ConfigHiveEventArgs::RefType ea) { if (ea->Object->GetType() == "component") { - LoadComponent(ea->Object->GetName()); + string path; + + if (!ea->Object->GetProperty("path", &path)) + throw exception(/*"Missing path property"*/); + + LoadComponent(path, ea->Object); } return 0;