XmlProjectStateManager.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » Core » State » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » Core » State » XmlProjectStateManager.cs

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())
        {
        }

    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

    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
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.