2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file PluginRegistry.h
18 * @author Grzegorz Krawczyk (g.krawczyk@samgsung.com)
22 #include "PluginRegistry.h"
23 #include "PluginRegistration.h"
24 #include "PluginRegistrationImpl.h"
25 #include "ExportedApi.h"
33 #include <dpl/log/log.h>
34 #include <dpl/log/secure_log.h>
35 #include <dpl/foreach.h>
36 #include <dpl/scope_guard.h>
38 namespace WrtPluginsApi {
39 void PluginRegistry::AddPlugin(const std::string& libraryName,
42 LogDebug("Adding plugin for library: " << libraryName);
44 auto libraryIt = m_plugins.find(libraryName);
45 if (m_plugins.end() == libraryIt) {
46 m_plugins[libraryName] = &plugin;
50 Plugin* PluginRegistry::GetPlugin(const std::string& libraryName)
52 auto it = m_plugins.find(libraryName);
53 if (it == m_plugins.end()) {
54 if (!LoadFromFile(libraryName)) {
55 LogError("Failed to load lib" << libraryName);
56 ThrowMsg(PluginNotFound, "Failed to load plugin");
59 return m_plugins[libraryName];
65 void PluginRegistry::RemovePlugin(const std::string& libraryName,
68 auto it = m_plugins.find(libraryName);
69 if (it != m_plugins.end()) {
70 if (&plugin == it->second) {
76 void PluginRegistry::UnloadAll()
78 LogDebug("Unload all plugins");
80 FOREACH(libraryIt, m_libraries)
82 auto pluginIt = m_plugins.find(libraryIt->first);
83 if (m_plugins.end() != pluginIt) {
84 void* handle = dlopen(libraryIt->first.c_str(), RTLD_NOW);
86 const char* error = (const char*)dlerror();
87 LogError("Error: " << (error != NULL ? error : "unknown"));
90 DPL_SCOPE_EXIT(handle) {
91 if (dlclose(handle) != 0) {
92 const char* error = dlerror();
95 std::string errstr{error};
96 _E("%s", errstr.c_str());
100 _E("unknown error while closing plug-in library");
105 ExportedApi* entryPoint =
106 static_cast<ExportedApi*>
107 (dlsym(handle, GetExportedSymbolName()));
108 if (NULL == entryPoint) {
109 const char* error = (const char*)dlerror();
110 LogError("Error: " << (error != NULL ? error : "unknown"));
113 if (entryPoint->Unregister == NULL) {
114 LogError("Error Unregister function not set");
118 PluginRegistration registration(
119 new PluginRegistration::Impl(*this, libraryIt->first));
121 entryPoint->Unregister(registration, (pluginIt->second));
123 m_plugins.erase(pluginIt);
125 dlclose(libraryIt->second);
129 bool PluginRegistry::LoadFromFile(const std::string& libraryName)
131 void* handle = dlopen(libraryName.c_str(), RTLD_NOW);
133 const char* error = (const char*)dlerror();
134 LogError("Error: " << (error != NULL ? error : "unknown"));
137 m_libraries[libraryName] = handle;
139 ExportedApi* entryPoint =
140 static_cast<ExportedApi*>(dlsym(handle, GetExportedSymbolName()));
141 if (NULL == entryPoint) {
142 const char* error = (const char*)dlerror();
143 LogError("Error: " << (error != NULL ? error : "unknown"));
147 if (entryPoint->Register == NULL) {
148 LogError("Error Register function not set");
151 if (entryPoint->Unregister == NULL) {
152 LogError("Error Unregister function not set");
156 PluginRegistration registration(
157 new PluginRegistration::Impl(*this, libraryName));
158 entryPoint->Register(registration);
163 PluginRegistry::~PluginRegistry()
165 //TODO discuss ... when the unload should be called