Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <summary>
        /// 处理share级
        /// </summary>
        /// <param name="change"></param>
        /// <param name="adding">是否新加</param>
        private void HandleShareFile(ChangeQueue change, bool adding)
        {
            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 = change.Encoding ?? 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 != null && ta.JsonShareFile.Name == shareBuilder.JsonShareFile.Name))
                    {
                        if (adding)
                        {
                            shareBuilder.Dispose();
                            throw new Exception(string.Format("存在相同的共享文件{0}", shareBuilder.JsonShareFile.Name));
                        }
                    }
                    else
                    {
                        this.shareConfiguration.Add(shareBuilder);
                    }
                }
                break;

                case ConfigFileType.Xml:
                {
                    if (this.shareConfiguration.Any(ta => ta.XmlShareFile != null && ta.XmlShareFile.Name == shareBuilder.XmlShareFile.Name))
                    {
                        if (adding)
                        {
                            shareBuilder.Dispose();
                            throw new Exception(string.Format("存在相同的共享文件{0}", 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 != null && 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 != null && 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);
                var be = newBuilder.Build(oldBuilder);
                if (be.FileType == ConfigFileType.Json)
                {
                    this.shareConfiguration.RemoveAll(ta => ta.JsonShareFile != null && ta.JsonShareFile.Name.IsEquals(be.JsonShareFile.Name));
                }
                else if (be.FileType == ConfigFileType.Xml)
                {
                    this.shareConfiguration.RemoveAll(ta => ta.XmlShareFile != null && ta.XmlShareFile.Name.IsEquals(be.XmlShareFile.Name));
                }

                this.shareConfiguration.Add(be);
                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 != null && 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;
            }
        }