Exemplo n.º 1
0
        private void WriteAppToLog(ChangeQueue change, IShareFileReference builder)
        {
            if (this.LoggerBuilder == null)
            {
                return;
            }

            this.EatException(() =>
            {
                var sb = new StringBuilder();
                sb.AppendLine();
                sb.AppendFormat("app {0} action : {1}, at {2};", builder.Builder.Name, change.ToAction(), DateTime.Now.ToString());
                if (builder.Reference.IsNotNullOrEmpty())
                {
                    sb.AppendFormat("the share files [");
                    var sname = string.Empty;
                    foreach (var r in builder.Reference)
                    {
                        if (r == null)
                        {
                            continue;
                        }

                        sb.AppendLine();
                        sb.AppendFormat("{0}", r.Name);
                    }
                    sb.AppendLine();
                    sb.AppendFormat("] are using the app {0} file;", builder.Builder.Name);
                }

                this.LoggerBuilder.Invoke().Build(typeof(ConfigurationWatcher)).Info(sb.ToString());
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// 处理app级
        /// </summary>
        /// <param name="change"></param>
        /// <param name="outsideEncoding"></param>
        private void HandleAppFile(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:
            {
                IShareFileReference newBuilder = null;
                switch (newFile.Extension.ToLower())
                {
                case ".json":
                {
                    var fileInfo = new ConfigFileInfo()
                    {
                        File = newFile, Encoding = outsideEncoding ?? Encoding.UTF8
                    };
                    var builder = new JsonConfigurationBuilder(this.shareConfiguration, fileInfo, this.keyValueFinder)
                    {
                    }.Build();
                    if (this.appConfiguration.Any(ta => ta.Builder.Name == builder.Name))
                    {
                    }
                    else
                    {
                        this.appConfiguration.Add(builder);
                    }

                    newBuilder = builder;
                }
                break;

                case ".conf":
                {
                    var fileInfo = new ConfigFileInfo()
                    {
                        File = newFile, Encoding = outsideEncoding
                    };
                    var builder = new XmlConfigurationBuilder(this.shareConfiguration, fileInfo, this.keyValueFinder)
                    {
                    }.Build();
                    if (this.appConfiguration.Any(ta => ta.Builder.Name == builder.Name))
                    {
                    }
                    else
                    {
                        this.appConfiguration.Add(builder);
                    }

                    newBuilder = builder;
                }
                break;
                }

                if (!change.PathChanged && !this.fileWather.ContainsKey(newFile.FullName))
                {
                    var watcher = new Never.IO.FileWatcher(newFile)
                    {
                        EnableRaisingEvents = true
                    };
                    this.fileWather.Add(newFile.FullName, watcher);
                    this.moreTimeLimit.Add(newFile.FullName, DateTime.Now);
                    watcher.Created += AppWatcher_Created;
                    watcher.Changed += AppWatcher_Changed;
                    watcher.Deleted += AppWatcher_Deleted;
                    watcher.Renamed += AppWatcher_Renamed;
                }

                this.EatException(() =>
                    {
                        this.OnAppFileChanged?.Invoke(this, new ConfigurationWatcherEventArgs()
                        {
                            Builders = new[] { newBuilder.Builder }
                        });
                    });

                this.WriteAppToLog(change, newBuilder);
            }
            break;

            //修改
            case 1:
            {
                var oldBuilder = this.appConfiguration.FirstOrDefault(ta => ta.Builder.File.File.FullName == newFile.FullName);
                oldBuilder.Builder.Rebuild(this.shareConfiguration);
                this.EatException(() =>
                    {
                        this.OnAppFileChanged?.Invoke(this, new ConfigurationWatcherEventArgs()
                        {
                            Builders = new[] { oldBuilder.Builder }
                        });
                    });

                this.WriteAppToLog(change, oldBuilder);
            }
            break;

            //重命名
            case 2:
            {
                var oldBuilder = this.appConfiguration.FirstOrDefault(ta => ta.Builder.File.File.FullName == oldFile.FullName);
                var refence    = default(IConfigurationBuilder);
                switch (oldBuilder.Builder.FileType)
                {
                case ConfigFileType.Json:
                {
                    var fileInfo = new ConfigFileInfo()
                    {
                        File = newFile, Encoding = oldBuilder.Builder.File.Encoding
                    };
                    var newBuilder = new JsonConfigurationBuilder(this.shareConfiguration, fileInfo, this.keyValueFinder)
                    {
                    }.Build();
                    this.appConfiguration.Remove(oldBuilder);
                    this.appConfiguration.Add(newBuilder);
                    refence = newBuilder;
                }
                break;

                case ConfigFileType.Xml:
                {
                    var fileInfo = new ConfigFileInfo()
                    {
                        File = newFile, Encoding = oldBuilder.Builder.File.Encoding
                    };
                    var newBuilder = new XmlConfigurationBuilder(this.shareConfiguration, fileInfo, this.keyValueFinder)
                    {
                    }.Build();
                    this.appConfiguration.Remove(oldBuilder);
                    this.appConfiguration.Add(newBuilder);
                    refence = newBuilder;
                }
                break;
                }


                if (!change.PathChanged && this.fileWather.ContainsKey(oldFile.FullName))
                {
                    var watcher = this.fileWather[oldFile.FullName];
                    watcher.Path   = System.IO.Path.GetDirectoryName(newFile.FullName);
                    watcher.Filter = newFile.Name;
                }

                if (refence != null)
                {
                    this.EatException(() =>
                        {
                            this.OnAppFileRenamed?.Invoke(this, new ConfigurationWatcherEventArgs()
                            {
                                Builders = new[] { refence }
                            });
                        });
                }

                this.WriteAppToLog(change, refence as IShareFileReference);
            }
            break;

            //删除
            case 3:
            {
                var oldBuilder = this.appConfiguration.FirstOrDefault(ta => ta.Builder.File.File.FullName == oldFile.FullName);
                this.appConfiguration.Remove(oldBuilder);

                if (!change.PathChanged && this.fileWather.ContainsKey(oldFile.FullName))
                {
                    this.fileWather[oldFile.FullName].Dispose();
                    this.fileWather.Remove(oldFile.FullName);
                }

                this.EatException(() =>
                    {
                        this.OnAppFileDeleted?.Invoke(this, new ConfigurationWatcherEventArgs()
                        {
                            Builders = new[] { oldBuilder.Builder }
                        });
                    });

                this.WriteAppToLog(change, oldBuilder);
            }
            break;
            }
        }