All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PluginManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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  void getPlugins(std::vector<std::string>& plugins) 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 returns the list of plugins that could not be loaded. */
91  const boost::ptr_vector<PluginInfo>& getBrokenPlugins() const;
92 
93  /*!
94  \brief It returns true if the plugin is in the broken list of plugins.
95  */
96  bool isBrokenPlugin(const std::string& pluginName) const;
97 
98  /*!
99  \brief It returns true if the plugin is in the not-loaded list of plugins.
100  */
101  bool isUnloadedPlugin(const std::string& pluginName) const;
102 
103  /*!
104  \brief It returns true if the plugin is loaded otherwise returns false.
105 
106  \param pname The plugin name to be checked.
107 
108  \return It returns true if the plugin in the list are loaded otherwise returns false.
109  */
110  bool isLoaded(const std::string& pname) const;
111 
112  /*!
113  \brief Adds plug-in to unload list.
114 
115  \param plugin Information of the plug-in.
116  */
117  void add(const PluginInfo& plugin);
118 
119  /*!
120  \brief Adds plugin to unload list and take its ownership.
121 
122  \param plugin Information of the plug-in.
123  */
124  void add(PluginInfo* plugin);
125 
126  /*
127  \brief Removes plug-in from the manager.
128 
129  \details This method removes the plug-in from the manager. If the plug-in was loaded, unload it and remove it from the manager.
130  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.
131 
132  \param plugin Name of the plug-in to be removed.
133 
134  \exception This method can raise te::plugin::Exception, because internally it calls te::plugin::PluginManager::detach and
135  te::plugin::PluginManager::getPlugin methods and don't handle exceptions raised by it;
136 
137  \sa te::plugin::PluginManager::detach, te::plugin::PluginManager::getPlugin
138  */
139  void remove(const std::string& plugin);
140 
141  /*!
142  \brief It loads all the plugins in the not-loaded list or searchs for installed plugin with installed finders.
143 
144  PluginManager will check the dependency between plugins before trying to load them.
145 
146  The associated plugin finders will be used to locate the information about plugins.
147 
148  If no plugin finder is available this method will use the default finder to look up for plugins.
149 
150  After the fitrst call to this method the plugins list is cached until clear is explicitly called.
151 
152  \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.
153 
154  \exception Exception It throws an exception if any plugin can not be loaded.
155 
156  \note This method will unload all previously loaded plugins.
157 
158  \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.
159  */
160  void loadAll(const bool start = true);
161 
162  /*!
163  \brief It try to unload all plugins.
164 
165  This method will call shutdown for all managed plugins.
166 
167  \exception It will raise an exception if it is not possible to unload a plugin.
168  */
169  void unloadAll();
170 
171  /*!
172  \brief Unload all plugins and them clear the internal list.
173 
174  After calling this method if you call loadAll the manager will look for plugins
175  using the registered finders.
176  */
177  void clear();
178 
179  /*!
180  \brief It tries to load all informed plugins.
181 
182  PluginManager will check the dependency between plugins before trying to load them.
183 
184  \param plugins The list of plugins to be loaded
185  \param start If true it will try to startup the plugin if false it doesn't automatically call plugin startup method.
186 
187  \exception Exception It throws an exception if any plugin can not be loaded or started.
188 
189  \note If a plugin in the list is already loaded this methods will throw an exception.
190 
191  \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.
192 
193  \note The manager will sort the plugins according to their load priority.
194  */
195  void load(boost::ptr_vector<PluginInfo>& plugins, const bool start = true);
196 
197  /*!
198  \brief It loads the informed plugin and adds it to the list of managed plugins.
199 
200  PluginManager will check the dependency between plugins before trying to load the informed plugin.
201 
202  \param pInfo Information about the plugin to be loaded.
203  \param start If true it will try to startup the plugin if false it doesn't automatically call plugin startup method.
204 
205  \exception Exception It throws an exception if something goes wrong during plugin load.
206 
207  \note This method can checks for category dependency and plugin dependency. It throws an error if the dependency is not satisfied.
208 
209  \note If a plugin in the list is already loaded this methods will raise an exception.
210 
211  \note If an exception occurs the plugin will be added to the list of broken plugins (except if it is already loaded!).
212 
213  \note If no plugin folder or an invalid folder was supplied the interal finders will be used to locate the plugin file.
214  */
215  void load(const PluginInfo& pInfo, const bool start = true);
216 
217  void load(const std::string& pluginName);
218 
219  /*!
220  \brief It tries to unload a given plugin.
221 
222  This method will call plugin's shutdown method if needed.
223 
224  \param name The plugin to be unloaded.
225 
226  \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.
227  */
228  void unload(const std::string& name);
229 
230  /*!
231  \brief It tries to unload a given plugin.
232 
233  This method will call plugin's shutdown method if needed.
234 
235  \param plugin The plugin to be unloaded. The pointer will be invalidated if it acomplishes the task.
236 
237  \exception It will raise an exception if plugin's code is not unloaded or if it fails to shutdown.
238 
239  \note It doesn't throw exceptions if the plugin is not managed by the manager.
240  */
241  void unload(AbstractPlugin* plugin);
242 
243  /*!
244  \brief It try to shutdown all plugins.
245 
246  This method calls shutdown for all managed plugins.
247 
248  \exception It will raise an exception if it is not possible to shutdown a plugin.
249  */
250  void shutdownAll();
251 
252  /*!
253  \brief It returns the number of plugins kept in the manager.
254 
255  \return The number of plugins in the manager.
256 
257  \note This will account for loaded, unloaded and broken plugins.
258  */
259  std::size_t getNumPlugins() const;
260 
261  /*!
262  \brief It returns true if each plugin in the list are loaded otherwise returns false.
263 
264  \param plugins A list with plugin names to be checked.
265 
266  \return It returns true if all plugins in the list are loaded otherwise returns false.
267 
268  \note This method can be used to check if a given plugin has all pre-requisites to be loaded.
269  */
270  bool isLoaded(const std::vector<std::string>& plugins) const;
271 
272  /*!
273  \brief It searches for all plugins that depends on the given plugin.
274 
275  \param pluginName The plugin to be checked.
276 
277  \return It returns the list of plugins that depends on the given plugin.
278  */
279  std::vector<std::string> getDependents(const std::string& pluginName) const;
280 
281  /*!
282  \brief If there is a plugin that depends on the informed plugin it returns true, otherwise, if no plugin depends on it, return false.
283 
284  \param pluginName The plugin to be checked.
285 
286  \return It returns true if there is a plugin that depends on the given plugin.
287  */
288  bool hasDependents(const std::string& pluginName) const;
289 
290  /*!
291  \brief It detaches the given plugin from the list of loaded plugins.
292 
293  \param pluginName The plugin name.
294 
295  \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.
296 
297  \note The caller of this method will take the ownership of the given plugin.
298  */
299  AbstractPlugin* detach(const std::string& pluginName);
300 
301  /*!
302  \brief Get plugins category types.
303 
304  \param A vector to output the name of plugins categories managed by this singleton.
305  */
306  void getCategories(std::vector<std::string>& categories) const;
307 
308  /*!
309  \brief Add a new category type.
310 
311  \param name The category name.
312  */
313  void addCategory(const std::string& name);
314 
315  protected:
316 
317  /*!
318  \brief It sorts the plugins according to their dependency.
319  */
320  void sort(boost::ptr_vector<PluginInfo>& plugins) const;
321 
322  /*!
323  \brief It removes the given plugin from the category and then updates the internal category index.
324 
325  \param plugin The plugin to be removed from the given category.
326  \param category The categpry to be updated.
327  */
328  void removeFromCategory(AbstractPlugin* plugin, const std::string& category);
329 
330  void moveToBrokenList(const PluginInfo& pInfo);
331 
332  void removeFromBrokenList(const PluginInfo& pInfo);
333 
334  void removeFromUnloadedList(const PluginInfo& pInfo);
335 
336  void moveDependentsToBrokenList(const std::string& plugin, const bool& unloadPlugin=false);
337 
338  void updateDependents(const std::string& plugin);
339 
340  private:
341 
342  /*! \brief It creates a new plugin. */
343  PluginManager();
344 
345  /*! \brief Singleton destructor. */
346  ~PluginManager();
347 
348  private:
349 
350  std::vector<AbstractFinder*> m_finders; //!< The list of plugin finders.
351  std::vector<AbstractPlugin*> m_plugins; //!< The list of managed plugins: this will be need to unload accordinly the plugins!
352  std::map<std::string, AbstractPlugin*> m_pluginsMap; //!< A map from (plugin's name) to (plugin instance).
353  std::map<std::string, std::vector<AbstractPlugin*> > m_pluginCategoryMap; //!< A map from (plugin category) to (plugins in category)
354  boost::ptr_vector<PluginInfo> m_unloadedPlugins; //!< The list of plugins that are not loaded.
355  boost::ptr_vector<PluginInfo> m_brokenPlugins; //!< The list of plugins that could not be loaded.
356  };
357 
358  } // end namespace plugin
359 } // end namespace te
360 
361 #endif // __TERRALIB_PLUGIN_INTERNAL_PLUGINMANAGER_H
boost::ptr_vector< PluginInfo > m_brokenPlugins
The list of plugins that could not be loaded.
Template support for singleton pattern.
Definition: Singleton.h:100
std::map< std::string, std::vector< AbstractPlugin * > > m_pluginCategoryMap
A map from (plugin category) to (plugins in category)
The basic information about a plugin.
Definition: PluginInfo.h:61
#define TEPLUGINEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:151
An abstract class for TerraLib Plugins.
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&#39;s name) to (plugin instance).
Configuration flags for the TerraLib Plugin module.
std::vector< AbstractPlugin * > m_plugins
The list of managed plugins: this will be need to unload accordinly the plugins!
A singleton for managing plugins.
Definition: PluginManager.h:61
An exception class for the Plugin module.
std::vector< AbstractFinder * > m_finders
The list of plugin finders.