Ejemplo n.º 1
0
        /// <summary>
        /// Process the file directory
        /// </summary>
        public FileMatchConfigurationProvider(IConfigurationManager configurationManager, IPolicyEnforcementService pepService, ILocalizationService localizationService)
        {
            this.m_pepService          = pepService;
            this.m_configuration       = configurationManager.GetSection <FileMatchConfigurationSection>();
            this.m_localizationService = localizationService;
            // When application has started
            ApplicationServiceContext.Current.Started += (o, e) =>
            {
                try
                {
                    if (this.m_configuration == null)
                    {
                        this.m_matchConfigurations = new ConcurrentDictionary <string, ConfigCacheObject>();
                        return;
                    }

                    this.m_matchConfigurations = new ConcurrentDictionary <string, ConfigCacheObject>();
                    this.m_tracer.TraceInfo("Loading match configurations...");
                    foreach (var configDir in this.m_configuration.FilePath)
                    {
                        if (!Path.IsPathRooted(configDir.Path))
                        {
                            configDir.Path = Path.Combine(Path.GetDirectoryName(this.GetType().Assembly.Location), configDir.Path);
                        }

                        if (!Directory.Exists(configDir.Path))
                        {
                            this.m_tracer.TraceWarning("Skipping {0} because it doesn't exist!", configDir.Path);
                        }
                        else
                        {
                            foreach (var fileName in Directory.GetFiles(configDir.Path, "*.xml"))
                            {
                                this.m_tracer.TraceInfo("Attempting load of {0}", fileName);
                                try
                                {
                                    MatchConfiguration config = null;
                                    using (var fs = System.IO.File.OpenRead(fileName))
                                    {
                                        config = MatchConfiguration.Load(fs);
                                    }

                                    var originalPath = fileName;
                                    if (!Guid.TryParse(Path.GetFileNameWithoutExtension(fileName), out Guid uuid) || uuid != config.Uuid) // Migrate the config
                                    {
                                        originalPath = Path.Combine(configDir.Path, $"{config.Uuid}.xml");
                                        File.Move(fileName, originalPath);
                                        using (var fs = File.Create(originalPath))
                                        {
                                            config.Save(fs);
                                        }
                                    }

                                    this.m_matchConfigurations.TryAdd(config.Id, new ConfigCacheObject()
                                    {
                                        OriginalFilePath = originalPath,
                                        Configuration    = config
                                    });
                                }
                                catch (Exception ex)
                                {
                                    this.m_tracer.TraceWarning("Could not load {0} - SKIPPING - {1}", fileName, ex.Message);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.m_tracer.TraceError("Could not fully load configuration for matching : {0}", ex);
                }
            };
        }