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>
33 #include <drizzled/cursor.h>
34 #include <drizzled/abort_exception.h>
35 #include <drizzled/util/find_ptr.h>
37 #include <boost/bind.hpp>
38 #include <boost/foreach.hpp>
44 module::Registry::Registry() :
46 depend_graph_(new module::Graph()),
52 module::Registry::~Registry()
59 BOOST_FOREACH(plugin::Plugin::map::reference it, plugin_registry)
60 it.second->shutdownPlugin();
62 plugin::Plugin::vector error_plugins;
63 BOOST_FOREACH(plugin::Plugin::map::reference it, plugin_registry)
65 if (it.second->removeLast())
66 error_plugins.push_back(it.second);
71 BOOST_FOREACH(plugin::Plugin::vector::reference it, error_plugins)
74 plugin_registry.clear();
81 BOOST_FOREACH(ModuleMap::reference it, module_registry_)
83 module_registry_.clear();
85 BOOST_FOREACH(LibraryMap::reference it, library_registry_)
87 library_registry_.clear();
90 void module::Registry::shutdown()
95 module::Module* module::Registry::find(
const std::string& name)
97 return find_ptr2(module_registry_, boost::to_lower_copy(name));
100 void module::Registry::add(module::Module *handle)
102 std::string add_str(boost::to_lower_copy(handle->getName()));
104 module_registry_[add_str]= handle;
106 Vertex vertex_info(add_str, handle);
107 VertexDesc handle_vertex= boost::add_vertex(depend_graph_->getGraph());
108 depend_graph_->properties(handle_vertex)= vertex_info;
110 handle->setVertexHandle(
new VertexHandle(handle_vertex));
113 void module::Registry::remove(module::Module *handle)
115 module_registry_.erase(boost::to_lower_copy(handle->getName()));
118 void module::Registry::buildDeps()
120 BOOST_FOREACH(ModuleMap::reference map_iter, module_registry_)
122 Module* handle= map_iter.second;
123 BOOST_FOREACH(Module::Depends::const_reference handle_deps, handle->getDepends())
125 std::string dep_str(boost::to_lower_copy(handle_deps));
126 bool found_dep=
false;
127 for (vertex_iter it= boost::vertices(depend_graph_->getGraph()).first; it != vertices(depend_graph_->getGraph()).second; it++)
129 if (depend_graph_->properties(*it).getName() == dep_str)
132 add_edge(handle->getVertexHandle()->getVertexDesc(), *it, depend_graph_->getGraph());
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());
147 module::Registry::ModuleList module::Registry::getList()
151 VertexList vertex_list;
152 boost::topological_sort(depend_graph_->getGraph(), std::back_inserter(vertex_list));
154 BOOST_FOREACH(VertexList::reference it, vertex_list)
156 if (Module* mod_ptr= depend_graph_->properties(it).getModule())
157 plugins.push_back(mod_ptr);
162 module::Library *module::Registry::addLibrary(
const std::string &plugin_name,
bool builtin)
165 module::Library *library= findLibrary(plugin_name);
169 library= module::Library::loadLibrary(plugin_name, builtin);
173 library_registry_.insert(make_pair(plugin_name, library));
178 void module::Registry::removeLibrary(
const std::string &plugin_name)
180 LibraryMap::iterator iter= library_registry_.find(plugin_name);
181 if (iter != library_registry_.end())
184 library_registry_.erase(iter);
188 module::Library *module::Registry::findLibrary(
const std::string &plugin_name)
const
190 return find_ptr2(library_registry_, plugin_name);
193 void module::Registry::shutdownModules()
195 module_shutdown(*
this);