2005-06-14 17:06:59 +02:00
|
|
|
/* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
* Copyright (C) 2005 Artem Pavlenko
|
|
|
|
*
|
|
|
|
* Mapnik is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//$Id: datasource_cache.cpp 23 2005-03-22 22:16:34Z pavlenko $
|
|
|
|
|
|
|
|
#include "datasource_cache.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
using namespace std;
|
|
|
|
using namespace boost;
|
|
|
|
|
|
|
|
datasource_cache::datasource_cache()
|
|
|
|
{
|
|
|
|
if (lt_dlinit()) throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
datasource_cache::~datasource_cache()
|
|
|
|
{
|
|
|
|
lt_dlexit();
|
|
|
|
}
|
|
|
|
|
2005-12-12 14:15:33 +01:00
|
|
|
std::map<string,boost::shared_ptr<PluginInfo> > datasource_cache::plugins_;
|
2005-06-14 17:06:59 +02:00
|
|
|
bool datasource_cache::registered_=false;
|
|
|
|
|
2005-12-14 18:01:09 +01:00
|
|
|
datasource_p datasource_cache::create(const parameters& params)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2005-12-12 14:15:33 +01:00
|
|
|
datasource_p ds;
|
2005-06-14 17:06:59 +02:00
|
|
|
try
|
|
|
|
{
|
2005-12-14 18:01:09 +01:00
|
|
|
const std::string type=params.get("type");
|
2005-12-12 14:15:33 +01:00
|
|
|
map<string,boost::shared_ptr<PluginInfo> >::iterator itr=plugins_.find(type);
|
2005-06-14 17:06:59 +02:00
|
|
|
if (itr!=plugins_.end())
|
|
|
|
{
|
|
|
|
if (itr->second->handle())
|
|
|
|
{
|
|
|
|
create_ds* create_datasource = (create_ds*) lt_dlsym(itr->second->handle(), "create");
|
|
|
|
if (!create_datasource)
|
|
|
|
{
|
|
|
|
std::cerr << "Cannot load symbols: " << lt_dlerror() << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-12-12 14:15:33 +01:00
|
|
|
ds=datasource_p(create_datasource(params),datasource_deleter());
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "Cannot load library: " << " "<< lt_dlerror() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cout<<"datasource="<<ds<<" type="<<type<<std::endl;
|
|
|
|
}
|
|
|
|
catch (datasource_exception& ex)
|
|
|
|
{
|
|
|
|
std::cerr<<ex.what()<<std::endl;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::cerr<<"exception caught "<<std::endl;
|
|
|
|
}
|
2005-12-12 14:15:33 +01:00
|
|
|
return ds;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool datasource_cache::insert(const std::string& type,const lt_dlhandle module)
|
|
|
|
{
|
2005-12-12 14:15:33 +01:00
|
|
|
return plugins_.insert(make_pair(type,boost::shared_ptr<PluginInfo>(new PluginInfo(type,module)))).second;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void datasource_cache::register_datasources(const std::string& str)
|
|
|
|
{
|
|
|
|
Lock lock(&mutex_);
|
|
|
|
filesystem::path path(str);
|
|
|
|
filesystem::directory_iterator end_itr;
|
|
|
|
if (exists(path))
|
|
|
|
{
|
|
|
|
for (filesystem::directory_iterator itr(path);itr!=end_itr;++itr )
|
|
|
|
{
|
2005-09-09 11:38:37 +02:00
|
|
|
if (!is_directory( *itr ) && itr->leaf()[0]!='.')
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2005-09-09 11:38:37 +02:00
|
|
|
lt_dlhandle module=lt_dlopenext(itr->string().c_str());
|
2005-09-08 15:52:42 +02:00
|
|
|
if (module)
|
|
|
|
{
|
|
|
|
datasource_name* ds_name = (datasource_name*) lt_dlsym(module, "datasource_name");
|
|
|
|
if (ds_name && insert(ds_name(),module))
|
|
|
|
{
|
|
|
|
std::cout<<"registered datasource : "<<ds_name()<<std::endl;
|
|
|
|
registered_=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr<<lt_dlerror()<<std::endl;
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2005-09-09 11:38:37 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|