MyCMMIConfiguration.cs 9.0 KB
using System;
using System.Collections;
using System.Configuration;
using System.Xml;
using System.Collections.Specialized;  

namespace Comm
{
    using SysFramework;

    /// <summary>
    /// Configuration类中用静态变量保存缺省的配置信息。并提供动态获取配置信息的功能,
    /// 动态配置信息写在 MyCMMI.config中
    /// <remarks>
    ///     Special considerations:
    ///     The MyCMMI application's configuaration settings are kept in 
    ///     the T1Configuration section of the Config.web file. A new
    ///     instance of this class is created automatically whenever the
    ///     settings file changes, so there is no need to cache any values.
    /// </remarks>
    /// </summary>

    public class Configuration : IConfigurationSectionHandler
    {
        //
        // Constant values for all expected entries in the T1Configuration section
        //
        private const String WEB_ENABLEPAGECACHE = "MyCMMI.Web.EnablePageCache";
        private const String WEB_PAGECACHEEXPIRESINSECONDS = "MyCMMI.Web.PageCacheExpiresInSeconds";
        private const String DATAACCESS_CONNECTIONSTRING = "MyCMMI.DataAccess.ConnectionString";
        private const String BIZRULE_SMTPSERVER = "MyCMMI.BizRule.SmtpServer";
        private const String BIZRULE_SMTPUSERNAME = "MyCMMI.BizRule.SmtpUserName";
        private const String BIZRULE_SMTPPASSWORD = "MyCMMI.BizRule.SmtpPassword";
        private const String WEB_ENABLESSL = "MyCMMI.Web.EnableSsl";
        private const String WEBSERVICE_CLIENTPATH = "MyCMMI.WebService.ClientPath";
        private const String MyCMMI_LICENCEPATH = "MyCMMI.MyCMMI.LicencePath";
        private const String MyCMMI_UPDATESWITCH = "MyCMMI.MyCMMI.UpdateSwitch";
        private const bool WEB_ENABLEPAGECACHE_DEFAULT = true;
        private const int WEB_PAGECACHEEXPIRESINSECONDS_DEFAULT = 3600;
        private const String DATAACCESS_CONNECTIONSTRING_DEFAULT = "222207218231207218162222224204201209218208224226179200212216221138179175165228198178197211219213219221212197174221197172217198230206211198229207174219222213216222206158155154155172168217214211203205229207212223151184215228201222170183171177189171";
        private const bool WEB_ENABLESSL_DEFAULT = false;
        private const String WEBSERVICE_CLIENTPATH_DEFAULT = @"C:\Program Files\Contamination Explorer";
        private const String MyCMMI_LICENCEPATH_DEFAULT = @"D:\MyCMMI.lic";
        private const String MyCMMI_UPDATESWITCH_DEFAULT = @"True";

        private static String dbConnectionString = DATAACCESS_CONNECTIONSTRING_DEFAULT;
        private static int pageCacheExpiresInSeconds = WEB_PAGECACHEEXPIRESINSECONDS_DEFAULT;
        private static bool enablePageCache = WEB_ENABLEPAGECACHE_DEFAULT;
        private static bool enableSsl = WEB_ENABLESSL_DEFAULT;
        private static string strClientPath = WEBSERVICE_CLIENTPATH_DEFAULT;
        private static string strMyCMMILicencePath = MyCMMI_LICENCEPATH_DEFAULT;
        private static string strMyCMMIUpdateSwitch = MyCMMI_UPDATESWITCH_DEFAULT;

