private void WriteShareToLog(ChangeQueue change, ShareConfigurationBuilder builder, IEnumerable <IShareFileReference> references) { if (this.LoggerBuilder == null) { return; } this.EatException(() => { var sb = new StringBuilder(); sb.AppendLine(); sb.AppendFormat("share {0} action : {1}, at {2};", builder.File.Name, change.ToAction(), DateTime.Now.ToString()); if (references.IsNotNullOrEmpty()) { sb.AppendLine(); sb.AppendFormat("the flowing files ["); var sname = string.Empty; foreach (var r in references) { if (r == null || r.Builder == null) { continue; } sb.AppendLine(); sb.AppendFormat("{0}", r.Builder.Name); } sb.AppendFormat("] are using the share {0} file;", builder.File.Name); } this.LoggerBuilder.Invoke().Build(typeof(ConfigurationWatcher)).Info(sb.ToString()); }); }
/// <summary> /// 在构建之中 /// </summary> /// <returns></returns> /// <param name="old"></param> public ShareConfigurationBuilder Build(ShareConfigurationBuilder old = null) { if (old != null) { foreach (var @event in old.events) { this.OnBuilding += @event; } old.Dispose(); } var e = new ShareFileEventArgs(this.JsonShareFile, this.XmlShareFile); this.events.ForEach(ta => ta.Invoke(this, e)); return(this); }
/// <summary> /// 处理share级 /// </summary> /// <param name="change"></param> /// <param name="outsideEncoding"></param> private void HandleShareFile(ChangeQueue change, Encoding outsideEncoding = null) { var oldFile = change.OldFullName == null ? null : new FileInfo(change.OldFullName); var newFile = change.FullName == null ? null : new FileInfo(change.FullName); if (newFile != null && !this.CanRefresh(newFile)) { return; } switch (change.Action) { //新加 case 0: { var shareBuilder = new ShareConfigurationBuilder(new ConfigFileInfo() { File = newFile, Encoding = outsideEncoding ?? Encoding.UTF8 }); if (this.ShareFileEventHandler != null) { shareBuilder.OnBuilding += this.ShareFileEventHandler; } shareBuilder.Build(); switch (shareBuilder.FileType) { case ConfigFileType.Json: { if (this.shareConfiguration.Any(ta => ta.JsonShareFile.Name == shareBuilder.JsonShareFile.Name)) { } else { this.shareConfiguration.Add(shareBuilder); } } break; case ConfigFileType.Xml: { if (this.shareConfiguration.Any(ta => ta.XmlShareFile.Name == shareBuilder.XmlShareFile.Name)) { } else { this.shareConfiguration.Add(shareBuilder); } } break; } if (!change.PathChanged && !this.fileWather.ContainsKey(newFile.FullName)) { var watcher = new Never.IO.FileWatcher(shareBuilder.File) { EnableRaisingEvents = true }; this.fileWather.Add(shareBuilder.File.FullName, watcher); this.moreTimeLimit.Add(shareBuilder.File.FullName, DateTime.Now); watcher.Created += ShareWatcher_Created; watcher.Changed += ShareWatcher_Changed; watcher.Deleted += ShareWatcher_Deleted; watcher.Renamed += ShareWatcher_Renamed; } this.WriteShareToLog(change, shareBuilder, null); } break; //修改 case 1: { var oldBuilder = this.shareConfiguration.FirstOrDefault(ta => ta.File.FullName == newFile.FullName).Rebuild(); //引起引用文件的修改,先找出相应的引用,找到后等新的builder替换成功后再更新引用文件里面的引用 var refences = new List <IShareFileReference>(); foreach (var b in new[] { oldBuilder.JsonShareFile, oldBuilder.XmlShareFile }) { if (b == null) { continue; } foreach (var app in this.appConfiguration) { if (app.Reference.IsNotNullOrEmpty() && app.Reference.Any(ta => ta.Name.IsEquals(b.Name))) { refences.Add(app); continue; } } } if (refences.IsNotNullOrEmpty()) { refences.UseForEach(ta => ta.Builder.Rebuild(this.shareConfiguration)); this.EatException(() => { this.OnShareFileChanged?.Invoke(this, new ConfigurationWatcherEventArgs() { Builders = refences.Select(ta => ta.Builder) }); }); } this.WriteShareToLog(change, oldBuilder, refences); } break; //重命名 case 2: { var oldBuilder = this.shareConfiguration.FirstOrDefault(ta => ta.File.FullName == oldFile.FullName); var newBuilder = new ShareConfigurationBuilder(new ConfigFileInfo() { File = newFile, Encoding = oldBuilder.Encoding }); //引起引用文件的修改,先找出相应的引用,找到后等新的builder替换成功后再更新引用文件里面的引用 var refences = new List <IShareFileReference>(); foreach (var b in new[] { oldBuilder.JsonShareFile, oldBuilder.XmlShareFile }) { if (b == null) { continue; } foreach (var app in this.appConfiguration) { if (app.Reference.IsNotNullOrEmpty() && app.Reference.Any(ta => ta.Name.IsEquals(b.Name))) { refences.Add(app); continue; } } } this.shareConfiguration.Remove(oldBuilder); this.shareConfiguration.Add(newBuilder.Build(oldBuilder)); oldBuilder = null; if (!change.PathChanged) { if (this.fileWather.ContainsKey(oldFile.FullName)) { var watcher = this.fileWather[oldFile.FullName]; watcher.Path = System.IO.Path.GetDirectoryName(newFile.FullName); watcher.Filter = newFile.Name; } } if (refences.IsNotNullOrEmpty()) { refences.UseForEach(ta => ta.Builder.Rebuild(this.shareConfiguration)); this.EatException(() => { this.OnShareFileRenamed?.Invoke(this, new ConfigurationWatcherEventArgs() { Builders = refences.Select(ta => ta.Builder) }); }); } this.WriteShareToLog(change, newBuilder, refences); } break; //删除 case 3: { var oldBuilder = this.shareConfiguration.FirstOrDefault(ta => ta.File.FullName == oldFile.FullName); //引起引用文件的修改,先找出相应的引用,找到后等新的builder替换成功后再更新引用文件里面的引用 var refences = new List <IShareFileReference>(); foreach (var b in new[] { oldBuilder.JsonShareFile, oldBuilder.XmlShareFile }) { if (b == null) { continue; } foreach (var app in this.appConfiguration) { if (app.Reference.IsNotNullOrEmpty() && app.Reference.Any(ta => ta.Name.IsEquals(b.Name))) { refences.Add(app); continue; } } } if (!change.PathChanged) { if (this.fileWather.ContainsKey(oldFile.FullName)) { this.fileWather[oldFile.FullName].Dispose(); this.fileWather.Remove(oldFile.FullName); } } this.shareConfiguration.Remove(oldBuilder); if (refences.IsNotNullOrEmpty()) { refences.UseForEach(ta => ta.Builder.Rebuild(this.shareConfiguration)); this.EatException(() => { this.OnShareFileDeleted?.Invoke(this, new ConfigurationWatcherEventArgs() { Builders = refences.Select(ta => ta.Builder) }); }); } this.WriteShareToLog(change, oldBuilder, refences); oldBuilder.Dispose(); } break; } }
/// <summary> /// 是否相同 /// </summary> /// <param name="other"></param> /// <returns></returns> public bool Equals(ShareConfigurationBuilder other) { return(this.File == other.File); }