2012-06-06 14:38:28 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2012-06-06 14:38:28 +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/configitem.hpp"
|
|
|
|
#include "config/configcompilercontext.hpp"
|
|
|
|
#include "config/applyrule.hpp"
|
|
|
|
#include "config/objectrule.hpp"
|
|
|
|
#include "config/configtype.hpp"
|
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/dynamictype.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/debug.hpp"
|
|
|
|
#include "base/workqueue.hpp"
|
|
|
|
#include "base/exception.hpp"
|
2014-08-12 13:46:54 +02:00
|
|
|
#include "base/stdiostream.hpp"
|
|
|
|
#include "base/netstring.hpp"
|
2014-10-26 19:59:49 +01:00
|
|
|
#include "base/json.hpp"
|
2014-10-01 17:01:47 +02:00
|
|
|
#include "base/configerror.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
2014-08-12 13:46:54 +02:00
|
|
|
#include <fstream>
|
2013-03-16 21:18:53 +01:00
|
|
|
#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;
|
|
|
|
|
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 debuginfo Debug information.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
ConfigItem::ConfigItem(const String& type, const String& name,
|
2014-10-16 17:44:06 +02:00
|
|
|
bool abstract, const Expression::Ptr& exprl,
|
2014-05-03 20:01:23 +02:00
|
|
|
const DebugInfo& debuginfo, const Dictionary::Ptr& scope,
|
2014-05-03 20:02:22 +02:00
|
|
|
const String& zone)
|
2013-10-16 13:37:54 +02:00
|
|
|
: m_Type(type), m_Name(name), m_Abstract(abstract), m_Validated(false),
|
2014-03-27 12:30:24 +01:00
|
|
|
m_ExpressionList(exprl), m_DebugInfo(debuginfo),
|
2014-05-03 20:02:22 +02:00
|
|
|
m_Scope(scope), m_Zone(zone)
|
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-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;
|
|
|
|
}
|
|
|
|
|
2014-03-24 11:23:47 +01:00
|
|
|
Dictionary::Ptr ConfigItem::GetScope(void) const
|
|
|
|
{
|
|
|
|
return m_Scope;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the expression list for the configuration item.
|
|
|
|
*
|
|
|
|
* @returns The expression list.
|
|
|
|
*/
|
2014-10-16 17:44:06 +02:00
|
|
|
Expression::Ptr ConfigItem::GetExpressionList(void) const
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
|
|
|
return m_ExpressionList;
|
|
|
|
}
|
|
|
|
|
2013-12-03 09:59:21 +01:00
|
|
|
Dictionary::Ptr ConfigItem::GetProperties(void)
|
|
|
|
{
|
2014-10-28 18:57:30 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2014-10-31 17:52:47 +01:00
|
|
|
VERIFY(!IsAbstract());
|
2014-10-28 18:57:30 +01:00
|
|
|
|
|
|
|
ObjectLock olock(this);
|
2013-12-12 06:30:11 +01:00
|
|
|
|
2014-03-24 11:23:47 +01:00
|
|
|
if (!m_Properties) {
|
2014-11-01 06:39:21 +01:00
|
|
|
Dictionary::Ptr locals = make_shared<Dictionary>();
|
|
|
|
locals->Set("__parent", m_Scope);
|
|
|
|
locals->Set("name", m_Name);
|
|
|
|
|
2014-03-24 11:23:47 +01:00
|
|
|
m_Properties = make_shared<Dictionary>();
|
2014-03-28 23:00:20 +01:00
|
|
|
m_Properties->Set("type", m_Type);
|
2014-05-03 20:02:22 +02:00
|
|
|
if (!m_Zone.IsEmpty())
|
|
|
|
m_Properties->Set("zone", m_Zone);
|
2014-11-01 06:39:21 +01:00
|
|
|
m_Properties->Set("__parent", locals);
|
2014-11-05 08:55:00 +01:00
|
|
|
GetExpressionList()->Evaluate(m_Properties, &m_DebugHints);
|
2014-03-24 11:23:47 +01:00
|
|
|
m_Properties->Remove("__parent");
|
2014-03-24 11:34:41 +01:00
|
|
|
|
2014-04-05 22:17:20 +02:00
|
|
|
String name = m_Name;
|
|
|
|
|
|
|
|
if (!m_Abstract) {
|
2014-11-03 00:44:04 +01:00
|
|
|
shared_ptr<NameComposer> nc = dynamic_pointer_cast<NameComposer>(Type::GetByName(m_Type));
|
2014-04-05 22:17:20 +02:00
|
|
|
|
2014-04-06 08:09:49 +02:00
|
|
|
if (nc) {
|
|
|
|
name = nc->MakeName(m_Name, m_Properties);
|
2014-04-05 22:17:20 +02:00
|
|
|
|
|
|
|
if (name.IsEmpty())
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Could not determine name for object"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-05 23:15:56 +02:00
|
|
|
if (name != m_Name)
|
|
|
|
m_Properties->Set("name", m_Name);
|
|
|
|
|
2014-04-05 22:17:20 +02:00
|
|
|
m_Properties->Set("__name", name);
|
|
|
|
|
|
|
|
VERIFY(m_Properties->Get("type") == GetType());
|
2014-03-24 11:23:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_Properties;
|
2013-12-03 09:59:21 +01:00
|
|
|
}
|
|
|
|
|
2014-11-05 08:55:00 +01:00
|
|
|
const DebugHint& ConfigItem::GetDebugHints(void) const
|
2014-08-12 15:31:47 +02:00
|
|
|
{
|
|
|
|
return m_DebugHints;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
2013-12-03 09:59:21 +01:00
|
|
|
* Commits the configuration item by creating a DynamicObject
|
2012-09-19 12:32:39 +02:00
|
|
|
* 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-11-13 14:56:31 +01:00
|
|
|
#ifdef _DEBUG
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "ConfigItem")
|
|
|
|
<< "Commit called for ConfigItem Type=" << GetType() << ", Name=" << GetName();
|
2013-11-13 14:56:31 +01:00
|
|
|
#endif /* _DEBUG */
|
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
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (IsAbstract())
|
|
|
|
return DynamicObject::Ptr();
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2014-10-28 18:57:30 +01:00
|
|
|
DynamicObject::Ptr dobj = dtype->CreateObject(GetProperties());
|
2014-09-09 14:49:21 +02:00
|
|
|
dobj->SetDebugInfo(m_DebugInfo);
|
2013-08-20 11:06:04 +02:00
|
|
|
dobj->Register();
|
2012-07-06 11:22:38 +02:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
m_Object = dobj;
|
2013-12-13 15:24:24 +01:00
|
|
|
|
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)
|
|
|
|
{
|
2014-04-05 22:17:20 +02:00
|
|
|
String name = m_Name;
|
|
|
|
|
|
|
|
/* If this is a non-abstract object we need to figure out
|
|
|
|
* its real name now - or assign it a temporary name. */
|
|
|
|
if (!m_Abstract) {
|
2014-11-03 00:44:04 +01:00
|
|
|
shared_ptr<NameComposer> nc = dynamic_pointer_cast<NameComposer>(Type::GetByName(m_Type));
|
2014-04-05 22:17:20 +02:00
|
|
|
|
2014-04-06 08:09:49 +02:00
|
|
|
if (nc) {
|
|
|
|
name = nc->MakeName(m_Name, Dictionary::Ptr());
|
2014-04-05 22:17:20 +02:00
|
|
|
|
|
|
|
ASSERT(name.IsEmpty() || name == m_Name);
|
|
|
|
|
|
|
|
if (name.IsEmpty())
|
|
|
|
name = Utility::NewUniqueID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<String, String> key = std::make_pair(m_Type, name);
|
2013-12-12 06:30:11 +01:00
|
|
|
ConfigItem::Ptr self = GetSelf();
|
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-03-19 07:09:06 +01:00
|
|
|
|
2013-12-12 06:30:11 +01:00
|
|
|
m_Items[key] = self;
|
2013-03-19 07:09:06 +01: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-12-12 06:30:11 +01:00
|
|
|
std::pair<String, String> key = std::make_pair(type, name);
|
2013-02-22 08:12:43 +01:00
|
|
|
ConfigItem::ItemMap::iterator it;
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-12-12 06:30:11 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
it = m_Items.find(key);
|
|
|
|
}
|
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-09-24 13:13:14 +02:00
|
|
|
void ConfigItem::ValidateItem(void)
|
2013-01-30 23:02:46 +01:00
|
|
|
{
|
2014-10-28 12:20:20 +01:00
|
|
|
if (m_Validated || IsAbstract())
|
2013-10-16 13:37:54 +02:00
|
|
|
return;
|
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
ConfigType::Ptr ctype = ConfigType::GetByName(GetType());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-09-24 13:13:14 +02:00
|
|
|
if (!ctype) {
|
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(false, "No validation type found for object '" + GetName() + "' of type '" + GetType() + "'");
|
|
|
|
|
|
|
|
return;
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
2013-09-24 13:13:14 +02:00
|
|
|
|
2014-10-28 11:54:56 +01:00
|
|
|
TypeRuleUtilities utils;
|
|
|
|
|
2014-10-01 16:13:11 +02:00
|
|
|
try {
|
2014-10-28 11:54:56 +01:00
|
|
|
ctype->ValidateItem(GetName(), GetProperties(), GetDebugInfo(), &utils);
|
2014-10-01 16:13:11 +02:00
|
|
|
} catch (const ConfigError& ex) {
|
|
|
|
const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
|
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, DiagnosticInformation(ex));
|
|
|
|
}
|
2013-10-16 13:37:54 +02:00
|
|
|
|
|
|
|
m_Validated = true;
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
|
2014-08-12 13:46:54 +02:00
|
|
|
void ConfigItem::WriteObjectsFile(const String& filename)
|
|
|
|
{
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "ConfigItem")
|
|
|
|
<< "Dumping config items to file '" << filename << "'";
|
2014-08-12 13:46:54 +02:00
|
|
|
|
|
|
|
String tempFilename = filename + ".tmp";
|
|
|
|
|
|
|
|
std::fstream fp;
|
|
|
|
fp.open(tempFilename.CStr(), std::ios_base::out);
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + tempFilename + "' file"));
|
|
|
|
|
|
|
|
StdioStream::Ptr sfp = make_shared<StdioStream>(&fp, false);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const ItemMap::value_type& kv, m_Items) {
|
|
|
|
ConfigItem::Ptr item = kv.second;
|
|
|
|
|
2014-10-31 17:52:47 +01:00
|
|
|
if (item->IsAbstract())
|
|
|
|
continue;
|
|
|
|
|
2014-08-12 13:46:54 +02:00
|
|
|
Dictionary::Ptr persistentItem = make_shared<Dictionary>();
|
|
|
|
|
|
|
|
persistentItem->Set("type", item->GetType());
|
|
|
|
persistentItem->Set("name", item->GetName());
|
2014-10-28 18:57:30 +01:00
|
|
|
persistentItem->Set("properties", item->GetProperties());
|
2014-11-05 08:55:00 +01:00
|
|
|
persistentItem->Set("debug_hints", item->GetDebugHints().ToDictionary());
|
2014-08-12 13:46:54 +02:00
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
String json = JsonEncode(persistentItem);
|
2014-08-12 13:46:54 +02:00
|
|
|
|
|
|
|
NetString::WriteStringToStream(sfp, json);
|
|
|
|
}
|
|
|
|
|
|
|
|
sfp->Close();
|
|
|
|
|
|
|
|
fp.close();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
_unlink(filename.CStr());
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
if (rename(tempFilename.CStr(), filename.CStr()) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("rename")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(tempFilename));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigItem::ValidateItems(const String& objectsFile)
|
2013-08-20 11:06:04 +02:00
|
|
|
{
|
2013-09-24 13:13:14 +02:00
|
|
|
if (ConfigCompilerContext::GetInstance()->HasErrors())
|
|
|
|
return false;
|
|
|
|
|
2013-12-10 21:44:38 +01:00
|
|
|
ParallelWorkQueue upq;
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2014-05-28 13:21:52 +02:00
|
|
|
Log(LogInformation, "ConfigItem", "Validating config items (step 1)...");
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2014-04-13 21:12:18 +02:00
|
|
|
BOOST_FOREACH(const ItemMap::value_type& kv, m_Items) {
|
|
|
|
upq.Enqueue(boost::bind(&ConfigItem::ValidateItem, kv.second));
|
|
|
|
}
|
2013-10-16 13:37:54 +02:00
|
|
|
|
2014-04-13 21:12:18 +02:00
|
|
|
upq.Join();
|
2014-03-19 20:59:18 +01:00
|
|
|
|
2014-04-13 21:12:18 +02:00
|
|
|
if (ConfigCompilerContext::GetInstance()->HasErrors())
|
|
|
|
return false;
|
2013-09-24 13:13:14 +02:00
|
|
|
|
2014-05-28 13:21:52 +02:00
|
|
|
Log(LogInformation, "ConfigItem", "Committing config items");
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
BOOST_FOREACH(const ItemMap::value_type& kv, m_Items) {
|
2013-12-10 21:44:38 +01:00
|
|
|
upq.Enqueue(boost::bind(&ConfigItem::Commit, kv.second));
|
2013-12-06 21:46:50 +01:00
|
|
|
}
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2013-12-10 21:44:38 +01:00
|
|
|
upq.Join();
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
std::vector<DynamicObject::Ptr> objects;
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const ItemMap::value_type& kv, m_Items) {
|
2013-12-12 06:30:11 +01:00
|
|
|
DynamicObject::Ptr object = kv.second->m_Object;
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (object)
|
|
|
|
objects.push_back(object);
|
|
|
|
}
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2014-05-28 13:21:52 +02:00
|
|
|
Log(LogInformation, "ConfigItem", "Triggering OnConfigLoaded signal for config items");
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2013-08-29 19:05:06 +02:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, objects) {
|
2013-12-10 21:44:38 +01:00
|
|
|
upq.Enqueue(boost::bind(&DynamicObject::OnConfigLoaded, object));
|
2013-08-29 19:05:06 +02:00
|
|
|
}
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2013-12-10 21:44:38 +01:00
|
|
|
upq.Join();
|
2013-08-29 19:05:06 +02:00
|
|
|
|
2014-05-28 13:21:52 +02:00
|
|
|
Log(LogInformation, "ConfigItem", "Evaluating 'object' rules (step 1)...");
|
2014-05-02 00:23:29 +02:00
|
|
|
ObjectRule::EvaluateRules(false);
|
2014-05-01 23:51:42 +02:00
|
|
|
|
2014-05-28 13:21:52 +02:00
|
|
|
Log(LogInformation, "ConfigItem", "Evaluating 'apply' rules...");
|
2014-05-02 00:23:29 +02:00
|
|
|
ApplyRule::EvaluateRules(true);
|
2014-04-23 12:44:36 +02:00
|
|
|
|
2014-05-28 13:21:52 +02:00
|
|
|
Log(LogInformation, "ConfigItem", "Evaluating 'object' rules (step 2)...");
|
2014-05-02 00:23:29 +02:00
|
|
|
ObjectRule::EvaluateRules(true);
|
2014-03-18 11:44:09 +01:00
|
|
|
|
2014-05-28 13:21:52 +02:00
|
|
|
Log(LogInformation, "ConfigItem", "Validating config items (step 2)...");
|
2013-10-16 13:37:54 +02:00
|
|
|
|
2014-04-13 21:12:18 +02:00
|
|
|
BOOST_FOREACH(const ItemMap::value_type& kv, m_Items) {
|
|
|
|
upq.Enqueue(boost::bind(&ConfigItem::ValidateItem, kv.second));
|
|
|
|
}
|
2013-09-24 13:13:14 +02:00
|
|
|
|
2014-04-13 21:12:18 +02:00
|
|
|
upq.Join();
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2014-08-12 13:46:54 +02:00
|
|
|
if (!objectsFile.IsEmpty())
|
|
|
|
ConfigItem::WriteObjectsFile(objectsFile);
|
|
|
|
|
2013-12-20 13:15:05 +01:00
|
|
|
ConfigItem::DiscardItems();
|
|
|
|
ConfigType::DiscardTypes();
|
|
|
|
|
2014-04-13 21:12:18 +02:00
|
|
|
/* log stats for external parsers */
|
|
|
|
BOOST_FOREACH(const DynamicType::Ptr& type, DynamicType::GetTypes()) {
|
|
|
|
int count = std::distance(type->GetObjects().first, type->GetObjects().second);
|
|
|
|
if (count > 0)
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "ConfigItem")
|
|
|
|
<< "Checked " << count << " " << type->GetName() << "(s).";
|
2013-12-13 15:24:24 +01:00
|
|
|
}
|
|
|
|
|
2014-04-13 21:12:18 +02:00
|
|
|
return !ConfigCompilerContext::GetInstance()->HasErrors();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigItem::ActivateItems(void)
|
|
|
|
{
|
2013-09-24 13:13:14 +02:00
|
|
|
if (ConfigCompilerContext::GetInstance()->HasErrors())
|
|
|
|
return false;
|
|
|
|
|
2013-08-29 10:40:43 +02:00
|
|
|
/* restore the previous program state */
|
2014-01-28 14:32:56 +01:00
|
|
|
try {
|
|
|
|
DynamicObject::RestoreObjects(Application::GetStatePath());
|
|
|
|
} catch (const std::exception& ex) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "ConfigItem")
|
|
|
|
<< "Failed to restore state file: " << DiagnosticInformation(ex);
|
2014-01-28 14:32:56 +01:00
|
|
|
}
|
2013-08-29 10:40:43 +02:00
|
|
|
|
2014-05-28 13:21:52 +02:00
|
|
|
Log(LogInformation, "ConfigItem", "Triggering Start signal for config items");
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2014-04-13 21:12:18 +02:00
|
|
|
ParallelWorkQueue upq;
|
|
|
|
|
2013-08-29 19:25:34 +02:00
|
|
|
BOOST_FOREACH(const DynamicType::Ptr& type, DynamicType::GetTypes()) {
|
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, type->GetObjects()) {
|
|
|
|
if (object->IsActive())
|
|
|
|
continue;
|
2013-08-29 19:05:06 +02:00
|
|
|
|
2013-11-13 14:56:31 +01:00
|
|
|
#ifdef _DEBUG
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "ConfigItem")
|
|
|
|
<< "Activating object '" << object->GetName() << "' of type '" << object->GetType()->GetName() << "'";
|
2013-11-13 14:56:31 +01:00
|
|
|
#endif /* _DEBUG */
|
2013-12-14 07:36:49 +01:00
|
|
|
upq.Enqueue(boost::bind(&DynamicObject::Activate, object));
|
2013-12-06 21:46:50 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2013-12-10 21:44:38 +01:00
|
|
|
upq.Join();
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
#ifdef _DEBUG
|
|
|
|
BOOST_FOREACH(const DynamicType::Ptr& type, DynamicType::GetTypes()) {
|
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, type->GetObjects()) {
|
2013-08-29 19:25:34 +02:00
|
|
|
ASSERT(object->IsActive());
|
|
|
|
}
|
2013-02-06 00:32:05 +01:00
|
|
|
}
|
2013-12-06 21:46:50 +01:00
|
|
|
#endif /* _DEBUG */
|
|
|
|
|
2014-05-28 13:21:52 +02:00
|
|
|
Log(LogInformation, "ConfigItem", "Activated all objects.");
|
2013-09-24 13:13:14 +02:00
|
|
|
|
|
|
|
return true;
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void ConfigItem::DiscardItems(void)
|
|
|
|
{
|
2013-12-12 06:30:11 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
m_Items.clear();
|
2013-02-06 00:32:05 +01:00
|
|
|
}
|