/*
* Copyright (c) 2009 Craig Sutherland
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThoughtWorks.CruiseControl.Remote.Monitor;
using System.ComponentModel;
using ThoughtWorks.CruiseControl.Remote;
using FastForward.WinCore.Configuration;
namespace FastForward.WinCore
{
///
/// A plugin that detects changes in the status of a project.
///
public abstract class StatusChangePlugin
: Plugin, IDisposable
{
#region Private fields
private Dictionary currentStatus = new Dictionary();
private Dictionary currentActivity = new Dictionary();
private bool isDisposed = false;
private bool isChanging = false;
#endregion
#region Constructors
///
/// Initialise a new .
///
///
protected StatusChangePlugin(PluginConfig configuration)
: base(configuration)
{
}
#endregion
#region Public methods
#region Terminate()
///
/// Terminate the plugin.
///
public override void Terminate()
{
Dispose();
}
#endregion
#region Dispose()
///
/// Clean up.
///
public virtual void Dispose()
{
if (!isDisposed)
{
// Stop all monitoring
Servers.ServerAdded -= Servers_ServerAdded;
Servers.ServerRemoved -= Servers_ServerRemoved;
foreach (var server in Servers.Servers)
{
RemoveServer(server);
}
isDisposed = true;
}
}
#endregion
#endregion
#region Protected methods
#region OnInitialise()
///
/// Initialise the actual implementation of this plugin.
///
protected override void OnInitialise()
{
// Associate the event handlers
foreach (var server in Servers.Servers)
{
AddServer(server);
}
Servers.ServerAdded += Servers_ServerAdded;
Servers.ServerRemoved += Servers_ServerRemoved;
}
#endregion
#region OnStatusChange()
///
/// Handle a status change.
///
/// The project that changed.
/// The old status of the project.
/// The old activity of the project.
protected virtual void OnStatusChange(Project project, IntegrationStatus oldStatus, ProjectActivity oldActivity)
{
}
#endregion
#region OnItemChange()
///
/// Handle a changed item.
///
protected virtual void OnItemChange()
{
}
#endregion
#endregion
#region Private methods
#region AddServer()
///
/// Start monitoring a new server.
///
///
private void AddServer(Server server)
{
isChanging = true;
try
{
foreach (var project in server.Projects)
{
AddProject(project);
}
server.ProjectAdded += server_ProjectAdded;
server.ProjectRemoved += server_ProjectRemoved;
OnItemChange();
}
finally
{
isChanging = false;
}
}
#endregion
#region RemoveServer()
///
/// Stop monitoring an existing server.
///
///
private void RemoveServer(Server server)
{
isChanging = true;
try
{
server.ProjectAdded -= server_ProjectAdded;
server.ProjectRemoved -= server_ProjectRemoved;
foreach (var project in server.Projects)
{
RemoveProject(project);
}
OnItemChange();
}
finally
{
isChanging = false;
}
}
#endregion
#region AddProject()
///
/// Start monitoring a new project.
///
///
private void AddProject(Project project)
{
// For some reason we can get duplicates here - perhaps a race condition?
if (!currentStatus.ContainsKey(project))
{
project.PropertyChanged += project_PropertyChanged;
currentStatus.Add(project, project.BuildStatus);
currentActivity.Add(project, project.Activity);
if (!isChanging) OnItemChange();
}
}
#endregion
#region RemoveProject()
///
/// Stop monitoring an existing project.
///
///
private void RemoveProject(Project project)
{
currentStatus.Remove(project);
currentActivity.Remove(project);
project.PropertyChanged -= project_PropertyChanged;
if (!isChanging) OnItemChange();
}
#endregion
#endregion
#region Event handlers
#region server_ProjectAdded()
///
/// A project has been added to a server.
///
///
///
private void server_ProjectAdded(object sender, ProjectChangedArgs e)
{
AddProject(e.Project);
}
#endregion
#region server_ProjectRemoved()
///
/// A project has been removed from a server.
///
///
///
private void server_ProjectRemoved(object sender, ProjectChangedArgs e)
{
RemoveProject(e.Project);
}
#endregion
#region Servers_ServerRemoved()
///
/// A server has been removed.
///
///
///
private void Servers_ServerRemoved(object sender, ServerChangedArgs e)
{
RemoveServer(e.Server);
}
#endregion
#region Servers_ServerAdded()
///
/// A server has been added.
///
///
///
private void Servers_ServerAdded(object sender, ServerChangedArgs e)
{
AddServer(e.Server);
}
#endregion
#region project_PropertyChanged()
///
/// A property has changed on a project.
///
///
///
private void project_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var project = sender as Project;
switch (e.PropertyName)
{
case "Activity":
case "BuildStatus":
// Allow any inherited classes to handle the status change
OnStatusChange(project, currentStatus[project], currentActivity[project]);
currentStatus[project] = project.BuildStatus;
currentActivity[project] = project.Activity;
break;
}
}
#endregion
#endregion
}
}