Skip to content
This repository has been archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
Refactoring of core #7 and configuration reader #11
Browse files Browse the repository at this point in the history
  • Loading branch information
spetz committed Jul 31, 2016
1 parent 3552ed5 commit db504bc
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 75 deletions.
25 changes: 12 additions & 13 deletions src/Examples/Warden.Spawn.Examples.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Warden.Spawn.Core;
Expand All @@ -11,22 +10,22 @@ public class Program
{
public static void Main(string[] args)
{
var watcherConfigurationTypes = new Dictionary<string, Type>
var watcherConfigurations = new List<IConfigurationType>
{
["Web"] = typeof(WebWatcherSpawnConfiguration)
WebWatcherSpawnConfiguration.Type
};
var integrationConfigurationTypes = new Dictionary<string, Type>();
var spawnConfigurationReader = new WardenSpawnJsonConfigurationReader(watcherConfigurationTypes,
integrationConfigurationTypes);
var configurationFile = File.ReadAllText("configuration.json");
var spawnConfiguration = spawnConfigurationReader.Read(configurationFile);
var extensions = new List<IExtension>
var configurationReader = new WardenSpawnJsonConfigurationReader(watcherConfigurations);
var configurationInput = File.ReadAllText("configuration.json");
var configuration = configurationReader.Read(configurationInput);

var watcherConfigurators = new List<IConfiguratorType>
{
new Extension("Web", ExtensionType.Watcher, typeof(WebWatcherSpawnConfigurator))
WebWatcherSpawnConfigurator.Type
};
var spawnConfigurator = new WardenSpawnConfigurator(extensions);
var spawn = spawnConfigurator.Configure(spawnConfiguration);
var configurator = new WardenSpawnConfigurator(watcherConfigurators);
var spawn = configurator.Configure(configuration);
var warden = spawn.Spawn();

System.Console.WriteLine($"Warden: '{warden.Name}' has been created and started monitoring.");
Task.WaitAll(warden.StartAsync());
}
Expand Down
28 changes: 23 additions & 5 deletions src/Examples/Warden.Spawn.Examples.Console/configuration.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
{
"wardenName": "Warden Spawn",
"watchers": [
{
"watchers": [{
"type": "Web",
"configuration": {
"url": "http://www.google.com"
}
},
"hooks": {
"name": "onCompletedAsync",
"actions": [
{
"type": "integration",
"integration": "sendGrid",
"checkResult": "invalid",
"action": {
"name": "sendEmailAsync",
"params": [
{
"name": "message",
"value": "Web watcher has returned invalid result."
}
]
}
}
]
}
}
]
}
]
}
16 changes: 16 additions & 0 deletions src/Warden.Spawn/Core/ConfigurationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace Warden.Spawn.Core
{
public class ConfigurationType : IConfigurationType
{
public Type Type { get; }
public string Name { get; }

public ConfigurationType(Type type, string name)
{
Type = type;
Name = name;
}
}
}
16 changes: 16 additions & 0 deletions src/Warden.Spawn/Core/ConfiguratorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace Warden.Spawn.Core
{
public class ConfiguratorType : IConfiguratorType
{
public Type Type { get; }
public string Name { get; }

public ConfiguratorType(Type type, string name)
{
Type = type;
Name = name;
}
}
}
18 changes: 0 additions & 18 deletions src/Warden.Spawn/Core/Extension.cs

This file was deleted.

8 changes: 0 additions & 8 deletions src/Warden.Spawn/Core/ExtensionType.cs

This file was deleted.

10 changes: 10 additions & 0 deletions src/Warden.Spawn/Core/IConfigurationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Warden.Spawn.Core
{
public interface IConfigurationType
{
Type Type { get; }
string Name { get; }
}
}
10 changes: 10 additions & 0 deletions src/Warden.Spawn/Core/IConfiguratorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Warden.Spawn.Core
{
public interface IConfiguratorType
{
Type Type { get; }
string Name { get; }
}
}
11 changes: 0 additions & 11 deletions src/Warden.Spawn/Core/IExtension.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Warden.Spawn/Core/WardenSpawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class WardenSpawn : IWardenSpawn