        /// <summary>
        ///     Called by ASP+ before the application starts to initialize
        ///     settings from the Config.Web file(s). 
        ///     <remarks>
        ///         The app domain will restart if these settings change, so 
        ///         there is no reason to read these values more than once. This 
        ///         function uses the NameValueSectionHandler base class to generate 
        ///         a hashtable from the XML, which is then used to store the current 
        ///         settings. Because all settings are read here, we do not actually 
        ///         store the generated hashtable object for later retrieval by
        ///         Context.GetConfig
        ///         . The application should use the accessor
        ///         functions directly.
        ///     </remarks>
        ///     <param name="parent">
        ///         An object created by processing a section with this name
        ///         in a Config.Web file in a parent directory.
        ///     </param>
        ///     <param name="configContext">An array of Xml information.</param>
        ///     <param name="section">
        ///         The Path of the Config.Web file relative to the root
        ///         of the web server.
        ///     </param>
        ///     <returns>
        ///		    <para>
        ///             A ConfigOutput object, which we leave empty because all settings
        ///             are stored at this point.
        ///		    </para>
        ///	    </returns>
        /// </summary>
        public Object Create(Object parent, object configContext, XmlNode section)
        {

            NameValueCollection settings;

            try
            {
                NameValueSectionHandler baseHandler = new NameValueSectionHandler();
                settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
            }
            catch
            {
                settings = null;
            }

            if (settings == null)
            {
                dbConnectionString = DATAACCESS_CONNECTIONSTRING_DEFAULT;
                pageCacheExpiresInSeconds = WEB_PAGECACHEEXPIRESINSECONDS_DEFAULT;
                enablePageCache = WEB_ENABLEPAGECACHE_DEFAULT;
                enableSsl = WEB_ENABLESSL_DEFAULT;
                strClientPath = WEBSERVICE_CLIENTPATH_DEFAULT;
                strMyCMMILicencePath = MyCMMI_LICENCEPATH_DEFAULT;
                strMyCMMIUpdateSwitch = MyCMMI_UPDATESWITCH_DEFAULT;
            }
            else
            {
                dbConnectionString = ApplicationConfiguration.ReadSetting(settings, DATAACCESS_CONNECTIONSTRING, DATAACCESS_CONNECTIONSTRING_DEFAULT);
                pageCacheExpiresInSeconds = ApplicationConfiguration.ReadSetting(settings, WEB_PAGECACHEEXPIRESINSECONDS, WEB_PAGECACHEEXPIRESINSECONDS_DEFAULT);
                enablePageCache = ApplicationConfiguration.ReadSetting(settings, WEB_ENABLEPAGECACHE, WEB_ENABLEPAGECACHE_DEFAULT);
                enableSsl = ApplicationConfiguration.ReadSetting(settings, WEB_ENABLESSL, WEB_ENABLESSL_DEFAULT);
                strClientPath = ApplicationConfiguration.ReadSetting(settings, WEBSERVICE_CLIENTPATH, WEBSERVICE_CLIENTPATH_DEFAULT);
                strMyCMMILicencePath = ApplicationConfiguration.ReadSetting(settings, MyCMMI_LICENCEPATH, MyCMMI_LICENCEPATH_DEFAULT);
                strMyCMMIUpdateSwitch = ApplicationConfiguration.ReadSetting(settings, MyCMMI_UPDATESWITCH, MyCMMI_UPDATESWITCH_DEFAULT);
            }

            return settings;
        }

        /// <value>
        ///     Property EnablePageCache is used to get MyCMMI's page cache setting. 
        ///     <remarks>Returns true if page caching is enabled, false otherwise.</remarks>
        /// </value>
        public static bool EnablePageCache
        {
            get
            {
                return enablePageCache;
            }
        }

        /// <value>
        ///     Property PageCacheExpiresInSeconds is used to get MyCMMI's page cache expiration timeout setting.  
        ///     <remarks>The number of seconds before a page cache should expire.</remarks>
        /// </value>
        public static int PageCacheExpiresInSeconds
        {
            get
            {
                return pageCacheExpiresInSeconds;
            }
        }

        /// <value>
        ///     Property ConnectionString is used to get MyCMMI's database connection string.
        ///     <remarks>The database connection string.</remarks>
        /// </value>
        public static String ConnectionString
        {
            get
            {
                return dbConnectionString;
            }
            set
            {
                dbConnectionString = value;
            }
        }

        /// <value>
        ///     Property ClientPath is used to get MyCMMI's Client Path string for Auto Client Update Utility.
        ///     <remarks>Get Client Path</remarks>
        /// </value>
        public static string ClientPath
        {
            get
            {
                return strClientPath;
            }
        }

        /// <summary>
        ///      Property ClientPath is used to get MyCMMI's Licence Path string for Validate Authority.
        ///      <remarks>Get Licence Path.</remarks>
        /// </summary>
        public static string MyCMMILicencePath
        {
            get
            {
                return strMyCMMILicencePath;
            }
        }

        /// <summary>
        ///      Property ClientPath is used to get MyCMMI's Update Switch string for Update .
        ///      <remarks>Get Update Switch.</remarks>
        /// </summary>
        public static string MyCMMIUpdateSwitch
        {
            get
            {
                return strMyCMMIUpdateSwitch;
            }
        }

        /// <value>
        ///     Property EnableSsl is used to get MyCMMI's SSL setting. 
        ///     <remarks>True if SSL is enabled, false otherwise.</remarks>
        /// </value>
        public static bool EnableSsl
        {
            get
            {
                return enableSsl;
            }
        }
    }
}