From 35553e564ba154ec926f16c18507cfa353dc458e Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch> Date: Tue, 19 Dec 2017 17:09:26 +0100 Subject: [PATCH] Move validation of the configuration into the ServiceConfig class. --- AutoTx/ATXProject.csproj | 1 + AutoTx/AutoTx.cs | 65 ++++++------------------------ AutoTx/XmlWrapper/ServiceConfig.cs | 44 ++++++++++++++++++++ 3 files changed, 58 insertions(+), 52 deletions(-) diff --git a/AutoTx/ATXProject.csproj b/AutoTx/ATXProject.csproj index 3281374..3e86040 100644 --- a/AutoTx/ATXProject.csproj +++ b/AutoTx/ATXProject.csproj @@ -54,6 +54,7 @@ <HintPath>..\..\robosharp\RoboSharp\bin\Debug\RoboSharp.dll</HintPath> </Reference> <Reference Include="System" /> + <Reference Include="System.Configuration" /> <Reference Include="System.Configuration.Install" /> <Reference Include="System.Core" /> <Reference Include="System.DirectoryServices" /> diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs index b6b0da6..a9c0123 100644 --- a/AutoTx/AutoTx.cs +++ b/AutoTx/AutoTx.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Configuration; using System.Diagnostics; using System.Linq; using System.ServiceProcess; @@ -126,6 +127,11 @@ namespace AutoTx _config = ServiceConfig.Deserialize(_configPath); writeLogDebug("Loaded config from " + _configPath); } + catch (ConfigurationErrorsException ex) { + writeLog("ERROR validating configuration file [" + _configPath + + "]: " + ex.Message); + throw new Exception("Error validating configuration."); + } catch (Exception ex) { writeLog("Error loading configuration XML: " + ex.Message, true); // this should terminate the service process: @@ -158,33 +164,6 @@ namespace AutoTx public void CheckConfiguration() { var configInvalid = false; try { - if (string.IsNullOrEmpty(_config.SourceDrive) || - string.IsNullOrEmpty(_config.IncomingDirectory) || - string.IsNullOrEmpty(_config.ManagedDirectory)) { - writeLog("ERROR: mandatory parameter missing!"); - configInvalid = true; - } - if (_config.SourceDrive.Substring(1) != @":\") { - writeLog("ERROR: SourceDrive must be a drive letter followed by a colon" + - @" and a backslash, e.g. 'D:\'!"); - configInvalid = true; - } - var driveInfo = new DriveInfo(_config.SourceDrive); - if (driveInfo.DriveType != DriveType.Fixed) { - writeLog("ERROR: SourceDrive (" + _config.SourceDrive + ") must be a " + - "local (fixed) drive, OS reports '" + driveInfo.DriveType + "')!"); - configInvalid = true; - } - - // spooling directories: IncomingDirectory + ManagedDirectory - if (_config.IncomingDirectory.StartsWith(@"\")) { - writeLog("ERROR: IncomingDirectory must not start with a backslash!"); - configInvalid = true; - } - if (_config.ManagedDirectory.StartsWith(@"\")) { - writeLog("ERROR: ManagedDirectory must not start with a backslash!"); - configInvalid = true; - } _incomingPath = Path.Combine(_config.SourceDrive, _config.IncomingDirectory); _managedPath = Path.Combine(_config.SourceDrive, _config.ManagedDirectory); if (CheckSpoolingDirectories() == false) { @@ -192,28 +171,11 @@ namespace AutoTx configInvalid = true; } - // DestinationDirectory - if (!_config.DestinationDirectory.StartsWith(@"\\")) { - writeLog("WARNING: DestinationDirectory is no UNC path!"); - } - if (!Directory.Exists(_config.DestinationDirectory)) { - writeLog("ERROR: can't find destination: " + _config.DestinationDirectory); - configInvalid = true; - } - - // TmpTransferDir - var tmpTransferPath = Path.Combine(_config.DestinationDirectory, - _config.TmpTransferDir); - if (!Directory.Exists(tmpTransferPath)) { - writeLog("ERROR: temporary transfer dir doesn't exist: " + tmpTransferPath); - configInvalid = true; - } - // CurrentTransferSrc if (_status.CurrentTransferSrc.Length > 0 && !Directory.Exists(_status.CurrentTransferSrc)) { writeLog("WARNING: status file contains non-existing source path of an " + - "unfinished transfer: " + _status.CurrentTransferSrc); + "unfinished transfer: " + _status.CurrentTransferSrc); _status.CurrentTransferSrc = ""; } @@ -221,15 +183,9 @@ namespace AutoTx if (_status.CurrentTargetTmp.Length > 0 && !Directory.Exists(ExpandCurrentTargetTmp())) { writeLog("WARNING: status file contains non-existing temporary path of an " + - "unfinished transfer: " + _status.CurrentTargetTmp); + "unfinished transfer: " + _status.CurrentTargetTmp); _status.CurrentTargetTmp = ""; } - - // ServiceTimer - if (_config.ServiceTimer < 1000) { - writeLog("ERROR: ServiceTimer must not be smaller than 1000 ms!"); - configInvalid = true; - } } catch (Exception ex) { writeLog("Error in CheckConfiguration(): " + ex.Message + " " + ex.StackTrace); @@ -326,6 +282,11 @@ namespace AutoTx catch (Exception ex) { writeLog("CheckGraceLocation() failed: " + ex.Message, true); } + + if (!string.IsNullOrEmpty(_config.ValidationWarnings)) { + writeLog("WARNING: some configuration settings might not be optimal:\n" + + _config.ValidationWarnings); + } } #endregion diff --git a/AutoTx/XmlWrapper/ServiceConfig.cs b/AutoTx/XmlWrapper/ServiceConfig.cs index d644cdb..384bd52 100644 --- a/AutoTx/XmlWrapper/ServiceConfig.cs +++ b/AutoTx/XmlWrapper/ServiceConfig.cs @@ -12,7 +12,10 @@ namespace AutoTx.XmlWrapper [Serializable] public class ServiceConfig { + [XmlIgnore] public string ValidationWarnings; + public ServiceConfig() { + ValidationWarnings = ""; // set values for the optional XML elements: SmtpHost = ""; SmtpPort = 25; @@ -134,8 +137,49 @@ namespace AutoTx.XmlWrapper var reader = File.OpenText(file); var config = (ServiceConfig) xs.Deserialize(reader); reader.Close(); + ValidateConfiguration(config); return config; } + private static void ValidateConfiguration(ServiceConfig c) { + if (string.IsNullOrEmpty(c.SourceDrive) || + string.IsNullOrEmpty(c.IncomingDirectory) || + string.IsNullOrEmpty(c.ManagedDirectory)) + throw new ConfigurationErrorsException("mandatory parameter missing!"); + + if (c.SourceDrive.Substring(1) != @":\") + throw new ConfigurationErrorsException("SourceDrive must be a drive " + + @"letter followed by a colon and a backslash, e.g. 'D:\'!"); + + // make sure SourceDrive is a local (fixed) disk: + var driveInfo = new DriveInfo(c.SourceDrive); + if (driveInfo.DriveType != DriveType.Fixed) + throw new ConfigurationErrorsException("SourceDrive (" + c.SourceDrive + + ") must be a local (fixed) drive, OS reports '" + driveInfo.DriveType + "')!"); + + + // spooling directories: IncomingDirectory + ManagedDirectory + if (c.IncomingDirectory.StartsWith(@"\")) + throw new ConfigurationErrorsException("IncomingDirectory must not start with a backslash!"); + if (c.ManagedDirectory.StartsWith(@"\")) + throw new ConfigurationErrorsException("ManagedDirectory must not start with a backslash!"); + + if (!Directory.Exists(c.DestinationDirectory)) + throw new ConfigurationErrorsException("can't find destination: " + c.DestinationDirectory); + + var tmpTransferPath = Path.Combine(c.DestinationDirectory, c.TmpTransferDir); + if (!Directory.Exists(tmpTransferPath)) + throw new ConfigurationErrorsException("temporary transfer dir doesn't exist: " + tmpTransferPath); + + if (c.ServiceTimer < 1000) + throw new ConfigurationErrorsException("ServiceTimer must not be smaller than 1000 ms!"); + + + // NON-CRITICAL stuff just adds messages to ValidationWarnings: + // DestinationDirectory + if (!c.DestinationDirectory.StartsWith(@"\\")) + c.ValidationWarnings += " - <DestinationDirectory> is not a UNC path!\n"; + } + } } \ No newline at end of file -- GitLab