2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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.
20 #include <ContextProvider.h>
21 #include <ProviderList.h>
22 #include "ProviderLoader.h"
24 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
28 typedef ContextProvider* (*create_t)(const char *subject);
30 std::map<const char*, const char*, CompareSubjectName> ProviderLoader::__providerLibMap;
32 ProviderLoader::ProviderLoader() :
37 ProviderLoader::~ProviderLoader()
42 ContextProvider* ProviderLoader::load(const char *subject)
44 ProviderLibMap::iterator it = __providerLibMap.find(subject);
45 if (it == __providerLibMap.end()) {
46 _W("No provider for '%s'", subject);
50 std::string path = LIB_DIRECTORY;
51 path = path + LIB_PREFIX + it->second + LIB_EXTENSION;
53 return __load(path.c_str(), subject);
56 ContextProvider* ProviderLoader::__load(const char *soPath, const char *subject)
58 _SI("Load '%s' from '%s'", subject, soPath);
60 __soHandle = dlopen(soPath, RTLD_LAZY | RTLD_GLOBAL);
61 IF_FAIL_RETURN_TAG(__soHandle, NULL, _E, "%s", dlerror());
63 create_t create = reinterpret_cast<create_t>(dlsym(__soHandle, "create"));
71 ContextProvider *prvd = create(subject);
73 _W("No provider for '%s'", subject);
82 void ProviderLoader::__unload()
91 bool ProviderLoader::init()
93 int size = ARRAY_SIZE(subjectLibraryList);
95 for (int i = 0; i < size; ++i) {
96 __providerLibMap[subjectLibraryList[i].subject] = subjectLibraryList[i].library;
97 _SD("'%s' -> '%s'", subjectLibraryList[i].subject, subjectLibraryList[i].library);
103 bool ProviderLoader::popTriggerTemplate(std::string &subject, int &operation, Json &attribute, Json &option)
106 static int size = ARRAY_SIZE(triggerTemplateList);
111 subject = triggerTemplateList[i].subject;
112 operation = triggerTemplateList[i].operation;
113 attribute = triggerTemplateList[i].attribute;
114 option = triggerTemplateList[i].option;