icinga2/lib/config/configcompilercontext.cpp

166 lines
4.4 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. *
******************************************************************************/
2013-03-17 20:19:29 +01:00
#include "config/configcompilercontext.h"
#include "base/utility.h"
2013-03-16 21:18:53 +01:00
#include "base/logger_fwd.h"
#include <boost/foreach.hpp>
using std::ifstream;
using namespace icinga;
ConfigCompilerContext *ConfigCompilerContext::m_Context = NULL;
ConfigCompilerContext::ConfigCompilerContext(void)
2013-05-03 11:26:18 +02:00
: m_Unit(Utility::NewUniqueID()), m_Flags(0)
{ }
void ConfigCompilerContext::AddItem(const ConfigItem::Ptr& item)
{
Log(LogDebug, "config", "Adding item to compiler context: type=" +
item->GetType() + "; name=" + item->GetName());
m_Items.push_back(item);
2013-03-16 21:18:53 +01:00
m_ItemsMap[std::make_pair(item->GetType(), item->GetName())] = item;
}
ConfigItem::Ptr ConfigCompilerContext::GetItem(const String& type, const String& name) const
{
2013-06-04 10:13:32 +02:00
std::map<std::pair<String, String>, ConfigItem::Ptr, pair_string_iless>::const_iterator it;
2013-03-16 21:18:53 +01:00
it = m_ItemsMap.find(std::make_pair(type, name));
if (it == m_ItemsMap.end())
return ConfigItem::Ptr();
return it->second;
}
2013-03-16 21:18:53 +01:00
std::vector<ConfigItem::Ptr> ConfigCompilerContext::GetItems(void) const
{
return m_Items;
}
void ConfigCompilerContext::AddType(const ConfigType::Ptr& type)
{
m_Types[type->GetName()] = type;
}
ConfigType::Ptr ConfigCompilerContext::GetType(const String& name) const
{
2013-06-04 10:13:32 +02:00
std::map<String, ConfigType::Ptr, string_iless>::const_iterator it;
it = m_Types.find(name);
if (it == m_Types.end())
return ConfigType::Ptr();
return it->second;
}
void ConfigCompilerContext::AddError(bool warning, const String& message)
{
m_Errors.push_back(ConfigCompilerError(warning, message));
}
2013-03-16 21:18:53 +01:00
std::vector<ConfigCompilerError> ConfigCompilerContext::GetErrors(void) const
{
return m_Errors;
}
void ConfigCompilerContext::SetFlags(int flags)
{
m_Flags = flags;
}
int ConfigCompilerContext::GetFlags(void) const
{
return m_Flags;
}
void ConfigCompilerContext::SetContext(ConfigCompilerContext *context)
{
ASSERT(m_Context == NULL || context == NULL);
m_Context = context;
}
ConfigCompilerContext *ConfigCompilerContext::GetContext(void)
{
return m_Context;
}
String ConfigCompilerContext::GetUnit(void) const
{
return m_Unit;
}
void ConfigCompilerContext::LinkItems(void)
{
SetContext(this);
Log(LogInformation, "config", "Linking config items...");
BOOST_FOREACH(const ConfigItem::Ptr& item, m_Items) {
item->Link();
}
SetContext(NULL);
}
void ConfigCompilerContext::ValidateItems(void)
{
SetContext(this);
Log(LogInformation, "config", "Validating config items...");
BOOST_FOREACH(const ConfigItem::Ptr& item, m_Items) {
2013-02-18 23:44:24 +01:00
ConfigType::Ptr ctype;
2013-02-18 23:44:24 +01:00
{
ctype = GetType(item->GetType());
2013-02-18 23:44:24 +01:00
if (!ctype) {
AddError(true, "No validation type found for object '" + item->GetName() + "' of type '" + item->GetType() + "'");
continue;
}
}
ctype->ValidateItem(item);
}
SetContext(NULL);
}
void ConfigCompilerContext::ActivateItems(void)
{
ASSERT(m_Context == NULL);
2013-03-16 21:18:53 +01:00
Log(LogInformation, "config", "Activating config items in compilation unit '" + m_Unit + "'");
BOOST_FOREACH(const ConfigItem::Ptr& item, m_Items) {
item->Register();
}
BOOST_FOREACH(const ConfigItem::Ptr& item, m_Items) {
2013-03-02 09:07:47 +01:00
item->Commit();
}
}