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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-06-01 16:49:33 +02:00
|
|
|
#include "i2-dyn.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-07-06 14:33:10 +02:00
|
|
|
ConfigItem::ConfigItem(const string& type, const string& name,
|
|
|
|
const ExpressionList::Ptr& exprl, const vector<string>& parents,
|
|
|
|
const DebugInfo& debuginfo)
|
|
|
|
: m_Type(type), m_Name(name), m_ExpressionList(exprl),
|
|
|
|
m_Parents(parents), m_DebugInfo(debuginfo)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
string ConfigItem::GetType(void) const
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
return m_Type;
|
|
|
|
}
|
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
string ConfigItem::GetName(void) const
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
return m_Name;
|
|
|
|
}
|
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
DebugInfo ConfigItem::GetDebugInfo(void) const
|
|
|
|
{
|
|
|
|
return m_DebugInfo;
|
|
|
|
}
|
|
|
|
|
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-06-06 14:38:28 +02:00
|
|
|
vector<string> ConfigItem::GetParents(void) const
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
|
|
|
return m_Parents;
|
|
|
|
}
|
|
|
|
|
2012-06-06 20:33:25 +02:00
|
|
|
void ConfigItem::CalculateProperties(Dictionary::Ptr dictionary) const
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
vector<string>::const_iterator it;
|
|
|
|
for (it = m_Parents.begin(); it != m_Parents.end(); it++) {
|
2012-06-06 14:38:28 +02:00
|
|
|
ConfigItem::Ptr parent = ConfigItem::GetObject(GetType(), *it);
|
2012-06-05 15:05:15 +02:00
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
stringstream message;
|
2012-06-06 14:38:28 +02:00
|
|
|
message << "Parent object '" << *it << "' does not exist (" << m_DebugInfo << ")";
|
2012-06-05 15:05:15 +02:00
|
|
|
throw domain_error(message.str());
|
|
|
|
}
|
|
|
|
|
2012-06-06 20:33:25 +02:00
|
|
|
parent->CalculateProperties(dictionary);
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 20:33:25 +02:00
|
|
|
m_ExpressionList->Execute(dictionary);
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
ConfigItem::Set::Ptr ConfigItem::GetAllObjects(void)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2012-06-06 14:38:28 +02:00
|
|
|
static ObjectSet<ConfigItem::Ptr>::Ptr allObjects;
|
2012-06-05 15:05:15 +02:00
|
|
|
|
|
|
|
if (!allObjects) {
|
2012-06-15 19:32:41 +02:00
|
|
|
allObjects = boost::make_shared<ObjectSet<ConfigItem::Ptr> >();
|
2012-06-05 15:05:15 +02:00
|
|
|
allObjects->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
return allObjects;
|
|
|
|
}
|
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
bool ConfigItem::GetTypeAndName(const ConfigItem::Ptr& object, pair<string, string> *key)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
|
|
|
*key = make_pair(object->GetType(), object->GetName());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
ConfigItem::TNMap::Ptr ConfigItem::GetObjectsByTypeAndName(void)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2012-06-06 14:38:28 +02:00
|
|
|
static ConfigItem::TNMap::Ptr tnmap;
|
2012-06-05 15:05:15 +02:00
|
|
|
|
|
|
|
if (!tnmap) {
|
2012-06-15 19:32:41 +02:00
|
|
|
tnmap = boost::make_shared<ConfigItem::TNMap>(GetAllObjects(), &ConfigItem::GetTypeAndName);
|
2012-06-05 15:05:15 +02:00
|
|
|
tnmap->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
return tnmap;
|
|
|
|
}
|
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
ConfigObject::Ptr ConfigItem::Commit(void)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2012-06-12 09:36:18 +02:00
|
|
|
ConfigObject::Ptr dobj = m_ConfigObject.lock();
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
Dictionary::Ptr properties = boost::make_shared<Dictionary>();
|
2012-06-12 09:36:18 +02:00
|
|
|
CalculateProperties(properties);
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-06-12 09:36:18 +02:00
|
|
|
if (!dobj)
|
|
|
|
dobj = ConfigObject::GetObject(GetType(), GetName());
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-06-12 09:36:18 +02:00
|
|
|
if (!dobj)
|
2012-06-15 19:32:41 +02:00
|
|
|
dobj = boost::make_shared<ConfigObject>(properties);
|
2012-06-12 09:36:18 +02:00
|
|
|
else
|
|
|
|
dobj->SetProperties(properties);
|
|
|
|
|
|
|
|
m_ConfigObject = dobj;
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
if (dobj->IsAbstract())
|
|
|
|
dobj->Unregister();
|
|
|
|
else
|
|
|
|
dobj->Commit();
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
/* TODO: Figure out whether there are any child objects which inherit
|
|
|
|
* from this config item and Commit() them as well */
|
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
ConfigItem::Ptr ci = GetObject(GetType(), GetName());
|
2012-06-16 13:06:06 +02:00
|
|
|
ConfigItem::Ptr self = GetSelf();
|
2012-06-06 14:38:28 +02:00
|
|
|
if (ci && ci != self) {
|
2012-06-12 09:36:18 +02:00
|
|
|
ci->m_ConfigObject.reset();
|
2012-06-06 14:38:28 +02:00
|
|
|
GetAllObjects()->RemoveObject(ci);
|
|
|
|
}
|
|
|
|
GetAllObjects()->CheckObject(self);
|
2012-07-06 11:22:38 +02:00
|
|
|
|
|
|
|
return dobj;
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
void ConfigItem::Unregister(void)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2012-06-27 18:43:34 +02:00
|
|
|
ConfigObject::Ptr dobj = m_ConfigObject.lock();
|
|
|
|
|
|
|
|
if (dobj)
|
|
|
|
dobj->Unregister();
|
2012-06-06 14:38:28 +02:00
|
|
|
|
2012-06-16 13:06:06 +02:00
|
|
|
GetAllObjects()->RemoveObject(GetSelf());
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
ConfigObject::Ptr ConfigItem::GetConfigObject(void) const
|
|
|
|
{
|
|
|
|
return m_ConfigObject.lock();
|
|
|
|
}
|
|
|
|
|
2012-06-11 08:21:47 +02:00
|
|
|
ConfigItem::Ptr ConfigItem::GetObject(const string& type, const string& name)
|
2012-06-05 15:05:15 +02:00
|
|
|
{
|
2012-06-06 14:38:28 +02:00
|
|
|
ConfigItem::TNMap::Range range;
|
2012-06-05 15:05:15 +02:00
|
|
|
range = GetObjectsByTypeAndName()->GetRange(make_pair(type, name));
|
|
|
|
|
|
|
|
assert(distance(range.first, range.second) <= 1);
|
|
|
|
|
|
|
|
if (range.first == range.second)
|
2012-06-06 14:38:28 +02:00
|
|
|
return ConfigItem::Ptr();
|
2012-06-05 15:05:15 +02:00
|
|
|
else
|
|
|
|
return range.first->second;
|
|
|
|
}
|