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-07-27 16:05:02 +02:00
|
|
|
REGISTER_CLASS(Component);
|
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
|
|
|
{
|
|
|
|
assert(Application::IsMainThread());
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (!IsLocal())
|
|
|
|
throw_exception(runtime_error("Component objects must be local."));
|
|
|
|
|
2012-07-23 10:24:27 +02:00
|
|
|
string path;
|
|
|
|
#ifdef _WIN32
|
2012-07-27 16:05:02 +02:00
|
|
|
path = GetName() + ".dll";
|
2012-07-23 10:24:27 +02:00
|
|
|
#else /* _WIN32 */
|
2012-07-27 16:05:02 +02:00
|
|
|
path = GetName() + ".la";
|
2012-07-23 10:24:27 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
Logger::Write(LogInformation, "base", "Loading component '" + GetName() + "' (using library '" + path + "')");
|
2012-07-23 10:24:27 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
HMODULE hModule = LoadLibrary(path.c_str());
|
|
|
|
|
|
|
|
if (hModule == NULL)
|
|
|
|
throw_exception(Win32Exception("LoadLibrary('" + path + "') failed", GetLastError()));
|
|
|
|
#else /* _WIN32 */
|
|
|
|
lt_dlhandle hModule = lt_dlopen(path.c_str());
|
|
|
|
|
|
|
|
if (hModule == NULL) {
|
|
|
|
throw_exception(runtime_error("Could not load module '" + path + "': " + lt_dlerror()));
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
CreateComponentFunction pCreateComponent;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
pCreateComponent = (CreateComponentFunction)GetProcAddress(hModule,
|
|
|
|
"CreateComponent");
|
|
|
|
#else /* _WIN32 */
|
|
|
|
# ifdef __GNUC__
|
|
|
|
/* suppress compiler warning for void * cast */
|
|
|
|
__extension__
|
|
|
|
# endif
|
|
|
|
pCreateComponent = (CreateComponentFunction)lt_dlsym(hModule,
|
|
|
|
"CreateComponent");
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
IComponent::Ptr impl;
|
2012-07-23 10:24:27 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (pCreateComponent == NULL)
|
|
|
|
throw_exception(runtime_error("Loadable module does not contain "
|
|
|
|
"CreateComponent function"));
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
/* pCreateComponent returns a raw pointer which we must wrap in a shared_ptr */
|
|
|
|
impl = IComponent::Ptr(pCreateComponent());
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (!impl)
|
2012-07-23 10:24:27 +02:00
|
|
|
throw_exception(runtime_error("CreateComponent function returned NULL."));
|
|
|
|
} catch (...) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
FreeLibrary(hModule);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
lt_dlclose(hModule);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
impl->m_Config = this;
|
|
|
|
SetImplementation(impl);
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
impl->Start();
|
2012-07-23 10:24:27 +02:00
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
Component::~Component(void)
|
2012-07-23 10:24:27 +02:00
|
|
|
{
|
2012-07-27 16:05:02 +02:00
|
|
|
IComponent::Ptr impl = GetImplementation();
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (impl)
|
|
|
|
impl->Stop();
|
2012-07-23 10:24:27 +02:00
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
IComponent::Ptr Component::GetImplementation(void) const
|
2012-07-23 10:24:27 +02:00
|
|
|
{
|
2012-07-27 16:05:02 +02:00
|
|
|
IComponent::Ptr impl;
|
|
|
|
GetTag("impl", &impl);
|
|
|
|
return impl;
|
|
|
|
}
|
2012-07-23 10:24:27 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
void Component::SetImplementation(const IComponent::Ptr& impl)
|
|
|
|
{
|
|
|
|
SetTag("impl", impl);
|
2012-07-23 10:24:27 +02:00
|
|
|
}
|
|
|
|
|
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-07-23 10:24:27 +02:00
|
|
|
void Component::AddSearchDir(const string& componentDirectory)
|
2012-03-31 16:03:42 +02:00
|
|
|
{
|
2012-07-23 10:24:27 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
SetDllDirectory(componentDirectory.c_str());
|
|
|
|
#else /* _WIN32 */
|
|
|
|
lt_dladdsearchdir(componentDirectory.c_str());
|
|
|
|
#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-07-27 16:05:02 +02:00
|
|
|
return m_Config->GetSelf();
|
2012-03-31 16:03:42 +02:00
|
|
|
}
|
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. */
|
|
|
|
}
|