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