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;
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
REGISTER_TYPE(Component);
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
boost::mutex Component::m_Mutex;
|
2013-02-24 01:10:34 +01:00
|
|
|
map<String, Component::Factory> Component::m_Factories;
|
|
|
|
|
2012-07-23 10:24:27 +02:00
|
|
|
/**
|
2012-07-27 16:05:02 +02:00
|
|
|
* Constructor for the component class.
|
2012-07-23 10:24:27 +02:00
|
|
|
*/
|
2012-07-27 16:05:02 +02:00
|
|
|
Component::Component(const Dictionary::Ptr& properties)
|
2012-07-30 10:17:29 +02:00
|
|
|
: DynamicObject(properties)
|
2012-07-23 10:24:27 +02:00
|
|
|
{
|
2012-07-27 16:05:02 +02:00
|
|
|
if (!IsLocal())
|
2013-02-06 12:51:12 +01:00
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Component objects must be local."));
|
2012-07-27 16:05:02 +02:00
|
|
|
|
2013-01-17 15:05:34 +01:00
|
|
|
Logger::Write(LogInformation, "base", "Loading component '" + GetName() + "'");
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
(void) Utility::LoadIcingaLibrary(GetName(), true);
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Component::Factory factory;
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
map<String, Factory>::iterator it;
|
|
|
|
it = m_Factories.find(GetName());
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
if (it == m_Factories.end())
|
|
|
|
BOOST_THROW_EXCEPTION(invalid_argument("Unknown component: " + GetName()));
|
|
|
|
|
|
|
|
factory = it->second;
|
|
|
|
}
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
m_Impl = factory();
|
|
|
|
|
|
|
|
if (!m_Impl)
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Component factory returned NULL."));
|
2012-07-23 10:24:27 +02:00
|
|
|
}
|
|
|
|
|
2012-09-14 14:41:17 +02:00
|
|
|
/**
|
|
|
|
* Destructor for the Component class.
|
|
|
|
*/
|
2012-07-27 16:05:02 +02:00
|
|
|
Component::~Component(void)
|
2012-07-23 10:24:27 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
if (m_Impl)
|
|
|
|
m_Impl->Stop();
|
2012-07-23 10:24:27 +02:00
|
|
|
}
|
|
|
|
|
2012-09-28 10:39:28 +02:00
|
|
|
/**
|
|
|
|
* Starts the component. Called when the DynamicObject is fully
|
|
|
|
* constructed/registered.
|
|
|
|
*/
|
|
|
|
void Component::Start(void)
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
m_Impl->SetConfig(GetSelf());
|
2012-09-28 10:39:28 +02:00
|
|
|
m_Impl->Start();
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
2012-07-23 10:24:27 +02:00
|
|
|
* Adds a directory to the component search path.
|
2012-04-24 14:02:15 +02:00
|
|
|
*
|
2012-07-23 10:24:27 +02:00
|
|
|
* @param componentDirectory The directory.
|
2012-04-24 14:02:15 +02:00
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
void Component::AddSearchDir(const String& componentDirectory)
|
2012-03-31 16:03:42 +02:00
|
|
|
{
|
2012-10-02 09:26:17 +02:00
|
|
|
Logger::Write(LogInformation, "base", "Adding library search dir: " +
|
|
|
|
componentDirectory);
|
|
|
|
|
2012-07-23 10:24:27 +02:00
|
|
|
#ifdef _WIN32
|
2012-08-02 09:38:08 +02:00
|
|
|
SetDllDirectory(componentDirectory.CStr());
|
2012-07-23 10:24:27 +02:00
|
|
|
#else /* _WIN32 */
|
2012-08-02 09:38:08 +02:00
|
|
|
lt_dladdsearchdir(componentDirectory.CStr());
|
2012-07-23 10:24:27 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the configuration for this component.
|
|
|
|
*
|
|
|
|
* @returns The configuration.
|
|
|
|
*/
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr IComponent::GetConfig(void) const
|
2012-03-31 16:03:42 +02:00
|
|
|
{
|
2012-09-28 10:39:28 +02:00
|
|
|
return m_Config.lock();
|
2012-03-31 16:03:42 +02:00
|
|
|
}
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* Sets the configuration for this component.
|
|
|
|
*/
|
|
|
|
void IComponent::SetConfig(const DynamicObject::Ptr& config)
|
|
|
|
{
|
|
|
|
m_Config = config;
|
|
|
|
}
|
|
|
|
|
2012-07-23 10:24:27 +02:00
|
|
|
/**
|
|
|
|
* Starts the component.
|
|
|
|
*/
|
2012-07-27 16:05:02 +02:00
|
|
|
void IComponent::Start(void)
|
2012-07-23 10:24:27 +02:00
|
|
|
{
|
|
|
|
/* Nothing to do in the default implementation. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the component.
|
|
|
|
*/
|
2012-07-27 16:05:02 +02:00
|
|
|
void IComponent::Stop(void)
|
2012-07-23 10:24:27 +02:00
|
|
|
{
|
|
|
|
/* Nothing to do in the default implementation. */
|
|
|
|
}
|
2013-02-24 01:10:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a component factory.
|
|
|
|
*/
|
|
|
|
void Component::Register(const String& name, const Component::Factory& factory)
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
m_Factories[name] = factory;
|
|
|
|
}
|