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>
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <mapnik/datasource_cache.hpp>
|
|
|
|
#include <mapnik/config_error.hpp>
|
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
// boost
|
2011-05-10 23:09:54 +02:00
|
|
|
#include <boost/make_shared.hpp>
|
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
|
|
|
|
2007-03-22 11:55:43 +01:00
|
|
|
// ltdl
|
|
|
|
#include <ltdl.h>
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-10-08 19:42:41 +02:00
|
|
|
// stl
|
|
|
|
#include <algorithm>
|
2011-06-24 02:53:00 +02:00
|
|
|
#include <iostream>
|
2007-10-08 19:42:41 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
namespace mapnik { namespace detail {
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
bool is_input_plugin (std::string const& filename)
|
|
|
|
{
|
|
|
|
return boost::algorithm::ends_with(filename,std::string(".input"));
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
datasource_cache_impl::datasource_cache_impl()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
if (lt_dlinit()) throw std::runtime_error("lt_dlinit() failed");
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
datasource_cache_impl::~datasource_cache_impl()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
lt_dlexit();
|
|
|
|
}
|
2007-08-15 19:23:45 +02:00
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
//std::map<std::string,boost::shared_ptr<PluginInfo> > datasource_cache::plugins_;
|
|
|
|
//bool datasource_cache::registered_=false;
|
|
|
|
//std::vector<std::string> datasource_cache::plugin_directories_;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
datasource_ptr datasource_cache_impl::create(const parameters& params, bool bind)
|
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");
|
|
|
|
}
|
|
|
|
|
2011-11-18 03:47:09 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2012-09-05 13:53:37 +02:00
|
|
|
//mutex::scoped_lock lock(mutex_);
|
2011-11-18 03:47:09 +01:00
|
|
|
#endif
|
2012-04-08 02:20:56 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
datasource_ptr ds;
|
2011-06-24 02:53:00 +02:00
|
|
|
std::map<std::string,boost::shared_ptr<PluginInfo> >::iterator itr=plugins_.find(*type);
|
2010-06-02 13:03:30 +02:00
|
|
|
if ( itr == plugins_.end() )
|
|
|
|
{
|
2011-06-24 02:53:00 +02:00
|
|
|
throw config_error(std::string("Could not create datasource. No plugin ") +
|
2010-07-15 23:11:41 +02:00
|
|
|
"found for type '" + * type + "' (searched in: " + plugin_directories() + ")");
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-04-08 02:20:56 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
if ( ! itr->second->handle())
|
|
|
|
{
|
2011-06-24 02:53:00 +02:00
|
|
|
throw std::runtime_error(std::string("Cannot load library: ") +
|
2010-06-02 13:03:30 +02:00
|
|
|
lt_dlerror());
|
|
|
|
}
|
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
|
|
|
|
create_ds* create_datasource =
|
2011-04-02 02:43:20 +02:00
|
|
|
reinterpret_cast<create_ds*>(lt_dlsym(itr->second->handle(), "create"));
|
2010-06-02 13:03:30 +02:00
|
|
|
|
2012-04-08 02:20:56 +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: ") +
|
2010-06-02 13:03:30 +02:00
|
|
|
lt_dlerror());
|
|
|
|
}
|
2012-04-08 02:20:56 +02:00
|
|
|
|
|
|
|
#ifdef MAPNIK_LOG
|
2012-09-05 13:53:37 +02:00
|
|
|
MAPNIK_LOG_DEBUG(datasource_cache_impl) << "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
|
|
|
{
|
2012-09-05 13:53:37 +02:00
|
|
|
MAPNIK_LOG_DEBUG(datasource_cache_impl) << "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
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
ds = datasource_ptr(create_datasource(params, bind), datasource_deleter());
|
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
MAPNIK_LOG_DEBUG(datasource_cache_impl) << "datasource_cache: Datasource=" << ds << " type=" << type;
|
2012-04-09 21:41:56 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
bool datasource_cache_impl::insert(std::string const& type,const lt_dlhandle module)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2011-05-10 23:09:54 +02:00
|
|
|
return plugins_.insert(make_pair(type,boost::make_shared<PluginInfo>
|
|
|
|
(type,module))).second;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
std::string datasource_cache_impl::plugin_directories()
|
2010-07-15 23:11:41 +02:00
|
|
|
{
|
|
|
|
return boost::algorithm::join(plugin_directories_,", ");
|
|
|
|
}
|
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
std::vector<std::string> datasource_cache_impl::plugin_names()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
std::vector<std::string> names;
|
|
|
|
std::map<std::string,boost::shared_ptr<PluginInfo> >::const_iterator itr;
|
|
|
|
for (itr = plugins_.begin();itr!=plugins_.end();++itr)
|
|
|
|
{
|
|
|
|
names.push_back(itr->first);
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
void datasource_cache_impl::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
|
2012-09-05 13:53:37 +02:00
|
|
|
//mutex::scoped_lock lock(mapnik::singleton<mapnik::datasource_cache,
|
|
|
|
// mapnik::CreateStatic>::mutex_);
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2011-01-04 16:22:49 +01:00
|
|
|
boost::filesystem::path path(str);
|
2012-01-17 07:09:46 +01:00
|
|
|
// TODO - only push unique paths
|
2010-07-15 23:11:41 +02:00
|
|
|
plugin_directories_.push_back(str);
|
2011-01-04 16:22:49 +01:00
|
|
|
boost::filesystem::directory_iterator end_itr;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
if (exists(path) && is_directory(path))
|
|
|
|
{
|
2011-01-04 16:22:49 +01:00
|
|
|
for (boost::filesystem::directory_iterator itr(path);itr!=end_itr;++itr )
|
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)
|
2011-01-04 16:22:49 +01:00
|
|
|
if (!is_directory( *itr ) && is_input_plugin(itr->path().filename().string()))
|
|
|
|
#else // v2
|
2012-02-02 02:53:35 +01:00
|
|
|
if (!is_directory( *itr ) && is_input_plugin(itr->path().leaf()))
|
|
|
|
#endif
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
#if (BOOST_FILESYSTEM_VERSION == 3)
|
2012-06-14 01:25:59 +02:00
|
|
|
if (register_datasource(itr->path().string().c_str()))
|
2011-01-04 16:22:49 +01:00
|
|
|
#else // v2
|
2012-06-14 01:25:59 +02:00
|
|
|
if (register_datasource(itr->string().c_str()))
|
2011-01-04 16:22:49 +01:00
|
|
|
#endif
|
2012-06-14 01:25:59 +02:00
|
|
|
{
|
|
|
|
registered_ = true;
|
2010-11-17 20:45:51 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +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
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
bool datasource_cache_impl::register_datasource(std::string const& str)
|
2012-06-14 01:25:59 +02:00
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
lt_dlhandle module = lt_dlopen(str.c_str());
|
|
|
|
if (module)
|
|
|
|
{
|
|
|
|
// http://www.mr-edd.co.uk/blog/supressing_gcc_warnings
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__extension__
|
|
|
|
#endif
|
|
|
|
datasource_name* ds_name =
|
|
|
|
reinterpret_cast<datasource_name*>(lt_dlsym(module, "datasource_name"));
|
|
|
|
if (ds_name && insert(ds_name(),module))
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_DEBUG(datasource_cache) << "datasource_cache: Registered=" << ds_name();
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
else if (!ds_name)
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(datasource_cache)
|
|
|
|
<< "Problem loading plugin library '"
|
|
|
|
<< str << "' (plugin is lacking compatible interface)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(datasource_cache)
|
|
|
|
<< "Problem loading plugin library: " << str
|
|
|
|
<< " (dlopen failed - plugin likely has an unsatisfied dependency or incompatible ABI)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
MAPNIK_LOG_ERROR(datasource_cache)
|
|
|
|
<< "Exception caught while loading plugin library: " << str;
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2012-09-05 13:53:37 +02:00
|
|
|
}}
|