Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.IO;
11 : using Cqrs.Configuration;
12 :
13 : namespace Cqrs.Bus
14 : {
15 : /// <summary>
16 : /// Indicates the position in a store where the stream has been read up to by storing the value in a file.
17 : /// </summary>
18 : public class FileBasedLastEventProcessedStore : IStoreLastEventProcessed
19 1 : {
20 : /// <summary>
21 : /// The configuration setting that holds the location of file to store position information in.
22 : /// </summary>
23 : public const string AppSettingsKey = "Cqrs.FileBasedLastEventProcessed.Location";
24 :
25 : /// <summary>
26 : /// The default location of the file to store position information in.
27 : /// </summary>
28 : public const string AppSettingsDefaultValue = @"%EVENTSTORE_HOME%\LastEventProcessedLocation";
29 :
30 : /// <summary>
31 : /// The relative or absolute path of the file to store the current location in
32 : /// </summary>
33 : protected string FileName { get; private set; }
34 :
35 : /// <summary>
36 : /// Instantiates a new instance of <see cref="FileBasedLastEventProcessedStore"/>.
37 : /// </summary>
38 1 : public FileBasedLastEventProcessedStore(IConfigurationManager configurationManager)
39 : {
40 : string location = configurationManager.GetSetting(AppSettingsKey);
41 : if (string.IsNullOrEmpty(location))
42 : {
43 : location = AppSettingsDefaultValue;
44 : }
45 :
46 : FileName = Environment.ExpandEnvironmentVariables(location);
47 : }
48 :
49 : /// <summary>
50 : /// Reads and writes the location within the store where the stream has been read up to <see cref="FileName"/>.
51 : /// </summary>
52 : public string EventLocation
53 : {
54 : get
55 : {
56 : return File.Exists(FileName) ? File.ReadAllText(FileName) : string.Empty;
57 : }
58 :
59 : set
60 : {
61 : File.WriteAllText(FileName, value);
62 : }
63 : }
64 : }
65 : }
|