| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 | using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using Aitex.Core.RT.Log;using System.IO;using Aitex.Common.Util;using System.Collections.Concurrent;using Aitex.Core.RT.DataCenter;using Aitex.Core.RT.OperationCenter;using Aitex.Core.Util;namespace Aitex.Core.RT.ConfigCenter{    public class ConfigManager : Singleton<ConfigManager>, ICommonConfig    {        Dictionary<string, object> _dic = new Dictionary<string, object>();        ConcurrentDictionary<string, DataItem<object>> _keyValueMap = new ConcurrentDictionary<string, DataItem<object>>(StringComparer.OrdinalIgnoreCase);        object _locker = new object();        public ConfigManager()        {        }        public void Initialize()        {            CONFIG.InnerConfigManager = this;            OP.Subscribe("SetConfig", InvokeSetConfig);        }        private bool InvokeSetConfig(string arg1, object[] arg2)        {            //SC.SetItemValue((string)args[0], args[1]);            if (arg2.Length == 3)            {                if ((string)arg2[0] == "Save")                {                    SaveFileContent((string)arg2[1], (string)arg2[2]);                }            }            //if ((string)args[0] == SCName.System_Language)            //{            //    //RtApplication.UpdateCultureResource(((int)SC.GetItemValue(SCName.System_Language)) == 2 ? "zh-CN" : "en-US");            //}            return true;        }        public void Terminate()        {        }        public bool SaveFileContent(string fileName, string content)        {            bool isSave = false;            try            {                var filePath = PathManager.GetCfgDir() + "\\" + fileName;                System.IO.File.WriteAllText(filePath, content, Encoding.UTF8);                isSave = true;            }            catch (Exception)            {               // throw;            }            return isSave;        }        public List<string> GetFileListByFolderBrowser(string folderBrowser)        {            var result = new List<string>();            try            {                string filePath = PathManager.GetAppDir() + "\\" + folderBrowser + "\\";                var di = new DirectoryInfo(filePath);                var fis = di.GetFiles("*.*", SearchOption.AllDirectories);                foreach (var fi in fis)                {                    string str = fi.FullName.Replace(filePath,"");                    result.Add(str);                }            }            catch (Exception ex)            {                LOG.Write(ex);            }            return result;        }        /// <summary>        /// 如果文件不存在,返回NULL.如果读取错误,返回""        /// </summary>        /// <param name="fileName"></param>        /// <returns></returns>        public string GetFileContent(string fileName)        {            if (!Path.IsPathRooted(fileName))                fileName = PathManager.GetCfgDir() + fileName;            if (!File.Exists(fileName))                return null;            StringBuilder s = new StringBuilder();            try            {                using (StreamReader sr = new StreamReader(fileName))                {                    while (!sr.EndOfStream)                    {                        s.AppendLine(sr.ReadLine());                    }                }            }            catch (Exception ex)            {                LOG.Write(ex);                return "";            }            return s.ToString();        }        /// <summary>        /// 如果文件不存在,返回NULL.如果读取错误,返回""        /// </summary>        /// <param name="fileName"></param>        /// <returns></returns>        public string GetRootFileContent(string fileName)        {            if (!Path.IsPathRooted(fileName))                fileName = PathManager.GetAppDir() + "\\" + fileName;            if (!File.Exists(fileName))                return null;            StringBuilder s = new StringBuilder();            try            {                using (StreamReader sr = new StreamReader(fileName, Encoding.UTF8))                {                    while (!sr.EndOfStream)                    {                        s.AppendLine(sr.ReadLine());                    }                }            }            catch (Exception ex)            {                LOG.Write(ex);                return "";            }            return s.ToString();        }        public object GetConfig(string config)        {            return _dic.ContainsKey(config) ? _dic[config] : null;        }        public void SetConfig(string config, object value)        {            _dic[config] = value;        }        public void Subscribe(string module, string key, Func<object> getter)        {            if (string.IsNullOrWhiteSpace(key))                throw new ArgumentNullException("key");            if (!string.IsNullOrEmpty(module))                key = module + "." + key;            if (_keyValueMap.ContainsKey(key))                throw new Exception(string.Format("Duplicated Key:{0}", key));            if (getter == null)                throw new ArgumentNullException("getter");            _keyValueMap.TryAdd(key, new DataItem<object>(getter));        }        public object Poll(string key)        {            return _keyValueMap.ContainsKey(key) ? _keyValueMap[key].Value : null;        }        public Dictionary<string, object> PollConfig(IEnumerable<string> keys)        {            Dictionary<string, object> result = new Dictionary<string, object>();            foreach (string key in keys)            {                if (_keyValueMap.ContainsKey(key))                    result[key] = _keyValueMap[key].Value;                else                {                    LOG.Error("undefined config:" + key);                }            }            return result;        }    }}
 |