2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* 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
|
|
|
******************************************************************************/
|
|
|
|
|
2012-03-31 15:18:09 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
ConfigObject::ConfigObject(Dictionary::Ptr properties, const ConfigObject::Set::Ptr& container)
|
|
|
|
: m_Container(container ? container : GetAllObjects()),
|
2012-06-15 19:32:41 +02:00
|
|
|
m_Properties(properties), m_Tags(boost::make_shared<Dictionary>())
|
2012-06-12 09:36:18 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
void ConfigObject::SetProperties(Dictionary::Ptr properties)
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2012-06-12 09:36:18 +02:00
|
|
|
m_Properties = properties;
|
|
|
|
}
|
2012-04-20 13:49:04 +02:00
|
|
|
|
2012-06-12 09:36:18 +02:00
|
|
|
Dictionary::Ptr ConfigObject::GetProperties(void) const
|
|
|
|
{
|
|
|
|
return m_Properties;
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2012-06-12 09:36:18 +02:00
|
|
|
Dictionary::Ptr ConfigObject::GetTags(void) const
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2012-06-12 09:36:18 +02:00
|
|
|
return m_Tags;
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2012-06-12 09:36:18 +02:00
|
|
|
string ConfigObject::GetType(void) const
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2012-06-12 09:36:18 +02:00
|
|
|
string type;
|
2012-07-09 16:19:56 +02:00
|
|
|
GetProperties()->Get("__type", &type);
|
2012-06-12 09:36:18 +02:00
|
|
|
return type;
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string ConfigObject::GetName(void) const
|
|
|
|
{
|
2012-06-12 09:36:18 +02:00
|
|
|
string name;
|
2012-07-09 16:19:56 +02:00
|
|
|
GetProperties()->Get("__name", &name);
|
2012-06-12 09:36:18 +02:00
|
|
|
return name;
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2012-06-12 09:36:18 +02:00
|
|
|
bool ConfigObject::IsLocal(void) const
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2012-06-12 11:24:02 +02:00
|
|
|
bool value = false;
|
2012-07-09 16:19:56 +02:00
|
|
|
GetProperties()->Get("__local", &value);
|
2012-06-12 11:24:02 +02:00
|
|
|
return value;
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2012-06-12 09:36:18 +02:00
|
|
|
bool ConfigObject::IsAbstract(void) const
|
2012-04-20 14:20:25 +02:00
|
|
|
{
|
2012-06-12 11:24:02 +02:00
|
|
|
bool value = false;
|
2012-07-09 16:19:56 +02:00
|
|
|
GetProperties()->Get("__abstract", &value);
|
2012-06-12 11:24:02 +02:00
|
|
|
return value;
|
2012-04-20 14:20:25 +02:00
|
|
|
}
|
|
|
|
|
2012-07-02 14:38:37 +02:00
|
|
|
void ConfigObject::SetSource(const string& value)
|
|
|
|
{
|
2012-07-09 16:19:56 +02:00
|
|
|
GetProperties()->Set("__source", value);
|
2012-07-02 14:38:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string ConfigObject::GetSource(void) const
|
|
|
|
{
|
|
|
|
string value;
|
2012-07-09 16:19:56 +02:00
|
|
|
GetProperties()->Get("__source", &value);
|
2012-07-02 14:38:37 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-07-02 19:25:33 +02:00
|
|
|
void ConfigObject::SetCommitTimestamp(time_t ts)
|
|
|
|
{
|
2012-07-09 16:19:56 +02:00
|
|
|
GetProperties()->Set("__tx", static_cast<long>(ts));
|
2012-07-02 19:25:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
time_t ConfigObject::GetCommitTimestamp(void) const
|
|
|
|
{
|
|
|
|
long value = false;
|
2012-07-09 16:19:56 +02:00
|
|
|
GetProperties()->Get("__tx", &value);
|
2012-07-02 19:25:33 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-05-09 10:15:51 +02:00
|
|
|
void ConfigObject::Commit(void)
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2012-07-10 13:00:53 +02:00
|
|
|
assert(Application::IsMainThread());
|
|
|
|
|
2012-06-12 09:36:18 +02:00
|
|
|
ConfigObject::Ptr dobj = GetObject(GetType(), GetName());
|
2012-06-16 13:06:06 +02:00
|
|
|
ConfigObject::Ptr self = GetSelf();
|
2012-06-12 09:36:18 +02:00
|
|
|
assert(!dobj || dobj == self);
|
2012-06-13 13:42:55 +02:00
|
|
|
m_Container->CheckObject(self);
|
2012-07-02 19:25:33 +02:00
|
|
|
|
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
SetCommitTimestamp(now);
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigObject::Unregister(void)
|
|
|
|
{
|
2012-07-10 13:00:53 +02:00
|
|
|
assert(Application::IsMainThread());
|
|
|
|
|
2012-06-16 13:06:06 +02:00
|
|
|
ConfigObject::Ptr self = GetSelf();
|
2012-06-13 13:42:55 +02:00
|
|
|
m_Container->RemoveObject(self);
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectSet<ConfigObject::Ptr>::Ptr ConfigObject::GetAllObjects(void)
|
|
|
|
{
|
|
|
|
static ObjectSet<ConfigObject::Ptr>::Ptr allObjects;
|
|
|
|
|
|
|
|
if (!allObjects) {
|
2012-06-15 19:32:41 +02:00
|
|
|
allObjects = boost::make_shared<ObjectSet<ConfigObject::Ptr> >();
|
2012-06-12 09:36:18 +02:00
|
|
|
allObjects->Start();
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
2012-06-12 09:36:18 +02:00
|
|
|
|
|
|
|
return allObjects;
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
2012-06-12 09:36:18 +02:00
|
|
|
|
|
|
|
ConfigObject::TNMap::Ptr ConfigObject::GetObjectsByTypeAndName(void)
|
|
|
|
{
|
|
|
|
static ConfigObject::TNMap::Ptr tnmap;
|
|
|
|
|
|
|
|
if (!tnmap) {
|
2012-06-15 19:32:41 +02:00
|
|
|
tnmap = boost::make_shared<ConfigObject::TNMap>(GetAllObjects(), &ConfigObject::TypeAndNameGetter);
|
2012-06-12 09:36:18 +02:00
|
|
|
tnmap->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
return tnmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigObject::Ptr ConfigObject::GetObject(string type, string name)
|
|
|
|
{
|
|
|
|
ConfigObject::TNMap::Range range;
|
|
|
|
range = GetObjectsByTypeAndName()->GetRange(make_pair(type, name));
|
|
|
|
|
|
|
|
assert(distance(range.first, range.second) <= 1);
|
|
|
|
|
|
|
|
if (range.first == range.second)
|
|
|
|
return ConfigObject::Ptr();
|
|
|
|
else
|
|
|
|
return range.first->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigObject::TypeAndNameGetter(const ConfigObject::Ptr& object, pair<string, string> *key)
|
|
|
|
{
|
2012-07-14 15:59:59 +02:00
|
|
|
*key = make_pair(object->GetType(), object->GetName());
|
2012-06-12 09:36:18 +02:00
|
|
|
|
2012-07-14 15:59:59 +02:00
|
|
|
return true;
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function<bool (ConfigObject::Ptr)> ConfigObject::MakeTypePredicate(string type)
|
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
return boost::bind(&ConfigObject::TypePredicate, _1, type);
|
2012-06-12 09:36:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigObject::TypePredicate(const ConfigObject::Ptr& object, string type)
|
|
|
|
{
|
|
|
|
return (object->GetType() == type);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigObject::TMap::Ptr ConfigObject::GetObjectsByType(void)
|
|
|
|
{
|
|
|
|
static ObjectMap<string, ConfigObject::Ptr>::Ptr tmap;
|
|
|
|
|
|
|
|
if (!tmap) {
|
2012-06-15 19:32:41 +02:00
|
|
|
tmap = boost::make_shared<ConfigObject::TMap>(GetAllObjects(), &ConfigObject::TypeGetter);
|
2012-06-12 09:36:18 +02:00
|
|
|
tmap->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigObject::TypeGetter(const ConfigObject::Ptr& object, string *key)
|
|
|
|
{
|
|
|
|
*key = object->GetType();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigObject::TMap::Range ConfigObject::GetObjects(string type)
|
|
|
|
{
|
|
|
|
return GetObjectsByType()->GetRange(type);
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
2012-07-14 12:44:37 +02:00
|
|
|
|
|
|
|
void ConfigObject::RemoveTag(const string& key)
|
|
|
|
{
|
|
|
|
GetTags()->Remove(key);
|
|
|
|
}
|
2012-07-14 15:59:59 +02:00
|
|
|
|
2012-07-16 08:19:51 +02:00
|
|
|
ScriptTask::Ptr ConfigObject::InvokeMethod(const string& method,
|
2012-07-15 10:58:03 +02:00
|
|
|
const vector<Variant>& arguments, ScriptTask::CompletionCallback callback)
|
2012-07-14 15:59:59 +02:00
|
|
|
{
|
2012-07-16 08:19:51 +02:00
|
|
|
Dictionary::Ptr methods;
|
2012-07-14 15:59:59 +02:00
|
|
|
string funcName;
|
2012-07-16 08:19:51 +02:00
|
|
|
if (!GetProperty("methods", &methods) || !methods->Get(method, &funcName))
|
2012-07-14 15:59:59 +02:00
|
|
|
return ScriptTask::Ptr();
|
|
|
|
|
|
|
|
ScriptFunction::Ptr func = ScriptFunction::GetByName(funcName);
|
|
|
|
|
|
|
|
if (!func)
|
|
|
|
throw invalid_argument("Function '" + funcName + "' does not exist.");
|
|
|
|
|
2012-07-15 17:15:49 +02:00
|
|
|
ScriptTask::Ptr task = boost::make_shared<ScriptTask>(func, arguments);
|
|
|
|
task->Start(callback);
|
2012-07-14 15:59:59 +02:00
|
|
|
|
|
|
|
return task;
|
|
|
|
}
|