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