All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../common/Translator.h"
28 #include "../common/PlatformUtils.h"
29 #include "DefaultFinder.h"
30 #include "PluginInfo.h"
31 #include "Utils.h"
32 
33 // Boost
34 #include <boost/filesystem.hpp>
35 #include <boost/format.hpp>
36 
37 // STL
38 #include <cassert>
39 #include <cstdlib>
40 
41 
43 {
45 }
46 
48 {
49 }
50 
51 void te::plugin::DefaultFinder::getDefaultDirs( std::vector< std::string >& dirs ) const
52 {
53  dirs.clear();
54 
55  // The first default directory
56 
57  dirs.push_back( boost::filesystem::system_complete( "." ).string() );
58 
59  // let's check if there is a directory called TE_DEFAULT_PLUGINS_DIR in the current application dir
60 
61  if(boost::filesystem::is_directory(TE_DEFAULT_PLUGINS_DIR))
62  {
63  dirs.push_back( boost::filesystem::system_complete(TE_DEFAULT_PLUGINS_DIR).string() );
64  }
65 
66 // if the default dir is not available in the current dir let's try an environment variable defined as TERRALIB_DIR_ENVIRONMENT_VARIABLE
67 
69 
70  if(!plgDir.empty())
71  {
72  boost::filesystem::path p(plgDir);
73 
74  if(boost::filesystem::is_directory(p))
75  dirs.push_back( boost::filesystem::system_complete(p).string() );
76  }
77 
78 #ifdef TE_PLUGINS_INSTALL_PATH
79  {
80  boost::filesystem::path p(TE_PLUGINS_INSTALL_PATH);
81 
82  if(boost::filesystem::is_directory(p))
83  dirs.push_back( boost::filesystem::system_complete(p).string() );
84  }
85 #endif
86 }
87 
88 void te::plugin::DefaultFinder::addPluginsDir(const std::string& path)
89 {
90  if(!boost::filesystem::is_directory(path))
91  throw Exception((boost::format(TE_TR("Default plugin directory is invalid: %1%.")) % path).str());
92 
93  boost::filesystem::path p(boost::filesystem::system_complete(path));
94 
95  std::string s(p.string());
96 
97  if(std::find(m_pluginsDir.begin(), m_pluginsDir.end(), s) != m_pluginsDir.end())
98  return; // dir already in the path
99 
100  m_pluginsDir.push_back(s);
101 }
102 
103 const std::vector<std::string>& te::plugin::DefaultFinder::getPluginsDir() const
104 {
105  return m_pluginsDir;
106 }
107 
108 void te::plugin::DefaultFinder::getPlugins(boost::ptr_vector<PluginInfo>& plugins)
109 {
110 // is there a base dir for looking for plugins?
111  std::size_t ndirs = m_pluginsDir.size();
112 
113 // let's look in each plugins base dir
114  for(std::size_t i = 0; i < ndirs; ++i)
115  {
116  if(!boost::filesystem::is_directory(m_pluginsDir[i]))
117  throw Exception((boost::format(TE_TR("The base plugin directory is invalid: %1%.")) % m_pluginsDir[i]).str());
118 
119  boost::filesystem::path path(m_pluginsDir[i]);
120 
121  for(boost::filesystem::directory_iterator it(path), itEnd; it != itEnd; ++it)
122  {
123  if(boost::filesystem::is_regular_file(it->status()))
124  {
125  std::string ext = boost::filesystem::extension(it->path());
126 
127  if(ext == TE_DEFAULT_PLUGIN_EXTENSION)
128  plugins.push_back(GetInstalledPlugin(it->path().string()));
129  }
130 // check just in direct sub-dirs, don't go recursively for ever!
131 // if(boost::filesystem::is_directory(it->status()))
132 // {
133 // boost::filesystem::path foundPlugin = (*it);
134 //
135 //
136 // std::cout <<std::endl <<foundPlugin.string();
137 //
138 //
139 // foundPlugin /= TE_DEFAULT_PLUGIN_FILE_NAME;
140 //
141 // if(boost::filesystem::is_regular_file(foundPlugin))
142 // {
143 //// try to read the plugin XML configuration file and add info to the output vector
144 // plugins.push_back(GetInstalledPlugin(foundPlugin.string()));
145 // }
146 // }
147  }
148  }
149 }
150 
TECOMMONEXPORT std::string FindInTerraLibPath(const std::string &p)
Returns the path relative to a directory or file in the context of TerraLib.
const std::vector< std::string > & getPluginsDir() const
It returns the list of plugins base directories.
#define TE_DEFAULT_PLUGINS_DIR
The default look up plugin dir.
Definition: Config.h:75
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
#define TE_DEFAULT_PLUGIN_EXTENSION
The default extension for plugins description files.
Definition: Config.h:82
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.
DefaultFinder()
Constructor.
std::vector< std::string > m_pluginsDir
The base list of directories to search for plugins.
Utility functions for dealing with plugins.
A plugin finder that search for plugins in some special directories defined by compile time macros...
The basic information about a plugin.
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...
Definition: Utils.cpp:80