Fawkes API  Fawkes Development Version
plugin.cpp
1 
2 /***************************************************************************
3  * plugin.cpp - XML-RPC methods related to plugin management
4  *
5  * Created: Mon Aug 31 00:56:55 2009
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "plugin.h"
24 
25 #include <logging/logger.h>
26 #include <plugin/manager.h>
27 
28 #include <algorithm>
29 #include <map>
30 #include <string>
31 #include <vector>
32 #include <xmlrpc-c/girerr.hpp>
33 
34 /** @class XmlRpcPluginMethods "plugin.h"
35  * Wrapper class for plugin related XML-RPC methods.
36  * @author Tim Niemueller
37  */
38 
39 /** Constructor.
40  * @param registry XML registry, methods will be automatically registered
41  * @param plugin_manager plugin manager used for listing, loading and unloading plugins
42  * @param logger logger to output informational and error messages
43  */
44 XmlRpcPluginMethods::XmlRpcPluginMethods(std::shared_ptr<xmlrpc_c::registry> registry,
45  fawkes::PluginManager * plugin_manager,
46  fawkes::Logger * logger)
47 {
48  xmlrpc_registry_ = registry;
49  plugin_manager_ = plugin_manager;
50  logger_ = logger;
51  plugin_list_.reset(new plugin_list(plugin_manager));
52  plugin_load_.reset(new plugin_load(plugin_manager, logger));
53  plugin_unload_.reset(new plugin_unload(plugin_manager, logger));
54  xmlrpc_registry_->addMethod("plugin.list", &*plugin_list_);
55  xmlrpc_registry_->addMethod("plugin.load", &*plugin_load_);
56  xmlrpc_registry_->addMethod("plugin.unload", &*plugin_unload_);
57 }
58 
59 /** Destructor. */
61 {
62  plugin_list_.reset();
63  plugin_load_.reset();
64  plugin_unload_.reset();
65 }
66 
67 /** @class XmlRpcPluginMethods::plugin_list "plugin.h"
68  * Plugin list XML-RPC method.
69  * @author Tim Niemueller
70  */
71 
72 /** Constructor.
73  * @param plugin_manager plugin manager to query for plugin listings.
74  */
76 {
77  _signature = "A:";
78  _help = "Returns array of plugins. Each entry is a struct consisting of the "
79  "entries name, desc, and loaded.";
80 
81  plugin_manager_ = plugin_manager;
82 }
83 
84 /** Virtual empty destructor. */
86 {
87 }
88 
89 /** Execute method.
90  * @param params parameters
91  * @param result result value
92  */
93 void
94 XmlRpcPluginMethods::plugin_list::execute(xmlrpc_c::paramList const &params,
95  xmlrpc_c::value *const result)
96 {
97  std::list<std::pair<std::string, std::string>> available_plugins;
98  std::list<std::string> loadedp;
99  available_plugins = plugin_manager_->get_available_plugins();
100  loadedp = plugin_manager_->get_loaded_plugins();
101 
102  loadedp.sort();
103 
104  std::vector<xmlrpc_c::value> array;
105 
106  std::list<std::pair<std::string, std::string>>::iterator i;
107  for (i = available_plugins.begin(); i != available_plugins.end(); ++i) {
108  std::map<std::string, xmlrpc_c::value> elem;
109  elem.insert(std::make_pair("name", xmlrpc_c::value_string(i->first)));
110  elem.insert(std::make_pair("desc", xmlrpc_c::value_string(i->second)));
111  bool loaded = std::binary_search(loadedp.begin(), loadedp.end(), i->first);
112  elem.insert(std::make_pair("loaded", xmlrpc_c::value_boolean(loaded)));
113  array.push_back(xmlrpc_c::value_struct(elem));
114  }
115 
116  *result = xmlrpc_c::value_array(array);
117 }
118 
119 /** @class XmlRpcPluginMethods::plugin_load "plugin.h"
120  * XML-RPC method to load a plugin.
121  * @author Tim Niemueller
122  */
123 
124 /** Constructor.
125  * @param plugin_manager plugin manager to query for plugin listings.
126  * @param logger logger to report problems
127  */
129  fawkes::Logger * logger)
130 {
131  _signature = "b:s";
132  _help = "Load plugin specified as argument, returns true on success, false otherwise.";
133 
134  plugin_manager_ = plugin_manager;
135  logger_ = logger;
136 }
137 
138 /** Virtual empty destructor. */
140 {
141 }
142 
143 /** Execute method.
144  * @param params parameters
145  * @param result result value
146  */
147 void
148 XmlRpcPluginMethods::plugin_load::execute(xmlrpc_c::paramList const &params,
149  xmlrpc_c::value *const result)
150 {
151  try {
152  std::string plugin_name = params.getString(0);
153  plugin_manager_->load(plugin_name.c_str());
154  } catch (girerr::error &e) {
155  throw xmlrpc_c::fault(e.what(), xmlrpc_c::fault::CODE_UNSPECIFIED);
156  } catch (fawkes::Exception &e) {
157  logger_->log_warn("XML-RPC plugin.load", e);
158  *result = xmlrpc_c::value_boolean(false);
159  }
160 
161  *result = xmlrpc_c::value_boolean(true);
162 }
163 
164 /** @class XmlRpcPluginMethods::plugin_unload "plugin.h"
165  * XML-RPC method to unload a plugin.
166  * @author Tim Niemueller
167  */
168 
169 /** Constructor.
170  * @param plugin_manager plugin manager to query for plugin listings.
171  * @param logger logger to report problems
172  */
174  fawkes::Logger * logger)
175 {
176  _signature = "b:s";
177  _help = "Unload plugin specified as argument, returns true on success, false otherwise.";
178 
179  plugin_manager_ = plugin_manager;
180  logger_ = logger;
181 }
182 
183 /** Virtual empty destructor. */
185 {
186 }
187 
188 /** Execute method.
189  * @param params parameters
190  * @param result result value
191  */
192 void
193 XmlRpcPluginMethods::plugin_unload::execute(xmlrpc_c::paramList const &params,
194  xmlrpc_c::value *const result)
195 {
196  try {
197  std::string plugin_name = params.getString(0);
198  plugin_manager_->unload(plugin_name.c_str());
199  } catch (girerr::error &e) {
200  throw xmlrpc_c::fault(e.what(), xmlrpc_c::fault::CODE_UNSPECIFIED);
201  } catch (fawkes::Exception &e) {
202  logger_->log_warn("XML-RPC plugin.unload", e);
203  *result = xmlrpc_c::value_boolean(false);
204  }
205 
206  *result = xmlrpc_c::value_boolean(true);
207 }
XmlRpcPluginMethods::plugin_list::plugin_list
plugin_list(fawkes::PluginManager *plugin_manager)
Constructor.
Definition: plugin.cpp:75
fawkes::PluginManager
Definition: manager.h:53
XmlRpcPluginMethods::~XmlRpcPluginMethods
~XmlRpcPluginMethods()
Destructor.
Definition: plugin.cpp:60
XmlRpcPluginMethods::plugin_load
Definition: plugin.h:52
XmlRpcPluginMethods::plugin_list::~plugin_list
virtual ~plugin_list()
Virtual empty destructor.
Definition: plugin.cpp:85
XmlRpcPluginMethods::plugin_load::execute
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: plugin.cpp:148
XmlRpcPluginMethods::plugin_unload::plugin_unload
plugin_unload(fawkes::PluginManager *plugin_manager, fawkes::Logger *logger)
Constructor.
Definition: plugin.cpp:173
fawkes::PluginManager::get_available_plugins
std::list< std::pair< std::string, std::string > > get_available_plugins()
Generate list of all available plugins.
Definition: manager.cpp:224
fawkes::PluginManager::get_loaded_plugins
std::list< std::string > get_loaded_plugins()
Get list of loaded plugins.
Definition: manager.cpp:240
XmlRpcPluginMethods::plugin_unload::execute
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: plugin.cpp:193
XmlRpcPluginMethods::plugin_unload
Definition: plugin.h:64
XmlRpcPluginMethods::plugin_unload::~plugin_unload
virtual ~plugin_unload()
Virtual empty destructor.
Definition: plugin.cpp:184
fawkes::Logger
Definition: logger.h:41
fawkes::PluginManager::load
void load(const std::string &plugin_list)
Load plugin.
Definition: manager.cpp:331
fawkes::Logger::log_warn
virtual void log_warn(const char *component, const char *format,...)=0
XmlRpcPluginMethods::plugin_list
Definition: plugin.h:41
XmlRpcPluginMethods::XmlRpcPluginMethods
XmlRpcPluginMethods(std::shared_ptr< xmlrpc_c::registry > registry, fawkes::PluginManager *plugin_manager, fawkes::Logger *logger)
Constructor.
Definition: plugin.cpp:44
XmlRpcPluginMethods::plugin_list::execute
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: plugin.cpp:94
XmlRpcPluginMethods::plugin_load::plugin_load
plugin_load(fawkes::PluginManager *plugin_manager, fawkes::Logger *logger)
Constructor.
Definition: plugin.cpp:128
XmlRpcPluginMethods::plugin_load::~plugin_load
virtual ~plugin_load()
Virtual empty destructor.
Definition: plugin.cpp:139
fawkes::PluginManager::unload
void unload(const std::string &plugin_name)
Unload plugin.
Definition: manager.cpp:433
fawkes::Exception
Definition: exception.h:41