ConfigManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.RT.OperationCenter;
  12. using Aitex.Core.Util;
  13. namespace Aitex.Core.RT.ConfigCenter
  14. {
  15. public class ConfigManager : Singleton<ConfigManager>, ICommonConfig
  16. {
  17. Dictionary<string, object> _dic = new Dictionary<string,object>();
  18. ConcurrentDictionary<string, DataItem<object>> _keyValueMap = new ConcurrentDictionary<string, DataItem<object>>(StringComparer.OrdinalIgnoreCase);
  19. object _locker = new object();
  20. public ConfigManager()
  21. {
  22. }
  23. public void Initialize()
  24. {
  25. CONFIG.InnerConfigManager = this;
  26. OP.Subscribe("SetConfig", InvokeSetConfig);
  27. }
  28. private bool InvokeSetConfig(string arg1, object[] arg2)
  29. {
  30. //SC.SetItemValue((string)args[0], args[1]);
  31. //if ((string)args[0] == SCName.System_Language)
  32. //{
  33. // //RtApplication.UpdateCultureResource(((int)SC.GetItemValue(SCName.System_Language)) == 2 ? "zh-CN" : "en-US");
  34. //}
  35. return true;
  36. }
  37. public void Terminate()
  38. {
  39. }
  40. /// <summary>
  41. /// 如果文件不存在,返回NULL.如果读取错误,返回""
  42. /// </summary>
  43. /// <param name="fileName"></param>
  44. /// <returns></returns>
  45. public string GetFileContent(string fileName)
  46. {
  47. if (!Path.IsPathRooted(fileName))
  48. fileName = PathManager.GetCfgDir() + "\\" + fileName;
  49. if (!File.Exists(fileName))
  50. return null;
  51. StringBuilder s = new StringBuilder();
  52. try
  53. {
  54. using (StreamReader sr = new StreamReader(fileName))
  55. {
  56. while (!sr.EndOfStream)
  57. {
  58. s.Append(sr.ReadLine());
  59. }
  60. }
  61. }
  62. catch (Exception ex)
  63. {
  64. LOG.Write(ex);
  65. return "";
  66. }
  67. return s.ToString();
  68. }
  69. public object GetConfig(string config)
  70. {
  71. return _dic.ContainsKey(config) ? _dic[config] : null;
  72. }
  73. public void SetConfig(string config, object value)
  74. {
  75. _dic[config] = value;
  76. }
  77. public void Subscribe(string module, string key, Func<object> getter)
  78. {
  79. if (string.IsNullOrWhiteSpace(key))
  80. throw new ArgumentNullException("key");
  81. if (!string.IsNullOrEmpty(module))
  82. key = module + "." + key;
  83. if (_keyValueMap.ContainsKey(key))
  84. throw new Exception(string.Format("Duplicated Key:{0}", key));
  85. if (getter == null)
  86. throw new ArgumentNullException("getter");
  87. _keyValueMap.TryAdd(key, new DataItem<object>(getter));
  88. }
  89. public object Poll(string key)
  90. {
  91. return _keyValueMap.ContainsKey(key) ? _keyValueMap[key].Value : null;
  92. }
  93. public Dictionary<string, object> PollConfig(IEnumerable<string> keys)
  94. {
  95. Dictionary<string, object> result = new Dictionary<string,object>();
  96. foreach (string key in keys)
  97. {
  98. if (_keyValueMap.ContainsKey(key))
  99. result[key] = _keyValueMap[key].Value;
  100. else
  101. {
  102. LOG.Error("undefined config:" + key);
  103. }
  104. }
  105. return result;
  106. }
  107. }
  108. }