using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using umbraco.interfaces;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models.Rdbms;
using Umbraco.Core.Sync;
using Umbraco.Web.Routing;
namespace Umbraco.Web
{
///
/// An implementation that works by storing messages in the database.
///
///
/// This binds to appropriate umbraco events in order to trigger the Boot(), Sync() & FlushBatch() calls
///
public class BatchedDatabaseServerMessenger : DatabaseServerMessenger, IApplicationEventHandler
{
public BatchedDatabaseServerMessenger(ApplicationContext appContext, bool enableDistCalls, DatabaseServerMessengerOptions options)
: base(appContext, enableDistCalls, options)
{
}
#region Application Event Handler implementation
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
///
/// Bootup is completed, this will not execute if the application is not configured
///
///
///
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
UmbracoModule.EndRequest += UmbracoModule_EndRequest;
UmbracoModule.RouteAttempt += UmbracoModule_RouteAttempt;
if (applicationContext.DatabaseContext.IsDatabaseConfigured == false || applicationContext.DatabaseContext.CanConnect == false)
{
applicationContext.ProfilingLogger.Logger.Warn(
"The app cannot connect to the database, this server cannot be initialized with "
+ typeof(BatchedDatabaseServerMessenger) + ", distributed calls will not be enabled for this server");
}
else
{
Boot();
}
}
#endregion
private void UmbracoModule_RouteAttempt(object sender, RoutableAttemptEventArgs e)
{
switch (e.Outcome)
{
case EnsureRoutableOutcome.IsRoutable:
Sync();
break;
case EnsureRoutableOutcome.NotDocumentRequest:
//so it's not a document request, we'll check if it's a back office request
if (e.HttpContext.Request.Url.IsBackOfficeRequest(HttpRuntime.AppDomainAppVirtualPath))
{
//it's a back office request, we should sync!
Sync();
}
break;
//case EnsureRoutableOutcome.NotReady:
//case EnsureRoutableOutcome.NotConfigured:
//case EnsureRoutableOutcome.NoContent:
//default:
// break;
}
}
private void UmbracoModule_EndRequest(object sender, EventArgs e)
{
// will clear the batch - will remain in HttpContext though - that's ok
FlushBatch();
}
protected override void DeliverRemote(IEnumerable servers, ICacheRefresher refresher, MessageType messageType, IEnumerable