From 7fac5b454eb07cac368f576dadb6d1c54623c1e9 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 14 Feb 2013 12:02:02 +0100 Subject: [PATCH] Basic support for other scripting languages. --- itl/types.conf | 8 ++++ lib/base/Makefile.am | 6 +++ lib/base/i2-base.h | 3 ++ lib/base/script.cpp | 60 ++++++++++++++++++++++++++++++ lib/base/script.h | 55 ++++++++++++++++++++++++++++ lib/base/scriptinterpreter.cpp | 57 +++++++++++++++++++++++++++++ lib/base/scriptinterpreter.h | 67 ++++++++++++++++++++++++++++++++++ lib/base/scriptlanguage.cpp | 50 +++++++++++++++++++++++++ lib/base/scriptlanguage.h | 52 ++++++++++++++++++++++++++ 9 files changed, 358 insertions(+) create mode 100644 lib/base/script.cpp create mode 100644 lib/base/script.h create mode 100644 lib/base/scriptinterpreter.cpp create mode 100644 lib/base/scriptinterpreter.h create mode 100644 lib/base/scriptlanguage.cpp create mode 100644 lib/base/scriptlanguage.h diff --git a/itl/types.conf b/itl/types.conf index 8f71747e3..14bbe9901 100644 --- a/itl/types.conf +++ b/itl/types.conf @@ -239,3 +239,11 @@ type Notification { }, %attribute string "notification_command" } + +type Script { + %require "language", + %attribute string "language", + + %require "code", + %attribute string "code" +} diff --git a/lib/base/Makefile.am b/lib/base/Makefile.am index eb6b036ef..ac4d619ff 100644 --- a/lib/base/Makefile.am +++ b/lib/base/Makefile.am @@ -41,8 +41,14 @@ libbase_la_SOURCES = \ qstring.h \ ringbuffer.cpp \ ringbuffer.h \ + script.cpp \ + script.h \ scriptfunction.cpp \ scriptfunction.h \ + scriptinterpreter.cpp \ + scriptinterpreter.h \ + scriptlanguage.cpp \ + scriptlanguage.h \ scripttask.cpp \ scripttask.h \ socket.cpp \ diff --git a/lib/base/i2-base.h b/lib/base/i2-base.h index e05270c01..21aa1f47f 100644 --- a/lib/base/i2-base.h +++ b/lib/base/i2-base.h @@ -202,6 +202,9 @@ namespace tuples = boost::tuples; #include "scripttask.h" #include "dynamicobject.h" #include "dynamictype.h" +#include "script.h" +#include "scriptinterpreter.h" +#include "scriptlanguage.h" #include "logger.h" #include "application.h" #include "component.h" diff --git a/lib/base/script.cpp b/lib/base/script.cpp new file mode 100644 index 000000000..23418af47 --- /dev/null +++ b/lib/base/script.cpp @@ -0,0 +1,60 @@ +/****************************************************************************** + * 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 * + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ******************************************************************************/ + +#include "i2-base.h" + +using namespace icinga; + +REGISTER_TYPE(Script, NULL); + +/** + * Constructor for the Script class. + * + * @param properties A serialized dictionary containing attributes. + */ +Script::Script(const Dictionary::Ptr& properties) + : DynamicObject(properties) +{ } + +String Script::GetLanguage(void) const +{ + return Get("language"); +} + +String Script::GetCode(void) const +{ + return Get("code"); +} + +void Script::OnAttributeUpdate(const String& name, const Value& oldValue) +{ + if (name == "code") + Reload(); +} + +void Script::Reload(void) +{ + if (!m_Interpreter) { + ScriptLanguage::Ptr language = ScriptLanguage::GetByName(GetLanguage()); + + m_Interpreter = language->CreateInterpreter(GetSelf()); + } else { + m_Interpreter->Reload(); + } +} diff --git a/lib/base/script.h b/lib/base/script.h new file mode 100644 index 000000000..47c300246 --- /dev/null +++ b/lib/base/script.h @@ -0,0 +1,55 @@ +/****************************************************************************** + * 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 * + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ******************************************************************************/ + +#ifndef SCRIPT_H +#define SCRIPT_H + +namespace icinga +{ + +class ScriptInterpreter; + +/** + * A script. + * + * @ingroup base + */ +class I2_BASE_API Script : public DynamicObject +{ +public: + typedef shared_ptr