softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontainer::ContainerOptionParser Class Reference

Public Member Functions

std::unique_ptr< DynamicContainerOptionsparse (const std::string &config)
 Parse config needed for starting up the container in a correct manner. More...
 

Detailed Description

Definition at line 49 of file containeroptionparser.h.

Member Function Documentation

std::unique_ptr< DynamicContainerOptions > softwarecontainer::ContainerOptionParser::parse ( const std::string &  config)

Parse config needed for starting up the container in a correct manner.

This reads a config string into json format, makes sure it is formatted correctly, and then calls readConfigElement for its elements.

Parameters
configa configuration string
Returns
a DynamicContainerOptions object with the given settings set.
Exceptions
aContainerOptionParserError in case of bad input

Definition at line 73 of file containeroptionparser.cpp.

Referenced by softwarecontainer::SoftwareContainerAgent::createContainer().

74 {
75  if (config.size() == 0) {
76  std::string errorMessage("Empty JSON config strings are not supported.");
77  log_error() << errorMessage;
78  throw ContainerOptionParseError(errorMessage);
79  }
80 
81  json_error_t error;
82  json_t *root = json_loads(config.c_str(), 0, &error);
83 
84  if (!root) {
85  std::string errorMessage("Could not parse config: "
86  + std::string(error.text)
87  + config);
88  log_error() << errorMessage;
89  throw ContainerOptionParseError(errorMessage);
90  }
91 
92  if (!json_is_array(root)) {
93  std::string errorMessage("Root JSON element is not an array");
94  log_error() << errorMessage;
95  json_decref(root);
96  throw ContainerOptionParseError(errorMessage);
97  }
98 
99  size_t index;
100  json_t *element;
101 
102  try {
103  json_array_foreach(root, index, element) {
104  readConfigElement(element);
105  }
106  } catch (ContainerOptionParseError &err) {
107  json_decref(root);
108  throw;
109  }
110  json_decref(root);
111 
112  std::unique_ptr<DynamicContainerOptions> ret = std::move(m_options);
113  m_options.reset(new DynamicContainerOptions());
114  return ret;
115 }

Here is the caller graph for this function:


The documentation for this class was generated from the following files: