2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2012-07-09 20:32:02 +02:00
|
|
|
* *
|
|
|
|
* 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"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
2014-11-16 16:20:39 +01:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
2012-07-06 14:33:10 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
ConfigItemBuilder::ConfigItemBuilder(void)
|
2016-08-28 14:12:07 +02:00
|
|
|
: 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)
|
2016-08-28 14:12:07 +02:00
|
|
|
: m_Abstract(false), m_DefaultTmpl(false), m_IgnoreOnError(false)
|
2012-07-06 14:33:10 +02:00
|
|
|
{
|
|
|
|
m_DebugInfo = debugInfo;
|
|
|
|
}
|
|
|
|
|
2017-05-11 14:21:30 +02:00
|
|
|
void ConfigItemBuilder::SetType(const Type::Ptr& type)
|
2012-07-06 14:33:10 +02:00
|
|
|
{
|
|
|
|
m_Type = type;
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
void ConfigItemBuilder::SetScope(const Dictionary::Ptr& scope)
|
2014-03-24 11:23:47 +01:00
|
|
|
{
|
|
|
|
m_Scope = scope;
|
|
|
|
}
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
void ConfigItemBuilder::SetZone(const String& zone)
|
2014-05-03 20:01:23 +02:00
|
|
|
{
|
2014-05-03 20:02:22 +02:00
|
|
|
m_Zone = zone;
|
2014-05-03 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
void ConfigItemBuilder::SetPackage(const String& package)
|
2015-08-17 16:08:57 +02:00
|
|
|
{
|
2015-08-28 17:58:29 +02:00
|
|
|
m_Package = package;
|
2015-08-17 16:08:57 +02:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
void ConfigItemBuilder::AddExpression(Expression *expr)
|
2012-07-06 14:33:10 +02:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
m_Expressions.push_back(expr);
|
2012-07-06 14:33:10 +02:00
|
|
|
}
|
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
void ConfigItemBuilder::SetFilter(const boost::shared_ptr<Expression>& filter)
|
|
|
|
{
|
|
|
|
m_Filter = filter;
|
|
|
|
}
|
|
|
|
|
2016-08-28 10:27:43 +02:00
|
|
|
void ConfigItemBuilder::SetDefaultTemplate(bool defaultTmpl)
|
|
|
|
{
|
|
|
|
m_DefaultTmpl = defaultTmpl;
|
|
|
|
}
|
|
|
|
|
2015-10-05 12:44:11 +02:00
|
|
|
void ConfigItemBuilder::SetIgnoreOnError(bool ignoreOnError)
|
|
|
|
{
|
|
|
|
m_IgnoreOnError = ignoreOnError;
|
|
|
|
}
|
|
|
|
|
2012-07-06 14:33:10 +02:00
|
|
|
ConfigItem::Ptr ConfigItemBuilder::Compile(void)
|
|
|
|
{
|
2017-05-11 14:21:30 +02:00
|
|
|
if (!m_Type) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream msgbuf;
|
2017-05-11 14:21:30 +02:00
|
|
|
msgbuf << "The type of an object must be specified";
|
2015-08-15 10:17:45 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo));
|
2013-02-01 14:45:55 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 14:21:30 +02:00
|
|
|
ConfigType *ctype = dynamic_cast<ConfigType *>(m_Type.get());
|
2016-08-16 11:02:10 +02:00
|
|
|
|
|
|
|
if (!ctype) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream msgbuf;
|
2017-05-11 14:21:30 +02:00
|
|
|
msgbuf << "The type '" + m_Type->GetName() + "' cannot be used for config objects";
|
2015-08-15 10:17:45 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo));
|
2013-02-06 00:32:05 +01:00
|
|
|
}
|
|
|
|
|
2015-08-15 09:40:42 +02:00
|
|
|
if (m_Name.FindFirstOf("!") != String::NPos) {
|
|
|
|
std::ostringstream msgbuf;
|
2017-05-11 14:21:30 +02:00
|
|
|
msgbuf << "Name for object '" << m_Name << "' of type '" << m_Type->GetName() << "' is invalid: Object names may not contain '!'";
|
2015-08-15 09:40:42 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo));
|
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
std::vector<Expression *> exprs;
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr templateArray = new Array();
|
2014-03-28 14:46:19 +01:00
|
|
|
templateArray->Add(m_Name);
|
2014-03-30 10:00:11 +02:00
|
|
|
|
2015-04-15 08:36:17 +02:00
|
|
|
exprs.push_back(new SetExpression(MakeIndexer(ScopeThis, "templates"), OpSetAdd,
|
2014-12-13 17:48:58 +01:00
|
|
|
new LiteralExpression(templateArray), m_DebugInfo));
|
2014-03-30 10:00:11 +02:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
DictExpression *dexpr = new DictExpression(m_Expressions, m_DebugInfo);
|
|
|
|
dexpr->MakeInline();
|
|
|
|
exprs.push_back(dexpr);
|
2012-07-06 14:33:10 +02:00
|
|
|
|
2016-08-28 10:27:43 +02:00
|
|
|
#ifdef I2_DEBUG
|
|
|
|
if (!m_Abstract) {
|
|
|
|
bool foundDefaultImport = false;
|
|
|
|
|
|
|
|
for (Expression *expr : m_Expressions) {
|
|
|
|
if (dynamic_cast<ImportDefaultTemplatesExpression *>(expr)) {
|
|
|
|
foundDefaultImport = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(foundDefaultImport);
|
|
|
|
}
|
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
boost::shared_ptr<DictExpression> exprl = boost::make_shared<DictExpression>(exprs, m_DebugInfo);
|
2014-11-09 19:48:28 +01:00
|
|
|
exprl->MakeInline();
|
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
return new ConfigItem(m_Type, m_Name, m_Abstract, exprl, m_Filter,
|
2016-08-28 10:27:43 +02:00
|
|
|
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
|
|
|
|