icinga2/lib/config/configitem.cpp

368 lines
9.2 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 *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "i2-config.h"
using namespace icinga;
ConfigItem::ItemMap ConfigItem::m_Items;
boost::signal<void (const ConfigItem::Ptr&)> ConfigItem::OnCommitted;
boost::signal<void (const ConfigItem::Ptr&)> ConfigItem::OnRemoved;
2012-09-19 12:32:39 +02:00
/**
* Constructor for the ConfigItem class.
*
* @param type The object type.
* @param name The name of the item.
* @param exprl Expression list for the item.
* @param parents Parent objects for the item.
* @param debuginfo Debug information.
*/
ConfigItem::ConfigItem(const String& type, const String& name,
const String& unit, const ExpressionList::Ptr& exprl,
const vector<String>& parents, const DebugInfo& debuginfo)
: m_Type(type), m_Name(name), m_Unit(unit), m_ExpressionList(exprl),
2012-07-06 14:33:10 +02:00
m_Parents(parents), m_DebugInfo(debuginfo)
{
}
2012-09-19 12:32:39 +02:00
/**
* Retrieves the type of the configuration item.
*
* @returns The type.
*/
String ConfigItem::GetType(void) const
{
return m_Type;
}
2012-09-19 12:32:39 +02:00
/**
* Retrieves the name of the configuration item.
*
* @returns The name.
*/
String ConfigItem::GetName(void) const
{
return m_Name;
}
/**
* Retrieves the name of the compilation unit this item belongs to.
*
* @returns The unit name.
*/
String ConfigItem::GetUnit(void) const
{
return m_Unit;
}
2012-09-19 12:32:39 +02:00
/**
* Retrieves the debug information for the configuration item.
*
* @returns The debug information.
*/
DebugInfo ConfigItem::GetDebugInfo(void) const
{
return m_DebugInfo;
}
2012-09-19 12:32:39 +02:00
/**
* Retrieves the expression list for the configuration item.
*
* @returns The expression list.
*/
ExpressionList::Ptr ConfigItem::GetExpressionList(void) const
{
return m_ExpressionList;
}
2012-09-19 12:32:39 +02:00
/**
* Retrieves the list of parents for the configuration item.
*
* @returns The list of parents.
*/
vector<String> ConfigItem::GetParents(void) const
{
return m_Parents;
}
Dictionary::Ptr ConfigItem::Link(void) const
{
Dictionary::Ptr attrs = boost::make_shared<Dictionary>();
InternalLink(attrs);
return attrs;
}
2012-09-19 12:32:39 +02:00
/**
* Calculates the object's properties based on parent objects and the object's
* expression list.
*
* @param dictionary The dictionary that should be used to store the
* properties.
*/
void ConfigItem::InternalLink(const Dictionary::Ptr& dictionary) const
{
BOOST_FOREACH(const String& name, m_Parents) {
2012-07-16 22:00:50 +02:00
ConfigItem::Ptr parent = ConfigItem::GetObject(GetType(), name);
if (!parent) {
stringstream message;
2012-09-19 12:32:39 +02:00
message << "Parent object '" << name << "' does not"
" exist (" << m_DebugInfo << ")";
2012-07-17 20:41:06 +02:00
throw_exception(domain_error(message.str()));
}
parent->InternalLink(dictionary);
}
m_ExpressionList->Execute(dictionary);
}
2012-09-19 12:32:39 +02:00
/**
* Commits the configuration item by creating or updating a DynamicObject
* object.
*
* @returns The DynamicObject that was created/updated.
*/
2012-07-30 10:17:29 +02:00
DynamicObject::Ptr ConfigItem::Commit(void)
{
Logger::Write(LogDebug, "base", "Commit called for ConfigItem Type=" + GetType() + ", Name=" + GetName());
2012-07-30 10:17:29 +02:00
DynamicObject::Ptr dobj = m_DynamicObject.lock();
Dictionary::Ptr properties = Link();
/* Create a fake update in the format that
* DynamicObject::ApplyUpdate expects. */
Dictionary::Ptr attrs = boost::make_shared<Dictionary>();
String key;
Value data;
BOOST_FOREACH(tie(key, data), properties) {
Dictionary::Ptr attr = boost::make_shared<Dictionary>();
attr->Set("data", data);
attr->Set("type", Attribute_Config);
attr->Set("tx", DynamicObject::GetCurrentTx());
attrs->Set(key, attr);
}
Dictionary::Ptr update = boost::make_shared<Dictionary>();
update->Set("attrs", attrs);
update->Set("configTx", DynamicObject::GetCurrentTx());
DynamicType::Ptr dtype = DynamicType::GetByName(GetType());
2013-01-23 10:41:38 +01:00
if (!dtype)
throw_exception(runtime_error("Type '" + GetType() + "' does not exist."));
if (!dobj)
dobj = dtype->GetObject(GetName());
if (!dobj)
dobj = dtype->CreateObject(update);
else
dobj->ApplyUpdate(update, Attribute_Config);
2012-07-30 10:17:29 +02:00
m_DynamicObject = dobj;
if (dobj->IsAbstract())
dobj->Unregister();
else
dobj->Register();
pair<String, String> ikey = make_pair(GetType(), GetName());
ItemMap::iterator it = m_Items.find(ikey);
/* unregister the old item from its parents */
if (it != m_Items.end()) {
ConfigItem::Ptr oldItem = it->second;
oldItem->UnregisterFromParents();
/* steal the old item's children */
m_ChildObjects = oldItem->m_ChildObjects;
}
/* register this item with its parents */
BOOST_FOREACH(const String& parentName, m_Parents) {
ConfigItem::Ptr parent = GetObject(GetType(), parentName);
parent->RegisterChild(GetSelf());
}
/* We need to make a copy of the child objects becauuse the
* OnParentCommitted() handler is going to update the list. */
set<ConfigItem::WeakPtr> children = m_ChildObjects;
/* notify our children of the update */
BOOST_FOREACH(const ConfigItem::WeakPtr wchild, children) {
const ConfigItem::Ptr& child = wchild.lock();
if (!child)
continue;
child->OnParentCommitted();
}
m_Items[ikey] = GetSelf();
OnCommitted(GetSelf());
return dobj;
}
2012-09-19 12:32:39 +02:00
/**
* Unregisters the configuration item.
*/
void ConfigItem::Unregister(void)
{
2012-07-30 10:17:29 +02:00
DynamicObject::Ptr dobj = m_DynamicObject.lock();
2012-06-27 18:43:34 +02:00
if (dobj)
dobj->Unregister();
ConfigItem::ItemMap::iterator it;
it = m_Items.find(make_pair(GetType(), GetName()));
if (it != m_Items.end())
m_Items.erase(it);
UnregisterFromParents();
OnRemoved(GetSelf());
}
void ConfigItem::RegisterChild(const ConfigItem::Ptr& child)
{
m_ChildObjects.insert(child);
}
void ConfigItem::UnregisterChild(const ConfigItem::Ptr& child)
{
m_ChildObjects.erase(child);
}
void ConfigItem::UnregisterFromParents(void)
{
BOOST_FOREACH(const String& parentName, m_Parents) {
ConfigItem::Ptr parent = GetObject(GetType(), parentName);
if (parent)
parent->UnregisterChild(GetSelf());
}
}
/*
* Notifies an item that one of its parents has been committed.
*/
void ConfigItem::OnParentCommitted(void)
{
if (GetObject(GetType(), GetName()) == static_cast<ConfigItem::Ptr>(GetSelf()))
Commit();
}
2012-09-19 12:32:39 +02:00
/**
* Retrieves the DynamicObject that belongs to the configuration item.
*
* @returns The DynamicObject.
*/
2012-07-30 10:17:29 +02:00
DynamicObject::Ptr ConfigItem::GetDynamicObject(void) const
{
2012-07-30 10:17:29 +02:00
return m_DynamicObject.lock();
}
2012-09-19 12:32:39 +02:00
/**
* Retrieves a configuration item by type and name.
*
* @param type The type of the ConfigItem that is to be looked up.
* @param name The name of the ConfigItem that is to be looked up.
* @returns The configuration item.
*/
ConfigItem::Ptr ConfigItem::GetObject(const String& type, const String& name)
{
ConfigItem::ItemMap::iterator it;
ConfigCompilerContext *context = ConfigCompilerContext::GetContext();
if (context) {
ConfigItem::Ptr item = context->GetItem(type, name);
if (item)
return item;
/* ignore already active objects while we're in the compiler
* context and linking to existing items is disabled. */
if ((context->GetFlags() & CompilerLinkExisting) == 0)
return ConfigItem::Ptr();
}
it = m_Items.find(make_pair(type, name));
if (it != m_Items.end())
return it->second;
return ConfigItem::Ptr();
}
/**
* Dumps the config item to the specified stream using Icinga's config item
* syntax.
*
* @param fp The stream.
*/
void ConfigItem::Dump(ostream& fp) const
{
fp << "object \"" << m_Type << "\" \"" << m_Name << "\"";
if (m_Parents.size() > 0) {
fp << " inherits";
bool first = true;
BOOST_FOREACH(const String& name, m_Parents) {
if (!first)
fp << ",";
else
first = false;
fp << " \"" << name << "\"";
}
}
fp << " {" << "\n";
m_ExpressionList->Dump(fp, 1);
fp << "}" << "\n";
}
void ConfigItem::UnloadUnit(const String& unit)
{
Logger::Write(LogInformation, "config", "Unloading config items from compilation unit '" + unit + "'");
vector<ConfigItem::Ptr> obsoleteItems;
ConfigItem::Ptr item;
BOOST_FOREACH(tie(tuples::ignore, item), m_Items) {
if (item->GetUnit() != unit)
continue;
obsoleteItems.push_back(item);
}
BOOST_FOREACH(item, obsoleteItems) {
item->Unregister();
}
}