2006-10-03 10:44:04 +02: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
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* Copyright (C) 2006 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
|
|
|
//$Id: datasource_cache.cpp 23 2005-03-22 22:16:34Z pavlenko $
|
|
|
|
|
2007-10-08 19:42:41 +02:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/datasource_cache.hpp>
|
|
|
|
|
|
|
|
#include <mapnik/config_error.hpp>
|
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
// boost
|
2009-01-19 23:53:05 +01:00
|
|
|
#include <boost/version.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>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
namespace mapnik
|
|
|
|
{
|
2007-08-15 19:23:45 +02:00
|
|
|
using namespace std;
|
|
|
|
using namespace boost;
|
|
|
|
|
|
|
|
bool is_input_plugin (std::string const& filename)
|
|
|
|
{
|
2007-08-28 12:19:48 +02:00
|
|
|
return boost::algorithm::ends_with(filename,std::string(".input"));
|
2007-08-15 19:23:45 +02:00
|
|
|
}
|
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-08-15 19:23:45 +02:00
|
|
|
datasource_cache::datasource_cache()
|
|
|
|
{
|
2007-09-25 20:47:12 +02:00
|
|
|
if (lt_dlinit()) throw std::runtime_error("lt_dlinit() failed");
|
2007-08-15 19:23:45 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-08-15 19:23:45 +02:00
|
|
|
datasource_cache::~datasource_cache()
|
|
|
|
{
|
|
|
|
lt_dlexit();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<string,boost::shared_ptr<PluginInfo> > datasource_cache::plugins_;
|
|
|
|
bool datasource_cache::registered_=false;
|
2007-03-16 11:11:37 +01:00
|
|
|
|
2007-09-25 20:47:12 +02:00
|
|
|
datasource_ptr datasource_cache::create(const parameters& params)
|
2007-08-15 19:23:45 +02:00
|
|
|
{
|
2007-09-25 20:47:12 +02:00
|
|
|
boost::optional<std::string> type = params.get<std::string>("type");
|
|
|
|
if ( ! type)
|
|
|
|
{
|
|
|
|
throw config_error(string("Could not create datasource. Required ") +
|
|
|
|
"parameter 'type' is missing");
|
|
|
|
}
|
|
|
|
|
|
|
|
datasource_ptr ds;
|
|
|
|
map<string,boost::shared_ptr<PluginInfo> >::iterator itr=plugins_.find(*type);
|
|
|
|
if ( itr == plugins_.end() )
|
|
|
|
{
|
|
|
|
throw config_error(string("Could not create datasource. No plugin ") +
|
|
|
|
"found for type '" + * type + "'");
|
|
|
|
}
|
|
|
|
if ( ! itr->second->handle())
|
|
|
|
{
|
|
|
|
throw std::runtime_error(string("Cannot load library: ") +
|
|
|
|
lt_dlerror());
|
|
|
|
}
|
|
|
|
|
|
|
|
create_ds* create_datasource =
|
|
|
|
(create_ds*) lt_dlsym(itr->second->handle(), "create");
|
|
|
|
|
|
|
|
if ( ! create_datasource)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(string("Cannot load symbols: ") +
|
|
|
|
lt_dlerror());
|
|
|
|
}
|
2008-01-11 11:09:54 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "size = " << params.size() << "\n";
|
2007-10-05 13:27:00 +02:00
|
|
|
parameters::const_iterator i = params.begin();
|
|
|
|
for (;i!=params.end();++i)
|
|
|
|
{
|
2008-01-11 11:09:54 +01:00
|
|
|
std::clog << i->first << "=" << i->second << "\n";
|
2007-10-05 13:27:00 +02:00
|
|
|
}
|
2008-01-11 11:09:54 +01:00
|
|
|
#endif
|
2007-09-25 20:47:12 +02:00
|
|
|
ds=datasource_ptr(create_datasource(params), datasource_deleter());
|
|
|
|
|
2006-11-19 18:13:33 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2007-09-25 20:47:12 +02:00
|
|
|
std::clog<<"datasource="<<ds<<" type="<<type<<std::endl;
|
2006-11-19 18:13:33 +01:00
|
|
|
#endif
|
2007-09-25 20:47:12 +02:00
|
|
|
return ds;
|
2007-08-15 19:23:45 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-08-15 19:23:45 +02:00
|
|
|
bool datasource_cache::insert(const std::string& type,const lt_dlhandle module)
|
|
|
|
{
|
|
|
|
return plugins_.insert(make_pair(type,boost::shared_ptr<PluginInfo>
|
|
|
|
(new PluginInfo(type,module)))).second;
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2007-08-15 19:23:45 +02:00
|
|
|
void datasource_cache::register_datasources(const std::string& str)
|
|
|
|
{
|
2008-02-04 17:12:13 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2007-08-15 19:23:45 +02:00
|
|
|
mutex::scoped_lock lock(mapnik::singleton<mapnik::datasource_cache,
|
|
|
|
mapnik::CreateStatic>::mutex_);
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2007-08-15 19:23:45 +02:00
|
|
|
filesystem::path path(str);
|
|
|
|
filesystem::directory_iterator end_itr;
|
2009-01-19 23:53:05 +01:00
|
|
|
|
|
|
|
|
2007-08-15 19:23:45 +02:00
|
|
|
if (exists(path) && is_directory(path))
|
|
|
|
{
|
|
|
|
for (filesystem::directory_iterator itr(path);itr!=end_itr;++itr )
|
|
|
|
{
|
2009-01-19 23:53:05 +01:00
|
|
|
|
|
|
|
#if BOOST_VERSION < 103400
|
|
|
|
if (!is_directory( *itr ) && is_input_plugin(itr->leaf()))
|
|
|
|
#else
|
|
|
|
if (!is_directory( *itr ) && is_input_plugin(itr->path().leaf()))
|
|
|
|
#endif
|
|
|
|
|
2006-03-22 16:57:12 +01:00
|
|
|
{
|
2007-08-15 19:23:45 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
lt_dlhandle module=lt_dlopen(itr->string().c_str());
|
|
|
|
if (module)
|
|
|
|
{
|
|
|
|
datasource_name* ds_name =
|
|
|
|
(datasource_name*) lt_dlsym(module, "datasource_name");
|
|
|
|
if (ds_name && insert(ds_name(),module))
|
|
|
|
{
|
2006-11-19 18:13:33 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2007-08-15 19:23:45 +02:00
|
|
|
std::clog<<"registered datasource : "<<ds_name()<<std::endl;
|
2006-11-19 18:13:33 +01:00
|
|
|
#endif
|
2007-08-15 19:23:45 +02:00
|
|
|
registered_=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::clog << lt_dlerror() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|