public WardenSpawn(string wardenName,
IEnumerable<IWatcher> watchers,
IEnumerable<IIntegration> integrations)
IEnumerable<IIntegration> integrations = null)
{
_wardenName = wardenName;
_watchers = watchers ?? Enumerable.Empty<IWatcher>();
Expand Down
15 changes: 6 additions & 9 deletions src/Warden.Spawn/Core/WardenSpawnConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,37 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Warden.Integrations;
using Warden.Watchers;

namespace Warden.Spawn.Core
{
public class WardenSpawnConfigurator : IWardenSpawnConfigurator
{
private readonly IEnumerable<IExtension> _extensions;
private readonly IEnumerable<IConfiguratorType> _watcherConfiguratorTypes;

public WardenSpawnConfigurator(IEnumerable<IExtension> extensions)
public WardenSpawnConfigurator(IEnumerable<IConfiguratorType> watcherConfiguratorTypes)
{
_extensions = extensions;
_watcherConfiguratorTypes = watcherConfiguratorTypes;
}

public IWardenSpawn Configure(IWardenSpawnConfiguration configuration)
{
var watchers = new List<IWatcher>();
var integrations = new List<IIntegration>();
foreach (var watcherConfiguration in configuration.Watchers)
{
var extension = _extensions.Where(x => x.Type == ExtensionType.Watcher)
.FirstOrDefault(x => x.Name == watcherConfiguration.Name);
var extension = _watcherConfiguratorTypes.FirstOrDefault(x => x.Name.Equals(watcherConfiguration.Name));
if (extension == null)
continue;

var configurator = Activator.CreateInstance(extension.ConfiguratorType);
var configurator = Activator.CreateInstance(extension.Type);
var method = configurator.GetType()
.GetRuntimeMethods()
.First(x => x.Name.Equals("Configure"));
var watcher = method.Invoke(configurator, new object[] {watcherConfiguration}) as IWatcher;
watchers.Add(watcher);
}

return new WardenSpawn(configuration.WardenName, watchers, integrations);
return new WardenSpawn(configuration.WardenName, watchers);
}
}
}
30 changes: 20 additions & 10 deletions src/Warden.Spawn/Core/WardenSpawnJsonConfigurationReader.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Warden.Spawn.Core
{
public class WardenSpawnJsonConfigurationReader : IConfigurationReader<IWardenSpawnConfiguration>
{
private readonly IDictionary<string, Type> _watcherConfigurationTypes;
private readonly IDictionary<string, Type> _integrationConfigurationTypes;
private readonly IEnumerable<IConfigurationType> _watcherConfigurationTypes;
private readonly IEnumerable<IConfigurationType> _integrationConfigurationTypes;
private readonly JsonSerializerSettings _jsonSerializerSettings;

private readonly JsonSerializerSettings _defaultJsonSerializerSettings = new JsonSerializerSettings
Expand All @@ -29,12 +30,12 @@ public class WardenSpawnJsonConfigurationReader : IConfigurationReader<IWardenSp
}
};

public WardenSpawnJsonConfigurationReader(IDictionary<string, Type> watcherConfigurationTypes,
IDictionary<string, Type> integrationConfigurationTypes,
public WardenSpawnJsonConfigurationReader(IEnumerable<IConfigurationType> watcherConfigurationTypes,
IEnumerable<IConfigurationType> integrationConfigurationTypes = null,
JsonSerializerSettings jsonSerializerSettings = null)
{
_watcherConfigurationTypes = watcherConfigurationTypes;
_integrationConfigurationTypes = integrationConfigurationTypes;
_watcherConfigurationTypes = watcherConfigurationTypes ?? Enumerable.Empty<IConfigurationType>();
_integrationConfigurationTypes = integrationConfigurationTypes ?? Enumerable.Empty<IConfigurationType>();
_jsonSerializerSettings = jsonSerializerSettings ?? _defaultJsonSerializerSettings;
}

Expand All @@ -56,9 +57,13 @@ private IEnumerable<IWatcherSpawnConfiguration> ResolveWatcherConfigurations(dyn

foreach (var watcher in watchers)
{
var watcherType = _watcherConfigurationTypes[watcher.type.ToString()];
var configuration = _watcherConfigurationTypes
.FirstOrDefault(x => x.Name.Equals(watcher.type.ToString()));
if (configuration == null)
continue;

var watcherConfigurationText = JsonConvert.SerializeObject(watcher.configuration);
var watcherConfiguration = JsonConvert.DeserializeObject(watcherConfigurationText, watcherType);
var watcherConfiguration = JsonConvert.DeserializeObject(watcherConfigurationText, configuration.Type);

yield return watcherConfiguration;
}
Expand All @@ -72,9 +77,14 @@ private IEnumerable<IIntegrationSpawnConfiguration> ResolveIntegrationConfigurat

foreach (var integration in integrations)
{
var integrationType = _integrationConfigurationTypes[integration.type.ToString()];
var configuration = _integrationConfigurationTypes.FirstOrDefault(x =>
x.Name.Equals(integration.type.ToString()));
if (configuration == null)
continue;

var integrationConfigurationText = JsonConvert.SerializeObject(integration.configuration);
var integrationConfiguration = JsonConvert.DeserializeObject(integrationConfigurationText, integrationType);
var integrationConfiguration = JsonConvert.DeserializeObject(integrationConfigurationText,
configuration.Type);

yield return integrationConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Warden.Spawn.Watchers.Web
public class WebWatcherSpawnConfiguration : IWatcherSpawnConfiguration
{
public string Name => "Web";
public static IConfigurationType Type => new ConfigurationType(typeof(WebWatcherSpawnConfiguration), "Web");
public string Url { get; protected set; }

protected WebWatcherSpawnConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Warden.Spawn.Watchers.Web
{
public class WebWatcherSpawnConfigurator : IWatcherSpawnConfigurator<WebWatcherSpawnConfiguration>
{
public static IConfiguratorType Type => new ConfiguratorType(typeof(WebWatcherSpawnConfigurator), "Web");

public IWatcher Configure(WebWatcherSpawnConfiguration configuration)
{
var watcherConfiguration = WebWatcherConfiguration
Expand Down

0 comments on commit db504bc

Please sign in to comment.