]> granicus.if.org Git - icinga2/commitdiff
Fix --disable-shared
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 15 Mar 2013 10:19:52 +0000 (11:19 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 15 Mar 2013 10:21:16 +0000 (11:21 +0100)
Fixes #3852

16 files changed:
components/replication/replicationcomponent.cpp
icinga-app/Makefile.am
lib/base/Makefile.am
lib/base/dynamicobject.cpp
lib/base/dynamictype.h
lib/base/i2-base.h
lib/base/registry.h [new file with mode: 0644]
lib/base/scriptfunction.cpp
lib/base/scriptfunction.h
lib/base/scriptinterpreter.cpp
lib/base/singleton.h [new file with mode: 0644]
lib/config/configcompiler.cpp
lib/config/configcompiler.h
lib/config/configtype.cpp
lib/remoting/endpointmanager.cpp
lib/remoting/endpointmanager.h

index 060d221760d06b7983b3f3c2abad3d5bb26e59e2..559c1d2022855e62c68793e5db12ca35c230969d 100644 (file)
@@ -106,13 +106,7 @@ void ReplicationComponent::EndpointConnectedHandler(const Endpoint::Ptr& endpoin
                                continue;
 
                        RequestMessage request = MakeObjectMessage(object, "config::ObjectUpdate", 0, true);
-
-                       EndpointManager::Ptr em = EndpointManager::GetInstance();
-
-                       {
-                               ObjectLock elock(em);
-                               em->SendUnicastMessage(m_Endpoint, endpoint, request);
-                       }
+                       EndpointManager::GetInstance()->SendUnicastMessage(m_Endpoint, endpoint, request);
 
                }
        }
@@ -190,12 +184,7 @@ void ReplicationComponent::FlushObjectHandler(double tx, const DynamicObject::Pt
                return;
 
        RequestMessage request = MakeObjectMessage(object, "config::ObjectUpdate", tx, true);
-
-       EndpointManager::Ptr em = EndpointManager::GetInstance();
-       {
-               ObjectLock olock(em);
-               em->SendMulticastMessage(m_Endpoint, request);
-       }
+       EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, request);
 }
 
 void ReplicationComponent::RemoteObjectUpdateHandler(const RequestMessage& request)
index 5ab2c55be5b42f724476bfbe6f04119aeea76c0c..6a83d397b617ec315c497e1dcca0a36b5bb4d594 100644 (file)
@@ -34,6 +34,7 @@ icinga2_LDADD = \
        -dlopen ${top_builddir}/components/compat/libcompat.la \
        -dlopen ${top_builddir}/components/delegation/libdelegation.la \
        -dlopen ${top_builddir}/components/demo/libdemo.la \
+       -dlopen ${top_builddir}/components/livestatus/liblivestatus.la \
        -dlopen ${top_builddir}/components/notification/libnotification.la
 
 if PYTHON_USE
index a4e0502abef354f9856e50699862e8dbad8a9044..b0d180d8af9f6a3601836432035713475ab80d55 100644 (file)
@@ -44,6 +44,7 @@ libbase_la_SOURCES =  \
        process.h \
        qstring.cpp \
        qstring.h \
+       registry.h \
        ringbuffer.cpp \
        ringbuffer.h \
        script.cpp \
@@ -56,6 +57,7 @@ libbase_la_SOURCES =  \
        scriptlanguage.h \
        scripttask.cpp \
        scripttask.h \
+       singleton.h \
        socket.cpp \
        socket.h \
        stacktrace.cpp \
index a6a4a6d40fa65a3ae2fc4f70e1466b9f555480a8..38e9b380edcd4981c2fd3e581d8c0e524ebc3097 100644 (file)
@@ -439,7 +439,7 @@ ScriptTask::Ptr DynamicObject::MakeMethodTask(const String& method,
        if (funcName.IsEmpty())
                return ScriptTask::Ptr();
 
-       ScriptFunction::Ptr func = ScriptFunction::GetByName(funcName);
+       ScriptFunction::Ptr func = ScriptFunctionRegistry::GetInstance()->GetItem(funcName);
 
        if (!func)
                BOOST_THROW_EXCEPTION(invalid_argument("Function '" + funcName + "' does not exist."));
index abae276604bf960e38fa5739f12153a0258878b7..e1a427082d7784a638cb95ebbbeac5fa1b123d71 100644 (file)
@@ -68,6 +68,14 @@ private:
        static boost::mutex& GetStaticMutex(void);
 };
 
