PluginManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib 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 Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/plugin/PluginManager.h
22 
23  \brief A singleton for managing plugins.
24 */
25 
26 #ifndef __TERRALIB_PLUGIN_INTERNAL_PLUGINMANAGER_H
27 #define __TERRALIB_PLUGIN_INTERNAL_PLUGINMANAGER_H
28 
29 // TerraLib
30 #include "../common/Singleton.h"
31 #include "Config.h"
32 #include "Exception.h"
33 
34 // STL
35 #include <map>
36 #include <string>
37 #include <vector>
38 
39 // Boost
40 #include <boost/ptr_container/ptr_vector.hpp>
41 
42 namespace te
43 {
44  namespace plugin
45  {
46 // Forward declarations
47  class AbstractFinder;
48  class AbstractPlugin;
49 
50  struct PluginInfo;
51 
52  /*!
53  \class PluginManager
54 
55  \brief A singleton for managing plugins.
56 
57  \ingroup plugin
58 
59  \sa AbstractPlugin, PluginInfo, AbstractFinder, PluginEngine, PluginEngineFactory
60  */
61  class TEPLUGINEXPORT PluginManager : public te::common::Singleton<PluginManager>
62  {
64 
65  public:
66 
67  /*!
68  \brief It returns the list of plugins managed by PluginManager.
69 
70  \param plugins A vector to output the name of plugins managed by this singleton.
71 
72  \note The list will contain: loaded, not-loaded and broken plugins.
73  */
74  std::vector<std::string> getPlugins() const;
75 
76  /*!
77  \brief It returns the plugin identified by the given name.
78 
79  \param name The plugin name.
80 
81  \return A plugin identified by the given name.
82 
83  \exception Exception It throws an exception if there isn't a plugin with the given name in the manager.
84  */
85  const PluginInfo& getPlugin(const std::string& name) const;
86 
87  /*! \brief It returns the list of plugins that are not loaded. */
88  const boost::ptr_vector<PluginInfo>& getUnloadedPlugins() const;
89 
90  /*! \*brief It sets a list of unloaded plugins. */
91  void setUnloadedPlugins(boost::ptr_vector<te::plugin::PluginInfo> unloadedPlugins);
92 
93  /*! \brief It returns the list of plugins that could not be loaded. */
94  const boost::ptr_vector<PluginInfo>& getBrokenPlugins() const;
95 
96  /*! \*brief It sets a list of broken plugins. */
97  void setBrokenPlugins(boost::ptr_vector<te::plugin::PluginInfo> brokenPlugins);
98 
99  /*!
100  \brief It returns true if the plugin is in the broken list of plugins.
101  */
102  bool isBrokenPlugin(const std::string& pluginName) const;
103 
104  /*!
105  \brief It returns true if the plugin is in the not-loaded list of plugins.
106  */
107  bool isUnloadedPlugin(const std::string& pluginName) const;
108 
109  /*!
110  \brief It returns true if the plugin is loaded otherwise returns false.
111 
112  \param pname The plugin name to be checked.
113 
114  \return It returns true if the plugin in the list are loaded otherwise returns false.
115  */
116  bool isLoaded(const std::string& pname) const;
117 
118  /*!
119  \brief Adds plug-in to unload list.
120 
121  \param plugin Information of the plug-in.
122  */
123  void add(const PluginInfo& plugin);
124 
125  /*!
126  \brief Adds plugin to unload list and take its ownership.
127 
128  \param plugin Information of the plug-in.
129  */
130  void add(PluginInfo* plugin);
131 
132  /*
133  \brief Removes plug-in from the manager.
134 
135  \details This method removes the plug-in from the manager. If the plug-in was loaded, unload it and remove it from the manager.
136  If it was unloaded or broked, just removes it from the correct list. Note that all its dependents will be moved to the broken list.
137 
138  \param plugin Name of the plug-in to be removed.
139 
140  \exception This method can raise te::plugin::Exception, because internally it calls te::plugin::PluginManager::detach and
141  te::plugin::PluginManager::getPlugin methods and don't handle exceptions raised by it;
142 
143  \sa te::plugin::PluginManager::detach, te::plugin::PluginManager::getPlugin
144  */
145  void remove(const std::string& plugin);
146 
147  /*!
148  \brief It loads all the plugins in the not-loaded list or searchs for installed plugin with installed finders.
149 
150  PluginManager will check the dependency between plugins before trying to load them.
151 
152  The associated plugin finders will be used to locate the information about plugins.
153 
154  If no plugin finder is available this method will use the default finder to look up for plugins.
155 
156  After the fitrst call to this method the plugins list is cached until clear is explicitly called.
157 
158  \param start If true it will try to startup the plugins if false it doesn't automatically call plugins startup method and applications must control this.
159 
160  \exception Exception It throws an exception if any plugin can not be loaded.
161 
162  \note This method will unload all previously loaded plugins.
163 
164  \note If a plugin load fails this method will add it to the list of broken plugins and continues. In this case at the end an exception is thrown.
165  */
166  void loadAll(const bool start = true);
167 
168  /*!
169  \brief It try to unload all plugins.
170 
171  This method will call shutdown for all managed plugins.
172 
173  \exception It will raise an exception if it is not possible to unload a plugin.
174  */
175  void unloadAll();
176 
177  /*!
178  \brief Unload all plugins and them clear the internal list.
179 
180  After calling this method if you call loadAll the manager will look for plugins
181  using the registered finders.
182  */
183  void clear();
184 
185  /*!
186  \brief It tries to load all informed plugins.
187 
188  PluginManager will check the dependency between plugins before trying to load them.
189 
190  \param plugins The list of plugins to be loaded
191  \param start If true it will try to startup the plugin if false it doesn't automatically call plugin startup method.
192 
193  \exception Exception It throws an exception if any plugin can not be loaded or started.
194 
195  \note If a plugin in the list is already loaded this methods will throw an exception.
196 
197  \note If a plugin load fails this method will add it to the list of broken plugins and continues. In this case at the end an exception is thrown.
198 
199  \note The manager will sort the plugins according to their load priority.
200  */
201  void load(boost::ptr_vector<PluginInfo>& plugins, const bool start = true);
202 
203  /*!
204  \brief It loads the informed plugin and adds it to the list of managed plugins.
205 
206  PluginManager will check the dependency between plugins before trying to load the informed plugin.
207 
208  \param pInfo Information about the plugin to be loaded.
209  \param start If true it will try to startup the plugin if false it doesn't automatically call plugin startup method.
210 
211  \exception Exception It throws an exception if something goes wrong during plugin load.
212 
213  \note This method can checks for category dependency and plugin dependency. It throws an error if the dependency is not satisfied.
214 
215  \note If a plugin in the list is already loaded this methods will raise an exception.
216 
217  \note If an exception occurs the plugin will be added to the list of broken plugins (except if it is already loaded!).
218 
219  \note If no plugin folder or an invalid folder was supplied the interal finders will be used to locate the plugin file.
220  */
221  void load(const PluginInfo& pInfo, const bool start = true);
222 
223  void load(const std::string& pluginName);
224 
225  /*!
226  \brief It tries to unload a given plugin.
227 
228  This method will call plugin's shutdown method if needed.
229 
230  \param name The plugin to be unloaded.
231 
232  \exception It will raise an exception if plugin's code is not unloaded, if it fails to shutdown or if the plugin's is not managed by PluginManager.
233  */
234  void unload(const std::string& name);
235 
236  /*!
237  \brief It tries to unload a given plugin.
238 
239  This method will call plugin's shutdown method if needed.
240 
241  \param plugin The plugin to be unloaded. The pointer will be invalidated if it acomplishes the task.
242 
243  \exception It will raise an exception if plugin's code is not unloaded or if it fails to shutdown.
244 
245  \note It doesn't throw exceptions if the plugin is not managed by the manager.
246  */
247  void unload(AbstractPlugin* plugin);
248 
249  /*!
250  \brief It try to shutdown all plugins.
251 
252  This method calls shutdown for all managed plugins.
253 
254  \exception It will raise an exception if it is not possible to shutdown a plugin.
255  */
256  void shutdownAll();
257 
258  /*!
259  \brief It returns the number of plugins kept in the manager.
260 
261  \return The number of plugins in the manager.
262 
263  \note This will account for loaded, unloaded and broken plugins.
264  */
265  std::size_t getNumPlugins() const;
266 
267  /*!
268  \brief It returns true if each plugin in the list are loaded otherwise returns false.
269 
270  \param plugins A list with plugin names to be checked.
271 
272  \return It returns true if all plugins in the list are loaded otherwise returns false.
273 
274  \note This method can be used to check if a given plugin has all pre-requisites to be loaded.
275  */
276  bool isLoaded(const std::vector<std::string>& plugins) const;
277 
278  /*!
279  \brief It searches for all plugins that depends on the given plugin.
280 
281  \param pluginName The plugin to be checked.
282 
283  \return It returns the list of plugins that depends on the given plugin.
284  */
285  std::vector<std::string> getDependents(const std::string& pluginName) const;
286 
287  /*!
288  \brief If there is a plugin that depends on the informed plugin it returns true, otherwise, if no plugin depends on it, return false.
289 
290  \param pluginName The plugin to be checked.
291 
292  \return It returns true if there is a plugin that depends on the given plugin.
293  */
294  bool hasDependents(const std::string& pluginName) const;
295 
296  /*!
297  \brief It detaches the given plugin from the list of loaded plugins.
298 
299  \param pluginName The plugin name.
300 
301  \exception Exception If there are plugins that depends on the plugin being detached or if the plugin is not found this method raises an exception.
302 
303  \note The caller of this method will take the ownership of the given plugin.
304  */
305  AbstractPlugin* detach(const std::string& pluginName);
306 
307  /*!
308  \brief Get plugins category types.
309 
310  \param A vector to output the name of plugins categories managed by this singleton.
311  */
312  void getCategories(std::vector<std::string>& categories) const;
313 
314  /*!
315  \brief Add a new category type.
316 
317  \param name The category name.
318  */
319  void addCategory(const std::string& name);
320 
321  protected:
322 
323  /*!
324  \brief It sorts the plugins according to their dependency.
325  */
326  void sort(boost::ptr_vector<PluginInfo>& plugins) const;
327 
328  /*!
329  \brief It removes the given plugin from the category and then updates the internal category index.
330 
331  \param plugin The plugin to be removed from the given category.
332  \param category The categpry to be updated.
333  */
334  void removeFromCategory(AbstractPlugin* plugin, const std::string& category);
335 
336  void moveToBrokenList(const PluginInfo& pInfo);
337 
338  void removeFromBrokenList(const PluginInfo& pInfo);
339 
340  void removeFromUnloadedList(const PluginInfo& pInfo);
341 
342  void moveDependentsToBrokenList(const std::string& plugin, const bool& unloadPlugin=false);
343 
344  void updateDependents(const std::string& plugin);
345 
346  private:
347 
348  /*! \brief It creates a new plugin. */
349  PluginManager();
350 
351  /*! \brief Singleton destructor. */
352  ~PluginManager();
353 
354  private:
355 
356  std::vector<AbstractFinder*> m_finders; //!< The list of plugin finders.
357  std::vector<AbstractPlugin*> m_plugins; //!< The list of managed plugins: this will be need to unload accordinly the plugins!
358  std::map<std::string, AbstractPlugin*> m_pluginsMap; //!< A map from (plugin's name) to (plugin instance).
359  std::map<std::string, std::vector<AbstractPlugin*> > m_pluginCategoryMap; //!< A map from (plugin category) to (plugins in category)
360  boost::ptr_vector<PluginInfo> m_unloadedPlugins; //!< The list of plugins that are not loaded.
361  boost::ptr_vector<PluginInfo> m_brokenPlugins; //!< The list of plugins that could not be loaded.
362  };
363 
364  } // end namespace plugin
365 } // end namespace te
366 
367 #endif // __TERRALIB_PLUGIN_INTERNAL_PLUGINMANAGER_H
An abstract class for TerraLib Plugins.
boost::ptr_vector< PluginInfo > m_brokenPlugins
The list of plugins that could not be loaded.
std::vector< AbstractPlugin * > m_plugins
The list of managed plugins: this will be need to unload accordinly the plugins!
Configuration flags for the TerraLib Plugin module.
An exception class for the Plugin module.
A singleton for managing plugins.
Definition: PluginManager.h:61
boost::ptr_vector< PluginInfo > m_unloadedPlugins
The list of plugins that are not loaded.
std::map< std::string, AbstractPlugin * > m_pluginsMap
A map from (plugin's name) to (plugin instance).
URI C++ Library.
std::vector< AbstractFinder * > m_finders
The list of plugin finders.
#define TEPLUGINEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:120
The basic information about a plugin.
Definition: PluginInfo.h:61
std::map< std::string, std::vector< AbstractPlugin * > > m_pluginCategoryMap
A map from (plugin category) to (plugins in category)
Template support for singleton pattern.
Definition: Singleton.h:100