using System;
using System.Collections.Generic;
namespace Umbraco.Core.Standalone
{
internal class StandaloneCoreApplication : UmbracoApplicationBase
{
///
/// Initializes a new instance of the class.
///
protected StandaloneCoreApplication(string baseDirectory)
{
_baseDirectory = baseDirectory;
}
///
/// Provides the application boot manager.
///
/// An application boot manager.
protected override IBootManager GetBootManager()
{
return new StandaloneCoreBootManager(this, _handlersToAdd, _handlersToRemove, _baseDirectory);
}
#region Application
private readonly string _baseDirectory;
private static StandaloneCoreApplication _application;
private static bool _started;
private static readonly object AppLock = new object();
///
/// Gets the instance of the standalone Umbraco application.
///
public static StandaloneCoreApplication GetApplication(string baseDirectory)
{
lock (AppLock)
{
return _application ?? (_application = new StandaloneCoreApplication(baseDirectory));
}
}
///
/// Starts the application.
///
public void Start()
{
lock (AppLock)
{
if (_started)
throw new InvalidOperationException("Application has already started.");
Application_Start(this, EventArgs.Empty);
_started = true;
}
}
#endregion
#region IApplicationEventHandler management
private readonly List _handlersToAdd = new List();
private readonly List _handlersToRemove = new List();
///
/// Associates an type with the application.
///
/// The type to associate.
/// The application.
/// Types implementing from within
/// an executable are not automatically discovered by Umbraco and have to be
/// explicitely associated with the application using this method.
public StandaloneCoreApplication WithApplicationEventHandler()
where T : IApplicationEventHandler
{
_handlersToAdd.Add(typeof(T));
return this;
}
///
/// Dissociates an type from the application.
///
/// The type to dissociate.
/// The application.
public StandaloneCoreApplication WithoutApplicationEventHandler()
where T : IApplicationEventHandler
{
_handlersToRemove.Add(typeof(T));
return this;
}
///
/// Associates an type with the application.
///
/// The type to associate.
/// The application.
/// Types implementing from within
/// an executable are not automatically discovered by Umbraco and have to be
/// explicitely associated with the application using this method.
public StandaloneCoreApplication WithApplicationEventHandler(Type type)
{
if (type.Implements() == false)
throw new ArgumentException("Type does not implement IApplicationEventHandler.", "type");
_handlersToAdd.Add(type);
return this;
}
///
/// Dissociates an type from the application.
///
/// The type to dissociate.
/// The application.
public StandaloneCoreApplication WithoutApplicationEventHandler(Type type)
{
if (type.Implements() == false)
throw new ArgumentException("Type does not implement IApplicationEventHandler.", "type");
_handlersToRemove.Add(type);
return this;
}
#endregion
}
}