Refactored config object handling.

This commit is contained in:
Gunnar Beutner 2012-04-01 19:32:41 +02:00
parent 15a5c3d233
commit eb34b40d40
7 changed files with 42 additions and 42 deletions

View File

@ -171,28 +171,12 @@ ConfigHive::RefType Application::GetConfigHive(void)
return m_ConfigHive; return m_ConfigHive;
} }
Component::RefType Application::LoadComponent(string name) Component::RefType Application::LoadComponent(string path, ConfigObject::RefType componentConfig)
{ {
Component::RefType component; Component::RefType component;
Component *(*pCreateComponent)(); Component *(*pCreateComponent)();
component = GetComponent(name); Log("Loading component '%s'", path.c_str());
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);
HMODULE hModule = LoadLibrary(path.c_str()); HMODULE hModule = LoadLibrary(path.c_str());

View File

@ -36,7 +36,7 @@ public:
ConfigHive::RefType GetConfigHive(void); ConfigHive::RefType GetConfigHive(void);
shared_ptr<Component> LoadComponent(string name); shared_ptr<Component> LoadComponent(string path, ConfigObject::RefType componentConfig);
void UnloadComponent(string name); void UnloadComponent(string name);
shared_ptr<Component> GetComponent(string name); shared_ptr<Component> GetComponent(string name);
}; };

View File

@ -34,8 +34,6 @@ string ConfigObject::GetType(void) const
void ConfigObject::SetProperty(const string& name, const string& value) void ConfigObject::SetProperty(const string& name, const string& value)
{ {
string oldValue = GetProperty(name);
Properties[name] = value; Properties[name] = value;
ConfigHive::RefType hive = m_Hive.lock(); ConfigHive::RefType hive = m_Hive.lock();
@ -44,31 +42,38 @@ void ConfigObject::SetProperty(const string& name, const string& value)
ea->Source = hive; ea->Source = hive;
ea->Object = static_pointer_cast<ConfigObject>(shared_from_this()); ea->Object = static_pointer_cast<ConfigObject>(shared_from_this());
ea->Property = name; ea->Property = name;
ea->OldValue = oldValue;
string oldValue;
if (GetProperty(name, &oldValue))
ea->OldValue = oldValue;
hive->OnPropertyChanged(ea); 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); map<string, string>::const_iterator vi = Properties.find(name);
if (vi == Properties.end()) if (vi == Properties.end())
return defaultValue; return false;
return vi->second; *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); string stringValue;
if (value == string()) if (!GetProperty(name, &stringValue))
return defaultValue; return false;
return strtol(value.c_str(), NULL, 10); *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); string stringValue;
if (value == string()) if (!GetProperty(name, &stringValue))
return defaultValue; return false;
return strtod(value.c_str(), NULL); *value = strtod(stringValue.c_str(), NULL);
return true;
} }

View File

@ -39,9 +39,9 @@ public:
void SetPropertyInteger(const string& name, int value); void SetPropertyInteger(const string& name, int value);
void SetPropertyDouble(const string& name, double value); void SetPropertyDouble(const string& name, double value);
string GetProperty(const string& name, const string& defaultValue = string()) const; bool GetProperty(const string& name, string *value) const;
int GetPropertyInteger(const string& name, int defaultValue = 0) const; bool GetPropertyInteger(const string& name, int *value) const;
double GetPropertyDouble(const string& name, double defaultValue = 0.0f) const; bool GetPropertyDouble(const string& name, double *value) const;
}; };
} }

View File

@ -16,7 +16,11 @@ void ConfigFileComponent::Start(void)
ifstream fp; ifstream fp;
FIFO::RefType fifo = new_object<FIFO>(); 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()) if (fp.fail())
throw exception(/*"Could not open config file"*/); throw exception(/*"Could not open config file"*/);

View File

@ -23,7 +23,8 @@ void ConfigRpcComponent::Start(void)
ConnectionManager::RefType connectionManager = icingaApp->GetConnectionManager(); ConnectionManager::RefType connectionManager = icingaApp->GetConnectionManager();
ConfigHive::RefType configHive = icingaApp->GetConfigHive(); 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())); connectionManager->RegisterMethod("config::FetchObjects", bind_weak(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this()));
configHive->OnObjectCreated.bind(bind_weak(&ConfigRpcComponent::LocalObjectCreatedHandler, 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); JsonRpcMessage::RefType msg = MakeObjectMessage(ea->Object, "config::ObjectRemoved", false);
cJSON *params = msg->GetParams(); cJSON *params = msg->GetParams();
cJSON_AddStringToObject(params, "property", ea->Property.c_str()); 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()); cJSON_AddStringToObject(params, "value", value.c_str());
ConnectionManager::RefType connectionManager = GetIcingaApplication()->GetConnectionManager(); ConnectionManager::RefType connectionManager = GetIcingaApplication()->GetConnectionManager();

View File

@ -36,7 +36,12 @@ ConnectionManager::RefType IcingaApplication::GetConnectionManager(void)
int IcingaApplication::ConfigObjectCreatedHandler(ConfigHiveEventArgs::RefType ea) int IcingaApplication::ConfigObjectCreatedHandler(ConfigHiveEventArgs::RefType ea)
{ {
if (ea->Object->GetType() == "component") { 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; return 0;