public OptionsManager(string path, Logger logger) { DefaultOptions = new ETLOptions(); string options; try { using (StreamReader sr = new StreamReader($"{path}\\config.xml")) { options = sr.ReadToEnd(); } Xml = new ETLXmlOptions(options); xmlConfigured = true; Report = Xml.Report; Report += "Xml options loaded successfully. "; } catch { xmlConfigured = false; } try { using (StreamReader sr = new StreamReader($"{path}\\appsettings.json")) { options = sr.ReadToEnd(); } Json = new ETLJsonOptions(options); jsonConfigured = true; Report = Json.Report; Report += "Json options loaded successfully. "; } catch { jsonConfigured = false; } if (!jsonConfigured && !xmlConfigured) { Report = "Failed to load both of json and xml. Using default options and creating appdettings.json"; if (!File.Exists($"{path}\\appsettings.json")) { string json = Converter.Converter.SerializeJson(DefaultOptions); Validator.CreateDirectoryIfNotExist(path); using (StreamWriter sw = new StreamWriter($"{path}\\appsettings.json")) { sw.Write(json); } } if (!File.Exists($"{path}\\config.xml")) { string xml = Converter.Converter.SerializeXML(DefaultOptions); Validator.CreateDirectoryIfNotExist(path); using (StreamWriter sw = new StreamWriter($"{path}\\config.xml")) { sw.Write(xml); } } } }
public ETLXmlOptions(string xml) : base() { ETLOptions options = Converter.Converter.DeserializeXML <ETLOptions>(xml); ArchiveOptions = options.ArchiveOptions; EncryptionOptions = options.EncryptionOptions; SendingOptions = options.SendingOptions; LoggingOptions = options.LoggingOptions; Report = Validator.Validate(this); }
public ETLJsonOptions(string json) : base() { ETLOptions options = Converter.Converter.DeserializeJson <ETLOptions>(json); ArchiveOptions = options.ArchiveOptions; EncryptionOptions = options.EncryptionOptions; LoggingOptions = options.LoggingOptions; SendingOptions = options.SendingOptions; Report = Validator.Validate(this); }
public static string Validate(ETLOptions options) { string report = ""; #region Sending Validation SendingOptions sending = options.SendingOptions; if (!CreateDirectoryIfNotExist(sending.SourceDirectory)) { sending.SourceDirectory = "C:\\FileWatcher\\source"; CreateDirectoryIfNotExist(sending.SourceDirectory); report += "Cannot open source directory, using default. "; } if (!CreateDirectoryIfNotExist(sending.TargetDirectory)) { sending.TargetDirectory = "C:\\FileWatcher\\target"; report += "Cannot open target directory, using default. "; CreateDirectoryIfNotExist(sending.TargetDirectory); } if (!CreateDirectoryIfNotExist(sending.ArchiveDirectory)) { sending.ArchiveDirectory = "C:\\FileWatcher\\target\\archive"; report += "Cannot open source archive, using default. "; CreateDirectoryIfNotExist(sending.ArchiveDirectory); } #endregion #region Logging Validation LoggingOptions logging = options.LoggingOptions; if (!CreateFileIfNotExist(logging.LogPath)) { logging.LogPath = "C:\\FileWatcher\\target\\log.txt"; report += "Cannot open source log file, using default. "; CreateFileIfNotExist(logging.LogPath); } #endregion #region Encryption Validation EncryptionOptions encryption = options.EncryptionOptions; if (!encryption.RandomKey && encryption.Key.Length != 16) { report += "Encryption key's length must be 16, using random key. "; } #endregion #region Archive Validation ArchiveOptions archive = options.ArchiveOptions; if ((int)archive.CompressionLevel < 0 || (int)archive.CompressionLevel > 2) { archive.CompressionLevel = System.IO.Compression.CompressionLevel.Optimal; report += "Compression level value can't be below zero and abowe 2, using default value. "; } #endregion return(report); }
void Config() { string directory = AppDomain.CurrentDomain.BaseDirectory; optionsManager = new OptionsManager(directory, logger); ETLOptions options = optionsManager.GetOptions <ETLOptions>() as ETLOptions; logger = new Logger(options.LoggingOptions); logger.Log(optionsManager.Report); watcher = new FileSystemWatcher(options.SendingOptions.SourceDirectory); watcher.Filter = "*.txt"; watcher.Created += Created; watcher.EnableRaisingEvents = true; }
Options SeekForOption <T>(ETLOptions options) { if (typeof(T) == typeof(ETLOptions)) { return(options); } string name = typeof(T).Name; try { return(options.GetType().GetProperty(name).GetValue(options, null) as Options); } catch { throw new NotImplementedException(); } }