-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPluginManager.cpp
More file actions
342 lines (299 loc) · 9.32 KB
/
PluginManager.cpp
File metadata and controls
342 lines (299 loc) · 9.32 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
///////////////////////////////////////////////////////////////////////////////
// FILE: PluginManager.cpp
// PROJECT: Micro-Manager
// SUBSYSTEM: MMCore
//-----------------------------------------------------------------------------
// DESCRIPTION: Loading/unloading of device adapter modules
//
// COPYRIGHT: University of California, San Francisco, 2006-2014
//
// LICENSE: This file is distributed under the "Lesser GPL" (LGPL) license.
// License text is included with the source distribution.
//
// This file 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.
//
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
//
// AUTHOR: Nenad Amodaj, nenad@amodaj.com, 08/10/2005
#include "CoreUtils.h"
#include "Devices/DeviceInstance.h"
#include "Devices/HubInstance.h"
#include "Error.h"
#include "LibraryInfo/LibraryPaths.h"
#include "LoadableModules/LoadedDeviceAdapter.h"
#include "LoadableModules/LoadedDeviceAdapterImplMock.h"
#include "LoadableModules/LoadedDeviceAdapterImplRegular.h"
#include "PluginManager.h"
#include "ModuleInterface.h"
#include <algorithm>
#include <cstring>
#include <fstream>
#include <memory>
#include <set>
#include <string>
#include <vector>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <io.h>
#else
#include <sys/types.h>
#include <dirent.h>
#endif // _WIN32
#ifdef _WIN32
const char* const LIB_NAME_PREFIX = "mmgr_dal_";
#else
const char* const LIB_NAME_PREFIX = "libmmgr_dal_";
#endif
#ifdef __linux__
const char* const LIB_NAME_SUFFIX = ".so.0";
#else
const char* const LIB_NAME_SUFFIX = "";
#endif
namespace mmcore {
namespace internal {
///////////////////////////////////////////////////////////////////////////////
// CPluginManager class
// --------------------
CPluginManager::CPluginManager()
{
const std::vector<std::string> paths = GetDefaultSearchPaths();
SetSearchPaths(paths.begin(), paths.end());
}
CPluginManager::~CPluginManager()
{
}
/**
* Search for a library and return its absolute path.
*
* If no match is found, the filename is returned as is.
*
* @param filename the name of the file to look up.
*/
std::string
CPluginManager::FindInSearchPath(std::string filename)
{
for (const auto& p : searchPaths_) {
std::string path = p;
#ifdef _WIN32
path += "\\" + filename + ".dll";
#else
path += "/" + filename;
#endif
// test whether it exists
std::ifstream in(path.c_str(), std::ifstream::in);
in.close();
if (!in.fail())
return path;
}
return filename;
}
/**
* Load a plugin library.
*
* Since we want to have a consistent plugin discovery/loading mechanism,
* the search path -- if specified explicitly -- is traversed.
*
* This has to be done so that users do not have to make sure that
* DYLD_LIBRARY_PATH, LD_LIBRARY_PATH or PATH and java.library.path are in
* sync and include the correct paths.
*
* However, the OS-dependent default search path is still searched in the case
* where the library is not found in our list of search paths. (XXX This should
* perhaps change, to avoid surprises.)
*
* @param moduleName Simple module name without path, prefix, or suffix.
*/
std::shared_ptr<LoadedDeviceAdapter>
CPluginManager::GetDeviceAdapter(const std::string& moduleName)
{
if (moduleName.empty())
{
throw CMMError("Empty device adapter module name");
}
std::map< std::string, std::shared_ptr<LoadedDeviceAdapter> >::iterator it =
moduleMap_.find(moduleName);
if (it != moduleMap_.end())
{
return it->second;
}
std::string filename(LIB_NAME_PREFIX);
filename += moduleName;
filename += LIB_NAME_SUFFIX;
filename = FindInSearchPath(filename);
auto module = [&] {
try {
auto impl = std::make_unique<LoadedDeviceAdapterImplRegular>(filename);
return std::make_shared<LoadedDeviceAdapter>(moduleName, std::move(impl));
}
catch (const CMMError& e) {
throw CMMError("Failed to load device adapter " + ToQuotedString(moduleName) +
" from " + ToQuotedString(filename), e);
}
}();
moduleMap_[moduleName] = module;
return module;
}
std::shared_ptr<LoadedDeviceAdapter>
CPluginManager::GetDeviceAdapter(const char* moduleName)
{
if (!moduleName)
{
throw CMMError("Null device adapter module name");
}
return GetDeviceAdapter(std::string(moduleName));
}
void
CPluginManager::LoadMockAdapter(const std::string& name, MockDeviceAdapter* impl)
{
if (name.empty())
{
throw CMMError("Empty device adapter module name");
}
auto it = moduleMap_.find(name);
if (it != moduleMap_.end())
{
throw CMMError("Device adapter with name " + ToQuotedString(name) + " is already loaded");
}
moduleMap_[name] = std::make_shared<LoadedDeviceAdapter>(
name, std::make_unique<LoadedDeviceAdapterImplMock>(impl));
}
/**
* Unload a module.
*/
void
CPluginManager::UnloadPluginLibrary(const char* moduleName)
{
std::map< std::string, std::shared_ptr<LoadedDeviceAdapter> >::iterator it =
moduleMap_.find(moduleName);
if (it == moduleMap_.end())
throw CMMError("No device adapter named " + ToQuotedString(moduleName));
try
{
it->second->Unload();
}
catch (const CMMError& e)
{
throw CMMError("Cannot unload device adapter " + ToQuotedString(moduleName), e);
}
}
// TODO Use std::filesystem instead of this.
// This stop-gap implementation makes the assumption that the argument is in
// the format that could be returned from mmcore::internal::GetPathOfThisModule()
// (e.g. no trailing slashes; real filename present).
static std::string
GetDirName(const std::string& path)
{
#ifdef _WIN32
const char* pathSep = "\\/";
#else
const char* pathSep = "/";
#endif
size_t slashPos = path.find_last_of(pathSep);
if (slashPos == std::string::npos)
{
// No slash in path, but we assume it is a real filename
return ".";
}
if (slashPos == 0 && path[0] == '/') // Unix root dir
return "/";
return path.substr(0, slashPos);
}
std::vector<std::string>
CPluginManager::GetDefaultSearchPaths()
{
static std::vector<std::string> paths;
static bool initialized = false;
if (!initialized)
{
try
{
std::string coreModulePath = GetPathOfThisModule();
std::string coreModuleDir = GetDirName(coreModulePath);
paths.push_back(coreModuleDir);
}
catch (const CMMError&)
{
// TODO Log warning.
}
initialized = true;
}
return paths;
}
/**
* List all modules (device libraries) at a given path.
*/
void
CPluginManager::GetModules(std::vector<std::string> &modules, const char* searchPath)
{
#ifdef _WIN32
std::string path = searchPath;
path += "\\";
path += LIB_NAME_PREFIX;
path += "*.dll";
// Use _findfirst(), _findnext(), and _findclose() from Microsoft C library
struct _finddata_t moduleFile;
intptr_t hSearch;
hSearch = _findfirst(path.c_str(), &moduleFile);
if (hSearch != -1L) // Match found
{
do {
// remove prefix and suffix
std::string strippedName = std::string(moduleFile.name).substr(strlen(LIB_NAME_PREFIX));
strippedName = strippedName.substr(0, strippedName.find_first_of("."));
modules.push_back(strippedName);
} while (_findnext(hSearch, &moduleFile) == 0);
_findclose(hSearch);
}
#else // UNIX
DIR *dp;
struct dirent *dirp;
if ((dp = opendir(searchPath)) != NULL)
{
while ((dirp = readdir(dp)) != NULL)
{
const char* dir_name = dirp->d_name;
if (strncmp(dir_name, LIB_NAME_PREFIX, strlen(LIB_NAME_PREFIX)) == 0
#ifdef __linux__
&& strncmp(&dir_name[strlen(dir_name) - strlen(LIB_NAME_SUFFIX)], LIB_NAME_SUFFIX, strlen(LIB_NAME_SUFFIX)) == 0)
#else // OS X
&& strchr(&dir_name[strlen(dir_name) - strlen(LIB_NAME_SUFFIX)], '.') == NULL)
#endif
{
// remove prefix and suffix
std::string strippedName = std::string(dir_name).substr(strlen(LIB_NAME_PREFIX));
strippedName = strippedName.substr(0, strippedName.length() - strlen(LIB_NAME_SUFFIX));
modules.push_back(strippedName);
}
}
closedir(dp);
}
#endif // UNIX
}
/**
* List all modules (device libraries) in all search paths.
*/
std::vector<std::string>
CPluginManager::GetAvailableDeviceAdapters()
{
std::vector<std::string> modules;
for (const auto& path : searchPaths_)
GetModules(modules, path.c_str());
// Check for duplicates
// XXX Is this the right place to be doing this checking? Shouldn't it be an
// error to have duplicates even if we're not listing all libraries?
std::set<std::string> moduleSet;
for (std::vector<std::string>::const_iterator it = modules.begin(), end = modules.end(); it != end; ++it) {
if (moduleSet.count(*it)) {
std::string msg("Duplicate libraries found with name \"" + *it + "\"");
throw CMMError(msg.c_str(), DEVICE_DUPLICATE_LIBRARY);
}
}
return modules;
}
} // namespace internal
} // namespace mmcore