+/**
+ * A registry for DynamicType objects.
+ *
+ * @ingroup base
+ */
+class DynamicTypeRegistry : public Registry<DynamicType::Ptr>
+{ };
+
 /**
  * Helper class for registering DynamicObject implementation classes.
  *
index fb59d848e3bbb332cca0fcee903287322b41ca7f..6010fc9bf44940062859be89d048108c0f61bbc0 100644 (file)
@@ -223,6 +223,8 @@ namespace signals2 = boost::signals2;
 #include "tlsstream.h"
 #include "asynctask.h"
 #include "process.h"
+#include "singleton.h"
+#include "registry.h"
 #include "scriptfunction.h"
 #include "scripttask.h"
 #include "attribute.h"
diff --git a/lib/base/registry.h b/lib/base/registry.h
new file mode 100644 (file)
index 0000000..8bbf588
--- /dev/null
@@ -0,0 +1,110 @@
+/******************************************************************************
+ * 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 REGISTRY_H
+#define REGISTRY_H
+
+namespace icinga
+{
+
+/**
+ * A registry.
+ *
+ * @ingroup base
+ */
+template<typename T>
+class I2_BASE_API Registry
+{
+public:
+       typedef map<String, T, string_iless> ItemMap;
+
+       static Registry<T> *GetInstance(void)
+       {
+               return Singleton<Registry<T> >::GetInstance();
+       }
+
+       void Register(const String& name, const T& item)
+       {
+               bool old_item = false;
+
+               {
+                       boost::mutex::scoped_lock lock(m_Mutex);
+
+                       if (m_Items.erase(name) > 0)
+                               old_item = true;
+
+                       m_Items[name] = item;
+               }
+
+               if (old_item)
+                       OnUnregistered(name);
+
+               OnRegistered(name, item);
+       }
+
+       void Unregister(const String& name)
+       {
+               int erased;
+
+               {
+                       boost::mutex::scoped_lock lock(m_Mutex);
+                       erased = m_Items.erase(name);
+               }
+
+               if (erased > 0)
+                       OnUnregistered(name);
+       }
+
+       T GetItem(const String& name) const
+       {
+               boost::mutex::scoped_lock lock(m_Mutex);
+
+               typename ItemMap::const_iterator it;
+               it = m_Items.find(name);
+
+               if (it == m_Items.end())
+                       return T();
+
+               return it->second;
+       }
+
+       ItemMap GetItems(void) const
+       {
+               boost::mutex::scoped_lock lock(m_Mutex);
+
+               return m_Items; /* Makes a copy of the map. */
+       }
+
+       static signals2::signal<void (const String&, const T&)> OnRegistered;
+       static signals2::signal<void (const String&)> OnUnregistered;
+
+private:
+       mutable boost::mutex m_Mutex;
+       typename Registry<T>::ItemMap m_Items;
+};
+
+template<typename T>
+signals2::signal<void (const String&, const T&)> Registry<T>::OnRegistered;
+
+template<typename T>
+signals2::signal<void (const String&)> Registry<T>::OnUnregistered;
+
+}
+
+#endif /* REGISTRY_H */
index 3bfc9c87ac4a1a7a6fb04ee24fafcb6c5e76b401..dfa8904e49a6c4e1fb08d6f3b104205605042d2a 100644 (file)
 
 using namespace icinga;
 
-signals2::signal<void (const String&, const ScriptFunction::Ptr&)> ScriptFunction::OnRegistered;
-signals2::signal<void (const String&)> ScriptFunction::OnUnregistered;
-
 ScriptFunction::ScriptFunction(const Callback& function)
        : m_Callback(function)
 { }
 
