~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include <string>
#include <vector>
#include <map>

#include <drizzled/module/registry.h>
#include <drizzled/module/library.h>
#include <drizzled/module/graph.h>
#include <drizzled/module/vertex_handle.h>

#include <drizzled/plugin.h>
#include <drizzled/show.h>
#include <drizzled/cursor.h>
#include <drizzled/abort_exception.h>

#include <boost/bind.hpp>

using namespace std;

namespace drizzled
{

module::Registry::Registry() :
  module_registry_(),
  depend_graph_(new module::Graph()),
  plugin_registry(),
  deps_built_(false)
{ }


module::Registry::~Registry()
{
  plugin::Plugin::map::iterator plugin_iter;

  /* Give all plugins a chance to cleanup, before
   * all plugins are deleted.
   * This can be used if shutdown code references
   * other plugins.
   */
  plugin_iter= plugin_registry.begin();
  while (plugin_iter != plugin_registry.end())
  {
    (*plugin_iter).second->shutdownPlugin();
    ++plugin_iter;
  }

  plugin::Plugin::vector error_plugins;
  plugin_iter= plugin_registry.begin();
  while (plugin_iter != plugin_registry.end())
  {
    if ((*plugin_iter).second->removeLast())
    {
      error_plugins.push_back((*plugin_iter).second);
    }
    else
    {
      delete (*plugin_iter).second;
    }
    ++plugin_iter;
  }

  for (plugin::Plugin::vector::iterator iter= error_plugins.begin();
       iter != error_plugins.end(); iter++)
  {
    delete *iter;
  }

  plugin_registry.clear();

#if 0
  @TODO When we delete modules here, we segfault on a bad string. Why?
   ModuleMap::iterator module_iter= module_registry_.begin();

  while (module_iter != module_registry_.end())
  {
    delete (*module_iter).second;
    ++module_iter;
  }
  module_registry_.clear();
#endif
  LibraryMap::iterator library_iter= library_registry_.begin();
  while (library_iter != library_registry_.end())
  {
    delete (*library_iter).second;
    ++library_iter;
  }
  library_registry_.clear();
}

void module::Registry::shutdown()
{
  module::Registry& registry= singleton();
  delete &registry;
}

module::Module *module::Registry::find(std::string name)
{
  std::transform(name.begin(), name.end(), name.begin(), ::tolower);

  ModuleMap::iterator map_iter;
  map_iter= module_registry_.find(name);
  if (map_iter != module_registry_.end())
    return (*map_iter).second;
  return NULL;
}

void module::Registry::add(module::Module *handle)
{
  std::string add_str(handle->getName());
  transform(add_str.begin(), add_str.end(),
            add_str.begin(), ::tolower);

  module_registry_[add_str]= handle;

  Vertex vertex_info(add_str, handle);
  VertexDesc handle_vertex= boost::add_vertex(depend_graph_->getGraph());
  depend_graph_->properties(handle_vertex)= vertex_info;

  handle->setVertexHandle(new VertexHandle(handle_vertex));

}

void module::Registry::remove(module::Module *handle)
{
  std::string remove_str(handle->getName());
  std::transform(remove_str.begin(), remove_str.end(),
                 remove_str.begin(), ::tolower);

  module_registry_.erase(remove_str);
}

void module::Registry::copy(plugin::Plugin::vector &arg)
{    
  arg.reserve(plugin_registry.size());

  std::transform(plugin_registry.begin(),
                 plugin_registry.end(),
                 std::back_inserter(arg),
                 boost::bind(&plugin::Plugin::map::value_type::second, _1) );
  assert(arg.size() == plugin_registry.size());
}

void module::Registry::buildDeps()
{
  ModuleMap::iterator map_iter= module_registry_.begin();
  while (map_iter != module_registry_.end())
  {
    Module *handle= (*map_iter).second;
    Module::Depends::const_iterator handle_deps= handle->getDepends().begin();
    while (handle_deps != handle->getDepends().end())
    {
      std::string dep_str((*handle_deps));
      transform(dep_str.begin(), dep_str.end(),
                dep_str.begin(), ::tolower);

      bool found_dep= false;
      vertex_iter it= boost::vertices(depend_graph_->getGraph()).first;
      while (it != vertices(depend_graph_->getGraph()).second)
      {
        if (depend_graph_->properties(*it).getName() == dep_str)
        {
          found_dep= true;
          add_edge(handle->getVertexHandle()->getVertexDesc(), *it, depend_graph_->getGraph());
          break;
        }
        ++it;
      }
      if (not found_dep)
      {
        errmsg_printf(error::ERROR,
                      _("Couldn't process plugin module dependencies. "
                        "%s depends on %s but %s is not to be loaded.\n"),
                      handle->getName().c_str(),
                      dep_str.c_str(), dep_str.c_str());
        DRIZZLE_ABORT;
      }

      ++handle_deps;
    }
    ++map_iter;
  }
  deps_built_= true;
}

module::Registry::ModuleList module::Registry::getList()
{
  if (not deps_built_)
  {
    buildDeps();
  }

  std::vector<module::Module *> plugins;

  VertexList vertex_list;

  boost::topological_sort(depend_graph_->getGraph(), std::back_inserter(vertex_list));

  for (VertexList::iterator i = vertex_list.begin();
       i != vertex_list.end(); ++i)
  {
    Module *mod_ptr= depend_graph_->properties(*i).getModule();
    if (mod_ptr != NULL)
    {
      plugins.push_back(mod_ptr);
    }  
  }

  return plugins;
}

module::Library *module::Registry::addLibrary(const std::string &plugin_name,
                                              bool builtin)
{

  /* If this dll is already loaded just return it */
  module::Library *library= findLibrary(plugin_name);
  if (library != NULL)
  {
    return library;
  }

  library= module::Library::loadLibrary(plugin_name, builtin);
  if (library != NULL)
  {
    /* Add this dll to the map */
    library_registry_.insert(make_pair(plugin_name, library));
  }

  return library;
}

void module::Registry::removeLibrary(const std::string &plugin_name)
{
  std::map<std::string, module::Library *>::iterator iter=
    library_registry_.find(plugin_name);
  if (iter != library_registry_.end())
  {
    library_registry_.erase(iter);
    delete (*iter).second;
  }
}

module::Library *module::Registry::findLibrary(const std::string &plugin_name) const
{
  LibraryMap::const_iterator iter= library_registry_.find(plugin_name);
  if (iter != library_registry_.end())
    return (*iter).second;
  return NULL;
}

void module::Registry::shutdownModules()
{
  module_shutdown(*this);
}

} /* namespace drizzled */