All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DefaultFinder.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/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 "DefaultFinder.h"
29 #include "PluginInfo.h"
30 #include "Utils.h"
31 
32 // Boost
33 #include <boost/filesystem.hpp>
34 #include <boost/format.hpp>
35 
36 // STL
37 #include <cassert>
38 #include <cstdlib>
39 
41 {
43 }
44 
46 {
47 }
48 
49 void te::plugin::DefaultFinder::getDefaultDirs( std::vector< std::string >& dirs ) const
50 {
51  dirs.clear();
52 
53  // The first default directory
54 
55  dirs.push_back( boost::filesystem::system_complete( "." ).string() );
56 
57  // let's check if there is a directory called TE_DEFAULT_PLUGINS_DIR in the current application dir
58 
59  if(boost::filesystem::is_directory(TE_DEFAULT_PLUGINS_DIR))
60  {
61  dirs.push_back( boost::filesystem::system_complete(TE_DEFAULT_PLUGINS_DIR).string() );
62  }
63 
64 // if the default dir is not available in the current dir let's try an environment variable defined as TERRALIB_DIR_ENVIRONMENT_VARIABLE
65 
66  {
67  char* e = getenv(TE_DIR_ENVIRONMENT_VARIABLE);
68 
69  if(e != 0)
70  {
71  boost::filesystem::path p(e);
73 
74  if(boost::filesystem::is_directory(p))
75  dirs.push_back( boost::filesystem::system_complete(p).string() );
76  }
77  }
78 
79  #ifdef TE_PLUGINS_INSTALL_PATH
80  {
81  boost::filesystem::path p(TE_PLUGINS_INSTALL_PATH);
82 
83  if(boost::filesystem::is_directory(p))
84  dirs.push_back( boost::filesystem::system_complete(p).string() );
85  }
86  #endif
87 }
88 
89 void te::plugin::DefaultFinder::addPluginsDir(const std::string& path)
90 {
91  if(!boost::filesystem::is_directory(path))
92  throw Exception((boost::format(TR_PLUGIN("Default plugin directory is invalid: %1%.")) % path).str());
93 
94  boost::filesystem::path p(boost::filesystem::system_complete(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  {
117  if(!boost::filesystem::is_directory(m_pluginsDir[i]))
118  throw Exception((boost::format(TR_PLUGIN("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 // check just in direct sub-dirs, don't go recursively for ever!
125  if(boost::filesystem::is_directory(it->status()))
126  {
127  boost::filesystem::path foundPlugin = (*it);
128 
129  foundPlugin /= TE_DEFAULT_PLUGIN_FILE_NAME;
130 
131  if(boost::filesystem::is_regular_file(foundPlugin))
132  {
133 // try to read the plugin XML configuration file and add info to the output vector
134  plugins.push_back(GetInstalledPlugin(foundPlugin.string()));
135  }
136  }
137  }
138  }
139 }
140 
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
Utility functions for dealing with 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
std::vector< std::string > m_pluginsDir
The base list of directories to search for plugins.
void getDefaultDirs(std::vector< std::string > &dirs) const
It returns the default plugins directories.
#define TE_DEFAULT_PLUGINS_DIR
The default look up plugin dir.
Definition: Config.h:75
A plugin finder that search for plugins in some special directories defined by compile time macros...
DefaultFinder()
Constructor.
void addPluginsDir(const std::string &path)
It adds a new base location where the finder will search for installed plugins.
const std::vector< std::string > & getPluginsDir() const
It returns the list of plugins base directories.
void getPlugins(boost::ptr_vector< PluginInfo > &plugins)
This method searches for installed plugins and output the plugins information in the PluginInfo vecto...
#define TE_DEFAULT_PLUGIN_FILE_NAME
The XML file name with plugin information.
Definition: Config.h:82