DefaultFinder.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/DefaultFinder.cpp
22 
23  \brief A plugin finder that search for plugins in some special directories defined by compile time macros.
24 */
25 
26 // TerraLib
27 #include "../core/filesystem/FileSystem.h"
28 #include "../core/translator/Translator.h"
29 #include "../core/utils/Platform.h"
30 #include "DefaultFinder.h"
31 #include "PluginInfo.h"
32 #include "Utils.h"
33 
34 // Boost
35 #include <boost/filesystem.hpp>
36 #include <boost/format.hpp>
37 
38 // STL
39 #include <cassert>
40 #include <cstdlib>
41 
42 
44 {
46 }
47 
49 {
50 }
51 
52 void te::plugin::DefaultFinder::getDefaultDirs( std::vector< std::string >& dirs ) const
53 {
54  dirs.clear();
55 
56  // The first default directory
57 
58  dirs.push_back( te::core::FileSystem::systemCompletePath( "." ) );
59 
60  // let's check if there is a directory called TE_DEFAULT_PLUGINS_DIR in the current application dir
61 
63  {
65  }
66 
67 // if the default dir is not available in the current dir let's try an environment variable defined as TERRALIB_DIR_ENVIRONMENT_VARIABLE
68 
70 
71  if(!plgDir.empty())
72  {
73  boost::filesystem::path p(plgDir);
74 
75  if(te::core::FileSystem::isDirectory(p.string()))
76  dirs.push_back( te::core::FileSystem::systemCompletePath(p.string()) );
77  }
78 
79 #ifdef TE_PLUGINS_INSTALL_PATH
80  {
81  boost::filesystem::path p(TE_PLUGINS_INSTALL_PATH);
82 
84  dirs.push_back( te::core::FileSystem::systemCompletePath(p.string()) );
85  }
86 #endif
87 }
88 
89 void te::plugin::DefaultFinder::addPluginsDir(const std::string& path)
90 {
92  throw Exception((boost::format(TE_TR("Default plugin directory is invalid: %1%.")) % path).str());
93 
94  boost::filesystem::path p(te::core::FileSystem::systemCompletePath(path));
95 
96  std::string s(p.string());
97 
98  if(std::find(m_pluginsDir.begin(), m_pluginsDir.end(), s) != m_pluginsDir.end())
99  return; // dir already in the path
100 
101  m_pluginsDir.push_back(s);
102 }
103 
104 const std::vector<std::string>& te::plugin::DefaultFinder::getPluginsDir() const
105 {
106  return m_pluginsDir;
107 }
108 
109 void te::plugin::DefaultFinder::getPlugins(boost::ptr_vector<PluginInfo>& plugins)
110 {
111 // is there a base dir for looking for plugins?
112  std::size_t ndirs = m_pluginsDir.size();
113 
114 // let's look in each plugins base dir
115  for(std::size_t i = 0; i < ndirs; ++i)
116  {
118  throw Exception((boost::format(TE_TR("The base plugin directory is invalid: %1%.")) % m_pluginsDir[i]).str());
119 
120  boost::filesystem::path path(m_pluginsDir[i]);
121 
122  for(boost::filesystem::directory_iterator it(path), itEnd; it != itEnd; ++it)
123  {
124  if(te::core::FileSystem::isRegularFile(it->path().string()))
125  {
126  std::string ext = te::core::FileSystem::extension(it->path().string());
127 
128  if(ext == TE_DEFAULT_PLUGIN_EXTENSION)
129  plugins.push_back(GetInstalledPlugin(it->path().string()));
130  }
131 // check just in direct sub-dirs, don't go recursively for ever!
132 // if(te::core::FileSystem::isDirectory(it->status()))
133 // {
134 // boost::filesystem::path foundPlugin = (*it);
135 //
136 //
137 // std::cout <<std::endl <<foundPlugin.string();
138 //
139 //
140 // foundPlugin /= TE_DEFAULT_PLUGIN_FILE_NAME;
141 //
142 // if(te::core::FileSystem::isRegularFile(foundPlugin))
143 // {
144 //// try to read the plugin XML configuration file and add info to the output vector
145 // plugins.push_back(GetInstalledPlugin(foundPlugin.string()));
146 // }
147 // }
148  }
149  }
150 }
151 
static bool isDirectory(const std::string &path)
Checks if a given path in UTF-8 is a directory.
Definition: FileSystem.cpp:87
Base exception class for plugin module.
const std::vector< std::string > & getPluginsDir() const
It returns the list of plugins base directories.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
void getDefaultDirs(std::vector< std::string > &dirs) const
It returns the default plugins directories.
void addPluginsDir(const std::string &path)
It adds a new base location where the finder will search for installed plugins.
static std::string extension(const std::string &path)
Retrives the extension of a given file path in UTF-8.
Definition: FileSystem.cpp:82
DefaultFinder()
Constructor.
std::vector< std::string > m_pluginsDir
The base list of directories to search for plugins.
#define TE_DEFAULT_PLUGIN_EXTENSION
The default extension for plugins description files.
te::gm::Polygon * p
static std::string systemCompletePath(const std::string &path)
Composes an absolute path for the given path in UTF-8.
Definition: FileSystem.cpp:56
TECOREEXPORT std::string FindInTerraLibPath(const std::string &path)
Returns the path relative to a directory or file in the context of TerraLib.
#define TE_DEFAULT_PLUGINS_DIR
The default look up plugin dir.
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
Definition: FileSystem.cpp:98
void getPlugins(boost::ptr_vector< PluginInfo > &plugins)
This method searches for installed plugins and output the plugins information in the PluginInfo vecto...
TEPLUGINEXPORT PluginInfo * GetInstalledPlugin(const std::string &pluginFilePath)
It returns information about a given plugin provided its plugin configuration file name or dir...