2012-06-06 14:38:28 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "config/configitem.h"
|
|
|
|
#include "config/configcompilercontext.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/logger_fwd.h"
|
2013-03-18 11:02:18 +01:00
|
|
|
#include "base/utility.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
2012-06-01 16:49:33 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
boost::mutex ConfigItem::m_Mutex;
|
2012-07-27 16:05:02 +02:00
|
|
|
ConfigItem::ItemMap ConfigItem::m_Items;
|
2013-03-16 21:18:53 +01:00
|
|
|
boost::signals2::signal<void (const ConfigItem::Ptr&)> ConfigItem::OnCommitted;
|
|
|
|
boost::signals2::signal<void (const ConfigItem::Ptr&)> ConfigItem::OnRemoved;
|
2012-07-27 16:05:02 +02:00
|
|
|
|
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.
|
2013-03-11 12:04:10 +01:00
|
|
|
* @param unit The unit of the item.
|
|
|
|
* @param abstract Whether the item is a template.
|
2012-09-19 12:32:39 +02:00
|
|
|
* @param exprl Expression list for the item.
|
|
|
|
* @param parents Parent objects for the item.
|
|
|
|
* @param debuginfo Debug information.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
ConfigItem::ConfigItem(const String& type, const String& name,
|
2013-03-11 12:04:10 +01:00
|
|
|
const String& unit, bool abstract, const ExpressionList::Ptr& exprl,
|
2013-03-16 21:18:53 +01:00
|
|
|
const std::vector<String>& parents, const DebugInfo& debuginfo)
|
2013-03-11 12:04:10 +01:00
|
|
|
: m_Type(type), m_Name(name), m_Unit(unit), m_Abstract(abstract),
|
2013-03-19 07:09:06 +01:00
|
|
|
m_ExpressionList(exprl), m_ParentNames(parents), m_DebugInfo(debuginfo)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the type of the configuration item.
|
|
|
|
*
|
|
|
|
* @returns The type.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String ConfigItem::GetType(void) const
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
return m_Type;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the name of the configuration item.
|
|
|
|
*
|
|
|
|
* @returns The name.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String ConfigItem::GetName(void) const
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
return m_Name;
|
|
|
|
}
|
|
|
|
|
2013-02-06 00:32:05 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the name of the compilation unit this item belongs to.
|
|
|
|
*
|
|
|
|
* @returns The unit name.
|
|
|
|
*/
|
|
|
|
String ConfigItem::GetUnit(void) const
|
|
|
|
{
|
|
|
|
return m_Unit;
|
|
|
|
}
|
|
|
|
|
2013-03-11 12:04:10 +01:00
|
|
|
/**
|
|
|
|
* Checks whether the item is abstract.
|
|
|
|
*
|
|
|
|
* @returns true if the item is abstract, false otherwise.
|
|
|
|
*/
|
|
|
|
bool ConfigItem::IsAbstract(void) const
|
|
|
|
{
|
|
|
|
return m_Abstract;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the debug information for the configuration item.
|
|
|
|
*
|
|
|
|
* @returns The debug information.
|
|
|
|
*/
|
2012-07-06 11:22:38 +02:00
|
|
|
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.
|
|
|
|
*/
|
2012-06-06 14:38:28 +02:00
|
|
|
ExpressionList::Ptr ConfigItem::GetExpressionList(void) const
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
2013-03-19 07:09:06 +01:00
|
|
|
std::vector<ConfigItem::Ptr> ConfigItem::GetParents(void) const
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
|
|
|
return m_Parents;
|
|
|
|
}
|
|
|
|
|
2013-03-19 07:09:06 +01:00
|
|
|
void ConfigItem::Link(void)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-03-19 07:09:06 +01:00
|
|
|
m_LinkedExpressionList = boost::make_shared<ExpressionList>();
|
|
|
|
|
|
|
|
m_Parents.clear();
|
|
|
|
|
|
|
|
BOOST_FOREACH(const String& name, m_ParentNames) {
|
2013-02-18 14:40:24 +01:00
|
|
|
ConfigItem::Ptr parent;
|
|
|
|
|
|
|
|
ConfigCompilerContext *context = ConfigCompilerContext::GetContext();
|
|
|
|
|
|
|
|
if (context)
|
2013-03-04 15:52:42 +01:00
|
|
|
parent = context->GetItem(m_Type, name);
|
2013-02-18 14:40:24 +01:00
|
|
|
|
|
|
|
/* ignore already active objects while we're in the compiler
|
|
|
|
* context and linking to existing items is disabled. */
|
|
|
|
if (!parent && (!context || (context->GetFlags() & CompilerLinkExisting)))
|
2013-03-04 15:52:42 +01:00
|
|
|
parent = ConfigItem::GetObject(m_Type, name);
|
2012-06-05 15:05:15 +02:00
|
|
|
|
|
|
|
if (!parent) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream message;
|
2012-09-19 12:32:39 +02:00
|
|
|
message << "Parent object '" << name << "' does not"
|
|
|
|
" exist (" << m_DebugInfo << ")";
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument(message.str()));
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2013-03-19 07:09:06 +01:00
|
|
|
parent->Link();
|
|
|
|
|
|
|
|
ExpressionList::Ptr pexprl = parent->GetLinkedExpressionList();
|
|
|
|
m_LinkedExpressionList->AddExpression(Expression("", OperatorExecute, pexprl, m_DebugInfo));
|
|
|
|
|
|
|
|
m_Parents.push_back(parent);
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2013-03-19 07:09:06 +01:00
|
|
|
m_LinkedExpressionList->AddExpression(Expression("", OperatorExecute, m_ExpressionList, m_DebugInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpressionList::Ptr ConfigItem::GetLinkedExpressionList(void) const
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
return m_LinkedExpressionList;
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2013-03-02 09:07:47 +01:00
|
|
|
DynamicObject::Ptr ConfigItem::Commit(void)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
String type, name;
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogDebug, "base", "Commit called for ConfigItem Type=" + GetType() + ", Name=" + GetName());
|
2013-01-31 15:26:54 +01:00
|
|
|
|
2013-02-08 21:05:08 +01:00
|
|
|
/* Make sure the type is valid. */
|
2013-03-02 09:07:47 +01:00
|
|
|
DynamicType::Ptr dtype = DynamicType::GetByName(GetType());
|
2013-02-08 21:05:08 +01:00
|
|
|
|
|
|
|
if (!dtype)
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Type '" + GetType() + "' does not exist."));
|
2013-03-02 09:07:47 +01:00
|
|
|
|
|
|
|
/* Try to find an existing item with the same type and name. */
|
2013-03-16 21:18:53 +01:00
|
|
|
std::pair<String, String> ikey = std::make_pair(GetType(), GetName());
|
2013-03-02 09:07:47 +01:00
|
|
|
ConfigItem::Ptr oldItem;
|
2013-02-20 19:52:25 +01:00
|
|
|
|
|
|
|
{
|
2013-03-15 18:21:29 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-02-20 19:52:25 +01:00
|
|
|
|
|
|
|
ItemMap::iterator it = m_Items.find(ikey);
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
if (it != m_Items.end())
|
|
|
|
oldItem = it->second;
|
|
|
|
}
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<ConfigItem::WeakPtr> children;
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
if (oldItem) {
|
|
|
|
ObjectLock olock(oldItem);
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/* Unregister the old item from its parents. */
|
|
|
|
oldItem->UnregisterFromParents();
|
|
|
|
|
|
|
|
/* Steal the old item's children. */
|
2013-03-04 15:52:42 +01:00
|
|
|
children = oldItem->m_ChildObjects;
|
2013-03-02 09:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
m_ChildObjects = children;
|
|
|
|
}
|
|
|
|
|
2013-03-02 14:11:12 +01:00
|
|
|
ConfigItem::Ptr self = GetSelf();
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
{
|
2013-03-15 18:21:29 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-02-20 19:52:25 +01:00
|
|
|
|
|
|
|
/* Register this item. */
|
2013-03-02 14:11:12 +01:00
|
|
|
m_Items[ikey] = self;
|
2013-02-20 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
DynamicObject::Ptr dobj = m_DynamicObject.lock();
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
if (!dobj)
|
2013-03-04 15:52:42 +01:00
|
|
|
dobj = dtype->GetObject(m_Name);
|
2013-02-08 21:05:08 +01:00
|
|
|
|
|
|
|
/* Register this item with its parents. */
|
2013-03-19 07:09:06 +01:00
|
|
|
BOOST_FOREACH(const ConfigItem::Ptr& parent, m_Parents) {
|
2013-03-04 15:52:42 +01:00
|
|
|
parent->m_ChildObjects.insert(self);
|
2013-02-08 21:05:08 +01:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
/* Create a fake update in the format that
|
|
|
|
* DynamicObject::ApplyUpdate expects. */
|
|
|
|
Dictionary::Ptr attrs = boost::make_shared<Dictionary>();
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
double tx = DynamicObject::GetCurrentTx();
|
|
|
|
|
2013-03-19 07:09:06 +01:00
|
|
|
Link();
|
|
|
|
|
|
|
|
Dictionary::Ptr properties = boost::make_shared<Dictionary>();
|
|
|
|
GetLinkedExpressionList()->Execute(properties);
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(properties);
|
|
|
|
|
|
|
|
String key;
|
|
|
|
Value data;
|
2013-03-15 18:21:29 +01:00
|
|
|
BOOST_FOREACH(boost::tie(key, data), properties) {
|
2013-03-01 12:07:52 +01:00
|
|
|
Dictionary::Ptr attr = boost::make_shared<Dictionary>();
|
|
|
|
attr->Set("data", data);
|
|
|
|
attr->Set("type", Attribute_Config);
|
|
|
|
attr->Set("tx", tx);
|
|
|
|
attr->Seal();
|
|
|
|
|
|
|
|
attrs->Set(key, attr);
|
|
|
|
}
|
2012-08-02 09:38:08 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
attrs->Seal();
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
Dictionary::Ptr update = boost::make_shared<Dictionary>();
|
|
|
|
update->Set("attrs", attrs);
|
|
|
|
update->Set("configTx", DynamicObject::GetCurrentTx());
|
2013-03-01 12:07:52 +01:00
|
|
|
update->Seal();
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2013-02-08 21:05:08 +01:00
|
|
|
/* Update or create the object and apply the configuration settings. */
|
2013-02-18 14:40:24 +01:00
|
|
|
bool was_null = false;
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2013-03-11 13:07:21 +01:00
|
|
|
if (!dobj) {
|
|
|
|
if (!IsAbstract())
|
|
|
|
dobj = dtype->CreateObject(update);
|
|
|
|
|
2013-02-18 14:40:24 +01:00
|
|
|
was_null = true;
|
|
|
|
}
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
if (!was_null)
|
|
|
|
dobj->ApplyUpdate(update, Attribute_Config);
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
m_DynamicObject = dobj;
|
2013-02-18 14:40:24 +01:00
|
|
|
}
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2013-03-11 12:04:10 +01:00
|
|
|
if (dobj) {
|
|
|
|
if (IsAbstract())
|
|
|
|
dobj->Unregister();
|
|
|
|
else
|
|
|
|
dobj->Register();
|
|
|
|
}
|
2013-02-03 15:47:11 +01:00
|
|
|
|
2013-01-31 15:26:54 +01:00
|
|
|
/* notify our children of the update */
|
2013-02-03 15:47:11 +01:00
|
|
|
BOOST_FOREACH(const ConfigItem::WeakPtr wchild, children) {
|
2013-01-31 15:26:54 +01:00
|
|
|
const ConfigItem::Ptr& child = wchild.lock();
|
2012-07-06 11:22:38 +02:00
|
|
|
|
2013-01-31 15:26:54 +01:00
|
|
|
if (!child)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
child->OnParentCommitted();
|
|
|
|
}
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
OnCommitted(self);
|
2012-07-06 11:22:38 +02:00
|
|
|
|
|
|
|
return dobj;
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2013-03-19 07:09:06 +01:00
|
|
|
/**
|
|
|
|
* Registers the configuration item.
|
|
|
|
*/
|
|
|
|
void ConfigItem::Register(void)
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
m_Items[std::make_pair(m_Type, m_Name)] = GetSelf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Unregisters the configuration item.
|
|
|
|
*/
|
2012-06-06 14:38:28 +02:00
|
|
|
void ConfigItem::Unregister(void)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2012-06-27 18:43:34 +02:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
DynamicObject::Ptr dobj = m_DynamicObject.lock();
|
2013-03-02 09:07:47 +01:00
|
|
|
|
|
|
|
if (dobj)
|
2012-06-27 18:43:34 +02:00
|
|
|
dobj->Unregister();
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
ConfigItem::ItemMap::iterator it;
|
2013-03-16 21:18:53 +01:00
|
|
|
it = m_Items.find(std::make_pair(m_Type, m_Name));
|
2013-03-02 09:07:47 +01:00
|
|
|
|
|
|
|
if (it != m_Items.end())
|
|
|
|
m_Items.erase(it);
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
UnregisterFromParents();
|
|
|
|
}
|
2013-01-31 15:26:54 +01:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
OnRemoved(GetSelf());
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2013-01-31 15:26:54 +01:00
|
|
|
void ConfigItem::UnregisterFromParents(void)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-19 07:09:06 +01:00
|
|
|
BOOST_FOREACH(const ConfigItem::Ptr& parent, m_Parents) {
|
|
|
|
parent->m_ChildObjects.erase(GetSelf());
|
2013-01-31 15:26:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notifies an item that one of its parents has been committed.
|
|
|
|
*/
|
|
|
|
void ConfigItem::OnParentCommitted(void)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
ConfigItem::Ptr self = GetSelf();
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
if (GetObject(m_Type, m_Name) != self)
|
|
|
|
return;
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Commit();
|
2013-01-31 15:26:54 +01:00
|
|
|
}
|
|
|
|
|
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-06 11:22:38 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-07-30 10:17:29 +02:00
|
|
|
return m_DynamicObject.lock();
|
2012-07-06 11:22:38 +02:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
ConfigItem::Ptr ConfigItem::GetObject(const String& type, const String& name)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2013-03-15 18:21:29 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2012-06-05 15:05:15 +02:00
|
|
|
|
2013-02-22 08:12:43 +01:00
|
|
|
ConfigItem::ItemMap::iterator it;
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
it = m_Items.find(std::make_pair(type, name));
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-02-22 08:12:43 +01:00
|
|
|
if (it != m_Items.end())
|
|
|
|
return it->second;
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2013-02-05 13:06:42 +01:00
|
|
|
return ConfigItem::Ptr();
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
2013-01-30 23:02:46 +01:00
|
|
|
|
2013-02-06 00:32:05 +01:00
|
|
|
/**
|
|
|
|
* Dumps the config item to the specified stream using Icinga's config item
|
|
|
|
* syntax.
|
|
|
|
*
|
|
|
|
* @param fp The stream.
|
|
|
|
*/
|
2013-03-16 21:18:53 +01:00
|
|
|
void ConfigItem::Dump(std::ostream& fp) const
|
2013-01-30 23:02:46 +01:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-01-30 23:02:46 +01:00
|
|
|
fp << "object \"" << m_Type << "\" \"" << m_Name << "\"";
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2013-01-30 23:02:46 +01:00
|
|
|
if (m_Parents.size() > 0) {
|
|
|
|
fp << " inherits";
|
|
|
|
|
|
|
|
bool first = true;
|
2013-03-19 07:09:06 +01:00
|
|
|
BOOST_FOREACH(const String& name, m_ParentNames) {
|
2013-01-30 23:02:46 +01:00
|
|
|
if (!first)
|
|
|
|
fp << ",";
|
|
|
|
else
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
fp << " \"" << name << "\"";
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2013-01-30 23:02:46 +01:00
|
|
|
fp << " {" << "\n";
|
|
|
|
m_ExpressionList->Dump(fp, 1);
|
|
|
|
fp << "}" << "\n";
|
2013-02-03 15:47:11 +01:00
|
|
|
}
|
2013-02-06 00:32:05 +01:00
|
|
|
|
|
|
|
void ConfigItem::UnloadUnit(const String& unit)
|
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<ConfigItem::Ptr> obsoleteItems;
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "config", "Unloading config items from compilation unit '" + unit + "'");
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
ConfigItem::Ptr item;
|
|
|
|
BOOST_FOREACH(boost::tie(boost::tuples::ignore, item), m_Items) {
|
|
|
|
ObjectLock olock(item);
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
if (item->m_Unit != unit)
|
|
|
|
continue;
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
obsoleteItems.push_back(item);
|
|
|
|
}
|
2013-02-06 00:32:05 +01:00
|
|
|
}
|
|
|
|
|
2013-02-18 14:40:24 +01:00
|
|
|
BOOST_FOREACH(const ConfigItem::Ptr& item, obsoleteItems) {
|
2013-02-06 00:32:05 +01:00
|
|
|
item->Unregister();
|
|
|
|
}
|
|
|
|
}
|