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 LogError("Error: " << dlerror());
90 ExportedApi* entryPoint =
91 static_cast<ExportedApi*>
92 (dlsym(handle, GetExportedSymbolName()));
93 if (NULL == entryPoint) {
94 LogError("Error: " << dlerror());
97 if (entryPoint->Unregister == NULL) {
98 LogError("Error Unregister function not set");
102 PluginRegistration registration(
103 new PluginRegistration::Impl(*this, libraryIt->first));
105 entryPoint->Unregister(registration, (pluginIt->second));
107 m_plugins.erase(pluginIt);
109 dlclose(libraryIt->second);
113 bool PluginRegistry::LoadFromFile(const std::string& libraryName)
115 void* handle = dlopen(libraryName.c_str(), RTLD_NOW);
117 LogError("Error: " << dlerror());
120 m_libraries[libraryName] = handle;
122 ExportedApi* entryPoint =
123 static_cast<ExportedApi*>(dlsym(handle, GetExportedSymbolName()));
124 if (NULL == entryPoint) {
125 LogError("Error: " << dlerror());
129 if (entryPoint->Register == NULL) {
130 LogError("Error Register function not set");
133 if (entryPoint->Unregister == NULL) {
134 LogError("Error Unregister function not set");
138 PluginRegistration registration(
139 new PluginRegistration::Impl(*this, libraryName));
140 entryPoint->Register(registration);
145 PluginRegistry::~PluginRegistry()
147 //TODO discuss ... when the unload should be called