EnvironmentMgr.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. class EnvironmentMgr;
  27. enum EnvironmentType
  28. {
  29. UNDEFINED,
  30. XML
  31. };
  32. DECL_EXPORT EnvironmentMgr *getEnvironmentMgrInstance(const EnvironmentType envType);
  33. class DECL_EXPORT EnvironmentMgr
  34. {
  35. public:
  36. EnvironmentMgr();
  37. virtual ~EnvironmentMgr() {}
  38. bool loadSchema(const std::string &configPath, const std::string &masterConfigFile, const std::map<std::string, std::string> &cfgParms = std::map<std::string, std::string>());
  39. std::string getLastSchemaMessage() const;
  40. std::string getLastEnvironmentMessage() const { return m_message; }
  41. bool loadEnvironment(const std::string &qualifiedFilename);
  42. std::shared_ptr<EnvironmentNode> findEnvironmentNodeById(const std::string &nodeId) const;
  43. std::shared_ptr<EnvironmentNode> getNewEnvironmentNode(const std::string &parentNodeId, const std::string &inputItem, Status &status) const;
  44. std::shared_ptr<EnvironmentNode> addNewEnvironmentNode(const std::string &parentNodeId, const std::string &configType, std::vector<NameValue> &initAttributes, Status &status);
  45. std::shared_ptr<EnvironmentNode> addNewEnvironmentNode(const std::shared_ptr<EnvironmentNode> &pParentNode, const std::shared_ptr<SchemaItem> &pNewCfgItem, std::vector<NameValue> &initAttributes, Status &status);
  46. bool removeEnvironmentNode(const std::string &nodeId);
  47. bool saveEnvironment(const std::string &qualifiedFilename);
  48. void discardEnvironment() { m_pRootNode = nullptr; m_nodeIds.clear();}
  49. void validate(Status &status, bool includeHiddenNodes=false) const;
  50. std::string getRootNodeId() const;
  51. static std::string getUniqueKey();
  52. void fetchNodes(const std::string path, std::vector<std::shared_ptr<EnvironmentNode>> &nodes, const std::shared_ptr<EnvironmentNode> &pStartNode = nullptr) const;
  53. protected:
  54. void addPath(const std::shared_ptr<EnvironmentNode> pNode);
  55. virtual bool createParser() = 0;
  56. virtual std::vector<std::shared_ptr<EnvironmentNode>> doLoadEnvironment(std::istream &in, const std::shared_ptr<SchemaItem> &pSchemaItem) = 0;
  57. virtual bool save(std::ostream &out) = 0;
  58. void assignNodeIds(const std::shared_ptr<EnvironmentNode> &pNode);
  59. void insertExtraEnvironmentData(std::shared_ptr<EnvironmentNode> pNode);
  60. std::shared_ptr<SchemaItem> findInsertableItem(const std::shared_ptr<EnvironmentNode> &pNode, const std::string &itemType) const;
  61. void getInitAttributesFromItemType(const std::string &inputItemType, std::string &itemType, std::vector<NameValue> &initAttributes) const;
  62. protected:
  63. std::shared_ptr<SchemaItem> m_pSchema;
  64. std::shared_ptr<SchemaParser> m_pSchemaParser;
  65. std::shared_ptr<EnvironmentNode> m_pRootNode;
  66. std::map<std::string, std::shared_ptr<EnvironmentNode>> m_nodeIds;
  67. std::string m_message;
  68. std::vector<std::shared_ptr<EnvSupportLib>> m_supportLibs;
  69. private:
  70. static std::atomic_int m_key;
  71. };
  72. #endif