2006-10-03 10:44:04 +02:00
|
|
|
/*****************************************************************************
|
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
|
|
|
*
|
2011-10-23 15:04:25 +02:00
|
|
|
* Copyright (C) 2011 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>
|
2013-03-23 01:37:56 +01:00
|
|
|
#include <mapnik/plugin.hpp>
|
2013-06-03 04:28:24 +02:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2014-06-19 01:11:36 +02:00
|
|
|
#include <mapnik/utils.hpp>
|
2007-10-08 19:42:41 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
// boost
|
2005-06-14 17:06:59 +02:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2007-08-15 19:23:45 +02:00
|
|
|
#include <boost/algorithm/string.hpp>
|
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
|
|
|
|
2012-09-07 15:41:07 +02:00
|
|
|
namespace mapnik {
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2013-05-16 20:33:23 +02:00
|
|
|
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
|
|
|
|
2012-09-07 15:41:07 +02:00
|
|
|
datasource_cache::datasource_cache()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-04-10 04:37:02 +02:00
|
|
|
PluginInfo::init();
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2012-09-07 15:41:07 +02:00
|
|
|
datasource_cache::~datasource_cache()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-04-10 04:37:02 +02:00
|
|
|
PluginInfo::exit();
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2007-08-15 19:23:45 +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");
|
|
|
|
}
|
|
|
|
|
2013-05-16 20:33:23 +02:00
|
|
|
datasource_ptr ds;
|
|
|
|
|
|
|
|
#ifdef MAPNIK_STATIC_PLUGINS
|
|
|
|
// return if it's created, raise otherwise
|
|
|
|
ds = create_static_datasource(params);
|
|
|
|
if (ds)
|
|
|
|
{
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-11-18 03:47:09 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2013-10-11 14:20:46 +02:00
|
|
|
mapnik::scoped_lock lock(mutex_);
|
2011-11-18 03:47:09 +01:00
|
|
|
#endif
|
2012-04-08 02:20:56 +02:00
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
std::map<std::string,std::shared_ptr<PluginInfo> >::iterator itr=plugins_.find(*type);
|
2013-05-16 20:33:23 +02:00
|
|
|
if (itr == plugins_.end())
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-12-07 08:06:12 +01:00
|
|
|
std::string s("Could not create datasource for type: '");
|
|
|
|
s += *type + "'";
|
2012-10-30 19:00:24 +01:00
|
|
|
if (plugin_directories_.empty())
|
|
|
|
{
|
2013-10-21 19:24:51 +02:00
|
|
|
s += " (no datasource plugin directories have been successfully registered)";
|
2012-10-30 19:00:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-21 19:24:51 +02:00
|
|
|
s += " (searched for datasource plugins in '" + plugin_directories() + "')";
|
2012-10-30 19:00:24 +01:00
|
|
|
}
|
2012-12-07 08:06:12 +01:00
|
|
|
throw config_error(s);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-04-08 02:20:56 +02:00
|
|
|
|
2013-05-16 20:33:23 +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: ") +
|
2013-04-10 04:37:02 +02:00
|
|
|
itr->second->get_error());
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-04-08 02:20:56 +02:00
|
|
|
|
2011-04-02 02:43:20 +02:00
|
|
|
// http://www.mr-edd.co.uk/blog/supressing_gcc_warnings
|
2012-02-02 02:53:35 +01:00
|
|
|
#ifdef __GNUC__
|
2011-04-02 02:43:20 +02:00
|
|
|
__extension__
|
2012-02-02 02:53:35 +01:00
|
|
|
#endif
|
2014-07-07 19:23:15 +02:00
|
|
|
create_ds create_datasource = reinterpret_cast<create_ds>(itr->second->get_symbol("create"));
|
2010-06-02 13:03:30 +02:00
|
|
|
|
2013-05-16 20:33:23 +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: ") +
|
2013-04-10 04:37:02 +02:00
|
|
|
itr->second->get_error());
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-04-08 02:20:56 +02:00
|
|
|
|
2013-05-16 20:33:23 +02:00
|
|
|
ds = datasource_ptr(create_datasource(params), datasource_deleter());
|
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifdef MAPNIK_LOG
|
2013-05-16 20:33:23 +02:00
|
|
|
MAPNIK_LOG_DEBUG(datasource_cache)
|
|
|
|
<< "datasource_cache: Datasource="
|
|
|
|
<< ds << " type=" << type;
|
|
|
|
|
2013-03-23 01:44:27 +01:00
|
|
|
MAPNIK_LOG_DEBUG(datasource_cache)
|
|
|
|
<< "datasource_cache: Size="
|
|
|
|
<< params.size();
|
2012-04-08 02:20:56 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
parameters::const_iterator i = params.begin();
|
2012-04-08 02:20:56 +02:00
|
|
|
for (; i != params.end(); ++i)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-03-23 01:44:27 +01:00
|
|
|
MAPNIK_LOG_DEBUG(datasource_cache)
|
|
|
|
<< "datasource_cache: -- "
|
|
|
|
<< i->first << "=" << i->second;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2008-01-11 11:09:54 +01:00
|
|
|
#endif
|
2007-09-25 20:47:12 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
2012-09-07 15:41:07 +02:00
|
|
|
std::string datasource_cache::plugin_directories()
|
2010-07-15 23:11:41 +02:00
|
|
|
{
|
|
|
|
return boost::algorithm::join(plugin_directories_,", ");
|
|
|
|
}
|
|
|
|
|
2012-09-07 15:41:07 +02:00
|
|
|
std::vector<std::string> datasource_cache::plugin_names()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
std::vector<std::string> names;
|
2013-05-16 20:33:23 +02:00
|
|
|
|
|
|
|
#ifdef MAPNIK_STATIC_PLUGINS
|
|
|
|
names = get_static_datasource_names();
|
|
|
|
#endif
|
|
|
|
|
2013-09-20 15:00:11 +02:00
|
|
|
std::map<std::string,std::shared_ptr<PluginInfo> >::const_iterator itr;
|
2013-05-16 20:33:23 +02:00
|
|
|
for (itr = plugins_.begin(); itr != plugins_.end(); ++itr)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
names.push_back(itr->first);
|
|
|
|
}
|
2013-05-16 20:33:23 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
return names;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2012-09-07 15:41:07 +02:00
|
|
|
void datasource_cache::register_datasources(std::string const& str)
|
2012-02-02 02:53:35 +01:00
|
|
|
{
|
2008-02-04 17:12:13 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2013-10-11 14:20:46 +02:00
|
|
|
mapnik::scoped_lock lock(mutex_);
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2014-02-07 02:05:46 +01:00
|
|
|
plugin_directories_.insert(str);
|
2013-06-03 04:28:24 +02:00
|
|
|
if (mapnik::util::exists(str) && mapnik::util::is_directory(str))
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2013-06-03 04:28:24 +02:00
|
|
|
boost::filesystem::directory_iterator end_itr;
|
2014-06-19 01:11:36 +02:00
|
|
|
#ifdef _WINDOWS
|
|
|
|
std::wstring wide_dir(mapnik::utf8_to_utf16(str));
|
|
|
|
for (boost::filesystem::directory_iterator itr(wide_dir); itr != end_itr; ++itr)
|
|
|
|
#else
|
2013-06-03 04:28:24 +02:00
|
|
|
for (boost::filesystem::directory_iterator itr(str); itr != end_itr; ++itr )
|
2014-06-19 01:11:36 +02:00
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2009-01-19 23:53:05 +01:00
|
|
|
|
2012-02-02 02:53:35 +01:00
|
|
|
#if (BOOST_FILESYSTEM_VERSION == 3)
|
2013-06-03 04:28:24 +02:00
|
|
|
if (!boost::filesystem::is_directory(*itr) && is_input_plugin(itr->path().filename().string()))
|
2011-01-04 16:22:49 +01:00
|
|
|
#else // v2
|
2013-06-03 04:28:24 +02:00
|
|
|
if (!boost::filesystem::is_directory(*itr) && is_input_plugin(itr->path().leaf()))
|
2012-02-02 02:53:35 +01:00
|
|
|
#endif
|
2013-05-16 20:33:23 +02:00
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
#if (BOOST_FILESYSTEM_VERSION == 3)
|
2013-05-16 20:33:23 +02:00
|
|
|
if (register_datasource(itr->path().string()))
|
2011-01-04 16:22:49 +01:00
|
|
|
#else // v2
|
2013-05-16 20:33:23 +02:00
|
|
|
if (register_datasource(itr->string()))
|
2011-01-04 16:22:49 +01:00
|
|
|
#endif
|
2013-05-16 20:33:23 +02:00
|
|
|
{
|
|
|
|
registered_ = true;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2013-05-16 20:33:23 +02:00
|
|
|
}
|
2010-11-17 20:45:51 +01:00
|
|
|
}
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2011-11-18 03:47:09 +01:00
|
|
|
|
2013-04-10 04:37:02 +02:00
|
|
|
bool datasource_cache::register_datasource(std::string const& filename)
|
2012-06-14 01:25:59 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-02-07 02:05:46 +01:00
|
|
|
if (!mapnik::util::exists(filename))
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(datasource_cache)
|
|
|
|
<< "Cannot load '"
|
|
|
|
<< filename << "' (plugin does not exist)";
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-20 15:00:11 +02:00
|
|
|
std::shared_ptr<PluginInfo> plugin = std::make_shared<PluginInfo>(filename,"datasource_name");
|
2013-04-10 04:37:02 +02:00
|
|
|
if (plugin->valid())
|
2012-06-14 01:25:59 +02:00
|
|
|
{
|
2013-04-10 04:37:02 +02:00
|
|
|
if (plugin->name().empty())
|
2012-06-14 01:25:59 +02:00
|
|
|
{
|
2013-04-10 04:37:02 +02:00
|
|
|
MAPNIK_LOG_ERROR(datasource_cache)
|
|
|
|
<< "Problem loading plugin library '"
|
|
|
|
<< filename << "' (plugin is lacking compatible interface)";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-07 03:24:35 +01:00
|
|
|
if (plugins_.insert(std::make_pair(plugin->name(),plugin)).second)
|
|
|
|
{
|
2014-02-07 03:42:20 +01:00
|
|
|
MAPNIK_LOG_DEBUG(datasource_cache)
|
2014-02-07 03:24:35 +01:00
|
|
|
<< "datasource_cache: Registered="
|
|
|
|
<< plugin->name();
|
|
|
|
return true;
|
|
|
|
}
|
2012-06-14 01:25:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(datasource_cache)
|
2013-03-23 01:44:27 +01:00
|
|
|
<< "Problem loading plugin library: "
|
2013-04-10 04:37:02 +02:00
|
|
|
<< filename << " (dlopen failed - plugin likely has an unsatisfied dependency or incompatible ABI)";
|
2012-06-14 01:25:59 +02:00
|
|
|
}
|
|
|
|
}
|
2013-03-23 01:58:33 +01:00
|
|
|
catch (std::exception const& ex)
|
2013-03-23 01:44:27 +01:00
|
|
|
{
|
2013-05-16 20:33:23 +02:00
|
|
|
MAPNIK_LOG_ERROR(datasource_cache)
|
|
|
|
<< "Exception caught while loading plugin library: "
|
|
|
|
<< filename << " (" << ex.what() << ")";
|
2012-06-14 01:25:59 +02:00
|
|
|
}
|
2014-02-07 02:05:46 +01:00
|
|
|
return false;
|
2012-06-14 01:25:59 +02:00
|
|
|
}
|
|
|
|
|
2012-09-07 15:41:07 +02:00
|
|
|
}
|