using System;
using log4net;
using Umbraco.Core.Logging;
using System.IO;
using System.Linq;
using Umbraco.Core.IO;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Publishing;
using umbraco.interfaces;
using Umbraco.Core.Events;
namespace Umbraco.Core.Services
{
///
/// These are used currently to return the temporary 'operation' interfaces for services
/// which are used to return a status from operational methods so we can determine if things are
/// cancelled, etc...
///
/// These will be obsoleted in v8 since all real services methods will be changed to have the correct result.
///
public static class ServiceWithResultExtensions
{
public static IContentServiceOperations WithResult(this IContentService contentService)
{
return (IContentServiceOperations)contentService;
}
public static IMediaServiceOperations WithResult(this IMediaService mediaService)
{
return (IMediaServiceOperations)mediaService;
}
}
///
/// The Umbraco ServiceContext, which provides access to the following services:
/// , , ,
/// , and .
///
public class ServiceContext
{
private Lazy _migrationEntryService;
private Lazy _publicAccessService;
private Lazy _taskService;
private Lazy _domainService;
private Lazy _auditService;
private Lazy _localizedTextService;
private Lazy _tagService;
private Lazy _contentService;
private Lazy _userService;
private Lazy _memberService;
private Lazy _mediaService;
private Lazy _contentTypeService;
private Lazy _dataTypeService;
private Lazy _fileService;
private Lazy _localizationService;
private Lazy _packagingService;
private Lazy _serverRegistrationService;
private Lazy _entityService;
private Lazy _relationService;
private Lazy _treeService;
private Lazy _sectionService;
private Lazy _macroService;
private Lazy _memberTypeService;
private Lazy _memberGroupService;
private Lazy _notificationService;
private Lazy _externalLoginService;
///
/// public ctor - will generally just be used for unit testing all items are optional and if not specified, the defaults will be used
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
public ServiceContext(
IContentService contentService = null,
IMediaService mediaService = null,
IContentTypeService contentTypeService = null,
IDataTypeService dataTypeService = null,
IFileService fileService = null,
ILocalizationService localizationService = null,
IPackagingService packagingService = null,
IEntityService entityService = null,
IRelationService relationService = null,
IMemberGroupService memberGroupService = null,
IMemberTypeService memberTypeService = null,
IMemberService memberService = null,
IUserService userService = null,
ISectionService sectionService = null,
IApplicationTreeService treeService = null,
ITagService tagService = null,
INotificationService notificationService = null,
ILocalizedTextService localizedTextService = null,
IAuditService auditService = null,
IDomainService domainService = null,
ITaskService taskService = null,
IMacroService macroService = null,
IPublicAccessService publicAccessService = null,
IExternalLoginService externalLoginService = null,
IMigrationEntryService migrationEntryService = null)
{
if (migrationEntryService != null) _migrationEntryService = new Lazy(() => migrationEntryService);
if (externalLoginService != null) _externalLoginService = new Lazy(() => externalLoginService);
if (auditService != null) _auditService = new Lazy(() => auditService);
if (localizedTextService != null) _localizedTextService = new Lazy(() => localizedTextService);
if (tagService != null) _tagService = new Lazy(() => tagService);
if (contentService != null) _contentService = new Lazy(() => contentService);
if (mediaService != null) _mediaService = new Lazy(() => mediaService);
if (contentTypeService != null) _contentTypeService = new Lazy(() => contentTypeService);
if (dataTypeService != null) _dataTypeService = new Lazy(() => dataTypeService);
if (fileService != null) _fileService = new Lazy(() => fileService);
if (localizationService != null) _localizationService = new Lazy(() => localizationService);
if (packagingService != null) _packagingService = new Lazy(() => packagingService);
if (entityService != null) _entityService = new Lazy(() => entityService);
if (relationService != null) _relationService = new Lazy(() => relationService);
if (sectionService != null) _sectionService = new Lazy(() => sectionService);
if (memberGroupService != null) _memberGroupService = new Lazy(() => memberGroupService);
if (memberTypeService != null) _memberTypeService = new Lazy(() => memberTypeService);
if (treeService != null) _treeService = new Lazy(() => treeService);
if (memberService != null) _memberService = new Lazy(() => memberService);
if (userService != null) _userService = new Lazy(() => userService);
if (notificationService != null) _notificationService = new Lazy(() => notificationService);
if (domainService != null) _domainService = new Lazy(() => domainService);
if (taskService != null) _taskService = new Lazy(() => taskService);
if (macroService != null) _macroService = new Lazy(() => macroService);
if (publicAccessService != null) _publicAccessService = new Lazy(() => publicAccessService);
}
///
/// Creates a service context with a RepositoryFactory which is used to construct Services
///
///
///
///
///
///
///
///
public ServiceContext(
RepositoryFactory repositoryFactory,
IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider,
IUnitOfWorkProvider fileUnitOfWorkProvider,
BasePublishingStrategy publishingStrategy,
CacheHelper cache,
ILogger logger,
IEventMessagesFactory eventMessagesFactory)
{
if (repositoryFactory == null) throw new ArgumentNullException("repositoryFactory");
if (dbUnitOfWorkProvider == null) throw new ArgumentNullException("dbUnitOfWorkProvider");
if (fileUnitOfWorkProvider == null) throw new ArgumentNullException("fileUnitOfWorkProvider");
if (publishingStrategy == null) throw new ArgumentNullException("publishingStrategy");
if (cache == null) throw new ArgumentNullException("cache");
if (logger == null) throw new ArgumentNullException("logger");
if (eventMessagesFactory == null) throw new ArgumentNullException("eventMessagesFactory");
BuildServiceCache(dbUnitOfWorkProvider, fileUnitOfWorkProvider, publishingStrategy, cache,
repositoryFactory,
logger, eventMessagesFactory);
}
///
/// Builds the various services
///
private void BuildServiceCache(
IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider,
IUnitOfWorkProvider fileUnitOfWorkProvider,
BasePublishingStrategy publishingStrategy,
CacheHelper cache,
RepositoryFactory repositoryFactory,
ILogger logger,
IEventMessagesFactory eventMessagesFactory)
{
var provider = dbUnitOfWorkProvider;
var fileProvider = fileUnitOfWorkProvider;
if (_migrationEntryService == null)
_migrationEntryService = new Lazy(() => new MigrationEntryService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_externalLoginService == null)
_externalLoginService = new Lazy(() => new ExternalLoginService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_publicAccessService == null)
_publicAccessService = new Lazy(() => new PublicAccessService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_taskService == null)
_taskService = new Lazy(() => new TaskService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_domainService == null)
_domainService = new Lazy(() => new DomainService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_auditService == null)
_auditService = new Lazy(() => new AuditService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_localizedTextService == null)
{
_localizedTextService = new Lazy(() => new LocalizedTextService(
new Lazy(() =>
{
var mainLangFolder = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Umbraco + "/config/lang/"));
var appPlugins = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.AppPlugins));
var configLangFolder = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Config + "/lang/"));
var pluginLangFolders = appPlugins.Exists == false
? Enumerable.Empty()
: appPlugins.GetDirectories()
.SelectMany(x => x.GetDirectories("Lang"))
.SelectMany(x => x.GetFiles("*.xml", SearchOption.TopDirectoryOnly))
.Where(x => Path.GetFileNameWithoutExtension(x.FullName).Length == 5)
.Select(x => new LocalizedTextServiceSupplementaryFileSource(x, false));
//user defined langs that overwrite the default, these should not be used by plugin creators
var userLangFolders = configLangFolder.Exists == false
? Enumerable.Empty()
: configLangFolder
.GetFiles("*.user.xml", SearchOption.TopDirectoryOnly)
.Where(x => Path.GetFileNameWithoutExtension(x.FullName).Length == 10)
.Select(x => new LocalizedTextServiceSupplementaryFileSource(x, true));
return new LocalizedTextServiceFileSources(
logger,
cache.RuntimeCache,
mainLangFolder,
pluginLangFolders.Concat(userLangFolders));
}),
logger));
}
if (_notificationService == null)
_notificationService = new Lazy(() => new NotificationService(provider, _userService.Value, _contentService.Value, logger));
if (_serverRegistrationService == null)
_serverRegistrationService = new Lazy(() => new ServerRegistrationService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_userService == null)
_userService = new Lazy(() => new UserService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_memberService == null)
_memberService = new Lazy(() => new MemberService(provider, repositoryFactory, logger, eventMessagesFactory, _memberGroupService.Value, _dataTypeService.Value));
if (_contentService == null)
_contentService = new Lazy(() => new ContentService(provider, repositoryFactory, logger, eventMessagesFactory, publishingStrategy, _dataTypeService.Value, _userService.Value));
if (_mediaService == null)
_mediaService = new Lazy(() => new MediaService(provider, repositoryFactory, logger, eventMessagesFactory, _dataTypeService.Value, _userService.Value));
if (_contentTypeService == null)
_contentTypeService = new Lazy(() => new ContentTypeService(provider, repositoryFactory, logger, eventMessagesFactory, _contentService.Value, _mediaService.Value));
if (_dataTypeService == null)
_dataTypeService = new Lazy(() => new DataTypeService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_fileService == null)
_fileService = new Lazy(() => new FileService(fileProvider, provider, repositoryFactory));
if (_localizationService == null)
_localizationService = new Lazy(() => new LocalizationService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_packagingService == null)
_packagingService = new Lazy(() => new PackagingService(logger, _contentService.Value, _contentTypeService.Value, _mediaService.Value, _macroService.Value, _dataTypeService.Value, _fileService.Value, _localizationService.Value, _userService.Value, repositoryFactory, provider));
if (_entityService == null)
_entityService = new Lazy(() => new EntityService(
provider, repositoryFactory, logger, eventMessagesFactory,
_contentService.Value, _contentTypeService.Value, _mediaService.Value, _dataTypeService.Value, _memberService.Value, _memberTypeService.Value,
cache.RuntimeCache));
if (_relationService == null)
_relationService = new Lazy(() => new RelationService(provider, repositoryFactory, logger, eventMessagesFactory, _entityService.Value));
if (_treeService == null)
_treeService = new Lazy(() => new ApplicationTreeService(logger, cache));
if (_sectionService == null)
_sectionService = new Lazy(() => new SectionService(_userService.Value, _treeService.Value, provider, cache));
if (_macroService == null)
_macroService = new Lazy(() => new MacroService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_memberTypeService == null)
_memberTypeService = new Lazy(() => new MemberTypeService(provider, repositoryFactory, logger, eventMessagesFactory, _memberService.Value));
if (_tagService == null)
_tagService = new Lazy(() => new TagService(provider, repositoryFactory, logger, eventMessagesFactory));
if (_memberGroupService == null)
_memberGroupService = new Lazy(() => new MemberGroupService(provider, repositoryFactory, logger, eventMessagesFactory));
}
///
/// Gets the
///
public IMigrationEntryService MigrationEntryService
{
get { return _migrationEntryService.Value; }
}
///
/// Gets the
///
public IPublicAccessService PublicAccessService
{
get { return _publicAccessService.Value; }
}
///
/// Gets the
///
public ITaskService TaskService
{
get { return _taskService.Value; }
}
///
/// Gets the
///
public IDomainService DomainService
{
get { return _domainService.Value; }
}
///
/// Gets the
///
public IAuditService AuditService
{
get { return _auditService.Value; }
}
///
/// Gets the
///
public ILocalizedTextService TextService
{
get { return _localizedTextService.Value; }
}
///
/// Gets the
///
public INotificationService NotificationService
{
get { return _notificationService.Value; }
}
///
/// Gets the
///
public IServerRegistrationService ServerRegistrationService
{
get { return _serverRegistrationService.Value; }
}
///
/// Gets the
///
public ITagService TagService
{
get { return _tagService.Value; }
}
///
/// Gets the
///
public IMacroService MacroService
{
get { return _macroService.Value; }
}
///
/// Gets the
///
public IEntityService EntityService
{
get { return _entityService.Value; }
}
///
/// Gets the
///
public IRelationService RelationService
{
get { return _relationService.Value; }
}
///
/// Gets the
///
public IContentService ContentService
{
get { return _contentService.Value; }
}
///
/// Gets the
///
public IContentTypeService ContentTypeService
{
get { return _contentTypeService.Value; }
}
///
/// Gets the
///
public IDataTypeService DataTypeService
{
get { return _dataTypeService.Value; }
}
///
/// Gets the
///
public IFileService FileService
{
get { return _fileService.Value; }
}
///
/// Gets the
///
public ILocalizationService LocalizationService
{
get { return _localizationService.Value; }
}
///
/// Gets the
///
public IMediaService MediaService
{
get { return _mediaService.Value; }
}
///
/// Gets the
///
public IPackagingService PackagingService
{
get { return _packagingService.Value; }
}
///
/// Gets the
///
public IUserService UserService
{
get { return _userService.Value; }
}
///
/// Gets the
///
public IMemberService MemberService
{
get { return _memberService.Value; }
}
///
/// Gets the
///
public ISectionService SectionService
{
get { return _sectionService.Value; }
}
///
/// Gets the
///
public IApplicationTreeService ApplicationTreeService
{
get { return _treeService.Value; }
}
///
/// Gets the MemberTypeService
///
public IMemberTypeService MemberTypeService
{
get { return _memberTypeService.Value; }
}
///
/// Gets the MemberGroupService
///
public IMemberGroupService MemberGroupService
{
get { return _memberGroupService.Value; }
}
public IExternalLoginService ExternalLoginService
{
get { return _externalLoginService.Value; }
}
}
}