using System.Collections.Specialized;
using System.Text;
using Newtonsoft.Json.Linq;
namespace JNPF.Extras.CollectiveOAuth.Utils;
///
/// 配置管理.
///
public class ConfigurationManager
{
///
/// 配置内容.
///
private static NameValueCollection _configurationCollection = new NameValueCollection();
///
/// 配置监听响应链堆栈.
///
private static Stack> FileListeners = new Stack>();
///
/// 默认路径.
///
private static string _defaultPath = Directory.GetCurrentDirectory() + "\\appsettings.json";
///
/// 最终配置文件路径.
///
private static string _configPath = null;
///
/// 配置节点关键字.
///
private static string _configSection = "AppSettings";
///
/// 配置外连接的后缀.
///
private static string _configUrlPostfix = "Url";
///
/// 最终修改时间戳.
///
private static long _timeStamp = 0L;
///
/// 配置外链关键词,例如:AppSettings.Url.
///
private static string _configUrlSection { get { return _configSection + "." + _configUrlPostfix; } }
static ConfigurationManager()
{
ConfigFinder(_defaultPath);
}
///
/// 确定配置文件路径.
///
private static void ConfigFinder(string Path)
{
_configPath = Path;
JObject config_json = new JObject();
while (config_json != null)
{
config_json = null;
FileInfo config_info = new FileInfo(_configPath);
if (!config_info.Exists) break;
FileListeners.Push(CreateListener(config_info));
config_json = LoadJsonFile(_configPath);
if (config_json[_configUrlSection] != null)
_configPath = config_json[_configUrlSection].ToString();
else break;
}
if (config_json == null || config_json[_configSection] == null) return;
LoadConfiguration();
}
///
/// 读取配置文件内容.
///
private static void LoadConfiguration()
{
FileInfo config = new FileInfo(_configPath);
var configColltion = new NameValueCollection();
JObject config_object = LoadJsonFile(_configPath);
if (config_object == null || !(config_object is JObject)) return;
if (config_object[_configSection] != null)
{
foreach (JProperty prop in config_object[_configSection])
{
configColltion[prop.Name] = prop.Value.ToString();
}
}
_configurationCollection = configColltion;
}
///
/// 解析Json文件.
///
/// 文件路径
///
private static JObject LoadJsonFile(string FilePath)
{
JObject config_object = null;
try
{
StreamReader sr = new StreamReader(FilePath, Encoding.Default);
config_object = JObject.Parse(sr.ReadToEnd());
sr.Close();
}
catch { }
return config_object;
}
///
/// 添加监听树节点.
///
///
///
private static KeyValuePair CreateListener(FileInfo info)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.BeginInit();
watcher.Path = info.DirectoryName;
watcher.Filter = info.Name;
watcher.IncludeSubdirectories = false;
watcher.EnableRaisingEvents = true;
watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Size;
watcher.Changed += new FileSystemEventHandler(ConfigChangeListener);
watcher.EndInit();
return new KeyValuePair(info.FullName, watcher);
}
///
/// 配置改变监听器.
///
///
///
private static void ConfigChangeListener(object sender, FileSystemEventArgs e)
{
long time = TimeStamp();
lock (FileListeners)
{
if (time > _timeStamp)
{
_timeStamp = time;
if (e.FullPath != _configPath || e.FullPath == _defaultPath)
{
while (FileListeners.Count > 0)
{
var listener = FileListeners.Pop();
listener.Value.Dispose();
if (listener.Key == e.FullPath) break;
}
ConfigFinder(e.FullPath);
}
else
{
LoadConfiguration();
}
}
}
}
private static long TimeStamp()
{
return (long)((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds * 100);
}
private static string c_configSection = null;
public static string ConfigSection
{
get { return _configSection; }
set { c_configSection = value; }
}
private static string c_configUrlPostfix = null;
public static string ConfigUrlPostfix
{
get { return _configUrlPostfix; }
set { c_configUrlPostfix = value; }
}
private static string c_defaultPath = null;
public static string DefaultPath
{
get { return _defaultPath; }
set { c_defaultPath = value; }
}
public static NameValueCollection AppSettings
{
get { return _configurationCollection; }
}
///
/// 手动刷新配置,修改配置后,请手动调用此方法,以便更新配置参数
///
public static void RefreshConfiguration()
{
lock (FileListeners)
{
// 修改配置
if (c_configSection != null) { _configSection = c_configSection; c_configSection = null; }
if (c_configUrlPostfix != null) { _configUrlPostfix = c_configUrlPostfix; c_configUrlPostfix = null; }
if (c_defaultPath != null) { _defaultPath = c_defaultPath; c_defaultPath = null; }
// 释放掉全部监听响应链
while (FileListeners.Count > 0)
FileListeners.Pop().Value.Dispose();
ConfigFinder(_defaultPath);
}
}
}