-/**
- * @threadsafety Always.
- */
-void ScriptFunction::Register(const String& name, const ScriptFunction::Ptr& function)
-{
-       boost::mutex::scoped_lock lock(GetMutex());
-
-       InternalGetFunctions()[name] = function;
-       OnRegistered(name, function);
-}
-
-/**
- * @threadsafety Always.
- */
-void ScriptFunction::Unregister(const String& name)
-{
-       boost::mutex::scoped_lock lock(GetMutex());
-
-       InternalGetFunctions().erase(name);
-       OnUnregistered(name);
-}
-
-/**
- * @threadsafety Always.
- */
-ScriptFunction::Ptr ScriptFunction::GetByName(const String& name)
-{
-       boost::mutex::scoped_lock lock(GetMutex());
-
-       map<String, ScriptFunction::Ptr>::iterator it;
-
-       it = InternalGetFunctions().find(name);
-
-       if (it == InternalGetFunctions().end())
-               return ScriptFunction::Ptr();
-
-       return it->second;
-}
-
 /**
  * @threadsafety Always.
  */
@@ -74,31 +32,3 @@ void ScriptFunction::Invoke(const ScriptTask::Ptr& task, const vector<Value>& ar
 {
        m_Callback(task, arguments);
 }
-
-/**
- * @threadsafety Always.
- */
-map<String, ScriptFunction::Ptr> ScriptFunction::GetFunctions(void)
-{
-       boost::mutex::scoped_lock lock(GetMutex());
-
-       return InternalGetFunctions(); /* makes a copy of the map */
-}
-
-/**
- * @threadsafety Caller must hold the mutex returned by GetMutex().
- */
-map<String, ScriptFunction::Ptr>& ScriptFunction::InternalGetFunctions(void)
-{
-       static map<String, ScriptFunction::Ptr> functions;
-       return functions;
-}
-
-/**
- * @threadsafety Always.
- */
-boost::mutex& ScriptFunction::GetMutex(void)
-{
-       static boost::mutex mtx;
-       return mtx;
-}
index 074c2fedc3196376e48656cb224f56817ee679d3..511415aee2c7df6a28267a5c051e7eeb51b23df5 100644 (file)
@@ -40,26 +40,22 @@ public:
 
        explicit ScriptFunction(const Callback& function);
 
-       static void Register(const String& name, const ScriptFunction::Ptr& function);
-       static void Unregister(const String& name);
-       static ScriptFunction::Ptr GetByName(const String& name);
-
-       static map<String, ScriptFunction::Ptr> GetFunctions(void);
-
-       static signals2::signal<void (const String&, const ScriptFunction::Ptr&)> OnRegistered;
-       static signals2::signal<void (const String&)> OnUnregistered;
-
 private:
        Callback m_Callback;
 
-       static map<String, ScriptFunction::Ptr>& InternalGetFunctions(void);
-       static boost::mutex& GetMutex(void);
-
        void Invoke(const shared_ptr<ScriptTask>& task, const vector<Value>& arguments);
 
        friend class ScriptTask;
 };
 
