mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Merge
--HG-- branch : dev
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Environment.Configuration;
|
||||
|
||||
@@ -91,5 +88,16 @@ this is
|
||||
a
|
||||
test"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FileExistsReturnsFalseForNonExistingFile() {
|
||||
Assert.That(_appDataFolder.FileExists("notexisting"), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FileExistsReturnsTrueForExistingFile() {
|
||||
_appDataFolder.CreateFile("alpha\\foo\\bar.txt", "");
|
||||
Assert.That(_appDataFolder.FileExists("alpha\\foo\\bar.txt"), Is.True);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -11,14 +11,27 @@ namespace Orchard.Tests.Environment.Topology {
|
||||
[TestFixture]
|
||||
public class DefaultTopologyDescriptorCacheTests {
|
||||
private IContainer _container;
|
||||
private string _tempFolder;
|
||||
private IAppDataFolder _appDataFolder;
|
||||
|
||||
[SetUp]
|
||||
public void Init() {
|
||||
_tempFolder = Path.GetTempFileName();
|
||||
File.Delete(_tempFolder);
|
||||
Directory.CreateDirectory(_tempFolder);
|
||||
_appDataFolder = new AppDataFolder();
|
||||
_appDataFolder.SetBasePath(_tempFolder);
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterInstance(_appDataFolder).As<IAppDataFolder>();
|
||||
builder.RegisterType<DefaultTopologyDescriptorCache>().As<ITopologyDescriptorCache>();
|
||||
_container = builder.Build();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void Term() {
|
||||
Directory.Delete(_tempFolder, true);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FetchReturnsNullForCacheMiss() {
|
||||
var service = _container.Resolve<ITopologyDescriptorCache>();
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Web.Hosting;
|
||||
@@ -16,6 +17,7 @@ namespace Orchard.Environment.Configuration {
|
||||
void CreateFile(string path, string content);
|
||||
string ReadFile(string path);
|
||||
void DeleteFile(string path);
|
||||
bool FileExists(string path);
|
||||
|
||||
string CreateDirectory(string path);
|
||||
|
||||
@@ -51,6 +53,11 @@ namespace Orchard.Environment.Configuration {
|
||||
File.Delete(Path.Combine(_basePath, path));
|
||||
}
|
||||
|
||||
public bool FileExists(string path) {
|
||||
var filePath = Path.Combine(_basePath, path);
|
||||
return File.Exists(filePath);
|
||||
}
|
||||
|
||||
public IEnumerable<string> ListFiles(string path) {
|
||||
var directoryPath = Path.Combine(_basePath, path);
|
||||
if (!Directory.Exists(directoryPath))
|
||||
|
@@ -1,17 +1,98 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Xml;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Environment.Topology.Models;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
|
||||
namespace Orchard.Environment.Topology {
|
||||
public class DefaultTopologyDescriptorCache : ITopologyDescriptorCache {
|
||||
readonly IDictionary<string, ShellTopologyDescriptor> _cache= new Dictionary<string, ShellTopologyDescriptor>();
|
||||
private readonly IAppDataFolder _appDataFolder;
|
||||
private const string TopologyCacheFileName = "cache.dat";
|
||||
|
||||
public DefaultTopologyDescriptorCache(IAppDataFolder appDataFolder) {
|
||||
_appDataFolder = appDataFolder;
|
||||
T = NullLocalizer.Instance;
|
||||
Logger = NullLogger.Instance;
|
||||
|
||||
}
|
||||
|
||||
public ILogger Logger { get; set; }
|
||||
private Localizer T { get; set; }
|
||||
|
||||
#region Implementation of ITopologyDescriptorCache
|
||||
|
||||
public ShellTopologyDescriptor Fetch(string name) {
|
||||
ShellTopologyDescriptor value;
|
||||
return _cache.TryGetValue(name, out value) ? value : null;
|
||||
VerifyCacheFile();
|
||||
var text = _appDataFolder.ReadFile(TopologyCacheFileName);
|
||||
XmlDocument xmlDocument = new XmlDocument();
|
||||
xmlDocument.LoadXml(text);
|
||||
XmlNode rootNode = xmlDocument.DocumentElement;
|
||||
foreach (XmlNode tenantNode in rootNode.ChildNodes) {
|
||||
if (String.Equals(tenantNode.Name, name, StringComparison.OrdinalIgnoreCase)) {
|
||||
var serializer = new DataContractSerializer(typeof(ShellTopologyDescriptor));
|
||||
var reader = new StringReader(tenantNode.InnerText);
|
||||
using (var xmlReader = XmlReader.Create(reader)) {
|
||||
return (ShellTopologyDescriptor) serializer.ReadObject(xmlReader, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Store(string name, ShellTopologyDescriptor descriptor) {
|
||||
_cache[name] = descriptor;
|
||||
VerifyCacheFile();
|
||||
var text = _appDataFolder.ReadFile(TopologyCacheFileName);
|
||||
bool tenantCacheUpdated = false;
|
||||
var saveWriter = new StringWriter();
|
||||
XmlDocument xmlDocument = new XmlDocument();
|
||||
xmlDocument.LoadXml(text);
|
||||
XmlNode rootNode = xmlDocument.DocumentElement;
|
||||
foreach (XmlNode tenantNode in rootNode.ChildNodes) {
|
||||
if (String.Equals(tenantNode.Name, name, StringComparison.OrdinalIgnoreCase)) {
|
||||
var serializer = new DataContractSerializer(typeof (ShellTopologyDescriptor));
|
||||
var writer = new StringWriter();
|
||||
using (var xmlWriter = XmlWriter.Create(writer)) {
|
||||
serializer.WriteObject(xmlWriter, descriptor);
|
||||
}
|
||||
tenantNode.InnerText = writer.ToString();
|
||||
tenantCacheUpdated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!tenantCacheUpdated) {
|
||||
XmlElement newTenant = xmlDocument.CreateElement(name);
|
||||
var serializer = new DataContractSerializer(typeof(ShellTopologyDescriptor));
|
||||
var writer = new StringWriter();
|
||||
using (var xmlWriter = XmlWriter.Create(writer)) {
|
||||
serializer.WriteObject(xmlWriter, descriptor);
|
||||
}
|
||||
newTenant.InnerText = writer.ToString();
|
||||
rootNode.AppendChild(newTenant);
|
||||
}
|
||||
|
||||
xmlDocument.Save(saveWriter);
|
||||
_appDataFolder.CreateFile(TopologyCacheFileName, saveWriter.ToString());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private void VerifyCacheFile() {
|
||||
if (!_appDataFolder.FileExists(TopologyCacheFileName)) {
|
||||
var writer = new StringWriter();
|
||||
using (XmlWriter xmlWriter = XmlWriter.Create(writer)) {
|
||||
if (xmlWriter != null) {
|
||||
xmlWriter.WriteStartDocument();
|
||||
xmlWriter.WriteStartElement("Tenants");
|
||||
xmlWriter.WriteEndElement();
|
||||
xmlWriter.WriteEndDocument();
|
||||
}
|
||||
}
|
||||
_appDataFolder.CreateFile(TopologyCacheFileName, writer.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -80,6 +80,9 @@
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
</Reference>
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Runtime.Serialization">
|
||||
<RequiredTargetFramework>3.0</RequiredTargetFramework>
|
||||
</Reference>
|
||||
<Reference Include="System.Transactions" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Web.Abstractions">
|
||||
|
Reference in New Issue
Block a user