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/foreach.h>
36 namespace WrtPluginsApi {
37 void PluginRegistry::AddPlugin(const std::string& libraryName,
40 LogDebug("Adding plugin for library: " << libraryName);
42 auto libraryIt = m_plugins.find(libraryName);
43 if (m_plugins.end() == libraryIt) {
44 m_plugins[libraryName] = &plugin;
48 Plugin* PluginRegistry::GetPlugin(const std::string& libraryName)
50 auto it = m_plugins.find(libraryName);
51 if (it == m_plugins.end()) {
52 if (!LoadFromFile(libraryName)) {
53 LogError("Failed to load lib" << libraryName);
54 ThrowMsg(PluginNotFound, "Failed to load plugin");
57 return m_plugins[libraryName];
63 void PluginRegistry::RemovePlugin(const std::string& libraryName,
66 auto it = m_plugins.find(libraryName);
67 if (it != m_plugins.end()) {
68 if (&plugin == it->second) {
74 void PluginRegistry::UnloadAll()
76 LogDebug("Unload all plugins");
78 typedef void (*UnregisterFunction)(PluginRegistration&, Plugin&);
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"));
91 ExportedApi* entryPoint =
92 static_cast<ExportedApi*>
93 (dlsym(handle, GetExportedSymbolName()));
94 if (NULL == entryPoint) {
95 const char* error = (const char*)dlerror();
96 LogError("Error: " << (error != NULL ? error : "unknown"));
99 if (entryPoint->Unregister == NULL) {
100 LogError("Error Unregister function not set");
104 PluginRegistration registration(
105 new PluginRegistration::Impl(*this, libraryIt->first));
107 entryPoint->Unregister(registration, (pluginIt->second));
109 m_plugins.erase(pluginIt);
111 dlclose(libraryIt->second);
115 bool PluginRegistry::LoadFromFile(const std::string& libraryName)
117 void* handle = dlopen(libraryName.c_str(), RTLD_NOW);
119 const char* error = (const char*)dlerror();
120 LogError("Error: " << (error != NULL ? error : "unknown"));
123 m_libraries[libraryName] = handle;
125 ExportedApi* entryPoint =
126 static_cast<ExportedApi*>(dlsym(handle, GetExportedSymbolName()));
127 if (NULL == entryPoint) {
128 const char* error = (const char*)dlerror();
129 LogError("Error: " << (error != NULL ? error : "unknown"));
133 if (entryPoint->Register == NULL) {
134 LogError("Error Register function not set");
137 if (entryPoint->Unregister == NULL) {
138 LogError("Error Unregister function not set");
142 PluginRegistration registration(
143 new PluginRegistration::Impl(*this, libraryName));
144 entryPoint->Register(registration);
149 PluginRegistry::~PluginRegistry()
151 //TODO discuss ... when the unload should be called