24 #include <plugin/loader.h> 26 #include <utils/system/dynamic_module/module_manager.h> 27 #include <utils/system/dynamic_module/module.h> 37 class PluginLoader::Data
41 std::map< Plugin *, Module * > plugin_module_map;
42 std::map< std::string, Plugin * > name_plugin_map;
43 std::map< Plugin *, std::string > plugin_name_map;
58 append(
"Plugin '%s' could not be loaded: %s", plugin, message);
76 append(
"Plugin '%s' could not be loaded: %s", plugin, message);
102 append(
"Plugin '%s' could not be unloaded", plugin_name);
148 PluginLoader::open_module(
const char *plugin_name)
150 std::string module_name = std::string(plugin_name) +
"." + d->mm->get_module_file_extension();
153 return d->mm->open_module(module_name.c_str());
161 PluginLoader::create_instance(
const char *plugin_name,
Module *module)
163 if ( ! module->
has_symbol(
"plugin_factory") ) {
164 throw PluginLoadException(plugin_name,
"Symbol 'plugin_factory' not found. Forgot EXPORT_PLUGIN?");
166 if ( ! module->
has_symbol(
"plugin_description") ) {
167 throw PluginLoadException(plugin_name,
"Symbol 'plugin_description' not found. Forgot PLUGIN_DESCRIPTION?");
170 PluginFactoryFunc pff = (PluginFactoryFunc)module->
get_symbol(
"plugin_factory");
203 std::string pn = plugin_name;
205 if ( d->name_plugin_map.find(pn) != d->name_plugin_map.end() ) {
206 return d->name_plugin_map[pn];
210 Module *module = open_module(plugin_name);
211 Plugin *p = create_instance(plugin_name, module);
213 d->plugin_module_map[p] = module;
214 d->name_plugin_map[pn] = p;
215 d->plugin_name_map[p] = pn;
232 Module *module = open_module(plugin_name);
234 if ( ! module->
has_symbol(
"plugin_description") ) {
235 throw PluginLoadException(plugin_name,
"Symbol 'plugin_description' not found. Forgot PLUGIN_DESCRIPTION?");
239 std::string rv = pdf();
240 d->mm->close_module(module);
253 return ( d->name_plugin_map.find(plugin_name) != d->name_plugin_map.end() );
271 if ( d->plugin_module_map.find(plugin) != d->plugin_module_map.end() ) {
273 PluginDestroyFunc pdf = (PluginDestroyFunc)d->plugin_module_map[plugin]->get_symbol(
"plugin_destroy");
277 d->mm->close_module(d->plugin_module_map[plugin]);
278 d->plugin_module_map.erase(plugin);
280 d->name_plugin_map.erase(d->plugin_name_map[plugin]);
281 d->plugin_name_map.erase(plugin);
PluginLoader(const char *plugin_base_dir, Configuration *config)
Constructor.
PluginUnloadException(const char *plugin_type, const char *add_msg=NULL)
Constructor.
void set_name(const char *name)
Set plugin name.
Fawkes library namespace.
This exception is thrown if the requested plugin could not be loaded.
~PluginLoadException()
Destructor.
PluginLoadException(const char *plugin, const char *message)
Constructor.
std::string plugin_name() const
Get name of plugin which failed to load.
void unload(Plugin *plugin)
Unload the given plugin This will unload the given plugin.
ModuleManager * get_module_manager() const
Get module manager.
Dynamic module loader for Linux, FreeBSD, and MacOS X.
Base class for exceptions in Fawkes.
std::string get_description(const char *plugin_name)
Get plugin description.
const char *(* PluginDescriptionFunc)()
Plugin description function for the shared library.
virtual void * get_symbol(const char *symbol_name)
Get a symbol from the module.
void copy_messages(const Exception &exc)
Copy messages from given exception.
bool is_loaded(const char *plugin_name)
Check if a plugin is loaded.
~PluginLoader()
Destructor.
Interface for configuration handling.
void append(const char *format,...)
Append messages to the message list.
virtual bool has_symbol(const char *symbol_name)
Check if the module has the given symbol.
Plugin * load(const char *plugin_name)
Load a specific plugin The plugin loader is clever and guarantees that every plugin is only loaded on...