-
Notifications
You must be signed in to change notification settings - Fork 226
/
XmlProjectStateManager.cs
208 lines (194 loc) · 6.87 KB
/
XmlProjectStateManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using ThoughtWorks.CruiseControl.Core.Util;
namespace ThoughtWorks.CruiseControl.Core.State
{
/// <summary>
/// Records the state of a project.
/// </summary>
public class XmlProjectStateManager
: IProjectStateManager
{
#region Fields
private readonly IFileSystem fileSystem;
private readonly IExecutionEnvironment executionEnvironment;
private readonly string persistanceFileName;
private Dictionary<string, bool> projectStates = null;
private bool isLoading;
#endregion
#region Constructors
/// <summary>
/// Initialise a new <see cref="XmlProjectStateManager"/> with the default path.
/// </summary>
public XmlProjectStateManager()
: this(new SystemIoFileSystem(), new ExecutionEnvironment())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="XmlProjectStateManager" /> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="executionEnvironment">The execution environment.</param>
/// <remarks></remarks>
public XmlProjectStateManager(IFileSystem fileSystem, IExecutionEnvironment executionEnvironment)
{
this.fileSystem = fileSystem;
this.executionEnvironment = executionEnvironment;
persistanceFileName = Path.Combine(this.executionEnvironment.GetDefaultProgramDataFolder(ApplicationType.Server), "ProjectsState.xml");
fileSystem.EnsureFolderExists(persistanceFileName);
}
#endregion
/// <summary>
/// Gets the name of the persistance file.
/// </summary>
/// <value>The name of the persistance file.</value>
/// <remarks></remarks>
public string PersistanceFileName
{
get { return persistanceFileName; }
}
#region Public methods
#region RecordProjectAsStopped()
/// <summary>
/// Records a project as stopped.
/// </summary>
/// <param name="projectName">The name of the project to record</param>
public void RecordProjectAsStopped(string projectName)
{
ChangeProjectState(projectName, false);
}
#endregion
#region RecordProjectAsStartable()
/// <summary>
/// Records a project as being able to start automatically.
/// </summary>
/// <param name="projectName">The name of the project to record</param>
public void RecordProjectAsStartable(string projectName)
{
ChangeProjectState(projectName, true);
}
#endregion
#region CheckIfProjectCanStart()
/// <summary>
/// Checks if a project can be started automatically.
/// </summary>
/// <param name="projectName">The name of the project to check.</param>
/// <returns></returns>
public bool CheckIfProjectCanStart(string projectName)
{
LoadProjectStates(false);
if (projectStates.ContainsKey(projectName))
{
return projectStates[projectName];
}
else
{
return true;
}
}
#endregion
#endregion
#region Private methods
#region LoadProjectStates()
/// <summary>
/// Loads all the states from a persistance file.
/// </summary>
/// <param name="forceLoad"></param>
private void LoadProjectStates(bool forceLoad)
{
if (forceLoad || (projectStates == null))
{
projectStates = new Dictionary<string, bool>();
if (fileSystem.FileExists(persistanceFileName))
{
isLoading = true;
try
{
var stateDocument = new XmlDocument();
using (var stream = fileSystem.OpenInputStream(persistanceFileName))
{
stateDocument.Load(stream);
}
foreach (XmlElement projectState in stateDocument.SelectNodes("/state/project"))
{
ChangeProjectState(projectState.InnerText, false);
}
}
finally
{
isLoading = false;
}
}
}
}
#endregion
#region SaveProjectStates()
/// <summary>
/// Saves all the states to a persistance file.
/// </summary>
private void SaveProjectStates()
{
if (projectStates != null)
{
var stateDocument = new XmlDocument();
var rootElement = stateDocument.CreateElement("state");
stateDocument.AppendChild(rootElement);
foreach (var projectName in projectStates.Keys)
{
if (!projectStates[projectName])
{
var projectElement = stateDocument.CreateElement("project");
projectElement.InnerText = projectName;
rootElement.AppendChild(projectElement);
}
}
using (var stream = fileSystem.OpenOutputStream(persistanceFileName))
{
var settings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
OmitXmlDeclaration = true,
Indent = false
};
using (var xmlWriter = XmlTextWriter.Create(stream, settings))
{
stateDocument.Save(xmlWriter);
}
}
}
}
#endregion
#region ChangeProjectState()
/// <summary>
/// See if we need to change the state and if so, change it, then persist the states
/// </summary>
/// <param name="projectName"></param>
/// <param name="newState"></param>
private void ChangeProjectState(string projectName, bool newState)
{
LoadProjectStates(false);
var saveStates = true;
if (projectStates.ContainsKey(projectName))
{
if (projectStates[projectName] != newState)
{
projectStates[projectName] = newState;
}
else
{
saveStates = false;
}
}
else
{
projectStates.Add(projectName, newState);
}
if (saveStates && !isLoading) SaveProjectStates();
}
#endregion
#endregion
}
}