Skip to content
Snippets Groups Projects
Commit 35553e56 authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Move validation of the configuration into the ServiceConfig class.

parent 9585edf8
No related branches found
No related tags found
No related merge requests found
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
<HintPath>..\..\robosharp\RoboSharp\bin\Debug\RoboSharp.dll</HintPath> <HintPath>..\..\robosharp\RoboSharp\bin\Debug\RoboSharp.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Configuration.Install" /> <Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.DirectoryServices" /> <Reference Include="System.DirectoryServices" />
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.ServiceProcess; using System.ServiceProcess;
...@@ -126,6 +127,11 @@ namespace AutoTx ...@@ -126,6 +127,11 @@ namespace AutoTx
_config = ServiceConfig.Deserialize(_configPath); _config = ServiceConfig.Deserialize(_configPath);
writeLogDebug("Loaded config from " + _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) { catch (Exception ex) {
writeLog("Error loading configuration XML: " + ex.Message, true); writeLog("Error loading configuration XML: " + ex.Message, true);
// this should terminate the service process: // this should terminate the service process:
...@@ -158,33 +164,6 @@ namespace AutoTx ...@@ -158,33 +164,6 @@ namespace AutoTx
public void CheckConfiguration() { public void CheckConfiguration() {
var configInvalid = false; var configInvalid = false;
try { 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); _incomingPath = Path.Combine(_config.SourceDrive, _config.IncomingDirectory);
_managedPath = Path.Combine(_config.SourceDrive, _config.ManagedDirectory); _managedPath = Path.Combine(_config.SourceDrive, _config.ManagedDirectory);
if (CheckSpoolingDirectories() == false) { if (CheckSpoolingDirectories() == false) {
...@@ -192,28 +171,11 @@ namespace AutoTx ...@@ -192,28 +171,11 @@ namespace AutoTx
configInvalid = true; 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 // CurrentTransferSrc
if (_status.CurrentTransferSrc.Length > 0 if (_status.CurrentTransferSrc.Length > 0
&& !Directory.Exists(_status.CurrentTransferSrc)) { && !Directory.Exists(_status.CurrentTransferSrc)) {
writeLog("WARNING: status file contains non-existing source path of an " + writeLog("WARNING: status file contains non-existing source path of an " +
"unfinished transfer: " + _status.CurrentTransferSrc); "unfinished transfer: " + _status.CurrentTransferSrc);
_status.CurrentTransferSrc = ""; _status.CurrentTransferSrc = "";
} }
...@@ -221,15 +183,9 @@ namespace AutoTx ...@@ -221,15 +183,9 @@ namespace AutoTx
if (_status.CurrentTargetTmp.Length > 0 if (_status.CurrentTargetTmp.Length > 0
&& !Directory.Exists(ExpandCurrentTargetTmp())) { && !Directory.Exists(ExpandCurrentTargetTmp())) {
writeLog("WARNING: status file contains non-existing temporary path of an " + writeLog("WARNING: status file contains non-existing temporary path of an " +
"unfinished transfer: " + _status.CurrentTargetTmp); "unfinished transfer: " + _status.CurrentTargetTmp);
_status.CurrentTargetTmp = ""; _status.CurrentTargetTmp = "";
} }
// ServiceTimer
if (_config.ServiceTimer < 1000) {
writeLog("ERROR: ServiceTimer must not be smaller than 1000 ms!");
configInvalid = true;
}
} }
catch (Exception ex) { catch (Exception ex) {
writeLog("Error in CheckConfiguration(): " + ex.Message + " " + ex.StackTrace); writeLog("Error in CheckConfiguration(): " + ex.Message + " " + ex.StackTrace);
...@@ -326,6 +282,11 @@ namespace AutoTx ...@@ -326,6 +282,11 @@ namespace AutoTx
catch (Exception ex) { catch (Exception ex) {
writeLog("CheckGraceLocation() failed: " + ex.Message, true); writeLog("CheckGraceLocation() failed: " + ex.Message, true);
} }
if (!string.IsNullOrEmpty(_config.ValidationWarnings)) {
writeLog("WARNING: some configuration settings might not be optimal:\n" +
_config.ValidationWarnings);
}
} }
#endregion #endregion
......
...@@ -12,7 +12,10 @@ namespace AutoTx.XmlWrapper ...@@ -12,7 +12,10 @@ namespace AutoTx.XmlWrapper
[Serializable] [Serializable]
public class ServiceConfig public class ServiceConfig
{ {
[XmlIgnore] public string ValidationWarnings;
public ServiceConfig() { public ServiceConfig() {
ValidationWarnings = "";
// set values for the optional XML elements: // set values for the optional XML elements:
SmtpHost = ""; SmtpHost = "";
SmtpPort = 25; SmtpPort = 25;
...@@ -134,8 +137,49 @@ namespace AutoTx.XmlWrapper ...@@ -134,8 +137,49 @@ namespace AutoTx.XmlWrapper
var reader = File.OpenText(file); var reader = File.OpenText(file);
var config = (ServiceConfig) xs.Deserialize(reader); var config = (ServiceConfig) xs.Deserialize(reader);
reader.Close(); reader.Close();
ValidateConfiguration(config);
return 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment