1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
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"
31
#include "drizzled/plugin.h"
32
#include "drizzled/show.h"
33
#include "drizzled/cursor.h"
34
#include "drizzled/abort_exception.h"
36
#include <boost/bind.hpp>
43
module::Registry::Registry() :
45
depend_graph_(new module::Graph()),
51
module::Registry::~Registry()
53
plugin::Plugin::map::iterator plugin_iter;
55
/* Give all plugins a chance to cleanup, before
56
* all plugins are deleted.
57
* 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();
67
plugin::Plugin::vector error_plugins;
68
plugin_iter= plugin_registry.begin();
69
while (plugin_iter != plugin_registry.end())
71
if ((*plugin_iter).second->removeLast())
73
error_plugins.push_back((*plugin_iter).second);
77
delete (*plugin_iter).second;
82
for (plugin::Plugin::vector::iterator iter= error_plugins.begin();
83
iter != error_plugins.end(); iter++)
88
plugin_registry.clear();
91
@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;
99
module_registry_.clear();
101
LibraryMap::iterator library_iter= library_registry_.begin();
102
while (library_iter != library_registry_.end())
104
delete (*library_iter).second;
107
library_registry_.clear();
110
void module::Registry::shutdown()
112
module::Registry& registry= singleton();
116
module::Module *module::Registry::find(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;
127
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);
133
module_registry_[add_str]= handle;
135
Vertex vertex_info(add_str, handle);
136
VertexDesc handle_vertex= boost::add_vertex(depend_graph_->getGraph());
137
depend_graph_->properties(handle_vertex)= vertex_info;
139
handle->setVertexHandle(new VertexHandle(handle_vertex));
143
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());
163
void module::Registry::buildDeps()
165
ModuleMap::iterator map_iter= module_registry_.begin();
166
while (map_iter != module_registry_.end())
168
Module *handle= (*map_iter).second;
169
Module::Depends::const_iterator handle_deps= handle->getDepends().begin();
170
while (handle_deps != handle->getDepends().end())
172
std::string dep_str((*handle_deps));
173
transform(dep_str.begin(), dep_str.end(),
174
dep_str.begin(), ::tolower);
176
bool found_dep= false;
177
vertex_iter it= boost::vertices(depend_graph_->getGraph()).first;
178
while (it != vertices(depend_graph_->getGraph()).second)
180
if (depend_graph_->properties(*it).getName() == dep_str)
183
add_edge(handle->getVertexHandle()->getVertexDesc(), *it, depend_graph_->getGraph());
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());
205
module::Registry::ModuleList module::Registry::getList()
212
std::vector<module::Module *> plugins;
214
VertexList vertex_list;
216
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)
221
Module *mod_ptr= depend_graph_->properties(*i).getModule();
224
plugins.push_back(mod_ptr);
231
module::Library *module::Registry::addLibrary(const std::string &plugin_name,
235
/* If this dll is already loaded just return it */
236
module::Library *library= findLibrary(plugin_name);
242
library= module::Library::loadLibrary(plugin_name, builtin);
245
/* Add this dll to the map */
246
library_registry_.insert(make_pair(plugin_name, library));
252
void module::Registry::removeLibrary(const std::string &plugin_name)
254
std::map<std::string, module::Library *>::iterator iter=
255
library_registry_.find(plugin_name);
256
if (iter != library_registry_.end())
258
library_registry_.erase(iter);
259
delete (*iter).second;
263
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;
271
void module::Registry::shutdownModules()
273
module_shutdown(*this);
276
} /* namespace drizzled */