ExeConfigurationSystem.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » EnterpriseServices » 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 » Inversion of Control Dependency Injection » Spring.net 
Spring.net » Spring » EnterpriseServices » ExeConfigurationSystem.cs
#region License

/*
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

namespace Spring.EnterpriseServices{
#if (NET_2_0 && !MONO)
    using System;
    using System.Configuration;
    using System.Reflection;
    using System.Xml;
    using Spring.Core.TypeResolution;
    using Spring.Util;

    using System.Configuration.Internal;

    /// <summary>
    /// SUBJECT TO CHANGE -FOR INTERNAL USE ONLY!<br/>
    /// Holds configuration information from a given configuration file, obtained by <see cref="ConfigurationManager.OpenExeConfiguration(string)"/>.
    /// You may use <see cref="ConfigurationUtils.SetConfigurationSystem"/> to replace the active configuration system.
    /// </summary>
    /// <seealso cref="ConfigurationManager.OpenExeConfiguration(string)"/>
    /// <seealso cref="ConfigurationUtils.SetConfigurationSystem"/>
    public class ExeConfigurationSystem : IChainableConfigSystem
    {
        private string _configPath;
        private Configuration _configuration;
        private IInternalConfigSystem _next;

        /// <summary>
        /// initializes this instance with a path to be passed into <see cref="ConfigurationManager.OpenExeConfiguration(string)"/>
        /// </summary>
        /// <param name="configPath"></param>
        public ExeConfigurationSystem(string configPath)
        {
            _configPath = configPath;
        }

        /// <summary>
        /// Purges cached configuration
        /// </summary>
        public void RefreshConfig(string sectionName)
        {
            if (_next != null)
            {
                _next.RefreshConfig(sectionName);
            }
            _configuration = null;
        }

        ///<summary>
        /// Only true if the underlying config system supports this.
        ///</summary>
        public bool SupportsUserConfig
        {
            get
            {
                EnsureInit();
                if (_next != null)
                {
                    return _next.SupportsUserConfig;
                }
                return false;
            }
        }

        /// <summary>
        /// Set the nested configuration system to delegate calls in case we can't resolve a config section ourselves
        /// </summary>
        public void SetInnerConfigurationSystem(IInternalConfigSystem innerConfigSystem)
        {
            _next = innerConfigSystem;
        }

        private void EnsureInit()
        {
            if (_configuration == null)
            {
                lock (this)
                {
                    if (_configuration == null)
                    {
                        _configuration = ConfigurationManager.OpenExeConfiguration(_configPath);
                    }
                }
            }
        }

        private delegate object ResolveSectionRuntimeObject(ConfigurationSection section);

        private static ResolveSectionRuntimeObject resolveSectionRuntimeObject =
            (ResolveSectionRuntimeObject)Delegate.CreateDelegate(typeof(ResolveSectionRuntimeObject),
                                                                  typeof (ConfigurationSection).GetMethod("GetRuntimeObject",
                                                                                                          BindingFlags.Instance |
                                                                                                          BindingFlags.NonPublic));

        /// <summary>
        /// Get the specified section
        /// </summary>
        /// <param name="sectionName"></param>
        /// <returns></returns>
        public object GetSection(string sectionName)
        {
            EnsureInit();
            ConfigurationSection thisSection = _configuration.GetSection(sectionName);

            object parent = null;
            if (_next != null)
            {
                parent = _next.GetSection(sectionName);
            }
            if (thisSection == null)
            {
                return parent;
            }

            object result = resolveSectionRuntimeObject(thisSection);
            if (result is DefaultSection)
            {
                string rawXml = thisSection.SectionInformation.GetRawXml();
                if (string.IsNullOrEmpty(rawXml))
                {
                    return null;
                }

                Type t = TypeResolutionUtils.ResolveType(thisSection.SectionInformation.Type);
                if (typeof(IConfigurationSectionHandler).IsAssignableFrom(t))
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(thisSection.SectionInformation.GetRawXml());
                    IConfigurationSectionHandler handler = (IConfigurationSectionHandler) Activator.CreateInstance(t);
                    return handler.Create(parent, null, xmlDoc.DocumentElement );
                }
                throw new ConfigurationErrorsException(string.Format("missing <section> declaration for section '{0}'", sectionName));
            }
            return result;
        }
    }
#endif
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.