2012-06-06 14:38:28 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 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 "base/application.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#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-11-06 19:35:47 +01:00
|
|
|
#include "base/serializer.hpp"
|
2014-10-26 19:59:49 +01:00
|
|
|
#include "base/json.hpp"
|
2014-12-18 15:11:57 +01:00
|
|
|
#include "base/exception.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.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;
|
2014-11-16 16:20:39 +01:00
|
|
|
ConfigItem::TypeMap ConfigItem::m_Items;
|
2014-11-09 04:17:34 +01:00
|
|
|
ConfigItem::ItemList ConfigItem::m_UnnamedItems;
|
2015-03-18 12:11:42 +01:00
|
|
|
ConfigItem::ItemList ConfigItem::m_CommittedItems;
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2015-08-12 15:27:35 +02:00
|
|
|
REGISTER_SCRIPTFUNCTION(commit_objects, &ConfigItem::CommitAndActivate);
|
2014-12-19 13:08:46 +01:00
|
|
|
|
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-11-09 19:48:28 +01:00
|
|
|
bool abstract, const boost::shared_ptr<Expression>& exprl,
|
2014-11-16 16:20:39 +01:00
|
|
|
const boost::shared_ptr<Expression>& filter,
|
2014-11-22 12:21:28 +01:00
|
|
|
const DebugInfo& debuginfo, const Dictionary::Ptr& scope,
|
2015-08-28 17:58:29 +02:00
|
|
|
const String& zone, const String& package)
|
2014-11-06 19:35:47 +01:00
|
|
|
: m_Type(type), m_Name(name), m_Abstract(abstract),
|
2014-11-16 16:20:39 +01:00
|
|
|
m_Expression(exprl), m_Filter(filter),
|
2015-08-17 16:08:57 +02:00
|
|
|
m_DebugInfo(debuginfo), m_Scope(scope), m_Zone(zone),
|
2015-08-28 17:58:29 +02:00
|
|
|
m_Package(package)
|
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-11-22 12:21:28 +01:00
|
|
|
Dictionary::Ptr ConfigItem::GetScope(void) const
|
2014-03-24 11:23:47 +01:00
|
|
|
{
|
|
|
|
return m_Scope;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the expression list for the configuration item.
|
|
|
|
*
|
|
|
|
* @returns The expression list.
|
|
|
|
*/
|
2014-11-09 19:48:28 +01:00
|
|
|
boost::shared_ptr<Expression> ConfigItem::GetExpression(void) const
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
2014-11-09 19:48:28 +01:00
|
|
|
return m_Expression;
|
2012-06-01 16:49:33 +02:00
|
|
|
}
|
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the object filter for the configuration item.
|
|
|
|
*
|
|
|
|
* @returns The filter expression.
|
|
|
|
*/
|
|
|
|
boost::shared_ptr<Expression> ConfigItem::GetFilter(void) const
|
|
|
|
{
|
|
|
|
return m_Filter;
|
|
|
|
}
|
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
class DefaultValidationUtils : public ValidationUtils
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool ValidateName(const String& type, const String& name) const override
|
|
|
|
{
|
|
|
|
return ConfigItem::GetObject(type, name) != ConfigItem::Ptr();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
2015-08-15 20:28:05 +02:00
|
|
|
* Commits the configuration item by creating a ConfigObject
|
2012-09-19 12:32:39 +02:00
|
|
|
* object.
|
|
|
|
*
|
2015-08-15 20:28:05 +02:00
|
|
|
* @returns The ConfigObject that was created/updated.
|
2012-09-19 12:32:39 +02:00
|
|
|
*/
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::Ptr ConfigItem::Commit(bool discard)
|
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
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "ConfigItem")
|
|
|
|
<< "Commit called for ConfigItem Type=" << GetType() << ", Name=" << GetName();
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2013-01-31 15:26:54 +01:00
|
|
|
|
2013-02-08 21:05:08 +01:00
|
|
|
/* Make sure the type is valid. */
|
2014-11-06 19:35:47 +01:00
|
|
|
Type::Ptr type = Type::GetByName(GetType());
|
2015-08-15 20:28:05 +02:00
|
|
|
ASSERT(type && ConfigObject::TypeInstance->IsAssignableFrom(type));
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (IsAbstract())
|
2015-08-15 20:28:05 +02:00
|
|
|
return ConfigObject::Ptr();
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::Ptr dobj = static_pointer_cast<ConfigObject>(type->Instantiate());
|
2014-11-06 19:35:47 +01:00
|
|
|
|
2014-09-09 14:49:21 +02:00
|
|
|
dobj->SetDebugInfo(m_DebugInfo);
|
2015-02-09 08:50:17 +01:00
|
|
|
dobj->SetTypeNameV(m_Type);
|
|
|
|
dobj->SetZoneName(m_Zone);
|
2015-08-28 17:58:29 +02:00
|
|
|
dobj->SetPackage(m_Package);
|
2014-11-15 15:57:23 +01:00
|
|
|
dobj->SetName(m_Name);
|
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
DebugHint debugHints;
|
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
ScriptFrame frame(dobj);
|
|
|
|
if (m_Scope)
|
|
|
|
m_Scope->CopyTo(frame.Locals);
|
|
|
|
m_Expression->Evaluate(frame, &debugHints);
|
2014-11-06 19:35:47 +01:00
|
|
|
|
|
|
|
if (discard)
|
2014-11-09 19:48:28 +01:00
|
|
|
m_Expression.reset();
|
2014-11-06 19:35:47 +01:00
|
|
|
|
2014-12-09 20:37:20 +01:00
|
|
|
String item_name;
|
|
|
|
String short_name = dobj->GetShortName();
|
|
|
|
|
|
|
|
if (!short_name.IsEmpty()) {
|
|
|
|
item_name = short_name;
|
|
|
|
dobj->SetName(short_name);
|
|
|
|
} else
|
|
|
|
item_name = m_Name;
|
|
|
|
|
|
|
|
String name = item_name;
|
2014-11-06 19:35:47 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
NameComposer *nc = dynamic_cast<NameComposer *>(type.get());
|
2014-11-06 19:35:47 +01:00
|
|
|
|
|
|
|
if (nc) {
|
2014-12-09 20:37:20 +01:00
|
|
|
name = nc->MakeName(name, dobj);
|
2014-11-06 19:35:47 +01:00
|
|
|
|
|
|
|
if (name.IsEmpty())
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Could not determine name for object"));
|
|
|
|
}
|
|
|
|
|
2014-12-09 20:37:20 +01:00
|
|
|
if (name != item_name)
|
|
|
|
dobj->SetShortName(item_name);
|
2014-11-06 19:35:47 +01:00
|
|
|
|
|
|
|
dobj->SetName(name);
|
2014-11-21 18:31:37 +01:00
|
|
|
dobj->OnConfigLoaded();
|
|
|
|
|
2015-03-18 12:11:42 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
m_CommittedItems.push_back(this);
|
|
|
|
}
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr persistentItem = new Dictionary();
|
2014-11-06 19:35:47 +01:00
|
|
|
|
|
|
|
persistentItem->Set("type", GetType());
|
|
|
|
persistentItem->Set("name", GetName());
|
2015-03-10 15:23:27 +01:00
|
|
|
persistentItem->Set("properties", Serialize(dobj, FAConfig));
|
2014-11-30 23:32:13 +01:00
|
|
|
|
|
|
|
Dictionary::Ptr dhint = debugHints.ToDictionary();
|
|
|
|
persistentItem->Set("debug_hints", dhint);
|
2014-11-06 19:35:47 +01:00
|
|
|
|
2015-02-05 15:05:07 +01:00
|
|
|
Array::Ptr di = new Array();
|
|
|
|
di->Add(m_DebugInfo.Path);
|
|
|
|
di->Add(m_DebugInfo.FirstLine);
|
|
|
|
di->Add(m_DebugInfo.FirstColumn);
|
|
|
|
di->Add(m_DebugInfo.LastLine);
|
|
|
|
di->Add(m_DebugInfo.LastColumn);
|
|
|
|
persistentItem->Set("debug_info", di);
|
|
|
|
|
2014-11-06 19:35:47 +01:00
|
|
|
ConfigCompilerContext::GetInstance()->WriteObject(persistentItem);
|
2014-11-11 23:06:47 +01:00
|
|
|
persistentItem.reset();
|
2014-11-06 19:35:47 +01:00
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
try {
|
|
|
|
DefaultValidationUtils utils;
|
|
|
|
dobj->Validate(FAConfig, utils);
|
|
|
|
} catch (ValidationError& ex) {
|
|
|
|
ex.SetDebugHint(dhint);
|
|
|
|
throw;
|
2014-11-06 19:35:47 +01:00
|
|
|
}
|
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
dhint.reset();
|
|
|
|
|
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-11-08 21:17:16 +01:00
|
|
|
Type::Ptr type = Type::GetByName(m_Type);
|
2013-12-12 06:30:11 +01:00
|
|
|
|
2014-11-09 04:17:34 +01:00
|
|
|
/* If this is a non-abstract object with a composite name
|
|
|
|
* we register it in m_UnnamedItems instead of m_Items. */
|
2014-11-08 21:17:16 +01:00
|
|
|
if (!m_Abstract && dynamic_cast<NameComposer *>(type.get())) {
|
2014-11-09 04:17:34 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2014-11-08 21:17:16 +01:00
|
|
|
m_UnnamedItems.push_back(this);
|
2014-11-09 04:17:34 +01:00
|
|
|
} else {
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2015-08-14 20:01:12 +02:00
|
|
|
|
|
|
|
ItemMap::const_iterator it = m_Items[m_Type].find(m_Name);
|
|
|
|
|
|
|
|
if (it != m_Items[m_Type].end()) {
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "A configuration item of type '" << GetType()
|
|
|
|
<< "' and name '" << GetName() << "' already exists ("
|
|
|
|
<< it->second->GetDebugInfo() << "), new declaration: " << GetDebugInfo();
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str()));
|
|
|
|
}
|
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
m_Items[m_Type][m_Name] = this;
|
2014-11-09 04:17:34 +01:00
|
|
|
}
|
2013-03-19 07:09:06 +01:00
|
|
|
}
|
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
/**
|
|
|
|
* Unregisters the configuration item.
|
|
|
|
*/
|
|
|
|
void ConfigItem::Unregister(void)
|
|
|
|
{
|
|
|
|
if (m_Object)
|
|
|
|
m_Object->Unregister();
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
m_UnnamedItems.erase(std::remove(m_UnnamedItems.begin(), m_UnnamedItems.end(), this), m_UnnamedItems.end());
|
|
|
|
m_Items[m_Type].erase(m_Name);
|
2015-08-25 17:19:31 +02:00
|
|
|
m_CommittedItems.erase(std::remove(m_CommittedItems.begin(), m_CommittedItems.end(), this), m_CommittedItems.end());
|
2015-08-13 09:02:52 +02: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
|
|
|
{
|
2015-03-18 12:11:42 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-12-12 06:30:11 +01:00
|
|
|
|
2015-03-18 12:11:42 +01:00
|
|
|
ConfigItem::TypeMap::const_iterator it = m_Items.find(type);
|
2014-11-16 16:20:39 +01:00
|
|
|
|
2015-03-18 12:11:42 +01:00
|
|
|
if (it == m_Items.end())
|
|
|
|
return ConfigItem::Ptr();
|
2014-11-16 16:20:39 +01:00
|
|
|
|
2015-03-18 12:11:42 +01:00
|
|
|
ConfigItem::ItemMap::const_iterator it2 = it->second.find(name);
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2015-03-18 12:11:42 +01:00
|
|
|
if (it2 == it->second.end())
|
|
|
|
return ConfigItem::Ptr();
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2015-03-18 12:11:42 +01:00
|
|
|
return it2->second;
|
2012-06-05 15:05:15 +02:00
|
|
|
}
|
2013-01-30 23:02:46 +01:00
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
bool ConfigItem::CommitNewItems(WorkQueue& upq, std::vector<ConfigItem::Ptr>& newItems)
|
2013-08-20 11:06:04 +02:00
|
|
|
{
|
2014-11-16 16:20:39 +01:00
|
|
|
typedef std::pair<ConfigItem::Ptr, bool> ItemPair;
|
|
|
|
std::vector<ItemPair> items;
|
2013-09-24 13:13:14 +02:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2014-11-09 04:17:34 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
BOOST_FOREACH(const TypeMap::value_type& kv, m_Items) {
|
2015-08-25 17:19:59 +02:00
|
|
|
BOOST_FOREACH(const ItemMap::value_type& kv2, kv.second) {
|
2015-03-19 15:47:46 +01:00
|
|
|
if (!kv2.second->m_Abstract && !kv2.second->m_Object)
|
|
|
|
items.push_back(std::make_pair(kv2.second, false));
|
2014-11-15 15:57:23 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-13 23:25:52 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
BOOST_FOREACH(const ConfigItem::Ptr& item, m_UnnamedItems) {
|
|
|
|
if (!item->m_Abstract && !item->m_Object)
|
|
|
|
items.push_back(std::make_pair(item, true));
|
2014-11-16 16:20:39 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
m_UnnamedItems.clear();
|
|
|
|
}
|
2013-02-06 00:32:05 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
if (items.empty())
|
|
|
|
return true;
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
BOOST_FOREACH(const ItemPair& ip, items) {
|
2015-08-13 09:02:52 +02:00
|
|
|
newItems.push_back(ip.first);
|
2015-03-19 15:47:46 +01:00
|
|
|
upq.Enqueue(boost::bind(&ConfigItem::Commit, ip.first, ip.second));
|
|
|
|
}
|
2014-11-21 18:31:37 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
upq.Join();
|
|
|
|
|
|
|
|
if (upq.HasExceptions())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::vector<ConfigItem::Ptr> new_items;
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
new_items.swap(m_CommittedItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<String> types;
|
2015-03-18 12:11:42 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
std::vector<Type::Ptr> all_types;
|
|
|
|
Dictionary::Ptr globals = ScriptGlobal::GetGlobals();
|
2015-02-25 12:43:03 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(globals);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, globals) {
|
|
|
|
if (kv.second.IsObjectType<Type>())
|
|
|
|
all_types.push_back(kv.second);
|
2014-11-15 15:57:23 +01:00
|
|
|
}
|
2015-03-19 15:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Type::Ptr& type, all_types) {
|
2015-08-15 20:28:05 +02:00
|
|
|
if (ConfigObject::TypeInstance->IsAssignableFrom(type))
|
2015-03-19 15:47:46 +01:00
|
|
|
types.insert(type->GetName());
|
|
|
|
}
|
2014-11-09 04:17:34 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
std::set<String> completed_types;
|
2015-02-25 12:43:03 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
while (types.size() != completed_types.size()) {
|
|
|
|
BOOST_FOREACH(const String& type, types) {
|
|
|
|
if (completed_types.find(type) != completed_types.end())
|
|
|
|
continue;
|
2015-02-25 12:43:03 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
Type::Ptr ptype = Type::GetByName(type);
|
|
|
|
bool unresolved_dep = false;
|
2015-02-25 12:43:03 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
/* skip this type (for now) if there are unresolved load dependencies */
|
|
|
|
BOOST_FOREACH(const String& loadDep, ptype->GetLoadDependencies()) {
|
|
|
|
if (types.find(loadDep) != types.end() && completed_types.find(loadDep) == completed_types.end()) {
|
|
|
|
unresolved_dep = true;
|
|
|
|
break;
|
2015-02-25 12:43:03 +01:00
|
|
|
}
|
2015-03-19 15:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unresolved_dep)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const ConfigItem::Ptr& item, new_items) {
|
2015-08-25 17:19:59 +02:00
|
|
|
ASSERT(item->m_Object);
|
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
if (item->m_Type == type)
|
2015-08-15 20:28:05 +02:00
|
|
|
upq.Enqueue(boost::bind(&ConfigObject::OnAllConfigLoaded, item->m_Object));
|
2015-03-19 15:47:46 +01:00
|
|
|
}
|
2015-02-25 12:43:03 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
completed_types.insert(type);
|
2015-02-25 12:43:03 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
upq.Join();
|
|
|
|
|
|
|
|
if (upq.HasExceptions())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const String& loadDep, ptype->GetLoadDependencies()) {
|
|
|
|
BOOST_FOREACH(const ConfigItem::Ptr& item, new_items) {
|
2015-08-25 17:19:59 +02:00
|
|
|
ASSERT(item->m_Object);
|
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
if (item->m_Type == loadDep)
|
2015-08-15 20:28:05 +02:00
|
|
|
upq.Enqueue(boost::bind(&ConfigObject::CreateChildObjects, item->m_Object, ptype));
|
2015-02-25 12:43:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
upq.Join();
|
2015-03-09 08:04:18 +01:00
|
|
|
|
|
|
|
if (upq.HasExceptions())
|
|
|
|
return false;
|
2015-03-19 15:47:46 +01:00
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
if (!CommitNewItems(upq, newItems))
|
2015-03-19 15:47:46 +01:00
|
|
|
return false;
|
2015-02-25 12:43:03 +01:00
|
|
|
}
|
2015-03-19 15:47:46 +01:00
|
|
|
}
|
2014-11-09 04:17:34 +01:00
|
|
|
|
2014-11-15 15:57:23 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-11-09 04:17:34 +01:00
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
bool ConfigItem::CommitItems(WorkQueue& upq)
|
2014-11-15 15:57:23 +01:00
|
|
|
{
|
|
|
|
Log(LogInformation, "ConfigItem", "Committing config items");
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
std::vector<ConfigItem::Ptr> newItems;
|
|
|
|
|
|
|
|
if (!CommitNewItems(upq, newItems)) {
|
2014-12-18 15:11:57 +01:00
|
|
|
upq.ReportExceptions("config");
|
2015-08-13 09:02:52 +02:00
|
|
|
|
|
|
|
BOOST_FOREACH(const ConfigItem::Ptr& item, newItems) {
|
|
|
|
item->Unregister();
|
|
|
|
}
|
|
|
|
|
2014-11-15 15:57:23 +01:00
|
|
|
return false;
|
2014-12-18 15:11:57 +01:00
|
|
|
}
|
2013-08-29 19:05:06 +02:00
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
ApplyRule::CheckMatches();
|
2013-12-20 13:15:05 +01:00
|
|
|
|
2014-04-13 21:12:18 +02:00
|
|
|
/* log stats for external parsers */
|
2015-08-13 09:02:52 +02:00
|
|
|
typedef std::map<Type::Ptr, int> ItemCountMap;
|
|
|
|
ItemCountMap itemCounts;
|
|
|
|
BOOST_FOREACH(const ConfigItem::Ptr& item, newItems) {
|
|
|
|
itemCounts[item->m_Object->GetReflectionType()]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FOREACH(const ItemCountMap::value_type& kv, itemCounts) {
|
|
|
|
Log(LogInformation, "ConfigItem")
|
2015-08-14 20:01:12 +02:00
|
|
|
<< "Instantiated " << kv.second << " " << (kv.second != 1 ? kv.first->GetPluralName() : kv.first->GetName()) << ".";
|
2013-12-13 15:24:24 +01:00
|
|
|
}
|
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
return true;
|
2014-04-13 21:12:18 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
bool ConfigItem::ActivateItems(WorkQueue& upq, bool restoreState)
|
2014-04-13 21:12:18 +02:00
|
|
|
{
|
2015-08-13 09:02:52 +02:00
|
|
|
if (restoreState) {
|
|
|
|
/* restore the previous program state */
|
|
|
|
try {
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::RestoreObjects(Application::GetStatePath());
|
2015-08-13 09:02:52 +02:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
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
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
BOOST_FOREACH(const ConfigType::Ptr& type, ConfigType::GetTypes()) {
|
|
|
|
BOOST_FOREACH(const ConfigObject::Ptr& object, type->GetObjects()) {
|
2013-08-29 19:25:34 +02:00
|
|
|
if (object->IsActive())
|
|
|
|
continue;
|
2013-08-29 19:05:06 +02:00
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "ConfigItem")
|
|
|
|
<< "Activating object '" << object->GetName() << "' of type '" << object->GetType()->GetName() << "'";
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2015-08-15 20:28:05 +02:00
|
|
|
upq.Enqueue(boost::bind(&ConfigObject::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();
|
2015-01-29 14:40:49 +01:00
|
|
|
|
|
|
|
if (upq.HasExceptions()) {
|
|
|
|
upq.ReportExceptions("ConfigItem");
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-13 15:24:24 +01:00
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2015-08-15 20:28:05 +02:00
|
|
|
BOOST_FOREACH(const ConfigType::Ptr& type, ConfigType::GetTypes()) {
|
|
|
|
BOOST_FOREACH(const ConfigObject::Ptr& object, type->GetObjects()) {
|
2013-08-29 19:25:34 +02:00
|
|
|
ASSERT(object->IsActive());
|
|
|
|
}
|
2013-02-06 00:32:05 +01:00
|
|
|
}
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2013-12-06 21:46:50 +01:00
|
|
|
|
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
|
|
|
|
2015-08-12 15:27:35 +02:00
|
|
|
bool ConfigItem::CommitAndActivate(void)
|
2014-12-19 13:08:46 +01:00
|
|
|
{
|
|
|
|
WorkQueue upq(25000, Application::GetConcurrency());
|
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
if (!CommitItems(upq))
|
2014-12-19 13:08:46 +01:00
|
|
|
return false;
|
2015-03-18 11:12:14 +01:00
|
|
|
|
2015-08-13 09:02:52 +02:00
|
|
|
if (!ActivateItems(upq, false))
|
2015-03-18 11:12:14 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-19 13:08:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
std::vector<ConfigItem::Ptr> ConfigItem::GetItems(const String& type)
|
|
|
|
{
|
|
|
|
std::vector<ConfigItem::Ptr> items;
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
TypeMap::const_iterator it = m_Items.find(type);
|
|
|
|
|
|
|
|
if (it == m_Items.end())
|
|
|
|
return items;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const ItemMap::value_type& kv, it->second)
|
|
|
|
{
|
|
|
|
items.push_back(kv.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}
|