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>
38 const char* DLL_ENTRY_POINT_NAME = "dll_api";
41 namespace WrtPluginsApi
44 void PluginRegistry::AddPlugin(const std::string& libraryName,
47 LogDebug("Adding plugin for library: " << libraryName);
49 auto libraryIt = m_plugins.find(libraryName);
50 if (m_plugins.end() == libraryIt)
52 m_plugins[libraryName] = &plugin;
56 Plugin* PluginRegistry::GetPlugin(const std::string& libraryName)
58 auto it = m_plugins.find(libraryName);
59 if (it == m_plugins.end())
61 if (!LoadFromFile(libraryName))
63 LogError("Failed to load lib" << libraryName);
64 ThrowMsg(PluginNotFound, "Failed to load plugin");
67 return m_plugins[libraryName];
73 void PluginRegistry::RemovePlugin(const std::string& libraryName,
76 auto it = m_plugins.find(libraryName);
77 if (it != m_plugins.end())
79 if (&plugin == it->second)
86 void PluginRegistry::UnloadAll()
88 LogDebug("Unload all plugins");
90 typedef void (*UnregisterFunction) (PluginRegistration&, Plugin&);
92 FOREACH(libraryIt, m_libraries)
94 auto pluginIt = m_plugins.find(libraryIt->first);
95 if (m_plugins.end() != pluginIt)
97 void* handle = dlopen(libraryIt->first.c_str(), RTLD_NOW);
99 LogError("Error: " << dlerror());
103 ExportedApi* entryPoint =
104 static_cast<ExportedApi*>
105 (dlsym(handle, GetExportedSymbolName()));
106 if (NULL == entryPoint)
108 LogError("Error: " << dlerror());
111 if (entryPoint->Unregister == NULL)
113 LogError("Error Unregister function not set");
117 PluginRegistration registration(
118 new PluginRegistration::Impl(*this, libraryIt->first));
120 entryPoint->Unregister(registration, (pluginIt->second));
122 m_plugins.erase(pluginIt);
124 dlclose(libraryIt->second);
128 bool PluginRegistry::LoadFromFile(const std::string& libraryName)
130 void* handle = dlopen(libraryName.c_str(), RTLD_NOW);
132 LogError("Error: " << dlerror());
135 m_libraries[libraryName] = handle;
137 ExportedApi* entryPoint =
138 static_cast<ExportedApi*>(dlsym(handle, GetExportedSymbolName()));
139 if (NULL == entryPoint)
141 LogError("Error: " << dlerror());
145 if (entryPoint->Register == NULL)
147 LogError("Error Register function not set");
150 if (entryPoint->Unregister== NULL)
152 LogError("Error Unregister function not set");
156 PluginRegistration registration(
157 new PluginRegistration::Impl(*this, libraryName));
158 entryPoint->Register(registration);
164 PluginRegistry::~PluginRegistry()
166 //TODO discuss ... when the unload should be called