attic/src/plugin/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 "Utils.h"
28 #include "../core/filesystem/FileSystem.h"
29 #include "../core/translator/Translator.h"
30 #include "../xml/Reader.h"
31 #include "../xml/ReaderFactory.h"
32 #include "AbstractPlugin.h"
33 #include "Exception.h"
34 #include "PluginInfo.h"
35 #include "PluginManager.h"
36 
37 // Boost
38 #include <boost/filesystem.hpp>
39 #include <boost/format.hpp>
40 
41 void te::plugin::UnloadAllPluginsFromEngine(const std::string& engine)
42 {
43  std::vector<std::string> plugins = PluginManager::getInstance().getPlugins();
44 
45  std::vector<std::string>::reverse_iterator it = plugins.rbegin();
46  std::vector<std::string>::reverse_iterator itend = plugins.rend();
47 
48  while(it != itend)
49  {
50 // if plugin wasn't find... maybe because we have propagate an unload...
51  if(!PluginManager::getInstance().isLoaded(*it))
52  {
53  ++it;
54  continue;
55  }
56 
57 // if we have found it... let's unload it!
58  const PluginInfo& plugin = PluginManager::getInstance().getPlugin(*it);
59 
60  if(plugin.m_engine == engine)
61  Unload(*it);
62 
63  ++it;
64  }
65 }
66 
67 void te::plugin::Unload(const std::string& plugin)
68 {
69 // check plugin dependency
70  std::vector<std::string> pdependents = PluginManager::getInstance().getDependents(plugin);
71 
72  for(std::size_t i = 0; i < pdependents.size(); ++i)
73  Unload(pdependents[i]);
74 
75  if(!PluginManager::getInstance().isLoaded(plugin))
76  return;
77 
78  PluginManager::getInstance().unload(plugin);
79 }
80 
81 te::plugin::PluginInfo* te::plugin::GetInstalledPlugin(const std::string& pluginFilePath)
82 {
83  boost::filesystem::path pluginFileName(pluginFilePath);
84 
85 // check if it was provided a plugin file name or just its dir
86  /* if(te::core::FileSystem::isDirectory(pluginFileName))
87  pluginFileName /= TE_DEFAULT_PLUGIN_FILE_NAME;*/
88 
89 // check
90  if(!te::core::FileSystem::isRegularFile(pluginFileName.string()))
91  throw Exception((boost::format(TE_TR("The informed plugin file is not valid: %1%.")) % pluginFileName).str());
92 
93  std::auto_ptr<te::xml::Reader> xmlReader(te::xml::ReaderFactory::make());
94 
95  xmlReader->read(pluginFileName.string());
96 
97  if(!xmlReader->next())
98  throw Exception(TE_TR("Could not read plugin information!"));
99 
100  if(xmlReader->getNodeType() != te::xml::START_ELEMENT)
101  throw Exception(TE_TR("The document has problems!"));
102 
103  if(xmlReader->getElementLocalName() != "PluginInfo")
104  throw Exception(TE_TR("The first tag in the document is not 'PluginInfo'!"));
105 
106  std::auto_ptr<PluginInfo> pInfo(new PluginInfo);
107  *pInfo << *xmlReader;
108 
109  return pInfo.release();
110 }
111 
Base exception class for 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:242
TEPLUGINEXPORT void Unload(const std::string &plugin)
It recursively unload the plugin and any dependent plugins.
std::string m_engine
The type of plugin execution engine: C++, JAVA.
TEPLUGINEXPORT void UnloadAllPluginsFromEngine(const std::string &engine)
It unloads all plugins from a given engine.
The basic information about a plugin.
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
Definition: FileSystem.cpp:98
TEPLUGINEXPORT PluginInfo * GetInstalledPlugin(const std::string &pluginFilePath)
It returns information about a given plugin provided its plugin configuration file name or dir...