1/********************************************************************
2 * Copyright (c) 2013 - 2014, Pivotal Inc.
3 * All rights reserved.
4 *
5 * Author: Zhanwei Wang
6 ********************************************************************/
7/********************************************************************
8 * 2014 -
9 * open source under Apache License Version 2.0
10 ********************************************************************/
11/**
12 * Licensed to the Apache Software Foundation (ASF) under one
13 * or more contributor license agreements. See the NOTICE file
14 * distributed with this work for additional information
15 * regarding copyright ownership. The ASF licenses this file
16 * to you under the Apache License, Version 2.0 (the
17 * "License"); you may not use this file except in compliance
18 * with the License. You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28#ifndef _HDFS_LIBHDFS3_COMMON_XMLCONFIG_H_
29#define _HDFS_LIBHDFS3_COMMON_XMLCONFIG_H_
30
31#include <stdint.h>
32#include <string>
33#include <sstream>
34#include <map>
35
36namespace Hdfs {
37
38/**
39 * A configure file parser.
40 */
41class Config {
42public:
43 /**
44 * Construct a empty Config instance.
45 */
46 Config() {
47 }
48
49 /**
50 * Construct a Config with given configure file.
51 * @param path The path of configure file.
52 * @throw HdfsBadConfigFoumat
53 */
54 Config(const char * path);
55
56 /**
57 * Parse the configure file.
58 * @throw HdfsBadConfigFoumat
59 */
60 void update(const char * path);
61
62 /**
63 * Get a string with given configure key.
64 * @param key The key of the configure item.
65 * @return The value of configure item.
66 * @throw HdfsConfigNotFound
67 */
68 const char * getString(const char * key) const;
69
70 /**
71 * Get a string with given configure key.
72 * Return the default value def if key is not found.
73 * @param key The key of the configure item.
74 * @param def The defalut value.
75 * @return The value of configure item.
76 */
77 const char * getString(const char * key, const char * def) const;
78
79 /**
80 * Get a string with given configure key.
81 * @param key The key of the configure item.
82 * @return The value of configure item.
83 * @throw HdfsConfigNotFound
84 */
85 const char * getString(const std::string & key) const;
86
87 /**
88 * Get a string with given configure key.
89 * Return the default value def if key is not found.
90 * @param key The key of the configure item.
91 * @param def The defalut value.
92 * @return The value of configure item.
93 */
94 const char * getString(const std::string & key,
95 const std::string & def) const;
96
97 /**
98 * Get a 64 bit integer with given configure key.
99 * @param key The key of the configure item.
100 * @return The value of configure item.
101 * @throw HdfsConfigNotFound
102 */
103 int64_t getInt64(const char * key) const;
104
105 /**
106 * Get a 64 bit integer with given configure key.
107 * Return the default value def if key is not found.
108 * @param key The key of the configure item.
109 * @param def The defalut value.
110 * @return The value of configure item.
111 */
112 int64_t getInt64(const char * key, int64_t def) const;
113
114 /**
115 * Get a 32 bit integer with given configure key.
116 * @param key The key of the configure item.
117 * @return The value of configure item.
118 * @throw HdfsConfigNotFound
119 */
120 int32_t getInt32(const char * key) const;
121
122 /**
123 * Get a 32 bit integer with given configure key.
124 * Return the default value def if key is not found.
125 * @param key The key of the configure item.
126 * @param def The defalut value.
127 * @return The value of configure item.
128 */
129 int32_t getInt32(const char * key, int32_t def) const;
130
131 /**
132 * Get a double with given configure key.
133 * @param key The key of the configure item.
134 * @return The value of configure item.
135 * @throw HdfsConfigNotFound
136 */
137 double getDouble(const char * key) const;
138
139 /**
140 * Get a double with given configure key.
141 * Return the default value def if key is not found.
142 * @param key The key of the configure item.
143 * @param def The defalut value.
144 * @return The value of configure item.
145 */
146 double getDouble(const char * key, double def) const;
147
148 /**
149 * Get a boolean with given configure key.
150 * @param key The key of the configure item.
151 * @return The value of configure item.
152 * @throw HdfsConfigNotFound
153 */
154 bool getBool(const char * key) const;
155
156 /**
157 * Get a boolean with given configure key.
158 * Return the default value def if key is not found.
159 * @param key The key of the configure item.
160 * @param def The default value.
161 * @return The value of configure item.
162 */
163 bool getBool(const char * key, bool def) const;
164
165 /**
166 * Set a configure item
167 * @param key The key will set.
168 * @param value The value will be set to.
169 */
170 template<typename T>
171 void set(const char * key, T const & value) {
172 std::stringstream ss;
173 ss.imbue(std::locale::classic());
174 ss << value;
175 kv[key] = ss.str();
176 }
177
178 /**
179 * Get the hash value of this object
180 *
181 * @return The hash value
182 */
183 size_t hash_value() const;
184
185private:
186 std::string path;
187 std::map<std::string, std::string> kv;
188};
189
190}
191
192#endif /* _HDFS_LIBHDFS3_COMMON_XMLCONFIG_H_ */
193