mirror of https://github.com/Icinga/icinga2.git
parent
c92625770c
commit
30c989074b
|
@ -607,7 +607,7 @@ String Application::GetPrefixDir(void)
|
|||
*/
|
||||
void Application::DeclarePrefixDir(const String& path)
|
||||
{
|
||||
ScriptVariable::Declare("IcingaPrefixDir", path);
|
||||
ScriptVariable::Set("IcingaPrefixDir", path, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -627,7 +627,7 @@ String Application::GetSysconfDir(void)
|
|||
*/
|
||||
void Application::DeclareSysconfDir(const String& path)
|
||||
{
|
||||
ScriptVariable::Declare("IcingaSysconfDir", path);
|
||||
ScriptVariable::Set("IcingaSysconfDir", path, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -647,7 +647,7 @@ String Application::GetLocalStateDir(void)
|
|||
*/
|
||||
void Application::DeclareLocalStateDir(const String& path)
|
||||
{
|
||||
ScriptVariable::Declare("IcingaLocalStateDir", path);
|
||||
ScriptVariable::Set("IcingaLocalStateDir", path, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -667,7 +667,7 @@ String Application::GetPkgDataDir(void)
|
|||
*/
|
||||
void Application::DeclarePkgDataDir(const String& path)
|
||||
{
|
||||
ScriptVariable::Declare("IcingaPkgDataDir", path);
|
||||
ScriptVariable::Set("IcingaPkgDataDir", path, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -687,7 +687,7 @@ String Application::GetStatePath(void)
|
|||
*/
|
||||
void Application::DeclareStatePath(const String& path)
|
||||
{
|
||||
ScriptVariable::Declare("IcingaStatePath", path);
|
||||
ScriptVariable::Set("IcingaStatePath", path, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -707,7 +707,7 @@ String Application::GetPidPath(void)
|
|||
*/
|
||||
void Application::DeclarePidPath(const String& path)
|
||||
{
|
||||
ScriptVariable::Declare("IcingaPidPath", path);
|
||||
ScriptVariable::Set("IcingaPidPath", path, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -727,7 +727,7 @@ String Application::GetApplicationType(void)
|
|||
*/
|
||||
void Application::DeclareApplicationType(const String& type)
|
||||
{
|
||||
ScriptVariable::Declare("ApplicationType", type);
|
||||
ScriptVariable::Set("ApplicationType", type, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -22,21 +22,58 @@
|
|||
|
||||
using namespace icinga;
|
||||
|
||||
ScriptVariable::ScriptVariable(const Value& data)
|
||||
: m_Data(data), m_Constant(false)
|
||||
{ }
|
||||
|
||||
ScriptVariable::Ptr ScriptVariable::GetByName(const String& name)
|
||||
{
|
||||
return ScriptVariableRegistry::GetInstance()->GetItem(name);
|
||||
}
|
||||
|
||||
void ScriptVariable::SetConstant(bool constant)
|
||||
{
|
||||
m_Constant = constant;
|
||||
}
|
||||
|
||||
bool ScriptVariable::IsConstant(void) const
|
||||
{
|
||||
return m_Constant;
|
||||
}
|
||||
|
||||
void ScriptVariable::SetData(const Value& data)
|
||||
{
|
||||
m_Data = data;
|
||||
}
|
||||
|
||||
Value ScriptVariable::GetData(void) const
|
||||
{
|
||||
return m_Data;
|
||||
}
|
||||
|
||||
Value ScriptVariable::Get(const String& name)
|
||||
{
|
||||
Value value = ScriptVariableRegistry::GetInstance()->GetItem(name);
|
||||
if (value.IsEmpty())
|
||||
Log(LogWarning, "icinga", "Tried to access empty variable: " + name);
|
||||
ScriptVariable::Ptr sv = GetByName(name);
|
||||
|
||||
return value;
|
||||
if (!sv) {
|
||||
Log(LogWarning, "icinga", "Tried to access undefined variable: " + name);
|
||||
return Empty;
|
||||
}
|
||||
|
||||
return sv->GetData();
|
||||
}
|
||||
|
||||
void ScriptVariable::Set(const String& name, const Value& value)
|
||||
ScriptVariable::Ptr ScriptVariable::Set(const String& name, const Value& value, bool overwrite)
|
||||
{
|
||||
ScriptVariableRegistry::GetInstance()->Register(name, value);
|
||||
ScriptVariable::Ptr sv = GetByName(name);
|
||||
|
||||
if (!sv) {
|
||||
sv = make_shared<ScriptVariable>(value);
|
||||
ScriptVariableRegistry::GetInstance()->Register(name, sv);
|
||||
} else if (overwrite) {
|
||||
sv->SetData(value);
|
||||
}
|
||||
|
||||
return sv;
|
||||
}
|
||||
|
||||
void ScriptVariable::Declare(const String& name, const Value& value)
|
||||
{
|
||||
ScriptVariableRegistry::GetInstance()->RegisterIfNew(name, value);
|
||||
}
|
||||
|
|
|
@ -29,22 +29,34 @@ namespace icinga
|
|||
{
|
||||
|
||||
/**
|
||||
* Global registry for script variables.
|
||||
* A script variables.
|
||||
*
|
||||
* @ingroup base
|
||||
*/
|
||||
class I2_BASE_API ScriptVariable
|
||||
class I2_BASE_API ScriptVariable : public Object
|
||||
{
|
||||
public:
|
||||
DECLARE_PTR_TYPEDEFS(ScriptVariable);
|
||||
|
||||
ScriptVariable(const Value& data);
|
||||
|
||||
void SetConstant(bool constant);
|
||||
bool IsConstant(void) const;
|
||||
|
||||
void SetData(const Value& data);
|
||||
Value GetData(void) const;
|
||||
|
||||
static ScriptVariable::Ptr GetByName(const String& name);
|
||||
|
||||
static Value Get(const String& name);
|
||||
static void Set(const String& name, const Value& value);
|
||||
static void Declare(const String& name, const Value& value);
|
||||
static ScriptVariable::Ptr Set(const String& name, const Value& value, bool overwrite = true);
|
||||
|
||||
private:
|
||||
static Registry<ScriptVariable, Value> m_Registry;
|
||||
Value m_Data;
|
||||
bool m_Constant;
|
||||
};
|
||||
|
||||
class I2_BASE_API ScriptVariableRegistry : public Registry<ScriptVariableRegistry, Value>
|
||||
class I2_BASE_API ScriptVariableRegistry : public Registry<ScriptVariableRegistry, ScriptVariable::Ptr>
|
||||
{
|
||||
public:
|
||||
static inline ScriptVariableRegistry *GetInstance(void)
|
||||
|
|
Loading…
Reference in New Issue