+/**
+ * A registry for script functions.
+ *
+ * @ingroup base
+ */
+class I2_BASE_API ScriptFunctionRegistry : public Registry<ScriptFunction::Ptr>
+{ };
+
 /**
  * Helper class for registering ScriptFunction implementation classes.
  *
@@ -70,10 +66,8 @@ class RegisterFunctionHelper
 public:
        RegisterFunctionHelper(const String& name, const ScriptFunction::Callback& function)
        {
-               if (!ScriptFunction::GetByName(name)) {
-                       ScriptFunction::Ptr func = boost::make_shared<ScriptFunction>(function);
-                       ScriptFunction::Register(name, func);
-               }
+               ScriptFunction::Ptr func = boost::make_shared<ScriptFunction>(function);
+               ScriptFunctionRegistry::GetInstance()->Register(name, func);
        }
 };
 
index 235c9fac71d6558068b8008e1dd9dc1d472cd0be..9929823f3538f5b4b1a059f8754c4a5ace7c44c3 100644 (file)
@@ -27,7 +27,7 @@ ScriptInterpreter::ScriptInterpreter(const Script::Ptr&)
 ScriptInterpreter::~ScriptInterpreter(void)
 {
        BOOST_FOREACH(const String& function, m_SubscribedFunctions) {
-               ScriptFunction::Unregister(function);
+               ScriptFunctionRegistry::GetInstance()->Unregister(function);
        }
 }
 
@@ -38,7 +38,7 @@ void ScriptInterpreter::SubscribeFunction(const String& name)
        m_SubscribedFunctions.insert(name);
 
        ScriptFunction::Ptr sf = boost::make_shared<ScriptFunction>(boost::bind(&ScriptInterpreter::ProcessCall, this, _1, name, _2));
-       ScriptFunction::Register(name, sf);
+       ScriptFunctionRegistry::GetInstance()->Register(name, sf);
 }
 
 void ScriptInterpreter::UnsubscribeFunction(const String& name)
@@ -46,5 +46,5 @@ void ScriptInterpreter::UnsubscribeFunction(const String& name)
        ObjectLock olock(this);
 
        m_SubscribedFunctions.erase(name);
-       ScriptFunction::Unregister(name);
+       ScriptFunctionRegistry::GetInstance()->Unregister(name);
 }
diff --git a/lib/base/singleton.h b/lib/base/singleton.h
new file mode 100644 (file)
index 0000000..4f65da0
--- /dev/null
@@ -0,0 +1,57 @@
+/******************************************************************************
+ * 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 SINGLETON_H
+#define SINGLETON_H
+
+namespace icinga
+{
+
+/**
+ * A singleton.
+ *
+ * @ingroup base
+ */
+template<typename T>
+class I2_BASE_API Singleton
+{
+public:
+       static T *GetInstance(void)
+       {
+               /* FIXME: This relies on static initializers being atomic. */
+               static boost::mutex mutex;
+               boost::mutex::scoped_lock lock(mutex);
+
+               if (!m_Instance)
+                       m_Instance = new T();
+
+               return m_Instance;
+       }
+private:
+       friend T *T::GetInstance(void);
+
+       static T *m_Instance;
+};
+
+template<typename T>
+T *Singleton<T>::m_Instance = NULL;
+
+}
+
+#endif /* SINGLETON_H */
index 487b714f6670e317eea11b58109566f176185f2a..4dfae8edc898f29d30334720bfcd918425dd2d2e 100644 (file)
@@ -210,14 +210,3 @@ void ConfigCompiler::AddIncludeSearchDir(const String& dir)
 
        m_IncludeSearchDirs.push_back(dir);
 }
-
-void ConfigCompiler::RegisterConfigFragment(const String& name, const String& fragment)
-{
-       GetConfigFragments()[name] = fragment;
-}
-
-map<String, String>& ConfigCompiler::GetConfigFragments(void)
-{
-       static map<String, String> fragments;
-       return fragments;
-}
index 6481a7bb7aecb0aa15ba0a37a87ac54f2490905a..fcd809c2a273537ee8c50ea383510ceb3890f56d 100644 (file)
@@ -58,9 +58,6 @@ public:
        size_t ReadInput(char *buffer, size_t max_bytes);
        void *GetScanner(void) const;
 
-       static void RegisterConfigFragment(const String& name, const String& fragment);
-       static map<String, String>& GetConfigFragments(void);
-
 private:
        String m_Path;
        istream *m_Input;
@@ -75,6 +72,9 @@ private:
        void DestroyScanner(void);
 };
 
