Skip to content

Commit

Permalink
Added settings to monitor configuration
Browse files Browse the repository at this point in the history
Process Path/Filter setting in DirectoryMonitors
  • Loading branch information
claco committed Nov 8, 2008
1 parent 5ee818f commit 5b1aa6b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 13 deletions.
7 changes: 7 additions & 0 deletions Siphon/Configuration/MonitorElement.vb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ Namespace Configuration
End Get
End Property

<ConfigurationProperty("settings")> _
Public Overridable ReadOnly Property Settings() As NameValueConfigurationCollection
Get
Return Me.Item("settings")
End Get
End Property

''' <summary>
''' Creates an instance of the currently configured monitor.
''' </summary>
Expand Down
18 changes: 8 additions & 10 deletions Siphon/Configuration/TimeElementCollection.vb
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@ Namespace Configuration
End Property

''' <summary>
''' Returns a monitor element by name.
''' Creates a new time element.
''' </summary>
''' <param name="name">String. The name of the monitor element to return.</param>
''' <value></value>
''' <returns>MonitorElement</returns>
''' <returns>TimeElement</returns>
''' <remarks></remarks>
Default Public Overridable Overloads ReadOnly Property Item(ByVal name As String) As TimeElement
Get
Return DirectCast(Me.BaseGet(name), TimeElement)
End Get
End Property

Protected Overloads Overrides Function CreateNewElement() As System.Configuration.ConfigurationElement
Return New TimeElement
End Function

''' <summary>
''' Gets he key for a time element.
''' </summary>
''' <param name="element">TimeElement. The time element to get the key for.</param>
''' <returns>TimeSpan</returns>
''' <remarks></remarks>
Protected Overrides Function GetElementKey(ByVal element As System.Configuration.ConfigurationElement) As Object
Return DirectCast(element, TimeElement).Value
End Function
Expand Down
33 changes: 31 additions & 2 deletions Siphon/Monitors/DirectoryMonitor.vb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Imports log4net
Imports System.Configuration
Imports log4net
Imports ChrisLaco.Siphon.Monitors
Imports ChrisLaco.Siphon.Schedules
Imports ChrisLaco.Siphon.Processors
Expand All @@ -14,6 +15,9 @@ Namespace Monitors

Private Shared ReadOnly Log As ILog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod.DeclaringType)
Private Const DEFAULT_FILTER As String = "*"
Private Const CONFIG_SETTING_PATH As String = "Path"
Private Const CONFIG_SETTING_FILTER As String = "Filter"

Private _createMissingFolders As Boolean
Private _filter As String = DEFAULT_FILTER
Private _path As String = String.Empty
Expand Down Expand Up @@ -60,6 +64,27 @@ Namespace Monitors
End Set
End Property

''' <summary>
''' Initializes the monitor using the supplied monitor configuration settings.
''' </summary>
''' <param name="config">MonitorElement. The configuraiton for the current monitor.</param>
''' <remarks></remarks>
Public Overrides Sub Initialize(ByVal config As Configuration.MonitorElement)
MyBase.Initialize(config)

Dim path As NameValueConfigurationElement = config.Settings(CONFIG_SETTING_PATH)
If path Is Nothing OrElse String.IsNullOrEmpty(path.Value) Then
Throw New ConfigurationErrorsException(String.Format("The setting {0} is required", CONFIG_SETTING_PATH))
Else
Me.Path = path.Value
End If

Dim filter As NameValueConfigurationElement = config.Settings(CONFIG_SETTING_FILTER)
If Not filter Is Nothing Then
Me.Filter = filter.Value
End If
End Sub

''' <summary>
''' Gets/sets the file filter to apply to the directory.
''' </summary>
Expand Down Expand Up @@ -90,7 +115,11 @@ Namespace Monitors
Return _path
End Get
Set(ByVal value As String)
_path = value.Trim
If String.IsNullOrEmpty(value) Then
Throw New ArgumentException("Path can not be empty or null")
Else
_path = value.Trim
End If
End Set
End Property

