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
|
/***************************************************************************
*
* Copyright 2012 BMW Car IT GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
****************************************************************************/
#include "PluginManager.h"
#include "Configuration.h"
#include "Log.h"
#include "IPlugin.h"
#include <IScene.h>
#include <dirent.h>
#include <errno.h>
#include <dlfcn.h>
#include <stdlib.h>
StaticPluginCreateFuncList PluginManager::mStaticPluginCreateFuncList;
//===========================================================================
// register statically linked plugins (configured by build system)
//===========================================================================
STATIC_PLUGIN_REGISTRATION
//===========================================================================
// class implementation
//===========================================================================
PluginManager::PluginManager(ICommandExecutor& executor, Configuration& config)
: mExecutor(executor)
, mConfiguration(config)
{
LOG_DEBUG("PluginManager", "loading plugins from " << mConfiguration.getPluginPath());
// create static plugins
createStaticallyLinkedPlugins();
#ifndef WITH_STATIC_LIBRARIES
// create dynamic plugins
getAllFilesInPluginPath(mConfiguration.getPluginPath());
createDynamicallyLinkedPlugins();
#endif
LOG_INFO("PluginManager", "created " << mPluginList.size() << " plugins");
}
PluginManager::~PluginManager()
{
PluginList::iterator iter = mPluginList.begin();
PluginList::iterator iterEnd = mPluginList.end();
for (; iter != iterEnd; ++iter)
{
IPlugin* plugin = *iter;
if (plugin)
{
LOG_INFO("PluginManager", "plugin " << plugin->pluginGetName() << " destroyed");
delete plugin;
}
}
mPluginList.clear();
}
void PluginManager::getRendererList(RendererList& list)
{
PluginList::const_iterator iter = mPluginList.begin();
PluginList::const_iterator iterEnd = mPluginList.end();
for (; iter != iterEnd; ++iter)
{
IPlugin* plugin = *iter;
ilmPluginApi api = plugin->pluginGetApi();
if (PLUGIN_IS_RENDERER(api))
{
IRenderer* renderer = dynamic_cast<IRenderer*>(plugin);
list.push_back(renderer);
}
}
}
void PluginManager::getHealthMonitorList(HealthMonitorList& list)
{
PluginList::const_iterator iter = mPluginList.begin();
PluginList::const_iterator iterEnd = mPluginList.end();
for (; iter != iterEnd; ++iter)
{
IPlugin* plugin = *iter;
ilmPluginApi api = plugin->pluginGetApi();
if (PLUGIN_IS_HEALTHMONITOR(api))
{
IHealthMonitor* monitor = dynamic_cast<IHealthMonitor*>(plugin);
if (monitor)
{
list.push_back(monitor);
}
}
}
}
void PluginManager::getSceneProviderList(SceneProviderList& list)
{
PluginList::const_iterator iter = mPluginList.begin();
PluginList::const_iterator iterEnd = mPluginList.end();
for (; iter != iterEnd; ++iter)
{
IPlugin* plugin = *iter;
ilmPluginApi api = plugin->pluginGetApi();
if (PLUGIN_IS_SCENEPROVIDER(api))
{
ISceneProvider* sceneprovider = dynamic_cast<ISceneProvider*>(plugin);
list.push_back(sceneprovider);
}
}
}
void PluginManager::getCommunicatorList(CommunicatorList& list)
{
PluginList::const_iterator iter = mPluginList.begin();
PluginList::const_iterator iterEnd = mPluginList.end();
for (; iter != iterEnd; ++iter)
{
IPlugin* plugin = *iter;
ilmPluginApi api = plugin->pluginGetApi();
if (PLUGIN_IS_COMMUNICATOR(api))
{
ICommunicator* comm = dynamic_cast<ICommunicator*>(plugin);
list.push_back(comm);
}
}
}
bool PluginManager::registerStaticPluginCreateFunction(StaticPluginCreateFunc func)
{
bool result = false;
if (func)
{
mStaticPluginCreateFuncList.push_back(func);
result = true;
}
return result;
}
void PluginManager::createStaticallyLinkedPlugins()
{
StaticPluginCreateFuncList::iterator iter = mStaticPluginCreateFuncList.begin();
StaticPluginCreateFuncList::iterator iterEnd = mStaticPluginCreateFuncList.end();
for (; iter != iterEnd; ++iter)
{
StaticPluginCreateFunc func = *iter;
IPlugin* plugin = (*func)(mExecutor, mConfiguration);
LOG_INFO("PluginManager", "creating plugin " << plugin->pluginGetName() << " (static linking)");
mPluginList.push_back(plugin);
}
}
void PluginManager::getAllFilesInPluginPath(std::string path)
{
DIR *directory = opendir(path.c_str());
struct dirent *itemInDirectory = 0;
while (directory && (itemInDirectory = readdir(directory)))
{
std::string entryName = itemInDirectory->d_name;
std::string fullPath = path + "/" + entryName;
if (entryName.at(0) == '.')
{
continue;
}
mFileList.push_back(fullPath);
LOG_DEBUG("PluginManager", "considering " << fullPath);
getAllFilesInPluginPath(fullPath);
}
closedir(directory);
}
void PluginManager::createDynamicallyLinkedPlugins()
{
FileList::const_iterator iter = mFileList.begin();
FileList::const_iterator iterEnd = mFileList.end();
for (; iter != iterEnd; ++iter)
{
IPlugin* plugin = createDynamicallyLinkedPlugin(*iter);
if (plugin)
{
mPluginList.push_back(plugin);
}
}
}
IPlugin* PluginManager::createDynamicallyLinkedPlugin(std::string path)
{
IPlugin* returnValue = NULL;
// open library
void *libraryHandle;
dlerror(); // Clear any existing error
libraryHandle = dlopen(path.c_str(), RTLD_NOW | RTLD_GLOBAL);
const char* dlopen_error = dlerror();
if (dlopen_error)
{
return NULL;
}
// load entry point
union
{
void* data;
IPlugin* (*createFunc)(ICommandExecutor&, Configuration&);
} convertUnion;
int cutBegin = path.find_last_of('/') + 4; // remove '*/lib' from name
int cutEnd = path.find_first_of('.', cutBegin); // remove '.extension' from name
std::string createFunctionName = "create";
createFunctionName += path.substr(cutBegin, cutEnd - cutBegin);
convertUnion.data = dlsym(libraryHandle, createFunctionName.c_str());
// create plugin instance from entry point
const char* dlsym_error = dlerror();
if (convertUnion.data && !dlsym_error)
{
returnValue = convertUnion.createFunc(mExecutor, mConfiguration);
LOG_INFO("PluginManager", "creating plugin " << returnValue->pluginGetName() << " (dynamic linking)");
}
else
{
LOG_DEBUG("PluginManager", "not a valid Plugin: " << path << ": " << dlsym_error);
dlclose(libraryHandle);
returnValue = NULL;
}
return returnValue;
}
|