protected virtual BrowscapXmlHelper GetBrowscapXmlHelper() { if (Singleton <BrowscapXmlHelper> .Instance != null) { return(Singleton <BrowscapXmlHelper> .Instance); } //no database created if (string.IsNullOrEmpty(_researchConfig.UserAgentStringsPath)) { return(null); } //prevent multi loading data lock (_locker) { //data can be loaded while we waited if (Singleton <BrowscapXmlHelper> .Instance != null) { return(Singleton <BrowscapXmlHelper> .Instance); } var userAgentStringsPath = _fileProvider.MapPath(_researchConfig.UserAgentStringsPath); var crawlerOnlyUserAgentStringsPath = !string.IsNullOrEmpty(_researchConfig.CrawlerOnlyUserAgentStringsPath) ? _fileProvider.MapPath(_researchConfig.CrawlerOnlyUserAgentStringsPath) : string.Empty; var browscapXmlHelper = new BrowscapXmlHelper(userAgentStringsPath, crawlerOnlyUserAgentStringsPath, _fileProvider); Singleton <BrowscapXmlHelper> .Instance = browscapXmlHelper; return(Singleton <BrowscapXmlHelper> .Instance); } }
/// <summary> /// Try to write web.config file /// </summary> /// <returns></returns> protected virtual bool TryWriteWebConfig() { try { _fileProvider.SetLastWriteTimeUtc(_fileProvider.MapPath(ResearchInfrastructureDefaults.WebConfigPath), DateTime.UtcNow); return(true); } catch { return(false); } }
/// <summary> /// Save data settings to the file /// </summary> /// <param name="settings">Data settings</param> /// <param name="fileProvider">File provider</param> public static void SaveSettings(DataSettings settings, IResearchFileProvider fileProvider = null) { Singleton <DataSettings> .Instance = settings ?? throw new ArgumentNullException(nameof(settings)); fileProvider = fileProvider ?? CommonHelper.DefaultFileProvider; var filePath = fileProvider.MapPath(ResearchDataSettingsDefaults.FilePath); //create file if not exists fileProvider.CreateFile(filePath); //save data settings to the file var text = JsonConvert.SerializeObject(Singleton <DataSettings> .Instance, Formatting.Indented); fileProvider.WriteAllText(filePath, text, Encoding.UTF8); }
/// <summary> /// Load data settings /// </summary> /// <param name="filePath">File path; pass null to use the default settings file</param> /// <param name="reloadSettings">Whether to reload data, if they already loaded</param> /// <param name="fileProvider">File provider</param> /// <returns>Data settings</returns> public static DataSettings LoadSettings(string filePath = null, bool reloadSettings = false, IResearchFileProvider fileProvider = null) { if (!reloadSettings && Singleton <DataSettings> .Instance != null) { return(Singleton <DataSettings> .Instance); } fileProvider = fileProvider ?? CommonHelper.DefaultFileProvider; filePath = filePath ?? fileProvider.MapPath(ResearchDataSettingsDefaults.FilePath); //check whether file exists if (!fileProvider.FileExists(filePath)) { //if not, try to parse the file that was used in previous nopCommerce versions filePath = fileProvider.MapPath(ResearchDataSettingsDefaults.ObsoleteFilePath); if (!fileProvider.FileExists(filePath)) { return(new DataSettings()); } //get data settings from the old txt file var dataSettings = new DataSettings(); using (var reader = new StringReader(fileProvider.ReadAllText(filePath, Encoding.UTF8))) { string settingsLine; while ((settingsLine = reader.ReadLine()) != null) { var separatorIndex = settingsLine.IndexOf(':'); if (separatorIndex == -1) { continue; } var key = settingsLine.Substring(0, separatorIndex).Trim(); var value = settingsLine.Substring(separatorIndex + 1).Trim(); switch (key) { case "DataProvider": dataSettings.DataProvider = System.Enum.TryParse(value, true, out DataProviderType providerType) ? providerType : DataProviderType.Unknown; continue; case "DataConnectionString": dataSettings.DataConnectionString = value; continue; default: dataSettings.RawDataSettings.Add(key, value); continue; } } } //save data settings to the new file SaveSettings(dataSettings, fileProvider); //and delete the old one fileProvider.DeleteFile(filePath); Singleton <DataSettings> .Instance = dataSettings; return(Singleton <DataSettings> .Instance); } var text = fileProvider.ReadAllText(filePath, Encoding.UTF8); if (string.IsNullOrEmpty(text)) { return(new DataSettings()); } //get data settings from the JSON file Singleton <DataSettings> .Instance = JsonConvert.DeserializeObject <DataSettings>(text); return(Singleton <DataSettings> .Instance); }