Added Plugin Manager Configuration Feature
[platform/upstream/iotivity.git] / service / protocol-plugin / plugin-manager / src / Config.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /// @file Config.cpp
22
23 /// @brief
24
25
26 #include "Config.h"
27
28
29 using namespace OIC;
30 using namespace rapidxml;
31 using namespace std;
32
33 Config *Config::s_configinstance = NULL;
34
35 Config::Config()
36 {
37     if (loadConfigFile("./pluginmanager.xml") != PM_S_OK)
38     {
39         fprintf(stderr, "PM Configuration file is not exist current Folder.\n" );
40         exit(EXIT_FAILURE);
41     }
42 }
43
44 Config::~Config(void)
45 {
46     s_configinstance->deleteinstance();
47     s_configinstance = NULL;
48 }
49
50 PMRESULT Config::loadConfigFile(const std::string configfilepath)
51 {
52     // Read the xml file
53     std::ifstream xmlFile(configfilepath.c_str());
54     xml_document<> doc;
55     //into a vector
56     std::vector<char> buffer((istreambuf_iterator<char>(xmlFile)), istreambuf_iterator<char>());
57     buffer.push_back('\0');
58
59     // Parse the buffer using the xml file parsing library into doc
60     parsing(buffer, &doc);
61
62     // Find our root node
63     xml_node<> *root_node = doc.first_node("pluginManager");
64     xml_node<> *pluginInfo = root_node->first_node("pluginInfo");
65
66     getXmlData(pluginInfo, "pluginPath");
67     getXmlData(pluginInfo, "maxMEM");
68     getXmlData(pluginInfo, "version");
69     getXmlData(pluginInfo, "name");
70
71     return PM_S_OK;
72 }
73
74 PMRESULT Config::parsing(std::vector<char> buffer, xml_document<> *doc)
75 {
76     // Parse the buffer using the xml file parsing library into doc
77     try
78     {
79         doc->parse<0>(&buffer[0]);
80     }
81     catch (rapidxml::parse_error err)
82     {
83         //print errors to screen
84         fprintf(stderr, "PM Configuration file parsing error \n");
85         exit(EXIT_FAILURE); //then exit
86     }
87     return PM_S_OK;
88 }
89
90 PMRESULT Config::getXmlData(xml_node<> *pluginInfo, std::string key)
91 {
92     xml_attribute<> *iAttr = NULL;
93     std::string value  = "";
94     if (iAttr = pluginInfo->first_attribute(key.c_str()))
95     {
96         value = iAttr->value();
97         setValue(key, value);
98         return PM_S_OK;
99     }
100     else
101     {
102         return PM_S_FALSE;
103     }
104 }
105
106 void Config::setValue(const std::string key, const std::string value)
107 {
108     m_configurationMap.insert( std::pair<std::string, std::string>(key, value));
109 }
110
111 std::string  Config::getValue(const std::string key)
112 {
113     std::map<std::string, std::string>::iterator m_iterator;
114
115     m_iterator = m_configurationMap.find(key.c_str());
116
117     if (m_iterator != m_configurationMap.end())
118     {
119         return m_iterator->second;
120     }
121     else
122     {
123         return "";
124     }
125 }
126
127 std::string  Config::getVersion()
128 {
129     std::map<std::string, std::string>::iterator m_iterator;
130
131     m_iterator = m_configurationMap.find("version");
132
133     if (m_iterator != m_configurationMap.end())
134     {
135         return m_iterator->second;
136     }
137     else
138     {
139         return "";
140     }
141 }
142
143 std::string  Config::getPluginPath()
144 {
145     std::map<std::string, std::string>::iterator m_iterator;
146
147     m_iterator = m_configurationMap.find("pluginPath");
148
149     if (m_iterator != m_configurationMap.end())
150     {
151         return m_iterator->second;
152     }
153     else
154     {
155         return "";
156     }
157 }