icinga2/lib/config/configitembuilder.cpp

147 lines
4.5 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
* *
* 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. *
******************************************************************************/
2014-05-25 16:23:35 +02:00
#include "config/configitembuilder.hpp"
#include "base/configtype.hpp"
2013-03-16 21:18:53 +01:00
#include <sstream>
2012-07-06 14:33:10 +02:00
using namespace icinga;
ConfigItemBuilder::ConfigItemBuilder(void)
: m_Abstract(false), m_DefaultTmpl(false), m_IgnoreOnError(false)
2012-07-06 14:33:10 +02:00
{
m_DebugInfo.FirstLine = 0;
m_DebugInfo.FirstColumn = 0;
m_DebugInfo.LastLine = 0;
m_DebugInfo.LastColumn = 0;
}
ConfigItemBuilder::ConfigItemBuilder(const DebugInfo& debugInfo)
: m_Abstract(false), m_DefaultTmpl(false), m_IgnoreOnError(false)
2012-07-06 14:33:10 +02:00
{
m_DebugInfo = debugInfo;
}
void ConfigItemBuilder::SetType(const Type::Ptr& type)
2012-07-06 14:33:10 +02:00
{
m_Type = type;
}
void ConfigItemBuilder::SetName(const String& name)
2012-07-06 14:33:10 +02:00
{
m_Name = name;
}
void ConfigItemBuilder::SetAbstract(bool abstract)
{
m_Abstract = abstract;
}
void ConfigItemBuilder::SetScope(const Dictionary::Ptr& scope)
{
m_Scope = scope;
}
void ConfigItemBuilder::SetZone(const String& zone)
{
m_Zone = zone;
}
void ConfigItemBuilder::SetPackage(const String& package)
{
m_Package = package;
}
2014-11-09 19:48:28 +01:00
void ConfigItemBuilder::AddExpression(Expression *expr)
2012-07-06 14:33:10 +02:00
{
m_Expressions.emplace_back(expr);
2012-07-06 14:33:10 +02:00
}
void ConfigItemBuilder::SetFilter(const std::shared_ptr<Expression>& filter)
{
m_Filter = filter;
}
void ConfigItemBuilder::SetDefaultTemplate(bool defaultTmpl)
{
m_DefaultTmpl = defaultTmpl;
}
void ConfigItemBuilder::SetIgnoreOnError(bool ignoreOnError)
{
m_IgnoreOnError = ignoreOnError;
}
2012-07-06 14:33:10 +02:00
ConfigItem::Ptr ConfigItemBuilder::Compile(void)
{
if (!m_Type) {
2013-03-16 21:18:53 +01:00
std::ostringstream msgbuf;
msgbuf << "The type of an object must be specified";
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo));
}
ConfigType *ctype = dynamic_cast<ConfigType *>(m_Type.get());
if (!ctype) {
2013-03-16 21:18:53 +01:00
std::ostringstream msgbuf;
msgbuf << "The type '" + m_Type->GetName() + "' cannot be used for config objects";
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo));
}
if (m_Name.FindFirstOf("!") != String::NPos) {
std::ostringstream msgbuf;
msgbuf << "Name for object '" << m_Name << "' of type '" << m_Type->GetName() << "' is invalid: Object names may not contain '!'";
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo));
}
std::vector<std::unique_ptr<Expression> > exprs;
2014-11-09 19:48:28 +01:00
Array::Ptr templateArray = new Array();
templateArray->Add(m_Name);
2014-03-30 10:00:11 +02:00
exprs.emplace_back(new SetExpression(MakeIndexer(ScopeThis, "templates"), OpSetAdd,
std::unique_ptr<LiteralExpression>(new LiteralExpression(templateArray)), m_DebugInfo));
2012-07-06 14:33:10 +02:00
#ifdef I2_DEBUG
if (!m_Abstract) {
bool foundDefaultImport = false;
for (const std::unique_ptr<Expression>& expr : m_Expressions) {
if (dynamic_cast<ImportDefaultTemplatesExpression *>(expr.get())) {
foundDefaultImport = true;
break;
}
}
ASSERT(foundDefaultImport);
}
#endif /* I2_DEBUG */
DictExpression *dexpr = new DictExpression(std::move(m_Expressions), m_DebugInfo);
dexpr->MakeInline();
exprs.emplace_back(dexpr);
std::shared_ptr<DictExpression> exprl = std::make_shared<DictExpression>(std::move(exprs), m_DebugInfo);
2014-11-09 19:48:28 +01:00
exprl->MakeInline();
return new ConfigItem(m_Type, m_Name, m_Abstract, exprl, m_Filter,
m_DefaultTmpl, m_IgnoreOnError, m_DebugInfo, m_Scope, m_Zone, m_Package);
2012-07-06 14:33:10 +02:00
}
2014-11-09 19:48:28 +01:00