/
datasource_cache.cpp
230 lines (203 loc) · 6.69 KB
/
datasource_cache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 Artem Pavlenko
*
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* 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
*
*****************************************************************************/
// mapnik
#include <mapnik/debug.hpp>
#include <mapnik/datasource_cache.hpp>
#include <mapnik/config_error.hpp>
#include <mapnik/params.hpp>
#include <mapnik/plugin.hpp>
#include <mapnik/util/fs.hpp>
#include <mapnik/utils.hpp>
// boost
#include <boost/make_shared.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/algorithm/string.hpp>
// stl
#include <algorithm>
#include <map>
#include <stdexcept>
namespace mapnik {
extern datasource_ptr create_static_datasource(parameters const& params);
extern std::vector<std::string> get_static_datasource_names();
bool is_input_plugin(std::string const& filename)
{
return boost::algorithm::ends_with(filename,std::string(".input"));
}
datasource_cache::datasource_cache()
{
PluginInfo::init();
}
datasource_cache::~datasource_cache()
{
PluginInfo::exit();
}
datasource_ptr datasource_cache::create(parameters const& params)
{
boost::optional<std::string> type = params.get<std::string>("type");
if ( ! type)
{
throw config_error(std::string("Could not create datasource. Required ") +
"parameter 'type' is missing");
}
datasource_ptr ds;
#ifdef MAPNIK_STATIC_PLUGINS
// return if it's created, raise otherwise
ds = create_static_datasource(params);
if (ds)
{
return ds;
}
#endif
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
std::map<std::string,boost::shared_ptr<PluginInfo> >::iterator itr=plugins_.find(*type);
if (itr == plugins_.end())
{
std::string s("Could not create datasource for type: '");
s += *type + "'";
if (plugin_directories_.empty())
{
s += " (no datasource plugin directories have been successfully registered)";
}
else
{
s += " (searched for datasource plugins in '" + plugin_directories() + "')";
}
throw config_error(s);
}
if (! itr->second->valid())
{
throw std::runtime_error(std::string("Cannot load library: ") +
itr->second->get_error());
}
// http://www.mr-edd.co.uk/blog/supressing_gcc_warnings
#ifdef __GNUC__
__extension__
#endif
create_ds* create_datasource = reinterpret_cast<create_ds*>(itr->second->get_symbol("create"));
if (! create_datasource)
{
throw std::runtime_error(std::string("Cannot load symbols: ") +
itr->second->get_error());
}
ds = datasource_ptr(create_datasource(params), datasource_deleter());
return ds;
}
std::string datasource_cache::plugin_directories()
{
return boost::algorithm::join(plugin_directories_,", ");
}
std::vector<std::string> datasource_cache::plugin_names()
{
std::vector<std::string> names;
#ifdef MAPNIK_STATIC_PLUGINS
names = get_static_datasource_names();
#endif
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;
}
void datasource_cache::register_datasources(std::string const& str)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
plugin_directories_.insert(str);
if (mapnik::util::exists(str) && mapnik::util::is_directory(str))
{
boost::filesystem::directory_iterator end_itr;
#ifdef _WINDOWS
std::wstring wide_dir(mapnik::utf8_to_utf16(str));
for (boost::filesystem::directory_iterator itr(wide_dir); itr != end_itr; ++itr)
#else
for (boost::filesystem::directory_iterator itr(str); itr != end_itr; ++itr )
#endif
{
#if (BOOST_FILESYSTEM_VERSION == 3)
if (!boost::filesystem::is_directory(*itr) && is_input_plugin(itr->path().filename().string()))
#else // v2
if (!boost::filesystem::is_directory(*itr) && is_input_plugin(itr->path().leaf()))
#endif
{
#if (BOOST_FILESYSTEM_VERSION == 3)
if (register_datasource(itr->path().string()))
#else // v2
if (register_datasource(itr->string()))
#endif
{
registered_ = true;
}
}
}
}
}
bool datasource_cache::register_datasource(std::string const& filename)
{
try
{
if (!mapnik::util::exists(filename))
{
MAPNIK_LOG_ERROR(datasource_cache)
<< "Cannot load '"
<< filename << "' (plugin does not exist)";
return false;
}
boost::shared_ptr<PluginInfo> plugin = boost::make_shared<PluginInfo>(filename,"datasource_name");
if (plugin->valid())
{
if (plugin->name().empty())
{
MAPNIK_LOG_ERROR(datasource_cache)
<< "Problem loading plugin library '"
<< filename << "' (plugin is lacking compatible interface)";
}
else
{
if (plugins_.insert(std::make_pair(plugin->name(),plugin)).second)
{
MAPNIK_LOG_DEBUG(datasource_cache)
<< "datasource_cache: Registered="
<< plugin->name();
return true;
}
}
}
else
{
MAPNIK_LOG_ERROR(datasource_cache)
<< "Problem loading plugin library: "
<< filename << " (dlopen failed - plugin likely has an unsatisfied dependency or incompatible ABI)";
}
}
catch (std::exception const& ex)
{
MAPNIK_LOG_ERROR(datasource_cache)
<< "Exception caught while loading plugin library: "
<< filename << " (" << ex.what() << ")";
}
return false;
}
}