rad  4.0.0
config.hpp
Go to the documentation of this file.
1 
9 #ifndef EXCIISERVER_CONFIG_HPP
10 #define EXCIISERVER_CONFIG_HPP
11 
12 #include "logger.hpp"
13 
14 #include <rad/assert.hpp>
15 #include <rad/exceptions.hpp>
16 
17 #include <config-ng/ciiConfigApi.hpp>
18 
19 #include <string>
20 #include <memory>
21 #include <chrono>
22 
23 namespace exciiserver {
24 
25 const char KEY_SEPARATOR_OLDB = '/';
26 const char KEY_SEPARATOR_CFG = '.';
27 const std::string CONFIG_ROOT_NODE = "app";
28 
32 const std::string CONFIG_DEFAULT_VERSION = std::string(VERSION);
33 const std::string CONFIG_DEFAULT_PROCNAME = "exciiserver";
34 //const std::string CONFIG_DEFAULT_FILENAME = ""; // by default use hard-coded values
35 const std::string CONFIG_DEFAULT_FILENAME = "exciiserver/config.yaml"; // by default use config file values
36 const std::string CONFIG_DEFAULT_SCXML_FILENAME = "exciiserver/sm.xml";
37 const std::string CONFIG_DEFAULT_LOG_LEVEL = "INFO";
38 const std::string CONFIG_DEFAULT_LOG_PROPERTIES = "exciiserver/log.properties";
39 const std::string CONFIG_DEFAULT_REQ_ENDPOINT = "zpb.rr://127.0.0.1:12081/";
40 const std::string CONFIG_DEFAULT_OLDB_URI_PREFIX = "cii.oldb:/elt/";
41 const int CONFIG_DEFAULT_OLDB_CONN_TIMEOUT = 2; // sec
42 
43 
48 class Config {
49  public:
57  Config();
58 
62  virtual ~Config();
63 
64  const elt::configng::CiiConfigDocument& GetConfigDoc() { return *m_config_doc; }
65 
74  bool ParseOptionsAndLoad(int argc, char* argv[]);
75 
83  void LoadConfig(const std::string& filename = "");
84 
94  void ConfigureLogging(const std::string& filename = "");
95 
103  //void MergeConfig(const elt::configng::CiiConfigInstanceNamespace& instance);
104  void MergeConfig(const elt::configng::CiiConfigDocument& cfg_doc);
105 
116  std::string ConvertKey(const std::string& key,
117  const char src_separator,
118  const char dst_separator);
119 
127  template<typename T>
128  T GetParam(const std::string& key,
129  const char separator = KEY_SEPARATOR_OLDB);
130 
138  template<typename T>
139  void SetParam(const std::string& key, const T& value,
140  const char separator = KEY_SEPARATOR_OLDB);
141 
146  template<typename T>
147  void AddParam(const std::string& key, const T& value,
148  const char separator = KEY_SEPARATOR_OLDB);
149 
155  void CreateDefaultConfig();
156  void CreateDefaultConfig(const std::string& default_cfg);
157 
162  std::string PrintNode(const elt::configng::CiiConfigInstanceNode& node,
163  const std::string& indentation);
164 
169  std::string Print(const elt::configng::CiiConfigInstanceNamespace& instances);
170 
176  std::string Print();
177 
178  Config(const Config&) = delete;
179  Config& operator=(const Config&) = delete;
180 
181  private:
182  /*
183  * We need to declare CiiConfigDocument as pointer since it logs info
184  * in the constructor and therefore uses [CII] logging before it has
185  * been properly configured.
186  */
187  std::unique_ptr<elt::configng::CiiConfigDocument> m_config_doc;
188 
189  /*
190  * CII Config does not allow (yet) to create an empty configuration
191  * and add config parameters. Therefore we need create CiiConfigDocument
192  * from a stream.
193  * We build the configuration using std::stringstream to be able to use
194  * the << operator.
195  */
196  std::stringstream m_params;
197 };
198 
199 
200 template<typename T>
201 void Config::AddParam(const std::string& key, const T& value, const char separator) {
202  RAD_TRACE(GetLogger());
203 
204  std::string key2 = key;
205  if (separator != KEY_SEPARATOR_CFG) {
206  key2 = ConvertKey(key, separator, KEY_SEPARATOR_CFG);
207  }
208 
209  m_params << " " << key2 << ": " << value << "\n";
210 }
211 
212 template<typename T>
213 T Config::GetParam(const std::string& key, const char separator) {
214  RAD_TRACE(GetLogger());
215 
216  std::string key2 = key;
217  if (separator != KEY_SEPARATOR_CFG) {
218  key2 = ConvertKey(key, separator, KEY_SEPARATOR_CFG);
219  }
220 
221  try {
222  RAD_ASSERTPTR(m_config_doc);
223  auto instance = m_config_doc->Instances();
224  if (instance[CONFIG_ROOT_NODE].Has(key2)) {
225  T val = instance[CONFIG_ROOT_NODE][key2].As<T>();
226  return val;
227  } else {
228  RAD_LOG_THROW(rad::Exception, GetLogger(), "Configuration parameter " +
229  key2 + " does not exists.");
230  }
231  } catch (std::exception& e) {
232  RAD_LOG_THROW(rad::Exception, GetLogger(), "Reading configuration parameter <" +
233  key2 + ">: " + std::string(e.what()));
234  }
235 }
236 
237 template<typename T>
238 void Config::SetParam(const std::string& key, const T& value, const char separator) {
239  RAD_TRACE(GetLogger());
240 
241  std::string key2 = key;
242  if (separator != KEY_SEPARATOR_CFG) {
243  key2 = ConvertKey(key, separator, KEY_SEPARATOR_CFG);
244  }
245 
246  try {
247  RAD_ASSERTPTR(m_config_doc);
248  auto instance = m_config_doc->Instances();
249  instance[CONFIG_ROOT_NODE][key2] = value;
250  } catch (std::exception& e) {
251  LOG4CPLUS_ERROR(GetLogger(), "Writing configuration parameter <"
252  << key2 << ">: " << e.what());
253  }
254 }
255 
256 } // namespace exciiserver
257 
258 #endif // EXCIISERVER_CONFIG_HPP
exceptions.hpp
Exception classes header file.
exciiserver::Config::Print
std::string Print()
Definition: config.cpp:341
exciiserver::Config::LoadConfig
void LoadConfig(const std::string &filename="")
Definition: config.cpp:166
exciiserver::CONFIG_ROOT_NODE
const std::string CONFIG_ROOT_NODE
Definition: config.hpp:27
RAD_TRACE
#define RAD_TRACE(logger)
Definition: logger.hpp:24
rad::Exception
Base class for the exceptions thrown by RAD and its users.
Definition: exceptions.hpp:53
logger.hpp
Default logger name.
exciiserver::CONFIG_DEFAULT_LOG_LEVEL
const std::string CONFIG_DEFAULT_LOG_LEVEL
Definition: config.hpp:37
exciiserver::KEY_SEPARATOR_CFG
const char KEY_SEPARATOR_CFG
Definition: config.hpp:26
exciiserver::Config::ConfigureLogging
void ConfigureLogging(const std::string &filename="")
Definition: config.cpp:201
exciiserver::CONFIG_DEFAULT_PROCNAME
const std::string CONFIG_DEFAULT_PROCNAME
Definition: config.hpp:33
exciiserver::CONFIG_DEFAULT_LOG_PROPERTIES
const std::string CONFIG_DEFAULT_LOG_PROPERTIES
Definition: config.hpp:38
exciiserver::Config::~Config
virtual ~Config()
Definition: config.cpp:60
exciiserver::Config::GetConfigDoc
const elt::configng::CiiConfigDocument & GetConfigDoc()
Definition: config.hpp:64
exciiserver::Config::ParseOptionsAndLoad
bool ParseOptionsAndLoad(int argc, char *argv[])
Definition: config.cpp:64
exciiserver::Config
Definition: config.hpp:48
RAD_ASSERTPTR
#define RAD_ASSERTPTR(a)
Definition: assert.hpp:19
exciiserver::Config::PrintNode
std::string PrintNode(const elt::configng::CiiConfigInstanceNode &node, const std::string &indentation)
Definition: config.cpp:282
exciiserver::Config::AddParam
void AddParam(const std::string &key, const T &value, const char separator=KEY_SEPARATOR_OLDB)
Definition: config.hpp:201
exciiserver::CONFIG_DEFAULT_VERSION
const std::string CONFIG_DEFAULT_VERSION
Definition: config.hpp:32
exciiserver::Config::MergeConfig
void MergeConfig(const elt::configng::CiiConfigDocument &cfg_doc)
Definition: config.cpp:223
exciiserver
Definition: actionMgr.cpp:23
exciiserver::CONFIG_DEFAULT_SCXML_FILENAME
const std::string CONFIG_DEFAULT_SCXML_FILENAME
Definition: config.hpp:36
assert.hpp
Assert header file.
RAD_LOG_THROW
#define RAD_LOG_THROW(exceptionType_t, logger, msg)
Throw exception with information about the throw location.
Definition: exceptions.hpp:356
exciiserver::Config::GetParam
T GetParam(const std::string &key, const char separator=KEY_SEPARATOR_OLDB)
Definition: config.hpp:213
exciiserver::GetLogger
log4cplus::Logger & GetLogger()
Definition: logger.cpp:14
exciiserver::Config::SetParam
void SetParam(const std::string &key, const T &value, const char separator=KEY_SEPARATOR_OLDB)
Definition: config.hpp:238
exciiserver::Config::CreateDefaultConfig
void CreateDefaultConfig()
Definition: config.cpp:346
exciiserver::CONFIG_DEFAULT_FILENAME
const std::string CONFIG_DEFAULT_FILENAME
Definition: config.hpp:35
exciiserver::Config::Config
Config()
Definition: config.cpp:25
exciiserver::CONFIG_DEFAULT_OLDB_URI_PREFIX
const std::string CONFIG_DEFAULT_OLDB_URI_PREFIX
Definition: config.hpp:40
exciiserver::Config::Config
Config(const Config &)=delete
exciiserver::CONFIG_DEFAULT_REQ_ENDPOINT
const std::string CONFIG_DEFAULT_REQ_ENDPOINT
Definition: config.hpp:39
exciiserver::CONFIG_DEFAULT_OLDB_CONN_TIMEOUT
const int CONFIG_DEFAULT_OLDB_CONN_TIMEOUT
Definition: config.hpp:41
exciiserver::Config::ConvertKey
std::string ConvertKey(const std::string &key, const char src_separator, const char dst_separator)
Definition: config.cpp:271
exciiserver::Config::operator=
Config & operator=(const Config &)=delete
Disable copy constructor.
exciiserver::KEY_SEPARATOR_OLDB
const char KEY_SEPARATOR_OLDB
Definition: config.hpp:25