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 #include <plugin/manager.h>
25 #include <logging/logger.h>
26 
27 #include <algorithm>
28 #include <map>
29 #include <string>
30 #include <vector>
31 
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(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 = new plugin_list(plugin_manager);
52  __plugin_load = new plugin_load(plugin_manager, logger);
53  __plugin_unload = 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  delete __plugin_list;
63  delete __plugin_load;
64  delete __plugin_unload;
65 }
66 
67 
68 /** @class XmlRpcPluginMethods::plugin_list "plugin.h"
69  * Plugin list XML-RPC method.
70  * @author Tim Niemueller
71  */
72 
73 /** Constructor.
74  * @param plugin_manager plugin manager to query for plugin listings.
75  */
77 {
78  _signature = "A:";
79  _help = "Returns array of plugins. Each entry is a struct consisting of the "
80  "entries name, desc, and loaded.";
81 
82  __plugin_manager = plugin_manager;
83 }
84 
85 /** Virtual empty destructor. */
87 {
88 }
89 
90 /** Execute method.
91  * @param params parameters
92  * @param result result value
93  */
94 void
95 XmlRpcPluginMethods::plugin_list::execute(xmlrpc_c::paramList const& params,
96  xmlrpc_c::value * const result)
97 {
98  std::list<std::pair<std::string, std::string> > available_plugins;
99  std::list<std::string> loadedp;
100  available_plugins = __plugin_manager->get_available_plugins();
101  loadedp = __plugin_manager->get_loaded_plugins();
102 
103  loadedp.sort();
104 
105  std::vector<xmlrpc_c::value> array;
106 
107  std::list<std::pair<std::string, std::string> >::iterator i;
108  for (i = available_plugins.begin(); i != available_plugins.end(); ++i) {
109  std::map<std::string, xmlrpc_c::value> elem;
110  elem.insert(std::make_pair("name", xmlrpc_c::value_string(i->first)));
111  elem.insert(std::make_pair("desc", xmlrpc_c::value_string(i->second)));
112  bool loaded = std::binary_search(loadedp.begin(), loadedp.end(), i->first);
113  elem.insert(std::make_pair("loaded", xmlrpc_c::value_boolean(loaded)));
114  array.push_back(xmlrpc_c::value_struct(elem));
115  }
116 
117  *result = xmlrpc_c::value_array(array);
118 }
119 
120 
121 /** @class XmlRpcPluginMethods::plugin_load "plugin.h"
122  * XML-RPC method to load a plugin.
123  * @author Tim Niemueller
124  */
125 
126 
127 /** Constructor.
128  * @param plugin_manager plugin manager to query for plugin listings.
129  * @param logger logger to report problems
130  */
132  fawkes::Logger *logger)
133 {
134  _signature = "b:s";
135  _help = "Load plugin specified as argument, returns true on success, false otherwise.";
136 
137  __plugin_manager = plugin_manager;
138  __logger = logger;
139 }
140 
141 /** Virtual empty destructor. */
143 {
144 }
145 
146 /** Execute method.
147  * @param params parameters
148  * @param result result value
149  */
150 void
151 XmlRpcPluginMethods::plugin_load::execute(xmlrpc_c::paramList const& params,
152  xmlrpc_c::value * const result)
153 {
154  try {
155  std::string plugin_name = params.getString(0);
156  __plugin_manager->load(plugin_name.c_str());
157  } catch (girerr::error &e) {
158  throw xmlrpc_c::fault(e.what(), xmlrpc_c::fault::CODE_UNSPECIFIED);
159  } catch (fawkes::Exception &e) {
160  __logger->log_warn("XML-RPC plugin.load", e);
161  *result = xmlrpc_c::value_boolean(false);
162  }
163 
164  *result = xmlrpc_c::value_boolean(true);
165 }
166 
167 
168 
169 /** @class XmlRpcPluginMethods::plugin_unload "plugin.h"
170  * XML-RPC method to unload a plugin.
171  * @author Tim Niemueller
172  */
173 
174 /** Constructor.
175  * @param plugin_manager plugin manager to query for plugin listings.
176  * @param logger logger to report problems
177  */
179  fawkes::Logger *logger)
180 {
181  _signature = "b:s";
182  _help = "Unload plugin specified as argument, returns true on success, false otherwise.";
183 
184  __plugin_manager = plugin_manager;
185  __logger = logger;
186 }
187 
188 /** Virtual empty destructor. */
190 {
191 }
192 
193 /** Execute method.
194  * @param params parameters
195  * @param result result value
196  */
197 void
198 XmlRpcPluginMethods::plugin_unload::execute(xmlrpc_c::paramList const& params,
199  xmlrpc_c::value * const result)
200 {
201  try {
202  std::string plugin_name = params.getString(0);
203  __plugin_manager->unload(plugin_name.c_str());
204  } catch (girerr::error &e) {
205  throw xmlrpc_c::fault(e.what(), xmlrpc_c::fault::CODE_UNSPECIFIED);
206  } catch (fawkes::Exception &e) {
207  __logger->log_warn("XML-RPC plugin.unload", e);
208  *result = xmlrpc_c::value_boolean(false);
209  }
210 
211  *result = xmlrpc_c::value_boolean(true);
212 }
plugin_list(fawkes::PluginManager *plugin_manager)
Constructor.
Definition: plugin.cpp:76
XML-RPC method to load a plugin.
Definition: plugin.h:50
std::list< std::pair< std::string, std::string > > get_available_plugins()
Generate list of all available plugins.
Definition: manager.cpp:218
~XmlRpcPluginMethods()
Destructor.
Definition: plugin.cpp:60
XML-RPC method to unload a plugin.
Definition: plugin.h:61
virtual ~plugin_list()
Virtual empty destructor.
Definition: plugin.cpp:86
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: plugin.cpp:151
Fawkes Plugin Manager.
Definition: manager.h:51
Plugin list XML-RPC method.
Definition: plugin.h:40
XmlRpcPluginMethods(xmlrpc_c::registry *registry, fawkes::PluginManager *plugin_manager, fawkes::Logger *logger)
Constructor.
Definition: plugin.cpp:44
Base class for exceptions in Fawkes.
Definition: exception.h:36
void load(const char *plugin_list)
Load plugin.
Definition: manager.cpp:302
virtual ~plugin_unload()
Virtual empty destructor.
Definition: plugin.cpp:189
std::list< std::string > get_loaded_plugins()
Get list of loaded plugins.
Definition: manager.cpp:234
void unload(const char *plugin_name)
Unload plugin.
Definition: manager.cpp:386
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: plugin.cpp:198
virtual void execute(xmlrpc_c::paramList const &params, xmlrpc_c::value *const result)
Execute method.
Definition: plugin.cpp:95
virtual ~plugin_load()
Virtual empty destructor.
Definition: plugin.cpp:142
plugin_unload(fawkes::PluginManager *plugin_manager, fawkes::Logger *logger)
Constructor.
Definition: plugin.cpp:178
plugin_load(fawkes::PluginManager *plugin_manager, fawkes::Logger *logger)
Constructor.
Definition: plugin.cpp:131
Interface for logging.
Definition: logger.h:34