ConfigManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using Aitex.Core.RT.Log;
  7. using System.IO;
  8. using Aitex.Common.Util;
  9. using System.Collections.Concurrent;
  10. using Aitex.Core.RT.DataCenter;
  11. using Aitex.Core.Util;
  12. namespace Aitex.Core.RT.ConfigCenter
  13. {
  14. public class ConfigManager : Singleton<ConfigManager>, ICommonConfig
  15. {
  16. Dictionary<string, object> _dic = new Dictionary<string,object>();
  17. ConcurrentDictionary<string, DataItem<object>> _keyValueMap = new ConcurrentDictionary<string, DataItem<object>>();
  18. object _locker = new object();
  19. public ConfigManager()
  20. {
  21. }
  22. public void Initialize()
  23. {
  24. CONFIG.InnerConfigManager = this;
  25. }
  26. public void Terminate()
  27. {
  28. }
  29. /// <summary>
  30. /// 如果文件不存在,返回NULL。如果读取错误,返回""
  31. /// </summary>
  32. /// <param name="fileName"></param>
  33. /// <returns></returns>
  34. public string GetFileContent(string fileName)
  35. {
  36. if (!Path.IsPathRooted(fileName))
  37. fileName = PathManager.GetCfgDir() + "\\" + fileName;
  38. if (!File.Exists(fileName))
  39. return null;
  40. StringBuilder s = new StringBuilder();
  41. try
  42. {
  43. using (StreamReader sr = new StreamReader(fileName))
  44. {
  45. while (!sr.EndOfStream)
  46. {
  47. s.Append(sr.ReadLine());
  48. }
  49. }
  50. }
  51. catch (Exception ex)
  52. {
  53. LOG.Write(ex);
  54. return "";
  55. }
  56. return s.ToString();
  57. }
  58. public object GetConfig(string config)
  59. {
  60. return _dic.ContainsKey(config) ? _dic[config] : null;
  61. }
  62. public void SetConfig(string config, object value)
  63. {
  64. _dic[config] = value;
  65. }
  66. public void Subscribe(string module, string key, Func<object> getter)
  67. {
  68. if (string.IsNullOrWhiteSpace(key))
  69. throw new ArgumentNullException("key");
  70. if (!string.IsNullOrEmpty(module))
  71. key = module + "." + key;
  72. if (_keyValueMap.ContainsKey(key))
  73. throw new Exception(string.Format("Duplicated Key:{0}", key));
  74. if (getter == null)
  75. throw new ArgumentNullException("getter");
  76. _keyValueMap.TryAdd(key, new DataItem<object>(getter));
  77. }
  78. public object Poll(string key)
  79. {
  80. return _keyValueMap.ContainsKey(key) ? _keyValueMap[key].Value : null;
  81. }
  82. public Dictionary<string, object> PollConfig(IEnumerable<string> keys)
  83. {
  84. Dictionary<string, object> result = new Dictionary<string,object>();
  85. foreach (string key in keys)
  86. {
  87. if (_keyValueMap.ContainsKey(key))
  88. result[key] = _keyValueMap[key].Value;
  89. else
  90. {
  91. LOG.Error("未定义的Config:" + key);
  92. }
  93. }
  94. return result;
  95. }
  96. }
  97. }