icinga2/base/configobject.cpp

212 lines
5.5 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
2012-05-11 13:33:57 +02:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "i2-base.h"
using namespace icinga;
ConfigObject::ConfigObject(Dictionary::Ptr properties, const ConfigObject::Set::Ptr& container)
: m_Container(container ? container : GetAllObjects()),
2012-06-15 19:32:41 +02:00
m_Properties(properties), m_Tags(boost::make_shared<Dictionary>())
{ }
ConfigObject::ConfigObject(string type, string name, const ConfigObject::Set::Ptr& container)
: m_Container(container ? container : GetAllObjects()),
2012-06-15 19:32:41 +02:00
m_Properties(boost::make_shared<Dictionary>()), m_Tags(boost::make_shared<Dictionary>())
{
SetProperty("__type", type);
SetProperty("__name", name);
}
void ConfigObject::SetProperties(Dictionary::Ptr properties)
{
m_Properties = properties;
}
2012-04-20 13:49:04 +02:00
Dictionary::Ptr ConfigObject::GetProperties(void) const
{
return m_Properties;
}
Dictionary::Ptr ConfigObject::GetTags(void) const
{
return m_Tags;
}
string ConfigObject::GetType(void) const
{
string type;
GetProperties()->Get("__type", &type);
return type;
}
string ConfigObject::GetName(void) const
{
string name;
GetProperties()->Get("__name", &name);
return name;
}
void ConfigObject::SetLocal(bool value)
{
GetProperties()->Set("__local", value ? 1 : 0);
}
bool ConfigObject::IsLocal(void) const
{
2012-06-12 11:24:02 +02:00
bool value = false;
GetProperties()->Get("__local", &value);
2012-06-12 11:24:02 +02:00
return value;
}
void ConfigObject::SetAbstract(bool value)
{
GetProperties()->Set("__abstract", value ? 1 : 0);
}
bool ConfigObject::IsAbstract(void) const
{
2012-06-12 11:24:02 +02:00
bool value = false;
GetProperties()->Get("__abstract", &value);
2012-06-12 11:24:02 +02:00
return value;
}
2012-07-02 14:38:37 +02:00
void ConfigObject::SetSource(const string& value)
{
GetProperties()->Set("__source", value);
2012-07-02 14:38:37 +02:00
}
string ConfigObject::GetSource(void) const
{
string value;
GetProperties()->Get("__source", &value);
2012-07-02 14:38:37 +02:00
return value;
}
void ConfigObject::SetCommitTimestamp(time_t ts)
{
GetProperties()->Set("__tx", static_cast<long>(ts));
}
time_t ConfigObject::GetCommitTimestamp(void) const
{
long value = false;
GetProperties()->Get("__tx", &value);
return value;
}
void ConfigObject::Commit(void)
{
assert(Application::IsMainThread());
ConfigObject::Ptr dobj = GetObject(GetType(), GetName());
ConfigObject::Ptr self = GetSelf();
assert(!dobj || dobj == self);
m_Container->CheckObject(self);
time_t now;
time(&now);
SetCommitTimestamp(now);
}
void ConfigObject::Unregister(void)
{
assert(Application::IsMainThread());
ConfigObject::Ptr self = GetSelf();
m_Container->RemoveObject(self);
}
ObjectSet<ConfigObject::Ptr>::Ptr ConfigObject::GetAllObjects(void)
{
static ObjectSet<ConfigObject::Ptr>::Ptr allObjects;
if (!allObjects) {
2012-06-15 19:32:41 +02:00
allObjects = boost::make_shared<ObjectSet<ConfigObject::Ptr> >();
allObjects->Start();
}
return allObjects;
}
ConfigObject::TNMap::Ptr ConfigObject::GetObjectsByTypeAndName(void)
{
static ConfigObject::TNMap::Ptr tnmap;
if (!tnmap) {
2012-06-15 19:32:41 +02:00
tnmap = boost::make_shared<ConfigObject::TNMap>(GetAllObjects(), &ConfigObject::TypeAndNameGetter);
tnmap->Start();
}
return tnmap;
}
ConfigObject::Ptr ConfigObject::GetObject(string type, string name)
{
ConfigObject::TNMap::Range range;
range = GetObjectsByTypeAndName()->GetRange(make_pair(type, name));
assert(distance(range.first, range.second) <= 1);
if (range.first == range.second)
return ConfigObject::Ptr();
else
return range.first->second;
}
bool ConfigObject::TypeAndNameGetter(const ConfigObject::Ptr& object, pair<string, string> *key)
{
*key = make_pair(object->GetType(), object->GetName());
return true;
}
function<bool (ConfigObject::Ptr)> ConfigObject::MakeTypePredicate(string type)
{
2012-06-15 19:32:41 +02:00
return boost::bind(&ConfigObject::TypePredicate, _1, type);
}
bool ConfigObject::TypePredicate(const ConfigObject::Ptr& object, string type)
{
return (object->GetType() == type);
}
ConfigObject::TMap::Ptr ConfigObject::GetObjectsByType(void)
{
static ObjectMap<string, ConfigObject::Ptr>::Ptr tmap;
if (!tmap) {
2012-06-15 19:32:41 +02:00
tmap = boost::make_shared<ConfigObject::TMap>(GetAllObjects(), &ConfigObject::TypeGetter);
tmap->Start();
}
return tmap;
}
bool ConfigObject::TypeGetter(const ConfigObject::Ptr& object, string *key)
{
*key = object->GetType();
return true;
}
ConfigObject::TMap::Range ConfigObject::GetObjects(string type)
{
return GetObjectsByType()->GetRange(type);
}