]> granicus.if.org Git - icinga2/commitdiff
Refactored config object handling.
authorGunnar Beutner <gunnar.beutner@netways.de>
Sun, 1 Apr 2012 17:32:41 +0000 (19:32 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Sun, 1 Apr 2012 17:32:41 +0000 (19:32 +0200)
base/application.cpp
base/application.h
base/configobject.cpp
base/configobject.h
configfilecomponent/configfilecomponent.cpp
configrpccomponent/configrpccomponent.cpp
icinga/icingaapplication.cpp

index cee433e4f47f37781b204c2edbc3be09331a07de..c675a9e691b5145edae3ae01cf2bd836b838cc44 100644 (file)
@@ -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<ConfigObject>();
-               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());
 
index a71e883fca81c611f3df36483d5face2b523788f..07eee7941233f2492841edfe67c568ea2f517998 100644 (file)
@@ -36,7 +36,7 @@ public:
 
        ConfigHive::RefType GetConfigHive(void);
 
-       shared_ptr<Component> LoadComponent(string name);
+       shared_ptr<Component> LoadComponent(string path, ConfigObject::RefType componentConfig);
        void UnloadComponent(string name);
        shared_ptr<Component> GetComponent(string name);
 };
index 838d19a9efbe42028ffead7d098bae910ad605d9..e2b564b6bf8cd2ba55cc015e7117ace3ca683b85 100644 (file)
@@ -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<ConfigObject>(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<string, string>::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;
 }
index 78bfc8c31296cf91c15ebdd62a2911e16b10f707..2ee0f5a995bc5715a4621d11de5bca8dd4fd595e 100644 (file)
@@ -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;
 };
 
 }
index 4c10c0d259ef5ee79bb7edafdea3bb72407cf990..34438e6fd09b6428904d0e6b8016e93396641861 100644 (file)
@@ -16,7 +16,11 @@ void ConfigFileComponent::Start(void)
        ifstream fp;
        FIFO::RefType fifo = new_object<FIFO>();
 
-       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"*/);
        
index e4be5255e00d0d7a35deb4ccbd0e02bd5d77131a..266eda1a4a1c9f2b32360451e5513ff86dc3ef0c 100644 (file)
@@ -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();
index 79e383e8b5c0180b96183af98d36028676df178d..ed68b8f73cdff69662413845b99ff14c8637b6fe 100644 (file)
@@ -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;