EnvironmentMgr.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2017 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef _CONFIG2_ENVIRONMENTMGR_HPP_
  14. #define _CONFIG2_ENVIRONMENTMGR_HPP_
  15. #include <string>
  16. #include <fstream>
  17. #include <vector>
  18. #include <atomic>
  19. #include "SchemaItem.hpp"
  20. #include "SchemaParser.hpp"
  21. #include "EnvironmentNode.hpp"
  22. #include "Status.hpp"
  23. #include "NameValue.hpp"
  24. #include "platform.h"
  25. #include "EnvSupportLib.hpp"
  26. #include "Cfgmgrlib.hpp"
  27. class EnvironmentMgr;
  28. enum EnvironmentType
  29. {
  30. UNDEFINED,
  31. XML
  32. };
  33. CFGMGRLIB_API EnvironmentMgr *getEnvironmentMgrInstance(const EnvironmentType envType);
  34. class CFGMGRLIB_API EnvironmentMgr
  35. {
  36. public:
  37. EnvironmentMgr();
  38. virtual ~EnvironmentMgr() {}
  39. bool loadSchema(const std::string &configPath, const std::string &masterConfigFile, const std::map<std::string, std::string> &cfgParms = std::map<std::string, std::string>());
  40. std::string getLastSchemaMessage() const;
  41. std::string getLastEnvironmentMessage() const { return m_message; }
  42. bool loadEnvironment(const std::string &qualifiedFilename);
  43. std::shared_ptr<EnvironmentNode> findEnvironmentNodeById(const std::string &nodeId) const;
  44. std::shared_ptr<EnvironmentNode> getNewEnvironmentNode(const std::string &parentNodeId, const std::string &inputItem, Status &status) const;
  45. std::shared_ptr<EnvironmentNode> addNewEnvironmentNode(const std::string &parentNodeId, const std::string &configType,
  46. std::vector<NameValue> &initAttributes, Status &status, bool allowInvalid=false, bool forceCreate=false);
  47. std::shared_ptr<EnvironmentNode> addNewEnvironmentNode(const std::shared_ptr<EnvironmentNode> &pParentNode, const std::string &nodeData,
  48. Status &status, const std::string &itemType);
  49. std::shared_ptr<EnvironmentNode> addNewEnvironmentNode(const std::shared_ptr<EnvironmentNode> &pParentNode, const std::shared_ptr<SchemaItem> &pNewCfgItem,
  50. std::vector<NameValue> &initAttributes, Status &status, bool allowInvalid=false, bool forceCreate=false);
  51. bool removeEnvironmentNode(const std::string &nodeId);
  52. virtual bool serialize(std::ostream &out, const std::shared_ptr<EnvironmentNode> &pStartNode);
  53. bool saveEnvironment(const std::string &qualifiedFilename);
  54. void discardEnvironment() { m_pRootNode = nullptr; m_nodeIds.clear();}
  55. void validate(Status &status, bool includeHiddenNodes=false) const;
  56. std::string getRootNodeId() const;
  57. static std::string getUniqueKey();
  58. void fetchNodes(const std::string path, std::vector<std::shared_ptr<EnvironmentNode>> &nodes, const std::shared_ptr<EnvironmentNode> &pStartNode = nullptr) const;
  59. bool isSchemaLoaded() const { return static_cast<bool>(m_pSchema); }
  60. bool isEnvironmentLoaded() const { return static_cast<bool>(m_pRootNode); }
  61. protected:
  62. void addPath(const std::shared_ptr<EnvironmentNode> pNode);
  63. virtual bool createParser() = 0;
  64. virtual std::vector<std::shared_ptr<EnvironmentNode>> doLoadEnvironment(std::istream &in, const std::shared_ptr<SchemaItem> &pSchemaItem, const std::string itemType = std::string("")) = 0;
  65. virtual bool save(std::ostream &out) = 0;
  66. void assignNodeIds(const std::shared_ptr<EnvironmentNode> &pNode);
  67. void insertExtraEnvironmentData(std::shared_ptr<EnvironmentNode> pNode);
  68. std::shared_ptr<SchemaItem> findInsertableItem(const std::shared_ptr<EnvironmentNode> &pNode, const std::string &itemType) const;
  69. void getPredefinedAttributeValues(const std::string &inputItemType, std::string &itemType,
  70. std::vector<NameValue> &initAttributes) const;
  71. protected:
  72. std::shared_ptr<SchemaItem> m_pSchema;
  73. std::shared_ptr<SchemaParser> m_pSchemaParser;
  74. std::shared_ptr<EnvironmentNode> m_pRootNode;
  75. std::map<std::string, std::shared_ptr<EnvironmentNode>> m_nodeIds;
  76. std::string m_message;
  77. std::vector<std::shared_ptr<EnvSupportLib>> m_supportLibs;
  78. private:
  79. static std::atomic_int m_key;
  80. };
  81. #endif