2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-10-18 09:27:04 +02:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
|
2012-05-10 12:06:41 +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 *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configobject.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "base/configobject-ti.cpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/serializer.hpp"
|
|
|
|
#include "base/netstring.hpp"
|
2014-10-26 19:59:49 +01:00
|
|
|
#include "base/json.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/stdiostream.hpp"
|
|
|
|
#include "base/debug.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/exception.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/initialize.hpp"
|
|
|
|
#include "base/workqueue.hpp"
|
2014-10-30 13:04:00 +01:00
|
|
|
#include "base/context.hpp"
|
2014-12-18 15:11:57 +01:00
|
|
|
#include "base/application.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <fstream>
|
2013-03-18 17:04:22 +01:00
|
|
|
#include <boost/exception/errinfo_api_function.hpp>
|
|
|
|
#include <boost/exception/errinfo_errno.hpp>
|
|
|
|
#include <boost/exception/errinfo_file_name.hpp>
|
2012-03-31 15:18:09 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
REGISTER_TYPE_WITH_PROTOTYPE(ConfigObject, ConfigObject::GetPrototype());
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
boost::signals2::signal<void (const ConfigObject::Ptr&)> ConfigObject::OnStateChanged;
|
2012-08-02 09:38:08 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool ConfigObject::IsActive() const
|
2012-08-02 09:38:08 +02:00
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
return GetActive();
|
2013-09-12 10:03:48 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool ConfigObject::IsPaused() const
|
2014-05-09 11:32:24 +02:00
|
|
|
{
|
|
|
|
return GetPaused();
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
void ConfigObject::SetExtension(const String& key, const Value& value)
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
Dictionary::Ptr extensions = GetExtensions();
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
if (!extensions) {
|
2014-11-08 21:17:16 +01:00
|
|
|
extensions = new Dictionary();
|
2013-10-26 09:41:45 +02:00
|
|
|
SetExtensions(extensions);
|
2013-07-05 09:37:04 +02:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:23:57 +01:00
|
|
|
extensions->Set(key, value);
|
2013-07-05 09:37:04 +02:00
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
Value ConfigObject::GetExtension(const String& key)
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
Dictionary::Ptr extensions = GetExtensions();
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
if (!extensions)
|
2014-11-13 11:23:57 +01:00
|
|
|
return Empty;
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
return extensions->Get(key);
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
void ConfigObject::ClearExtension(const String& key)
|
2013-07-05 09:37:04 +02:00
|
|
|
{
|
2013-10-26 09:41:45 +02:00
|
|
|
Dictionary::Ptr extensions = GetExtensions();
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
if (!extensions)
|
|
|
|
return;
|
|
|
|
|
|
|
|
extensions->Remove(key);
|
|
|
|
}
|
|
|
|
|
2018-01-04 06:11:04 +01:00
|
|
|
class ModAttrValidationUtils final : public ValidationUtils
|
2015-08-13 08:52:00 +02:00
|
|
|
{
|
|
|
|
public:
|
2018-01-04 05:12:56 +01:00
|
|
|
bool ValidateName(const String& type, const String& name) const override
|
2015-08-13 08:52:00 +02:00
|
|
|
{
|
2016-08-16 11:02:10 +02:00
|
|
|
Type::Ptr ptype = Type::GetByName(type);
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *dtype = dynamic_cast<ConfigType *>(ptype.get());
|
2015-08-13 08:52:00 +02:00
|
|
|
|
|
|
|
if (!dtype)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!dtype->GetObject(name))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-11 14:09:46 +02:00
|
|
|
void ConfigObject::ModifyAttribute(const String& attr, const Value& value, bool updateVersion)
|
2015-08-04 14:47:44 +02:00
|
|
|
{
|
|
|
|
Dictionary::Ptr original_attributes = GetOriginalAttributes();
|
|
|
|
bool updated_original_attributes = false;
|
|
|
|
|
|
|
|
Type::Ptr type = GetReflectionType();
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> tokens = attr.Split(".");
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2015-08-17 14:55:51 +02:00
|
|
|
String fieldName = tokens[0];
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2015-08-17 14:55:51 +02:00
|
|
|
int fid = type->GetFieldId(fieldName);
|
2015-08-04 14:47:44 +02:00
|
|
|
Field field = type->GetFieldInfo(fid);
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2015-10-20 08:20:35 +02:00
|
|
|
if (field.Attributes & FANoUserModify)
|
2015-10-15 16:33:20 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Attribute cannot be modified."));
|
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
if (field.Attributes & FAConfig) {
|
|
|
|
if (!original_attributes) {
|
|
|
|
original_attributes = new Dictionary();
|
|
|
|
SetOriginalAttributes(original_attributes, true);
|
|
|
|
}
|
|
|
|
}
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
Value oldValue = GetField(fid);
|
2015-08-17 14:55:51 +02:00
|
|
|
Value newValue;
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2015-08-17 14:55:51 +02:00
|
|
|
if (tokens.size() > 1) {
|
|
|
|
newValue = oldValue.Clone();
|
|
|
|
Value current = newValue;
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2015-08-17 14:55:51 +02:00
|
|
|
if (current.IsEmpty()) {
|
|
|
|
current = new Dictionary();
|
|
|
|
newValue = current;
|
|
|
|
}
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
String prefix = tokens[0];
|
|
|
|
|
2015-08-17 14:55:51 +02:00
|
|
|
for (std::vector<String>::size_type i = 1; i < tokens.size() - 1; i++) {
|
|
|
|
if (!current.IsObjectType<Dictionary>())
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Value must be a dictionary."));
|
|
|
|
|
|
|
|
Dictionary::Ptr dict = current;
|
2015-09-21 13:00:50 +02:00
|
|
|
|
2015-08-17 14:55:51 +02:00
|
|
|
const String& key = tokens[i];
|
2015-09-21 13:00:50 +02:00
|
|
|
prefix += "." + key;
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2016-08-16 22:03:30 +02:00
|
|
|
if (!dict->Get(key, ¤t)) {
|
2015-08-17 14:55:51 +02:00
|
|
|
current = new Dictionary();
|
|
|
|
dict->Set(key, current);
|
|
|
|
}
|
|
|
|
}
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2015-08-17 14:55:51 +02:00
|
|
|
if (!current.IsObjectType<Dictionary>())
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Value must be a dictionary."));
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2015-08-17 14:55:51 +02:00
|
|
|
Dictionary::Ptr dict = current;
|
2015-09-21 13:00:50 +02:00
|
|
|
|
2015-08-17 14:55:51 +02:00
|
|
|
const String& key = tokens[tokens.size() - 1];
|
2015-09-21 13:00:50 +02:00
|
|
|
prefix += "." + key;
|
|
|
|
|
|
|
|
/* clone it for original attributes */
|
|
|
|
oldValue = dict->Get(key).Clone();
|
|
|
|
|
|
|
|
if (field.Attributes & FAConfig) {
|
|
|
|
updated_original_attributes = true;
|
|
|
|
|
|
|
|
if (oldValue.IsObjectType<Dictionary>()) {
|
|
|
|
Dictionary::Ptr oldDict = oldValue;
|
|
|
|
ObjectLock olock(oldDict);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const auto& kv : oldDict) {
|
2015-09-21 13:00:50 +02:00
|
|
|
String key = prefix + "." + kv.first;
|
|
|
|
if (!original_attributes->Contains(key))
|
|
|
|
original_attributes->Set(key, kv.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store the new value as null */
|
|
|
|
if (value.IsObjectType<Dictionary>()) {
|
|
|
|
Dictionary::Ptr valueDict = value;
|
|
|
|
ObjectLock olock(valueDict);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const auto& kv : valueDict) {
|
2015-09-21 13:00:50 +02:00
|
|
|
String key = attr + "." + kv.first;
|
|
|
|
if (!original_attributes->Contains(key))
|
|
|
|
original_attributes->Set(key, Empty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!original_attributes->Contains(attr))
|
|
|
|
original_attributes->Set(attr, oldValue);
|
|
|
|
}
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2015-08-17 14:55:51 +02:00
|
|
|
dict->Set(key, value);
|
2015-09-21 13:00:50 +02:00
|
|
|
} else {
|
2015-08-17 14:55:51 +02:00
|
|
|
newValue = value;
|
2015-08-04 14:47:44 +02:00
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
if (field.Attributes & FAConfig) {
|
|
|
|
if (!original_attributes->Contains(attr)) {
|
|
|
|
updated_original_attributes = true;
|
|
|
|
original_attributes->Set(attr, oldValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 08:52:00 +02:00
|
|
|
ModAttrValidationUtils utils;
|
2018-01-11 07:08:09 +01:00
|
|
|
ValidateField(fid, Lazy<Value>{newValue}, utils);
|
2015-08-13 08:52:00 +02:00
|
|
|
|
2015-08-17 14:55:51 +02:00
|
|
|
SetField(fid, newValue);
|
2015-09-11 14:09:46 +02:00
|
|
|
|
2015-10-14 13:04:39 +02:00
|
|
|
if (updateVersion && (field.Attributes & FAConfig))
|
|
|
|
SetVersion(Utility::GetTime());
|
2015-08-04 14:47:44 +02:00
|
|
|
|
2015-10-14 13:04:39 +02:00
|
|
|
if (updated_original_attributes)
|
2015-08-04 14:47:44 +02:00
|
|
|
NotifyOriginalAttributes();
|
|
|
|
}
|
|
|
|
|
2015-10-22 14:32:14 +02:00
|
|
|
void ConfigObject::RestoreAttribute(const String& attr, bool updateVersion)
|
2015-08-04 14:47:44 +02:00
|
|
|
{
|
2015-09-18 15:01:44 +02:00
|
|
|
Type::Ptr type = GetReflectionType();
|
|
|
|
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> tokens = attr.Split(".");
|
2015-09-18 15:01:44 +02:00
|
|
|
|
|
|
|
String fieldName = tokens[0];
|
|
|
|
|
|
|
|
int fid = type->GetFieldId(fieldName);
|
|
|
|
|
|
|
|
Value currentValue = GetField(fid);
|
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
Dictionary::Ptr original_attributes = GetOriginalAttributes();
|
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
if (!original_attributes)
|
2015-08-04 14:47:44 +02:00
|
|
|
return;
|
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
Value oldValue = original_attributes->Get(attr);
|
2015-09-18 15:01:44 +02:00
|
|
|
Value newValue;
|
|
|
|
|
|
|
|
if (tokens.size() > 1) {
|
|
|
|
newValue = currentValue.Clone();
|
|
|
|
Value current = newValue;
|
|
|
|
|
|
|
|
if (current.IsEmpty())
|
2016-02-09 15:53:40 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot restore non-existent object attribute"));
|
2015-09-18 15:01:44 +02:00
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
String prefix = tokens[0];
|
2015-09-18 15:01:44 +02:00
|
|
|
|
|
|
|
for (std::vector<String>::size_type i = 1; i < tokens.size() - 1; i++) {
|
2015-09-21 13:00:50 +02:00
|
|
|
if (!current.IsObjectType<Dictionary>())
|
2015-09-18 15:01:44 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Value must be a dictionary."));
|
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
Dictionary::Ptr dict = current;
|
2015-09-18 15:01:44 +02:00
|
|
|
|
|
|
|
const String& key = tokens[i];
|
2015-09-21 13:00:50 +02:00
|
|
|
prefix += "." + key;
|
2015-09-18 15:01:44 +02:00
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
if (!dict->Contains(key))
|
2016-02-09 15:53:40 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot restore non-existent object attribute"));
|
2015-09-18 15:01:44 +02:00
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
current = dict->Get(key);
|
2015-09-18 15:01:44 +02:00
|
|
|
}
|
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
if (!current.IsObjectType<Dictionary>())
|
2015-09-18 15:01:44 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Value must be a dictionary."));
|
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
Dictionary::Ptr dict = current;
|
2015-09-18 15:01:44 +02:00
|
|
|
|
|
|
|
const String& key = tokens[tokens.size() - 1];
|
2015-09-21 13:00:50 +02:00
|
|
|
prefix += "." + key;
|
|
|
|
|
|
|
|
std::vector<String> restoredAttrs;
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(original_attributes);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const auto& kv : original_attributes) {
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> originalTokens = String(kv.first).Split(".");
|
2015-09-21 13:00:50 +02:00
|
|
|
|
|
|
|
if (tokens.size() > originalTokens.size())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool match = true;
|
|
|
|
for (std::vector<String>::size_type i = 0; i < tokens.size(); i++) {
|
|
|
|
if (tokens[i] != originalTokens[i]) {
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Dictionary::Ptr dict;
|
|
|
|
|
|
|
|
if (tokens.size() == originalTokens.size())
|
|
|
|
dict = current;
|
|
|
|
else {
|
|
|
|
Value currentSub = current;
|
|
|
|
|
|
|
|
for (std::vector<String>::size_type i = tokens.size() - 1; i < originalTokens.size() - 1; i++) {
|
|
|
|
dict = currentSub;
|
|
|
|
currentSub = dict->Get(originalTokens[i]);
|
|
|
|
|
|
|
|
if (!currentSub.IsObjectType<Dictionary>()) {
|
|
|
|
currentSub = new Dictionary();
|
|
|
|
dict->Set(originalTokens[i], currentSub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dict = currentSub;
|
|
|
|
}
|
|
|
|
|
|
|
|
dict->Set(originalTokens[originalTokens.size() - 1], kv.second);
|
|
|
|
restoredAttrs.push_back(kv.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& attr : restoredAttrs)
|
2015-09-21 13:00:50 +02:00
|
|
|
original_attributes->Remove(attr);
|
2015-09-18 15:01:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
} else {
|
2015-09-21 13:00:50 +02:00
|
|
|
newValue = oldValue;
|
2015-09-18 15:01:44 +02:00
|
|
|
}
|
|
|
|
|
2015-09-21 13:00:50 +02:00
|
|
|
original_attributes->Remove(attr);
|
2015-09-18 15:01:44 +02:00
|
|
|
SetField(fid, newValue);
|
2015-08-04 14:47:44 +02:00
|
|
|
|
2015-10-22 14:32:14 +02:00
|
|
|
if (updateVersion)
|
|
|
|
SetVersion(Utility::GetTime());
|
2015-08-04 14:47:44 +02:00
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
bool ConfigObject::IsAttributeModified(const String& attr) const
|
2015-08-04 14:47:44 +02:00
|
|
|
{
|
|
|
|
Dictionary::Ptr original_attributes = GetOriginalAttributes();
|
|
|
|
|
|
|
|
if (!original_attributes)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return original_attributes->Contains(attr);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ConfigObject::Register()
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2016-08-16 11:02:10 +02:00
|
|
|
TypeImpl<ConfigObject>::Ptr type = static_pointer_cast<TypeImpl<ConfigObject> >(GetReflectionType());
|
|
|
|
type->RegisterObject(this);
|
2013-02-27 12:44:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ConfigObject::Unregister()
|
2015-08-13 09:02:52 +02:00
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
2016-08-16 11:02:10 +02:00
|
|
|
TypeImpl<ConfigObject>::Ptr type = static_pointer_cast<TypeImpl<ConfigObject> >(GetReflectionType());
|
|
|
|
type->UnregisterObject(this);
|
2015-08-13 09:02:52 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void ConfigObject::Start(bool runtimeCreated)
|
2012-09-28 10:39:28 +02:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<ConfigObject>::Start(runtimeCreated);
|
2015-08-25 13:53:43 +02:00
|
|
|
|
2013-12-14 07:36:49 +01:00
|
|
|
ObjectLock olock(this);
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-12-14 07:36:49 +01:00
|
|
|
SetStartCalled(true);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ConfigObject::PreActivate()
|
2017-05-04 10:37:34 +02:00
|
|
|
{
|
|
|
|
CONTEXT("Setting 'active' to true for object '" + GetName() + "' of type '" + GetReflectionType()->GetName() + "'");
|
|
|
|
|
|
|
|
ASSERT(!IsActive());
|
|
|
|
SetActive(true, true);
|
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void ConfigObject::Activate(bool runtimeCreated)
|
2013-12-14 07:36:49 +01:00
|
|
|
{
|
2016-08-15 14:39:33 +02:00
|
|
|
CONTEXT("Activating object '" + GetName() + "' of type '" + GetReflectionType()->GetName() + "'");
|
2014-10-30 13:04:00 +01:00
|
|
|
|
2013-12-14 07:36:49 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
2016-05-12 14:00:19 +02:00
|
|
|
|
|
|
|
Start(runtimeCreated);
|
|
|
|
|
|
|
|
ASSERT(GetStartCalled());
|
2013-03-12 14:01:11 +01:00
|
|
|
|
2016-05-12 14:00:19 +02:00
|
|
|
if (GetHAMode() == HARunEverywhere)
|
|
|
|
SetAuthority(true);
|
|
|
|
}
|
2015-09-18 13:04:09 +02:00
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
NotifyActive();
|
2013-03-12 14:01:11 +01:00
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void ConfigObject::Stop(bool runtimeRemoved)
|
2012-06-12 09:36:18 +02:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<ConfigObject>::Stop(runtimeRemoved);
|
2015-08-25 13:53:43 +02:00
|
|
|
|
2013-12-14 07:36:49 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
SetStopCalled(true);
|
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void ConfigObject::Deactivate(bool runtimeRemoved)
|
2013-12-14 07:36:49 +01:00
|
|
|
{
|
2016-08-15 14:39:33 +02:00
|
|
|
CONTEXT("Deactivating object '" + GetName() + "' of type '" + GetReflectionType()->GetName() + "'");
|
2014-10-30 13:04:00 +01:00
|
|
|
|
2013-12-14 07:36:49 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
if (!IsActive())
|
|
|
|
return;
|
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
SetActive(false, true);
|
2013-12-14 07:36:49 +01:00
|
|
|
|
2016-05-12 14:00:19 +02:00
|
|
|
SetAuthority(false);
|
2015-12-10 16:54:43 +01:00
|
|
|
|
2016-05-12 14:00:19 +02:00
|
|
|
Stop(runtimeRemoved);
|
|
|
|
}
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-12-14 07:36:49 +01:00
|
|
|
ASSERT(GetStopCalled());
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
NotifyActive();
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ConfigObject::OnConfigLoaded()
|
2013-08-29 16:53:57 +02:00
|
|
|
{
|
|
|
|
/* Nothing to do here. */
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ConfigObject::OnAllConfigLoaded()
|
2014-11-21 18:31:37 +01:00
|
|
|
{
|
2016-08-16 11:02:10 +02:00
|
|
|
static ConfigType *ctype;
|
|
|
|
|
|
|
|
if (!ctype) {
|
|
|
|
Type::Ptr type = Type::GetByName("Zone");
|
|
|
|
ctype = dynamic_cast<ConfigType *>(type.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
String zoneName = GetZoneName();
|
|
|
|
|
|
|
|
if (!zoneName.IsEmpty())
|
|
|
|
m_Zone = ctype->GetObject(zoneName);
|
2014-11-21 18:31:37 +01:00
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
void ConfigObject::CreateChildObjects(const Type::Ptr& childType)
|
2015-03-19 15:47:46 +01:00
|
|
|
{
|
|
|
|
/* Nothing to do here. */
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ConfigObject::OnStateLoaded()
|
2013-08-29 16:53:57 +02:00
|
|
|
{
|
|
|
|
/* Nothing to do here. */
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ConfigObject::Pause()
|
2014-05-09 11:32:24 +02:00
|
|
|
{
|
|
|
|
SetPauseCalled(true);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ConfigObject::Resume()
|
2014-05-09 11:32:24 +02:00
|
|
|
{
|
|
|
|
SetResumeCalled(true);
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
void ConfigObject::SetAuthority(bool authority)
|
2014-05-09 11:32:24 +02:00
|
|
|
{
|
2016-05-12 14:00:19 +02:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2014-05-09 11:32:24 +02:00
|
|
|
if (authority && GetPaused()) {
|
|
|
|
SetResumeCalled(false);
|
|
|
|
Resume();
|
|
|
|
ASSERT(GetResumeCalled());
|
|
|
|
SetPaused(false);
|
|
|
|
} else if (!authority && !GetPaused()) {
|
2015-12-10 16:54:43 +01:00
|
|
|
SetPaused(true);
|
2014-05-09 11:32:24 +02:00
|
|
|
SetPauseCalled(false);
|
2014-05-09 13:02:30 +02:00
|
|
|
Pause();
|
2014-05-09 11:32:24 +02:00
|
|
|
ASSERT(GetPauseCalled());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
void ConfigObject::DumpObjects(const String& filename, int attributeTypes)
|
2012-07-24 13:13:02 +02:00
|
|
|
{
|
2015-08-15 20:28:05 +02:00
|
|
|
Log(LogInformation, "ConfigObject")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Dumping program state to file '" << filename << "'";
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::fstream fp;
|
2016-02-24 11:00:44 +01:00
|
|
|
String tempFilename = Utility::CreateTempFile(filename + ".XXXXXX", 0600, fp);
|
2016-08-09 10:59:08 +02:00
|
|
|
fp.exceptions(std::ofstream::failbit | std::ofstream::badbit);
|
2012-07-24 13:13:02 +02:00
|
|
|
|
|
|
|
if (!fp)
|
2014-06-26 14:16:37 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + tempFilename + "' file"));
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
StdioStream::Ptr sfp = new StdioStream(&fp, false);
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Type::Ptr& type : Type::GetAllTypes()) {
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *dtype = dynamic_cast<ConfigType *>(type.get());
|
2016-08-16 11:02:10 +02:00
|
|
|
|
|
|
|
if (!dtype)
|
|
|
|
continue;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const ConfigObject::Ptr& object : dtype->GetObjects()) {
|
2013-11-08 11:17:46 +01:00
|
|
|
Dictionary::Ptr update = Serialize(object, attributeTypes);
|
2012-08-02 09:38:08 +02:00
|
|
|
|
|
|
|
if (!update)
|
|
|
|
continue;
|
2012-07-24 14:18:33 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr persistentObject = new Dictionary({
|
|
|
|
{ "type", type->GetName() },
|
|
|
|
{ "name", object->GetName() },
|
|
|
|
{ "update", update }
|
|
|
|
});
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
String json = JsonEncode(persistentObject);
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
NetString::WriteStringToStream(sfp, json);
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-07 14:17:36 +02:00
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
sfp->Close();
|
2012-11-22 12:04:32 +01:00
|
|
|
|
2012-10-17 11:35:06 +02:00
|
|
|
fp.close();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
_unlink(filename.CStr());
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
if (rename(tempFilename.CStr(), filename.CStr()) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
2017-12-19 15:50:05 +01:00
|
|
|
<< boost::errinfo_api_function("rename")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(tempFilename));
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
void ConfigObject::RestoreObject(const String& message, int attributeTypes)
|
2014-05-18 09:15:27 +02:00
|
|
|
{
|
2014-10-26 19:59:49 +01:00
|
|
|
Dictionary::Ptr persistentObject = JsonDecode(message);
|
2014-05-18 09:15:27 +02:00
|
|
|
|
|
|
|
String type = persistentObject->Get("type");
|
|
|
|
String name = persistentObject->Get("name");
|
|
|
|
|
2016-08-16 11:02:10 +02:00
|
|
|
ConfigObject::Ptr object = GetObject(type, name);
|
2014-05-18 09:15:27 +02:00
|
|
|
|
|
|
|
if (!object)
|
|
|
|
return;
|
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2015-08-15 20:28:05 +02:00
|
|
|
Log(LogDebug, "ConfigObject")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Restoring object '" << name << "' of type '" << type << "'.";
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2014-05-18 09:15:27 +02:00
|
|
|
Dictionary::Ptr update = persistentObject->Get("update");
|
|
|
|
Deserialize(object, update, false, attributeTypes);
|
|
|
|
object->OnStateLoaded();
|
2014-10-29 11:14:04 +01:00
|
|
|
object->SetStateLoaded(true);
|
2014-05-18 09:15:27 +02:00
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
void ConfigObject::RestoreObjects(const String& filename, int attributeTypes)
|
2012-07-24 13:13:02 +02:00
|
|
|
{
|
2015-02-14 23:39:10 +01:00
|
|
|
if (!Utility::PathExists(filename))
|
|
|
|
return;
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
Log(LogInformation, "ConfigObject")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Restoring program state from file '" << filename << "'";
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
std::fstream fp;
|
|
|
|
fp.open(filename.CStr(), std::ios_base::in);
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
StdioStream::Ptr sfp = new StdioStream (&fp, false);
|
2012-07-24 13:13:02 +02:00
|
|
|
|
2013-01-30 10:52:52 +01:00
|
|
|
unsigned long restored = 0;
|
|
|
|
|
2018-08-09 15:37:23 +02:00
|
|
|
WorkQueue upq(25000, Configuration::Concurrency);
|
2016-06-14 08:19:13 +02:00
|
|
|
upq.SetName("ConfigObject::RestoreObjects");
|
2014-05-18 09:15:27 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String message;
|
2015-02-14 16:34:36 +01:00
|
|
|
StreamReadContext src;
|
2015-02-14 18:48:33 +01:00
|
|
|
for (;;) {
|
|
|
|
StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src);
|
|
|
|
|
|
|
|
if (srs == StatusEof)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (srs != StatusNewItem)
|
|
|
|
continue;
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
upq.Enqueue(std::bind(&ConfigObject::RestoreObject, message, attributeTypes));
|
2013-01-30 10:52:52 +01:00
|
|
|
restored++;
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
2012-11-22 12:04:32 +01:00
|
|
|
|
2012-11-23 11:02:34 +01:00
|
|
|
sfp->Close();
|
2013-01-30 10:52:52 +01:00
|
|
|
|
2014-05-18 09:15:27 +02:00
|
|
|
upq.Join();
|
|
|
|
|
2014-10-29 11:14:04 +01:00
|
|
|
unsigned long no_state = 0;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Type::Ptr& type : Type::GetAllTypes()) {
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *dtype = dynamic_cast<ConfigType *>(type.get());
|
2016-08-16 11:02:10 +02:00
|
|
|
|
|
|
|
if (!dtype)
|
|
|
|
continue;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const ConfigObject::Ptr& object : dtype->GetObjects()) {
|
2014-10-29 11:14:04 +01:00
|
|
|
if (!object->GetStateLoaded()) {
|
|
|
|
object->OnStateLoaded();
|
|
|
|
object->SetStateLoaded(true);
|
|
|
|
|
|
|
|
no_state++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
Log(LogInformation, "ConfigObject")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Restored " << restored << " objects. Loaded " << no_state << " new objects without state.";
|
2012-07-24 13:13:02 +02:00
|
|
|
}
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ConfigObject::StopObjects()
|
2012-08-07 13:08:14 +02:00
|
|
|
{
|
2019-02-20 12:38:57 +01:00
|
|
|
std::vector<Type::Ptr> types = Type::GetAllTypes();
|
|
|
|
|
|
|
|
std::sort(types.begin(), types.end(), [](const Type::Ptr& a, const Type::Ptr& b) {
|
|
|
|
if (a->GetActivationPriority() > b->GetActivationPriority())
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const Type::Ptr& type : types) {
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *dtype = dynamic_cast<ConfigType *>(type.get());
|
2016-08-16 11:02:10 +02:00
|
|
|
|
|
|
|
if (!dtype)
|
|
|
|
continue;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const ConfigObject::Ptr& object : dtype->GetObjects()) {
|
2019-02-19 13:49:52 +01:00
|
|
|
#ifdef I2_DEBUG
|
|
|
|
Log(LogDebug, "ConfigObject")
|
|
|
|
<< "Deactivate() called for config object '" << object->GetName() << "' with type '" << type->GetName() << "'.";
|
|
|
|
#endif /* I2_DEBUG */
|
2013-12-14 07:36:49 +01:00
|
|
|
object->Deactivate();
|
2012-08-07 13:08:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
void ConfigObject::DumpModifiedAttributes(const std::function<void(const ConfigObject::Ptr&, const String&, const Value&)>& callback)
|
2015-08-12 09:52:29 +02:00
|
|
|
{
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Type::Ptr& type : Type::GetAllTypes()) {
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *dtype = dynamic_cast<ConfigType *>(type.get());
|
2016-08-16 11:02:10 +02:00
|
|
|
|
|
|
|
if (!dtype)
|
|
|
|
continue;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const ConfigObject::Ptr& object : dtype->GetObjects()) {
|
2015-08-12 09:52:29 +02:00
|
|
|
Dictionary::Ptr originalAttributes = object->GetOriginalAttributes();
|
|
|
|
|
|
|
|
if (!originalAttributes)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ObjectLock olock(originalAttributes);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : originalAttributes) {
|
2015-09-21 13:00:50 +02:00
|
|
|
String key = kv.first;
|
|
|
|
|
|
|
|
Type::Ptr type = object->GetReflectionType();
|
|
|
|
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> tokens = key.Split(".");
|
2015-09-21 13:00:50 +02:00
|
|
|
|
|
|
|
String fieldName = tokens[0];
|
|
|
|
int fid = type->GetFieldId(fieldName);
|
|
|
|
|
|
|
|
Value currentValue = object->GetField(fid);
|
|
|
|
Value modifiedValue;
|
|
|
|
|
|
|
|
if (tokens.size() > 1) {
|
|
|
|
Value current = currentValue;
|
|
|
|
|
|
|
|
for (std::vector<String>::size_type i = 1; i < tokens.size() - 1; i++) {
|
|
|
|
if (!current.IsObjectType<Dictionary>())
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Value must be a dictionary."));
|
|
|
|
|
|
|
|
Dictionary::Ptr dict = current;
|
|
|
|
const String& key = tokens[i];
|
|
|
|
|
|
|
|
if (!dict->Contains(key))
|
|
|
|
break;
|
|
|
|
|
|
|
|
current = dict->Get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!current.IsObjectType<Dictionary>())
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Value must be a dictionary."));
|
|
|
|
|
|
|
|
Dictionary::Ptr dict = current;
|
|
|
|
const String& key = tokens[tokens.size() - 1];
|
|
|
|
|
|
|
|
modifiedValue = dict->Get(key);
|
|
|
|
} else
|
|
|
|
modifiedValue = currentValue;
|
|
|
|
|
|
|
|
callback(object, key, modifiedValue);
|
2015-08-12 09:52:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::Ptr ConfigObject::GetObject(const String& type, const String& name)
|
2012-12-04 08:42:24 +01:00
|
|
|
{
|
2016-08-16 11:02:10 +02:00
|
|
|
Type::Ptr ptype = Type::GetByName(type);
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *ctype = dynamic_cast<ConfigType *>(ptype.get());
|
2016-08-16 11:02:10 +02:00
|
|
|
|
|
|
|
if (!ctype)
|
2017-11-30 08:36:35 +01:00
|
|
|
return nullptr;
|
2016-08-16 11:02:10 +02:00
|
|
|
|
|
|
|
return ctype->GetObject(name);
|
2012-12-04 08:42:24 +01:00
|
|
|
}
|
2015-11-24 15:25:55 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
ConfigObject::Ptr ConfigObject::GetZone() const
|
2015-11-24 15:25:55 +01:00
|
|
|
{
|
|
|
|
return m_Zone;
|
|
|
|
}
|
2016-08-09 09:00:19 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Dictionary::Ptr ConfigObject::GetSourceLocation() const
|
2017-04-19 15:30:11 +02:00
|
|
|
{
|
|
|
|
DebugInfo di = GetDebugInfo();
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Dictionary({
|
|
|
|
{ "path", di.Path },
|
|
|
|
{ "first_line", di.FirstLine },
|
|
|
|
{ "first_column", di.FirstColumn },
|
|
|
|
{ "last_line", di.LastLine },
|
|
|
|
{ "last_column", di.LastColumn }
|
|
|
|
});
|
2017-04-19 15:30:11 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
NameComposer::~NameComposer()
|
2016-08-09 09:00:19 +02:00
|
|
|
{ }
|