/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.IO;
using System.Threading;
using SystemConfigurationSystem.Configuration;
namespace Everest.Library{
public class EverestContext
{
/// <summary>
/// Initializes a new instance of the <see cref="EverestContext"/> class.
/// </summary>
internal EverestContext()
{
}
string baseDirectory;
/// <summary>
/// Gets or sets the base directory.
/// </summary>
/// <value>The base directory.</value>
public string BaseDirectory
{
get
{
return baseDirectory;
}
set
{
baseDirectory = value;
string baseUrl = baseDirectory.Replace(AppDomain.CurrentDomain.BaseDirectory, "/").Replace(@"\", "/");
if (!baseUrl.EndsWith("/"))
{
baseUrl = baseUrl + "/";
}
BaseUrl = "~" + baseUrl;
}
}
/// <summary>
/// Gets or sets the base URL.
/// </summary>
/// <value>The base URL.</value>
public string BaseUrl { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is on CMS.
/// </summary>
/// <value><c>true</c> if this instance is on CMS; otherwise, <c>false</c>.</value>
public bool IsOnCms { get; set; }
/// <summary>
///
/// </summary>
private SystemConfiguration.Configuration configuration;
/// <summary>
/// Gets the configuration.
/// Get the configuration in Everest.config.
/// </summary>
/// <value>The configuration.</value>
public SystemConfiguration.Configuration Configuration
{
get
{
if (configuration == null)
{
SystemConfiguration.ExeConfigurationFileMap configMap = new System.Configuration.ExeConfigurationFileMap();
string configFile = Path.Combine(this.BaseDirectory, "Everest.config");
configMap.ExeConfigFilename = configFile;
configMap.LocalUserConfigFilename = configFile;
configuration = (SystemConfiguration.Configuration)SystemConfiguration.ConfigurationManager.OpenMappedExeConfiguration(configMap, SystemConfiguration.ConfigurationUserLevel.None);
}
return configuration;
}
}
#region Thread data
/// <summary>
/// Gets the slot.
/// </summary>
/// <returns></returns>
private static LocalDataStoreSlot GetThreadDataSlot(string name)
{
return Thread.GetNamedDataSlot("EverestDataStoreSlot");
}
/// <summary>
/// Saves the context.
/// </summary>
/// <param name="context">The context.</param>
public static EverestContext CreateEverestContext(bool isOnCms, string baseDirectory)
{
var context = new EverestContext()
{
IsOnCms = isOnCms,
BaseDirectory = baseDirectory
};
Thread.SetData(GetThreadDataSlot("EverestDataStoreSlot"), context);
return context;
}
/// <summary>
/// Gets the context.
/// </summary>
/// <returns></returns>
public static EverestContext GetContext()
{
object context = Thread.GetData(GetThreadDataSlot("EverestDataStoreSlot"));
return (EverestContext)context;
}
/// <summary>
/// Saves the thread data.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="data">The data.</param>
public static void SaveThreadData(string key, object data)
{
Thread.SetData(GetThreadDataSlot(key), data);
}
/// <summary>
/// Gets the thread data.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">The key.</param>
/// <returns></returns>
public static T GetThreadData<T>(string key)
{
return (T)Thread.GetData(GetThreadDataSlot(key));
}
#endregion
}
}
|