ConfigManager.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 (arg2.Length == 3)
  32. {
  33. if ((string)arg2[0] == "Save")
  34. {
  35. SaveFileContent((string)arg2[1], (string)arg2[2]);
  36. }
  37. }
  38. //if ((string)args[0] == SCName.System_Language)
  39. //{
  40. // //RtApplication.UpdateCultureResource(((int)SC.GetItemValue(SCName.System_Language)) == 2 ? "zh-CN" : "en-US");
  41. //}
  42. return true;
  43. }
  44. public void Terminate()
  45. {
  46. }
  47. public bool SaveFileContent(string fileName, string content)
  48. {
  49. bool isSave = false;
  50. try
  51. {
  52. var filePath = PathManager.GetCfgDir() + "\\" + fileName;
  53. System.IO.File.WriteAllText(filePath, content, Encoding.UTF8);
  54. isSave = true;
  55. }
  56. catch (Exception)
  57. {
  58. // throw;
  59. }
  60. return isSave;
  61. }
  62. public List<string> GetFileListByFolderBrowser(string folderBrowser)
  63. {
  64. var result = new List<string>();
  65. try
  66. {
  67. string filePath = PathManager.GetAppDir() + "\\" + folderBrowser + "\\";
  68. var di = new DirectoryInfo(filePath);
  69. var fis = di.GetFiles("*.*", SearchOption.AllDirectories);
  70. foreach (var fi in fis)
  71. {
  72. string str = fi.FullName.Replace(filePath,"");
  73. result.Add(str);
  74. }
  75. }
  76. catch (Exception ex)
  77. {
  78. LOG.Write(ex);
  79. }
  80. return result;
  81. }
  82. /// <summary>
  83. /// 如果文件不存在,返回NULL.如果读取错误,返回""
  84. /// </summary>
  85. /// <param name="fileName"></param>
  86. /// <returns></returns>
  87. public string GetFileContent(string fileName)
  88. {
  89. if (!Path.IsPathRooted(fileName))
  90. fileName = PathManager.GetCfgDir() + fileName;
  91. if (!File.Exists(fileName))
  92. return null;
  93. StringBuilder s = new StringBuilder();
  94. try
  95. {
  96. using (StreamReader sr = new StreamReader(fileName))
  97. {
  98. while (!sr.EndOfStream)
  99. {
  100. s.AppendLine(sr.ReadLine());
  101. }
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. LOG.Write(ex);
  107. return "";
  108. }
  109. return s.ToString();
  110. }
  111. /// <summary>
  112. /// 如果文件不存在,返回NULL.如果读取错误,返回""
  113. /// </summary>
  114. /// <param name="fileName"></param>
  115. /// <returns></returns>
  116. public string GetRootFileContent(string fileName)
  117. {
  118. if (!Path.IsPathRooted(fileName))
  119. fileName = PathManager.GetAppDir() + "\\" + fileName;
  120. if (!File.Exists(fileName))
  121. return null;
  122. StringBuilder s = new StringBuilder();
  123. try
  124. {
  125. using (StreamReader sr = new StreamReader(fileName, Encoding.UTF8))
  126. {
  127. while (!sr.EndOfStream)
  128. {
  129. s.AppendLine(sr.ReadLine());
  130. }
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. LOG.Write(ex);
  136. return "";
  137. }
  138. return s.ToString();
  139. }
  140. public object GetConfig(string config)
  141. {
  142. return _dic.ContainsKey(config) ? _dic[config] : null;
  143. }
  144. public void SetConfig(string config, object value)
  145. {
  146. _dic[config] = value;
  147. }
  148. public void Subscribe(string module, string key, Func<object> getter)
  149. {
  150. if (string.IsNullOrWhiteSpace(key))
  151. throw new ArgumentNullException("key");
  152. if (!string.IsNullOrEmpty(module))
  153. key = module + "." + key;
  154. if (_keyValueMap.ContainsKey(key))
  155. throw new Exception(string.Format("Duplicated Key:{0}", key));
  156. if (getter == null)
  157. throw new ArgumentNullException("getter");
  158. _keyValueMap.TryAdd(key, new DataItem<object>(getter));
  159. }
  160. public object Poll(string key)
  161. {
  162. return _keyValueMap.ContainsKey(key) ? _keyValueMap[key].Value : null;
  163. }
  164. public Dictionary<string, object> PollConfig(IEnumerable<string> keys)
  165. {
  166. Dictionary<string, object> result = new Dictionary<string, object>();
  167. foreach (string key in keys)
  168. {
  169. if (_keyValueMap.ContainsKey(key))
  170. result[key] = _keyValueMap[key].Value;
  171. else
  172. {
  173. LOG.Error("undefined config:" + key);
  174. }
  175. }
  176. return result;
  177. }
  178. }
  179. }