17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
#include "drizzled/module/registry.h"
27
#include "drizzled/module/library.h"
28
#include "drizzled/module/graph.h"
29
#include "drizzled/module/vertex_handle.h"
26
#include <drizzled/module/registry.h>
27
#include <drizzled/module/library.h>
28
#include <drizzled/module/graph.h>
29
#include <drizzled/module/vertex_handle.h>
31
#include "drizzled/plugin.h"
32
#include "drizzled/show.h"
33
#include "drizzled/cursor.h"
34
#include "drizzled/abort_exception.h"
31
#include <drizzled/plugin.h>
32
#include <drizzled/show.h>
33
#include <drizzled/cursor.h>
34
#include <drizzled/abort_exception.h>
35
#include <drizzled/util/find_ptr.h>
36
37
#include <boost/bind.hpp>
38
#include <boost/foreach.hpp>
38
40
using namespace std;
43
44
module::Registry::Registry() :
44
45
module_registry_(),
51
52
module::Registry::~Registry()
53
plugin::Plugin::map::iterator plugin_iter;
55
54
/* Give all plugins a chance to cleanup, before
56
55
* all plugins are deleted.
57
56
* This can be used if shutdown code references
60
plugin_iter= plugin_registry.begin();
61
while (plugin_iter != plugin_registry.end())
63
(*plugin_iter).second->shutdownPlugin();
59
BOOST_FOREACH(plugin::Plugin::map::reference it, plugin_registry)
60
it.second->shutdownPlugin();
67
62
plugin::Plugin::vector error_plugins;
68
plugin_iter= plugin_registry.begin();
69
while (plugin_iter != plugin_registry.end())
63
BOOST_FOREACH(plugin::Plugin::map::reference it, plugin_registry)
71
if ((*plugin_iter).second->removeLast())
73
error_plugins.push_back((*plugin_iter).second);
65
if (it.second->removeLast())
66
error_plugins.push_back(it.second);
77
delete (*plugin_iter).second;
82
for (plugin::Plugin::vector::iterator iter= error_plugins.begin();
83
iter != error_plugins.end(); iter++)
71
BOOST_FOREACH(plugin::Plugin::vector::reference it, error_plugins)
88
74
plugin_registry.clear();
91
78
@TODO When we delete modules here, we segfault on a bad string. Why?
92
ModuleMap::iterator module_iter= module_registry_.begin();
94
while (module_iter != module_registry_.end())
96
delete (*module_iter).second;
81
BOOST_FOREACH(ModuleMap::reference it, module_registry_)
99
83
module_registry_.clear();
101
LibraryMap::iterator library_iter= library_registry_.begin();
102
while (library_iter != library_registry_.end())
104
delete (*library_iter).second;
85
BOOST_FOREACH(LibraryMap::reference it, library_registry_)
107
87
library_registry_.clear();
110
90
void module::Registry::shutdown()
112
module::Registry& registry= singleton();
116
module::Module *module::Registry::find(std::string name)
95
module::Module* module::Registry::find(const std::string& name)
118
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
120
ModuleMap::iterator map_iter;
121
map_iter= module_registry_.find(name);
122
if (map_iter != module_registry_.end())
123
return (*map_iter).second;
97
return find_ptr2(module_registry_, boost::to_lower_copy(name));
127
100
void module::Registry::add(module::Module *handle)
129
std::string add_str(handle->getName());
130
transform(add_str.begin(), add_str.end(),
131
add_str.begin(), ::tolower);
102
std::string add_str(boost::to_lower_copy(handle->getName()));
133
104
module_registry_[add_str]= handle;
137
108
depend_graph_->properties(handle_vertex)= vertex_info;
139
110
handle->setVertexHandle(new VertexHandle(handle_vertex));
143
113
void module::Registry::remove(module::Module *handle)
145
std::string remove_str(handle->getName());
146
std::transform(remove_str.begin(), remove_str.end(),
147
remove_str.begin(), ::tolower);
149
module_registry_.erase(remove_str);
152
void module::Registry::copy(plugin::Plugin::vector &arg)
154
arg.reserve(plugin_registry.size());
156
std::transform(plugin_registry.begin(),
157
plugin_registry.end(),
158
std::back_inserter(arg),
159
boost::bind(&plugin::Plugin::map::value_type::second, _1) );
160
assert(arg.size() == plugin_registry.size());
115
module_registry_.erase(boost::to_lower_copy(handle->getName()));
163
118
void module::Registry::buildDeps()
165
ModuleMap::iterator map_iter= module_registry_.begin();
166
while (map_iter != module_registry_.end())
120
BOOST_FOREACH(ModuleMap::reference map_iter, module_registry_)
168
Module *handle= (*map_iter).second;
169
Module::Depends::const_iterator handle_deps= handle->getDepends().begin();
170
while (handle_deps != handle->getDepends().end())
122
Module* handle= map_iter.second;
123
BOOST_FOREACH(Module::Depends::const_reference handle_deps, handle->getDepends())
172
std::string dep_str((*handle_deps));
173
transform(dep_str.begin(), dep_str.end(),
174
dep_str.begin(), ::tolower);
125
std::string dep_str(boost::to_lower_copy(handle_deps));
176
126
bool found_dep= false;
177
vertex_iter it= boost::vertices(depend_graph_->getGraph()).first;
178
while (it != vertices(depend_graph_->getGraph()).second)
127
for (vertex_iter it= boost::vertices(depend_graph_->getGraph()).first; it != vertices(depend_graph_->getGraph()).second; it++)
180
129
if (depend_graph_->properties(*it).getName() == dep_str)
183
132
add_edge(handle->getVertexHandle()->getVertexDesc(), *it, depend_graph_->getGraph());
188
136
if (not found_dep)
190
errmsg_printf(error::ERROR,
191
_("Couldn't process plugin module dependencies. "
192
"%s depends on %s but %s is not to be loaded.\n"),
193
handle->getName().c_str(),
194
dep_str.c_str(), dep_str.c_str());
138
errmsg_printf(error::ERROR, _("Couldn't process plugin module dependencies. %s depends on %s but %s is not to be loaded.\n"),
139
handle->getName().c_str(), dep_str.c_str(), dep_str.c_str());
202
144
deps_built_= true;
205
147
module::Registry::ModuleList module::Registry::getList()
207
149
if (not deps_built_)
212
std::vector<module::Module *> plugins;
214
151
VertexList vertex_list;
216
152
boost::topological_sort(depend_graph_->getGraph(), std::back_inserter(vertex_list));
218
for (VertexList::iterator i = vertex_list.begin();
219
i != vertex_list.end(); ++i)
154
BOOST_FOREACH(VertexList::reference it, vertex_list)
221
Module *mod_ptr= depend_graph_->properties(*i).getModule();
156
if (Module* mod_ptr= depend_graph_->properties(it).getModule())
224
157
plugins.push_back(mod_ptr);
231
module::Library *module::Registry::addLibrary(const std::string &plugin_name,
162
module::Library *module::Registry::addLibrary(const std::string &plugin_name, bool builtin)
235
164
/* If this dll is already loaded just return it */
236
165
module::Library *library= findLibrary(plugin_name);
242
169
library= module::Library::loadLibrary(plugin_name, builtin);
245
172
/* Add this dll to the map */
246
173
library_registry_.insert(make_pair(plugin_name, library));
252
178
void module::Registry::removeLibrary(const std::string &plugin_name)
254
std::map<std::string, module::Library *>::iterator iter=
255
library_registry_.find(plugin_name);
180
LibraryMap::iterator iter= library_registry_.find(plugin_name);
256
181
if (iter != library_registry_.end())
258
184
library_registry_.erase(iter);
259
delete (*iter).second;
263
188
module::Library *module::Registry::findLibrary(const std::string &plugin_name) const
265
LibraryMap::const_iterator iter= library_registry_.find(plugin_name);
266
if (iter != library_registry_.end())
267
return (*iter).second;
190
return find_ptr2(library_registry_, plugin_name);
271
193
void module::Registry::shutdownModules()