mapnik/src/datasource_cache.cpp

261 lines
7.6 KiB
C++
Raw Normal View History

/*****************************************************************************
2012-02-02 02:53:35 +01:00
*
2006-03-31 12:32:02 +02:00
* This file is part of Mapnik (c++ mapping toolkit)
2005-06-14 17:06:59 +02:00
*
2015-06-16 12:49:16 +02:00
* Copyright (C) 2015 Artem Pavlenko
2005-06-14 17:06:59 +02:00
*
2006-03-31 12:32:02 +02:00
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
2005-06-14 17:06:59 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2006-03-31 12:32:02 +02:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
2005-06-14 17:06:59 +02:00
*
2006-03-31 12:32:02 +02:00
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
2005-06-14 17:06:59 +02:00
2007-10-08 19:42:41 +02:00
// mapnik
2012-04-08 02:20:56 +02:00
#include <mapnik/debug.hpp>
2014-07-23 23:02:36 +02:00
#include <mapnik/datasource.hpp>
2007-10-08 19:42:41 +02:00
#include <mapnik/datasource_cache.hpp>
#include <mapnik/config_error.hpp>
2013-01-14 07:02:18 +01:00
#include <mapnik/params.hpp>
#include <mapnik/plugin.hpp>
#include <mapnik/util/fs.hpp>
2007-10-08 19:42:41 +02:00
// boost
2014-10-22 01:37:27 +02:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
2014-07-23 23:16:11 +02:00
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/predicate.hpp>
2014-10-22 01:37:27 +02:00
#pragma GCC diagnostic pop
2007-10-08 19:42:41 +02:00
// stl
#include <algorithm>
2013-01-14 07:02:18 +01:00
#include <map>
2013-06-03 05:19:33 +02:00
#include <stdexcept>
2007-10-08 19:42:41 +02:00
namespace mapnik {
2012-02-02 02:53:35 +01:00
template class singleton<datasource_cache, CreateStatic>;
extern datasource_ptr create_static_datasource(parameters const& params);
extern std::vector<std::string> get_static_datasource_names();
2013-03-23 01:44:27 +01:00
bool is_input_plugin(std::string const& filename)
2010-06-02 13:03:30 +02:00
{
return boost::algorithm::ends_with(filename,std::string(".input"));
}
2012-02-02 02:53:35 +01:00
datasource_cache::datasource_cache()
2010-06-02 13:03:30 +02:00
{
PluginInfo::init();
2010-06-02 13:03:30 +02:00
}
2005-06-14 17:06:59 +02:00
datasource_cache::~datasource_cache()
2010-06-02 13:03:30 +02:00
{
PluginInfo::exit();
2010-06-02 13:03:30 +02:00
}
2013-03-23 01:44:27 +01:00
datasource_ptr datasource_cache::create(parameters const& params)
2010-06-02 13:03:30 +02:00
{
boost::optional<std::string> type = params.get<std::string>("type");
if ( ! type)
{
2011-06-24 02:53:00 +02:00
throw config_error(std::string("Could not create datasource. Required ") +
2010-06-02 13:03:30 +02:00
"parameter 'type' is missing");
}
datasource_ptr ds;
#ifdef MAPNIK_STATIC_PLUGINS
// return if it's created, raise otherwise
ds = create_static_datasource(params);
if (ds)
{
return ds;
}
#endif
std::map<std::string,std::shared_ptr<PluginInfo> >::iterator itr;
// add scope to ensure lock is released asap
{
#ifdef MAPNIK_THREADSAFE
std::lock_guard<std::recursive_mutex> lock(instance_mutex_);
#endif
itr=plugins_.find(*type);
if (itr == plugins_.end())
{
std::string s("Could not create datasource for type: '");
s += *type + "'";
if (plugin_directories_.empty())
{
s += " (no datasource plugin directories have been successfully registered)";
}
else
{
s += " (searched for datasource plugins in '" + plugin_directories() + "')";
}
throw config_error(s);
}
2010-06-02 13:03:30 +02:00
}
2012-04-08 02:20:56 +02:00
if (! itr->second->valid())
2010-06-02 13:03:30 +02:00
{
2011-06-24 02:53:00 +02:00
throw std::runtime_error(std::string("Cannot load library: ") +
itr->second->get_error());
2010-06-02 13:03:30 +02:00
}
2012-04-08 02:20:56 +02:00
// http://www.mr-edd.co.uk/blog/supressing_gcc_warnings
2012-02-02 02:53:35 +01:00
#ifdef __GNUC__
__extension__
2012-02-02 02:53:35 +01:00
#endif
create_ds create_datasource = reinterpret_cast<create_ds>(itr->second->get_symbol("create"));
2010-06-02 13:03:30 +02:00
if (! create_datasource)
2010-06-02 13:03:30 +02:00
{
2011-06-24 02:53:00 +02:00
throw std::runtime_error(std::string("Cannot load symbols: ") +
itr->second->get_error());
2010-06-02 13:03:30 +02:00
}
2012-04-08 02:20:56 +02:00
ds = datasource_ptr(create_datasource(params), datasource_deleter());
2010-06-02 13:03:30 +02:00
return ds;
}
std::string datasource_cache::plugin_directories()
{
#ifdef MAPNIK_THREADSAFE
std::lock_guard<std::recursive_mutex> lock(instance_mutex_);
#endif
return boost::algorithm::join(plugin_directories_,", ");
}
std::vector<std::string> datasource_cache::plugin_names()
2010-06-02 13:03:30 +02:00
{
std::vector<std::string> names;
#ifdef MAPNIK_STATIC_PLUGINS
names = get_static_datasource_names();
#endif
#ifdef MAPNIK_THREADSAFE
std::lock_guard<std::recursive_mutex> lock(instance_mutex_);
#endif
std::map<std::string,std::shared_ptr<PluginInfo> >::const_iterator itr;
for (itr = plugins_.begin(); itr != plugins_.end(); ++itr)
2010-06-02 13:03:30 +02:00
{
names.push_back(itr->first);
}
2010-06-02 13:03:30 +02:00
return names;
}
2012-02-02 02:53:35 +01:00
bool datasource_cache::register_datasources(std::string const& dir, bool recurse)
2012-02-02 02:53:35 +01:00
{
2008-02-04 17:12:13 +01:00
#ifdef MAPNIK_THREADSAFE
std::lock_guard<std::recursive_mutex> lock(instance_mutex_);
2008-02-04 17:12:13 +01:00
#endif
if (!mapnik::util::exists(dir))
{
return false;
}
plugin_directories_.insert(dir);
if (!mapnik::util::is_directory(dir))
{
return register_datasource(dir);
}
bool success = false;
try
2010-06-02 13:03:30 +02:00
{
for (std::string const& file_name : mapnik::util::list_directory(dir))
{
if (mapnik::util::is_directory(file_name) && recurse)
{
if (register_datasources(file_name, true))
{
success = true;
}
}
else
{
std::string base_name = mapnik::util::basename(file_name);
if (!boost::algorithm::starts_with(base_name,".") &&
mapnik::util::is_regular_file(file_name) &&
is_input_plugin(file_name))
{
if (register_datasource(file_name))
{
success = true;
}
2010-06-02 13:03:30 +02:00
}
}
}
}
catch (std::exception const& ex)
{
MAPNIK_LOG_ERROR(datasource_cache) << "register_datasources: " << ex.what();
}
return success;
2010-06-02 13:03:30 +02:00
}
bool datasource_cache::register_datasource(std::string const& filename)
{
#ifdef MAPNIK_THREADSAFE
std::lock_guard<std::recursive_mutex> lock(instance_mutex_);
#endif
try
{
if (!mapnik::util::exists(filename))
{
MAPNIK_LOG_ERROR(datasource_cache)
<< "Cannot load '"
<< filename << "' (plugin does not exist)";
return false;
}
std::shared_ptr<PluginInfo> plugin = std::make_shared<PluginInfo>(filename,"datasource_name");
if (plugin->valid())
{
if (plugin->name().empty())
{
MAPNIK_LOG_ERROR(datasource_cache)
<< "Problem loading plugin library '"
<< filename << "' (plugin is lacking compatible interface)";
}
else
{
if (plugins_.emplace(plugin->name(),plugin).second)
{
2014-02-07 03:42:20 +01:00
MAPNIK_LOG_DEBUG(datasource_cache)
<< "datasource_cache: Registered="
<< plugin->name();
return true;
}
}
}
else
{
MAPNIK_LOG_ERROR(datasource_cache)
2013-03-23 01:44:27 +01:00
<< "Problem loading plugin library: "
<< filename << " (dlopen failed - plugin likely has an unsatisfied dependency or incompatible ABI)";
}
}
2013-03-23 01:58:33 +01:00
catch (std::exception const& ex)
2013-03-23 01:44:27 +01:00
{
MAPNIK_LOG_ERROR(datasource_cache)
<< "Exception caught while loading plugin library: "
<< filename << " (" << ex.what() << ")";
}
return false;
}
}