20 #include "servicemanifestfileloader.h" 21 #include <glibmm/fileutils.h> 25 ServiceManifestFileLoader::ServiceManifestFileLoader(
const std::string &source) :
26 ServiceManifestLoader(source)
32 std::string errorMessage;
34 if (m_source.empty()) {
36 log_debug() <<
"Path to service manifests is empty";
37 }
else if (m_source.compare(
"/") == 0) {
38 errorMessage =
"Searching for configuration files from root dir not allowed";
39 log_error() << errorMessage;
42 log_debug() <<
"Path to service manifests is a directory";
43 loadServiceManifestDir();
44 }
else if (
isFile(m_source)) {
45 log_debug() <<
"Path to service manifest is a file";
46 if (isJsonFile(m_source)) {
47 loadServiceManifestFile(m_source);
49 errorMessage =
"Path to service manifest is not a json file";
50 log_debug() << errorMessage;
54 errorMessage =
"The path to the service manifest(s) is not a directory or file: \"" 56 log_error() << errorMessage;
62 void ServiceManifestFileLoader::loadServiceManifestDir()
64 std::string errorMessage;
67 log_warning() <<
"The service manifest directory is empty: " << m_source;
71 std::vector<std::string> files = fileList();
74 log_info() <<
"No configuration files found: " << m_source;
78 for (std::string file : files) {
79 std::string filePath = buildPath(m_source, file);
80 loadServiceManifestFile(filePath);
84 void ServiceManifestFileLoader::loadServiceManifestFile(
const std::string &filePath)
86 std::string errorMessage;
88 log_debug() <<
"Loading service manifest file: " << filePath;
91 json_t *fileroot = json_load_file(filePath.c_str(), 0, &error);
93 if (
nullptr == fileroot) {
94 errorMessage =
"Could not parse the service manifest: " 95 + filePath +
":" + std::to_string(error.line) +
" : " + std::string(error.text);
96 log_error() << errorMessage;
98 }
else if (!json_is_object(fileroot)) {
99 errorMessage =
"The service manifest root is not a json object: " 100 + filePath +
":" + std::to_string(error.line) +
" : " + std::string(error.text);
101 log_error() << errorMessage;
105 log_debug() <<
"Loaded service manifest: " << filePath;
107 m_content.push_back(fileroot);
115 std::vector<std::string> ServiceManifestFileLoader::fileList()
117 std::vector<std::string> files;
119 Glib::Dir dir(m_source);
121 std::string filename;
122 while ((filename = dir.read_name()) !=
"") {
123 if (isJsonFile(filename)) {
124 files.push_back(filename);
128 }
catch (Glib::FileError &err) {
129 log_error() <<
"Could not read files in directory: " << m_source;
135 bool ServiceManifestFileLoader::isJsonFile(
const std::string &filename)
138 if (filename.size() <= 5) {
142 return filename.substr(filename.size()-5) ==
".json";
An error occured in ConfigStore relating to the path to the Service Manifest(s)
bool isDirectoryEmpty(const std::string &path)
isDirectoryEmpty Check if path is empty
An error occured in ConfigStore when parsing a Service Manifest.
virtual std::vector< json_t * > loadContent() override
Loads the json content from the Service Manifest(s)
bool isDirectory(const std::string &path)
isDirectory Check if path is a directory
bool isFile(const std::string &path)
isFile Check if path is a file
Developers guide to adding a config item: