All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.cpp
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/Utils.cpp
22 
23  \brief Utility functions for dealing with plugins.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "../xml/Reader.h"
29 #include "../xml/ReaderFactory.h"
30 #include "AbstractPlugin.h"
31 #include "Exception.h"
32 #include "PluginInfo.h"
33 #include "PluginManager.h"
34 #include "Utils.h"
35 
36 // Boost
37 #include <boost/filesystem.hpp>
38 #include <boost/format.hpp>
39 
40 void te::plugin::UnloadAllPluginsFromEngine(const std::string& engine)
41 {
42  std::vector<std::string> plugins = PluginManager::getInstance().getPlugins();
43 
44  std::vector<std::string>::reverse_iterator it = plugins.rbegin();
45  std::vector<std::string>::reverse_iterator itend = plugins.rend();
46 
47  while(it != itend)
48  {
49 // if plugin wasn't find... maybe because we have propagate an unload...
50  if(!PluginManager::getInstance().isLoaded(*it))
51  {
52  ++it;
53  continue;
54  }
55 
56 // if we have found it... let's unload it!
57  const PluginInfo& plugin = PluginManager::getInstance().getPlugin(*it);
58 
59  if(plugin.m_engine == engine)
60  Unload(*it);
61 
62  ++it;
63  }
64 }
65 
66 void te::plugin::Unload(const std::string& plugin)
67 {
68 // check plugin dependency
69  std::vector<std::string> pdependents = PluginManager::getInstance().getDependents(plugin);
70 
71  for(std::size_t i = 0; i < pdependents.size(); ++i)
72  Unload(pdependents[i]);
73 
74  if(!PluginManager::getInstance().isLoaded(plugin))
75  return;
76 
77  PluginManager::getInstance().unload(plugin);
78 }
79 
80 te::plugin::PluginInfo* te::plugin::GetInstalledPlugin(const std::string& pluginFilePath)
81 {
82  boost::filesystem::path pluginFileName(pluginFilePath);
83 
84 // check if it was provided a plugin file name or just its dir
85  /* if(boost::filesystem::is_directory(pluginFileName))
86  pluginFileName /= TE_DEFAULT_PLUGIN_FILE_NAME;*/
87 
88 // check
89  if(!boost::filesystem::is_regular_file(pluginFileName))
90  throw Exception((boost::format(TE_TR("The informed plugin file is not valid: %1%.")) % pluginFileName).str());
91 
92  std::auto_ptr<te::xml::Reader> xmlReader(te::xml::ReaderFactory::make());
93 
94  xmlReader->read(pluginFileName.string());
95 
96  if(!xmlReader->next())
97  throw Exception(TE_TR("Could not read plugin information!"));
98 
99  if(xmlReader->getNodeType() != te::xml::START_ELEMENT)
100  throw Exception(TE_TR("The document has problems!"));
101 
102  if(xmlReader->getElementLocalName() != "PluginInfo")
103  throw Exception(TE_TR("The first tag in the document is not 'PluginInfo'!"));
104 
105  std::auto_ptr<PluginInfo> pInfo(new PluginInfo);
106  *pInfo << *xmlReader;
107 
108  return pInfo.release();
109 }
110 
An exception class for the Plugin module.
static te::xml::Reader * make()
It creates a new XML reader using the dafault implementation.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
TEPLUGINEXPORT void Unload(const std::string &plugin)
It recursively unload the plugin and any dependent plugins.
Definition: Utils.cpp:66
An abstract class for TerraLib Plugins.
std::string m_engine
The type of plugin execution engine: C++, JAVA.
Definition: PluginInfo.h:71
Utility functions for dealing with plugins.
TEPLUGINEXPORT void UnloadAllPluginsFromEngine(const std::string &engine)
It unloads all plugins from a given engine.
Definition: Utils.cpp:40
A singleton for managing plugins.
The basic information about a plugin.
Definition: PluginInfo.h:61
The basic information about a plugin.
TEPLUGINEXPORT PluginInfo * GetInstalledPlugin(const std::string &pluginFilePath)
It returns information about a given plugin provided its plugin configuration file name or dir...
Definition: Utils.cpp:80