EverestContext.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » Library » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » Library » EverestContext.cs
/*
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
    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.