Expand Down
10 changes: 9 additions & 1 deletion SiphonTests/Configuration/ConfigurationTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ Namespace Configuration
REM Test settingss
Assert.AreEqual("IntervalMonitor", section.Monitors(0).Name)
Assert.AreEqual("ChrisLaco.Siphon.Monitors.LocalDirectoryMonitor, Siphon", section.Monitors(0).Type)
Assert.AreEqual(2, section.Monitors(0).Settings.Count)
Assert.AreEqual("C:\Temp", section.Monitors(0).Settings("Path").Value)
Assert.AreEqual("*.tmp", section.Monitors(0).Settings("Filter").Value)
Assert.AreEqual("ChrisLaco.Siphon.Schedules.IntervalSchedule, Siphon", section.Monitors(0).Schedule.Type)
Assert.AreEqual("ChrisLaco.Tests.Siphon.Processors.MockFileProcessor, SiphonTests", section.Monitors(0).Processor.Type)
Assert.AreEqual(New TimeSpan(1, 2, 3, 4), section.Monitors(0).Schedule.Interval.Value)

Assert.AreEqual("DailyMonitor", section.Monitors(1).Name)
Assert.AreEqual("ChrisLaco.Siphon.Monitors.FtpDirectoryMonitor, Siphon", section.Monitors(1).Type)
Assert.AreEqual(1, section.Monitors(1).Settings.Count)
Assert.AreEqual("ftp://foo.bar.baz/", section.Monitors(1).Settings("Path").Value)
Assert.AreEqual("ChrisLaco.Siphon.Schedules.DailySchedule, Siphon", section.Monitors(1).Schedule.Type)
Assert.AreEqual("ChrisLaco.Tests.Siphon.Processors.MockQueueMessageProcessor, SiphonTests", section.Monitors(1).Processor.Type)
Assert.AreEqual(3, section.Monitors(1).Schedule.Daily.Count)
Expand Down Expand Up @@ -55,15 +60,18 @@ Namespace Configuration
Assert.IsInstanceOfType(GetType(MockQueueMessageProcessor), processor)

REM Test monitor create instances
Dim monitor As IDataMonitor = section.Monitors(0).CreateInstance
Dim monitor As IDataMonitor = section.Monitors("IntervalMonitor").CreateInstance
Assert.IsInstanceOfType(GetType(LocalDirectoryMonitor), monitor)
Assert.IsInstanceOfType(GetType(IntervalSchedule), monitor.Schedule)
Assert.AreEqual("C:\Temp", DirectCast(monitor, LocalDirectoryMonitor).Path)
Assert.AreEqual("*.tmp", DirectCast(monitor, LocalDirectoryMonitor).Filter)
Assert.AreEqual(New TimeSpan(1, 2, 3, 4), DirectCast(monitor.Schedule, IntervalSchedule).Interval)
Assert.IsInstanceOfType(GetType(MockFileProcessor), monitor.Processor)

monitor = section.Monitors(1).CreateInstance
Assert.IsInstanceOfType(GetType(FtpDirectoryMonitor), monitor)
Assert.IsInstanceOfType(GetType(DailySchedule), monitor.Schedule)
Assert.AreEqual("ftp://foo.bar.baz/", DirectCast(monitor, FtpDirectoryMonitor).Path)
Assert.AreEqual(3, DirectCast(monitor.Schedule, DailySchedule).Times.Count)
Assert.AreEqual(New TimeSpan(1, 23, 0), DirectCast(monitor.Schedule, DailySchedule).Times(0))
Assert.AreEqual(New TimeSpan(12, 23, 0), DirectCast(monitor.Schedule, DailySchedule).Times(1))
Expand Down
7 changes: 7 additions & 0 deletions SiphonTests/Configuration/test.exe.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
<siphon>
<monitors>
<monitor name="IntervalMonitor" type="ChrisLaco.Siphon.Monitors.LocalDirectoryMonitor, Siphon">
<settings>
<add name="Path" value="C:\Temp" />
<add name="Filter" value="*.tmp" />
</settings>
<schedule type="ChrisLaco.Siphon.Schedules.IntervalSchedule, Siphon">
<interval value="1.2:3:4" />
</schedule>
<processor type="ChrisLaco.Tests.Siphon.Processors.MockFileProcessor, SiphonTests" />
</monitor>
<monitor name="DailyMonitor" type="ChrisLaco.Siphon.Monitors.FtpDirectoryMonitor, Siphon">
<settings>
<add name="Path" value="ftp://foo.bar.baz/" />
</settings>
<schedule type="ChrisLaco.Siphon.Schedules.DailySchedule, Siphon">
<daily>
<time value="1:23" />
Expand Down

0 comments on commit 5b1aa6b

Please sign in to comment.