2006-10-03 08:44:04 +00:00
|
|
|
/*****************************************************************************
|
2006-03-31 10:32:02 +00:00
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2011-10-23 13:04:25 +00:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00: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 15:06:59 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 10:32:02 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00: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 15:06:59 +00:00
|
|
|
//$Id: datasource_cache.cpp 23 2005-03-22 22:16:34Z pavlenko $
|
|
|
|
|
2007-10-08 17:42:41 +00:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/datasource_cache.hpp>
|
|
|
|
|
|
|
|
#include <mapnik/config_error.hpp>
|
|
|
|
|
2006-10-04 11:22:18 +00:00
|
|
|
// boost
|
2009-01-19 22:53:05 +00:00
|
|
|
#include <boost/version.hpp>
|
2011-05-10 21:09:54 +00:00
|
|
|
#include <boost/make_shared.hpp>
|
2005-06-14 15:06:59 +00:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2007-08-15 17:23:45 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2007-10-08 17:42:41 +00:00
|
|
|
|
2007-03-22 10:55:43 +00:00
|
|
|
// ltdl
|
|
|
|
#include <ltdl.h>
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2007-10-08 17:42:41 +00:00
|
|
|
// stl
|
|
|
|
#include <algorithm>
|
2011-06-24 00:53:00 +00:00
|
|
|
#include <iostream>
|
2007-10-08 17:42:41 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2005-06-14 15:06:59 +00:00
|
|
|
namespace mapnik
|
|
|
|
{
|
2007-08-15 17:23:45 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
bool is_input_plugin (std::string const& filename)
|
|
|
|
{
|
|
|
|
return boost::algorithm::ends_with(filename,std::string(".input"));
|
|
|
|
}
|
2007-08-15 17:23:45 +00:00
|
|
|
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
datasource_cache::datasource_cache()
|
|
|
|
{
|
|
|
|
if (lt_dlinit()) throw std::runtime_error("lt_dlinit() failed");
|
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
datasource_cache::~datasource_cache()
|
|
|
|
{
|
|
|
|
lt_dlexit();
|
|
|
|
}
|
2007-08-15 17:23:45 +00:00
|
|
|
|
2011-06-24 00:53:00 +00:00
|
|
|
std::map<std::string,boost::shared_ptr<PluginInfo> > datasource_cache::plugins_;
|
2010-06-02 11:03:30 +00:00
|
|
|
bool datasource_cache::registered_=false;
|
2010-07-15 21:11:41 +00:00
|
|
|
std::vector<std::string> datasource_cache::plugin_directories_;
|
2007-03-16 10:11:37 +00:00
|
|
|
|
2010-10-24 06:34:18 +00:00
|
|
|
datasource_ptr datasource_cache::create(const parameters& params, bool bind)
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
|
|
|
boost::optional<std::string> type = params.get<std::string>("type");
|
|
|
|
if ( ! type)
|
|
|
|
{
|
2011-06-24 00:53:00 +00:00
|
|
|
throw config_error(std::string("Could not create datasource. Required ") +
|
2010-06-02 11:03:30 +00:00
|
|
|
"parameter 'type' is missing");
|
|
|
|
}
|
|
|
|
|
|
|
|
datasource_ptr ds;
|
2011-06-24 00:53:00 +00:00
|
|
|
std::map<std::string,boost::shared_ptr<PluginInfo> >::iterator itr=plugins_.find(*type);
|
2010-06-02 11:03:30 +00:00
|
|
|
if ( itr == plugins_.end() )
|
|
|
|
{
|
2011-06-24 00:53:00 +00:00
|
|
|
throw config_error(std::string("Could not create datasource. No plugin ") +
|
2010-07-15 21:11:41 +00:00
|
|
|
"found for type '" + * type + "' (searched in: " + plugin_directories() + ")");
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
|
|
|
if ( ! itr->second->handle())
|
|
|
|
{
|
2011-06-24 00:53:00 +00:00
|
|
|
throw std::runtime_error(std::string("Cannot load library: ") +
|
2010-06-02 11:03:30 +00:00
|
|
|
lt_dlerror());
|
|
|
|
}
|
2011-04-02 00:43:20 +00:00
|
|
|
// http://www.mr-edd.co.uk/blog/supressing_gcc_warnings
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__extension__
|
|
|
|
#endif
|
2010-06-02 11:03:30 +00:00
|
|
|
create_ds* create_datasource =
|
2011-04-02 00:43:20 +00:00
|
|
|
reinterpret_cast<create_ds*>(lt_dlsym(itr->second->handle(), "create"));
|
2010-06-02 11:03:30 +00:00
|
|
|
|
|
|
|
if ( ! create_datasource)
|
|
|
|
{
|
2011-06-24 00:53:00 +00:00
|
|
|
throw std::runtime_error(std::string("Cannot load symbols: ") +
|
2010-06-02 11:03:30 +00:00
|
|
|
lt_dlerror());
|
|
|
|
}
|
2008-01-11 10:09:54 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-06-02 11:03:30 +00:00
|
|
|
std::clog << "size = " << params.size() << "\n";
|
|
|
|
parameters::const_iterator i = params.begin();
|
|
|
|
for (;i!=params.end();++i)
|
|
|
|
{
|
2010-11-17 19:45:51 +00:00
|
|
|
std::clog << i->first << "=" << i->second << "\n";
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2008-01-11 10:09:54 +00:00
|
|
|
#endif
|
2010-10-24 06:34:18 +00:00
|
|
|
ds=datasource_ptr(create_datasource(params, bind), datasource_deleter());
|
2007-09-25 18:47:12 +00:00
|
|
|
|
2006-11-19 17:13:33 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-06-02 11:03:30 +00:00
|
|
|
std::clog<<"datasource="<<ds<<" type="<<type<<std::endl;
|
2006-11-19 17:13:33 +00:00
|
|
|
#endif
|
2010-06-02 11:03:30 +00:00
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool datasource_cache::insert(const std::string& type,const lt_dlhandle module)
|
|
|
|
{
|
2011-05-10 21:09:54 +00:00
|
|
|
return plugins_.insert(make_pair(type,boost::make_shared<PluginInfo>
|
|
|
|
(type,module))).second;
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 21:11:41 +00:00
|
|
|
std::string datasource_cache::plugin_directories()
|
|
|
|
{
|
|
|
|
return boost::algorithm::join(plugin_directories_,", ");
|
|
|
|
}
|
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
std::vector<std::string> datasource_cache::plugin_names ()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2009-06-22 06:59:56 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
void datasource_cache::register_datasources(const std::string& str)
|
|
|
|
{
|
2008-02-04 16:12:13 +00:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2010-11-17 19:45:51 +00:00
|
|
|
mutex::scoped_lock lock(mapnik::singleton<mapnik::datasource_cache,
|
2010-06-02 11:03:30 +00:00
|
|
|
mapnik::CreateStatic>::mutex_);
|
2008-02-04 16:12:13 +00:00
|
|
|
#endif
|
2011-01-04 15:22:49 +00:00
|
|
|
boost::filesystem::path path(str);
|
2010-07-15 21:11:41 +00:00
|
|
|
plugin_directories_.push_back(str);
|
2011-01-04 15:22:49 +00:00
|
|
|
boost::filesystem::directory_iterator end_itr;
|
2009-01-19 22:53:05 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
if (exists(path) && is_directory(path))
|
|
|
|
{
|
2011-01-04 15:22:49 +00:00
|
|
|
for (boost::filesystem::directory_iterator itr(path);itr!=end_itr;++itr )
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2009-01-19 22:53:05 +00:00
|
|
|
|
|
|
|
#if BOOST_VERSION < 103400
|
2010-11-17 19:45:51 +00:00
|
|
|
if (!is_directory( *itr ) && is_input_plugin(itr->leaf()))
|
2009-01-19 22:53:05 +00:00
|
|
|
#else
|
2011-01-04 15:22:49 +00:00
|
|
|
#if (BOOST_FILESYSTEM_VERSION == 3)
|
|
|
|
if (!is_directory( *itr ) && is_input_plugin(itr->path().filename().string()))
|
|
|
|
#else // v2
|
|
|
|
if (!is_directory( *itr ) && is_input_plugin(itr->path().leaf()))
|
|
|
|
#endif
|
2009-01-19 22:53:05 +00:00
|
|
|
#endif
|
2010-11-17 19:45:51 +00:00
|
|
|
{
|
|
|
|
try
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2010-11-19 23:02:58 +00:00
|
|
|
#ifdef LIBTOOL_SUPPORTS_ADVISE
|
2011-07-05 23:50:30 +00:00
|
|
|
/* Note: the below was added as a workaround pre http://trac.mapnik.org/ticket/790
|
|
|
|
It could now be removed, but also is not doing any harm AFAICT.
|
|
|
|
*/
|
|
|
|
|
2010-11-19 23:02:58 +00:00
|
|
|
// with ltdl >=2.2 we can actually pass RTDL_GLOBAL to dlopen via the
|
2010-11-17 19:45:51 +00:00
|
|
|
// ltdl advise trick which is required on linux unless plugins are directly
|
|
|
|
// linked to libmapnik (and deps) at build time. The only other approach is to
|
|
|
|
// set the dlopen flags in the calling process (like in the python bindings)
|
2010-11-19 23:02:58 +00:00
|
|
|
|
|
|
|
// clear errors
|
|
|
|
lt_dlerror();
|
|
|
|
|
|
|
|
lt_dlhandle module = 0;
|
2010-11-17 19:45:51 +00:00
|
|
|
lt_dladvise advise;
|
2010-11-19 23:02:58 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = lt_dlinit();
|
|
|
|
if (ret != 0) {
|
|
|
|
std::clog << "Datasource loader: could not intialize dynamic loading: " << lt_dlerror() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = lt_dladvise_init(&advise);
|
|
|
|
if (ret != 0) {
|
|
|
|
std::clog << "Datasource loader: could not intialize dynamic loading: " << lt_dlerror() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = lt_dladvise_global(&advise);
|
|
|
|
if (ret != 0) {
|
|
|
|
std::clog << "Datasource loader: could not intialize dynamic loading of global symbols: " << lt_dlerror() << "\n";
|
|
|
|
}
|
2011-01-04 15:22:49 +00:00
|
|
|
#if (BOOST_FILESYSTEM_VERSION == 3)
|
|
|
|
module = lt_dlopenadvise (itr->path().string().c_str(), advise);
|
|
|
|
#else // v2
|
2010-11-19 23:02:58 +00:00
|
|
|
module = lt_dlopenadvise (itr->string().c_str(), advise);
|
2011-01-04 15:22:49 +00:00
|
|
|
#endif
|
|
|
|
|
2010-11-17 19:45:51 +00:00
|
|
|
lt_dladvise_destroy(&advise);
|
|
|
|
#else
|
2011-01-04 15:22:49 +00:00
|
|
|
|
|
|
|
#if (BOOST_FILESYSTEM_VERSION == 3)
|
|
|
|
lt_dlhandle module = lt_dlopen(itr->path().string().c_str());
|
|
|
|
#else // v2
|
2010-11-17 19:45:51 +00:00
|
|
|
lt_dlhandle module = lt_dlopen(itr->string().c_str());
|
2011-01-04 15:22:49 +00:00
|
|
|
#endif
|
|
|
|
|
2010-11-17 19:45:51 +00:00
|
|
|
#endif
|
|
|
|
if (module)
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2011-04-02 00:43:20 +00:00
|
|
|
// http://www.mr-edd.co.uk/blog/supressing_gcc_warnings
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__extension__
|
|
|
|
#endif
|
2010-11-17 19:45:51 +00:00
|
|
|
datasource_name* ds_name =
|
2011-04-02 00:43:20 +00:00
|
|
|
reinterpret_cast<datasource_name*>(lt_dlsym(module, "datasource_name"));
|
2010-11-17 19:45:51 +00:00
|
|
|
if (ds_name && insert(ds_name(),module))
|
|
|
|
{
|
2006-11-19 17:13:33 +00:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-11-19 23:02:58 +00:00
|
|
|
std::clog << "Datasource loader: registered: " << ds_name() << std::endl;
|
2006-11-19 17:13:33 +00:00
|
|
|
#endif
|
2010-11-17 19:45:51 +00:00
|
|
|
registered_=true;
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-17 19:45:51 +00:00
|
|
|
else
|
|
|
|
{
|
2011-01-04 15:22:49 +00:00
|
|
|
#if (BOOST_FILESYSTEM_VERSION == 3)
|
|
|
|
std::clog << "Problem loading plugin library: " << itr->path().string()
|
2011-03-15 20:50:30 +00:00
|
|
|
<< " (dlopen failed - plugin likely has an unsatisfied dependency or incompatible ABI)" << std::endl;
|
2011-01-04 15:22:49 +00:00
|
|
|
#else // v2
|
|
|
|
std::clog << "Problem loading plugin library: " << itr->string()
|
2011-03-15 20:50:30 +00:00
|
|
|
<< " (dlopen failed - plugin likely has an unsatisfied dependency or incompatible ABI)" << std::endl;
|
2011-01-04 15:22:49 +00:00
|
|
|
#endif
|
2010-11-17 19:45:51 +00:00
|
|
|
}
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2010-11-17 19:45:51 +00:00
|
|
|
catch (...) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|