+class ConfigFragmentRegistry : public Registry<String>
+{ };
+
 /**
  * Helper class for registering config fragments.
  *
@@ -85,7 +85,7 @@ class RegisterConfigFragmentHelper
 public:
        RegisterConfigFragmentHelper(const String& name, const String& fragment)
        {
-               ConfigCompiler::RegisterConfigFragment(name, fragment);
+               ConfigFragmentRegistry::GetInstance()->Register(name, fragment);
        }
 };
 
index f20041d6cccef7045492a4f2a57a2a045ca49ded..318c4a65c3f3e92500df87fea97f6d0dfceedc17 100644 (file)
@@ -122,7 +122,7 @@ void ConfigType::ValidateDictionary(const Dictionary::Ptr& dictionary,
                String validator = ruleList->GetValidator();
 
                if (!validator.IsEmpty()) {
-                       ScriptFunction::Ptr func = ScriptFunction::GetByName(validator);
+                       ScriptFunction::Ptr func = ScriptFunctionRegistry::GetInstance()->GetItem(validator);
 
                        if (!func)
                                BOOST_THROW_EXCEPTION(invalid_argument("Validator function '" + validator + "' does not exist."));
@@ -203,7 +203,7 @@ void ConfigType::ValidateArray(const Array::Ptr& array,
                String validator = ruleList->GetValidator();
 
                if (!validator.IsEmpty()) {
-                       ScriptFunction::Ptr func = ScriptFunction::GetByName(validator);
+                       ScriptFunction::Ptr func = ScriptFunctionRegistry::GetInstance()->GetItem(validator);
 
                        if (!func)
                                BOOST_THROW_EXCEPTION(invalid_argument("Validator function '" + validator + "' does not exist."));
index 001921a5cf75284da91c83d6ca8703d5f2c2dd95..008db7411f945ef55326e7b75dfd0bef239758a6 100644 (file)
@@ -399,8 +399,8 @@ void EndpointManager::RequestTimerHandler(void)
        map<String, PendingRequest>::iterator it;
        for (it = m_Requests.begin(); it != m_Requests.end(); ++it) {
                if (it->second.HasTimedOut()) {
-                       it->second.Callback(GetSelf(), Endpoint::Ptr(),
-                           it->second.Request, ResponseMessage(), true);
+                       it->second.Callback(Endpoint::Ptr(), it->second.Request,
+                           ResponseMessage(), true);
 
                        m_Requests.erase(it);
 
@@ -424,17 +424,12 @@ void EndpointManager::ProcessResponseMessage(const Endpoint::Ptr& sender,
        if (it == m_Requests.end())
                return;
 
-       it->second.Callback(GetSelf(), sender, it->second.Request, message, false);
+       it->second.Callback(sender, it->second.Request, message, false);
 
        m_Requests.erase(it);
 }
 
-EndpointManager::Ptr EndpointManager::GetInstance(void)
+EndpointManager *EndpointManager::GetInstance(void)
 {
-       static EndpointManager::Ptr instance;
-
-       if (!instance)
-               instance = boost::make_shared<EndpointManager>();
-
-       return instance;
+       return Singleton<EndpointManager>::GetInstance();
 }
index bcdb14c149b815009e9283bf4ea237795c7bc801..adc2364244b85de9f3935ff45f1ff706fbee4656 100644 (file)
@@ -36,7 +36,7 @@ public:
 
        EndpointManager(void);
 
-       static EndpointManager::Ptr GetInstance(void);
+       static EndpointManager *GetInstance(void);
 
        void SetIdentity(const String& identity);
        String GetIdentity(void) const;
@@ -53,14 +53,14 @@ public:
        void SendMulticastMessage(const RequestMessage& message);
        void SendMulticastMessage(const Endpoint::Ptr& sender, const RequestMessage& message);
 
-       typedef function<void(const EndpointManager::Ptr&, const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> APICallback;
+       typedef function<void(const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> APICallback;
 
        void SendAPIMessage(const Endpoint::Ptr& sender, const Endpoint::Ptr& recipient, RequestMessage& message,
            const APICallback& callback, double timeout = 30);
 
        void ProcessResponseMessage(const Endpoint::Ptr& sender, const ResponseMessage& message);
 
-       signals2::signal<void (const EndpointManager::Ptr&, const Endpoint::Ptr&)> OnNewEndpoint;
+       signals2::signal<void (const Endpoint::Ptr&)> OnNewEndpoint;
 
 private:
        String m_Identity;
@@ -84,7 +84,7 @@ private:
        {
                double Timeout;
                RequestMessage Request;
-               function<void(const EndpointManager::Ptr&, const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> Callback;
+               function<void(const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> Callback;
 
                bool HasTimedOut(void) const
                {