Esempio n. 1
0
        public ExtensionManager(DataRepository dataRepository, string extensionModuleFolder, DataReporterSetting[] dataReporterSettings = null, DataListenerSetting[] dataListenerSettings = null, Assembly[] assemblies = null)
        {
            _dataRepository = dataRepository;

            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

            _extensionsPath = Path.Combine(baseDirectory, extensionModuleFolder);
            if (!Directory.Exists(_extensionsPath))
            {
                Directory.CreateDirectory(_extensionsPath);
            }

            if (dataReporterSettings != null && dataReporterSettings.Length > 0)
            {
                foreach (var setting in dataReporterSettings)
                {
                    _dataReporterSettingDictionary[setting.Name] = setting;
                }
            }
            else
            {
                _dataReporterSettingDictionary["*"] = new DataReporterSetting("*")
                {
                    IsLoaded = true
                };
            }
            if (dataListenerSettings != null && dataListenerSettings.Length > 0)
            {
                foreach (var setting in dataListenerSettings)
                {
                    _dataListenerSettingDictionary[setting.Name] = setting;
                }
            }
            else
            {
                _dataListenerSettingDictionary["*"] = new DataListenerSetting("*")
                {
                    IsLoaded = true
                };
            }
            LoadExtensions(_extensionsPath, assemblies);

            InitializeExtensions();

            _configFolderWatcher                     = new FileSystemWatcher();
            _configFolderWatcher.Path                = _extensionsPath;
            _configFolderWatcher.Changed            += ConfigFolderWatcherOnChanged;
            _configFolderWatcher.EnableRaisingEvents = true;
        }
Esempio n. 2
0
        public static bool SetDataListener(string name, string configFile = "", bool isTestMode = false, SimpleSettings settings = null)
        {
            lock (_lockObject)
            {
                if (DataListenerSettingDictionary.TryGetValue(name, out var dataListenerSetting))
                {
                    Logger.Error($"'{name}' is alreay regeistered as a data listener.");
                    return(false);
                }

                DataListenerSettingDictionary[name] = new DataListenerSetting(name, configFile, isTestMode, settings);
            }

            return(true);
        }
Esempio n. 3
0
        private void InitializeExtensions()
        {
            try
            {
                foreach (Type extensionType in _dataListenerTypeDictionary.Values)
                {
                    DataListenerSetting extensionSetting = null;
                    if (!_dataListenerSettingDictionary.TryGetValue(extensionType.Name, out extensionSetting))
                    {
                        if (!_dataListenerSettingDictionary.TryGetValue("*", out extensionSetting))
                        {
                            extensionSetting = null;
                        }
                    }

                    if (extensionSetting != null)
                    {
                        try
                        {
                            if (Activator.CreateInstance(extensionType) is IDataListener extension)
                            {
                                string configFilepath = Path.Combine(_extensionsPath, extensionSetting.ConfigFile);
                                if (string.IsNullOrWhiteSpace(extensionSetting.ConfigFile))
                                {
                                    configFilepath = Path.Combine(_extensionsPath, $"{extensionSetting.Name}.xml");
                                }
                                extension?.Initialize(configFilepath, extensionSetting.IsTestMode, extensionSetting.Settings, _dataRepository);
                                extensionSetting.IsLoaded = true;

                                _dataListenerDictionary[extensionType.Name] = extension;
                            }
                            else
                            {
                                Logger.Error($"Cannot create instance of '{extensionType.FullName}' as a IDataListener.");
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(e, $"Cannot initialize '{extensionSetting.Name}' as a IDataListener.");
                        }
                    }
                }

                foreach (Type extensionType in _dataReporterTypeDictionary.Values)
                {
                    DataReporterSetting extensionSetting = null;
                    if (!_dataReporterSettingDictionary.TryGetValue(extensionType.Name, out extensionSetting))
                    {
                        if (!_dataReporterSettingDictionary.TryGetValue("*", out extensionSetting))
                        {
                            extensionSetting = null;
                        }
                    }

                    if (extensionSetting != null)
                    {
                        try
                        {
                            if (Activator.CreateInstance(extensionType) is IDataReporter extension)
                            {
                                _dataReporterDictionary[extensionType.Name] = extension;

                                string configFilepath = Path.Combine(_extensionsPath, extensionSetting.ConfigFile);
                                if (string.IsNullOrWhiteSpace(extensionSetting.ConfigFile))
                                {
                                    configFilepath = Path.Combine(_extensionsPath, $"{extensionType.Name}.xml");
                                }
                                extension?.Initialize(configFilepath, extensionSetting.IsTestMode, extensionSetting.Settings, _dataRepository);
                                extensionSetting.IsLoaded = true;
                            }
                            else
                            {
                                Logger.Error($"Cannot create instance of '{extensionType.FullName}' as a IDataReporter.");
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(e, $"Cannot initialize '{extensionSetting.Name}' as a IDataReporter.");
                        }
                    }
                }
                foreach (var extensionSetting in _dataListenerSettingDictionary.Values)
                {
                    if (!extensionSetting.IsLoaded)
                    {
                        Logger.Error($"Cannot find IDataListener '{extensionSetting.Name}'.");
                    }
                }
                foreach (var extensionSetting in _dataReporterSettingDictionary.Values)
                {
                    if (!extensionSetting.IsLoaded)
                    {
                        Logger.Error($"Cannot find IDataReporter '{extensionSetting.Name}'.");
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "InitializeExtensions:");
            }
        }