ConfigManager.cs 6.5 KB

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