private static void SaveMetaDataSection(ChoPerformanceCounter pc)
        {
            string            xPath        = GetXPath(pc);
            ChoPCMetaDataInfo metaDataInfo = pc.MetaDataInfo;

            using (ChoXmlDocument xmlDocument = new ChoXmlDocument(MetaDataFilepath, false, false))
            {
                if (metaDataInfo == null)
                {
                    return;
                }

                XmlNode node = xmlDocument.XmlDocument.SelectSingleNode(xPath);
                if (node == null)
                {
                    node = xmlDocument.XmlDocument.MakeXPath(xPath);
                }

                if (node != null)
                {
                    ChoXmlDocument.SetOuterXml(node, metaDataInfo.ToXml());
                    xmlDocument.Save();
                }
            }
        }
Ejemplo n.º 2
0
        private void Initialize()
        {
            if (_isInitialized)
            {
                return;
            }

            lock (_padLock)
            {
                if (_isInitialized)
                {
                    return;
                }

                try
                {
                    ChoXmlDocument.CreateXmlFileIfEmpty(MetaDataFilePath);
                    Refresh();
                }
                finally
                {
                    _isInitialized = true;
                }
            }
        }
Ejemplo n.º 3
0
        private void SaveMetaDataSection(T pc)
        {
            string        xPath        = pc.NodeLocateXPath;
            TMetaDataInfo metaDataInfo = pc.MetaDataInfo;

            using (ChoXmlDocument xmlDocument = new ChoXmlDocument(MetaDataFilePath))
            {
                if (metaDataInfo == null)
                {
                    return;
                }

                XmlNode node = xmlDocument.XmlDocument.SelectSingleNode(xPath);
                if (node == null)
                {
                    node = xmlDocument.XmlDocument.MakeXPath(pc.NodeCreateXPath);
                }

                if (node != null)
                {
                    ChoXmlDocument.SetOuterXml(node, metaDataInfo.ToXml());
                    xmlDocument.Save();
                }
            }
        }
        private static void SetMetaDataSection(Type cmdLineObjType, ChoObjectInfo objectInfo)
        {
            if (cmdLineObjType == null)
            {
                return;
            }

            string elementPath = cmdLineObjType.Name;

            using (ChoXmlDocument xmlDocument = new ChoXmlDocument(_metaDataFilepath))
            {
                if (elementPath.IsNullOrEmpty())
                {
                    return;
                }

                XmlNode node = xmlDocument.XmlDocument.SelectSingleNode(elementPath);
                if (node == null)
                {
                    node = xmlDocument.XmlDocument.MakeXPath(elementPath);
                }

                if (node != null)
                {
                    xmlDocument.XmlDocument.InnerXml = ChoXmlDocument.AppendToInnerXml(node, objectInfo.ToXml());

                    xmlDocument.Save();
                }
            }
        }
        private static void Refresh()
        {
            try
            {
                LoadFile();
            }
            catch (Exception ex)
            {
                ChoTrace.Error(ex);

                try
                {
                    string newFile = ChoPath.AddExtension(MetaDataFilepath, ChoReservedFileExt.Err);
                    if (File.Exists(newFile))
                    {
                        File.Delete(newFile);
                    }
                    File.Move(MetaDataFilepath, newFile);

                    ChoXmlDocument.CreateXmlFileIfEmpty(MetaDataFilepath);
                    LoadFile();
                }
                catch (Exception innerEx)
                {
                    ChoTrace.Error(innerEx);
                }
            }
        }
        private static void Initialize()
        {
            if (_isInitialized)
            {
                return;
            }

            lock (_padLock)
            {
                if (_isInitialized)
                {
                    return;
                }

                try
                {
                    MetaDataFilepath = ConfigurationManager.AppSettings[PC_METADATA_FILEPATH_TAG];

                    if (MetaDataFilepath.IsNullOrWhiteSpace() || !File.Exists(MetaDataFilepath))
                    {
                        MetaDataFilepath = ChoPath.AddExtension(Path.ChangeExtension(Path.Combine(Path.GetDirectoryName(ChoApplication.ApplicationConfigFilePath), Path.GetFileNameWithoutExtension(ChoApplication.ApplicationConfigFilePath)), "perf"),
                                                                ChoReservedFileExt.MetaData);
                    }

                    ChoXmlDocument.CreateXmlFileIfEmpty(MetaDataFilepath);
                    Refresh();
                }
                finally
                {
                    _isInitialized = true;
                }
            }
        }
        private static void SetMetaDataSection(ChoCommandLineArgObject cmdLineArgObject, ChoPropertyInfos propertyInfos)
        {
            return;

            if (cmdLineArgObject == null)
            {
                return;
            }

            string elementPath = cmdLineArgObject.GetType().Name;

            using (ChoXmlDocument xmlDocument = new ChoXmlDocument(_metaDataFilepath))
            {
                if (elementPath.IsNullOrEmpty())
                {
                    return;
                }

                XmlNode node = xmlDocument.XmlDocument.SelectSingleNode(elementPath);
                if (node == null)
                {
                    node = xmlDocument.XmlDocument.MakeXPath(elementPath);
                }

                if (node != null)
                {
                    xmlDocument.XmlDocument.InnerXml = ChoXmlDocument.AppendToInnerXml(node, propertyInfos.ToXml());

                    xmlDocument.Save();
                }
            }
        }
Ejemplo n.º 8
0
        protected void PersistAsNameSpaceAwareXml(object data, ChoDictionaryService <string, object> stateInfo)
        {
            string configFilePath = stateInfo[UNDERLYING_CONFIG_PATH] as string;

            if (configFilePath.IsNullOrWhiteSpace())
            {
                return;
            }

            ChoXmlDocument.CreateXmlFileIfEmpty(configFilePath);

            using (ChoXmlDocument xmlDocument = new ChoXmlDocument(GetFullPath(configFilePath)))
            {
                PersistConfigSectionDefinition(xmlDocument.XmlDocument, data);
                XmlNode configNode = xmlDocument.XmlDocument.MakeXPath(ConfigElement.ConfigElementPath);
                if (configNode != null)
                {
                    string configXml = ToXml(data);
                    if (configXml.IsNull())
                    {
                        return;
                    }

                    ChoXmlDocument.SetNamespaceAwareOuterXml(configNode, configXml, ChoXmlDocument.CinchooNSURI);
                }
            }
        }
Ejemplo n.º 9
0
 private void LoadFile()
 {
     using (ChoXmlDocument xmlDocument = new ChoXmlDocument(MetaDataFilePath))
     {
         RootNode      = xmlDocument.XmlDocument.DocumentElement;
         _includeFiles = xmlDocument != null ? xmlDocument.IncludeFiles : null;
     }
 }
Ejemplo n.º 10
0
 private static void LoadFile()
 {
     using (ChoXmlDocument xmlDocument = new ChoXmlDocument(MetaDataFilepath, false, false))
     {
         _rootNode     = xmlDocument.XmlDocument.DocumentElement;
         _includeFiles = xmlDocument != null ? xmlDocument.IncludeFiles : null;
     }
 }
Ejemplo n.º 11
0
        private static void LoadConfigurationFile()
        {
            if (_appXmlDocument != null)
            {
                _appXmlDocument.Dispose();
                _appXmlDocument = null;
            }

            _appXmlDocument            = new ChoXmlDocument(_appConfigPath);
            _appIncludeConfigFilePaths = _appXmlDocument.IncludeFiles;

            if (_appXmlDocument != null && _appIncludeConfigFilePaths != null && _appIncludeConfigFilePaths.Length > 0)
            {
                _appXmlDocument.XmlDocument.Save(_appConfigPath);
            }

            if (_configurationChangeWatcher == null)
            {
                _configurationChangeWatcher = new ChoAppConfigurationChangeFileWatcher("configurations", _appConfigPath, _appIncludeConfigFilePaths);
                _configurationChangeWatcher.SetConfigurationChangedEventHandler(_key, new ChoConfigurationChangedEventHandler(_configurationChangeWatcher_ConfigurationChanged));
                ChoEnvironmentSettings.SetEnvironmentChangedEventHandlerNoCall("configurations", (sender, e) =>
                {
                    ChoAppConfigurationChangeFileWatcher configurationChangeWatcher = _configurationChangeWatcher;
                    _configurationChangeWatcher = null;
                    _configurationChangeWatcher_ConfigurationChanged(null, null);
                    configurationChangeWatcher.OnConfigurationChanged();
                    configurationChangeWatcher.Dispose();
                    configurationChangeWatcher = null;
                });
            }
            else
            {
                _configurationChangeWatcher.Reset(_appConfigPath, _appIncludeConfigFilePaths);
            }

            if (_systemConfigurationChangeWatcher == null)
            {
                try
                {
                    _systemConfigurationChangeWatcher = new ChoConfigurationChangeFileWatcher("systemConfigurations", AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                    _systemConfigurationChangeWatcher.SetConfigurationChangedEventHandler(_key, new ChoConfigurationChangedEventHandler(_systemConfigurationChangeWatcher_ConfigurationChanged));
                }
                catch (Exception ex)
                {
                    ChoApplication.Trace(ex.ToString());
                }
            }

            //Remove namespaces
            if (_appXmlDocument != null && _appIncludeConfigFilePaths != null && _appIncludeConfigFilePaths.Length > 0)
            {
                XDocument doc = XDocument.Load(_appConfigPath, LoadOptions.PreserveWhitespace);
                doc.Descendants().Attributes().Where(a => a.IsNamespaceDeclaration).Remove();
                doc.Save(_appConfigPath, SaveOptions.DisableFormatting);
            }
            _configuration = _appXmlDocument.XmlDocument.DocumentElement.ToObject <ChoConfiguration>();
            _configuration.Initialize();
        }
 public static void RestoreAppConfig()
 {
     if (File.Exists(AppConfigFilePath))
     {
         if (_appXmlDocument != null && _appXmlDocument.HasIncludeComments)
         {
             ChoXmlDocument.ContractNSave(_appXmlDocument, AppConfigFilePath);
         }
     }
 }
Ejemplo n.º 13
0
 internal static void RestoreAppConfig()
 {
     if (File.Exists(ChoGlobalApplicationSettings.Me.ApplicationConfigFilePath))
     {
         if (_appXmlDocument != null && _appXmlDocument.HasIncludeComments)
         {
             ChoXmlDocument.ContractNSave(_appXmlDocument, ChoGlobalApplicationSettings.Me.ApplicationConfigFilePath);
         }
     }
 }
Ejemplo n.º 14
0
        public override object Load(ChoBaseConfigurationElement configElement, XmlNode node)
        {
            base.Load(configElement, node);

            //if (configElement.ConfigFilePath.IsNullOrWhiteSpace())
            //    UnderlyingConfigFilePath = ChoConfigurationManager.GetConfigFile(node);
            //else
            //    UnderlyingConfigFilePath = configElement.ConfigFilePath;

            UnderlyingConfigFilePath = ChoConfigurationManager.GetConfigFile(node);
            if (UnderlyingConfigFilePath.IsNullOrWhiteSpace())
            {
                configElement[ChoConfigurationConstants.FORCE_PERSIST] = true;
                UnderlyingConfigFilePath = configElement.ConfigFilePath;
            }

            ConfigElement[UNDERLYING_CONFIG_PATH] = UnderlyingConfigFilePath;

            ConfigFilePath = GetFullPath(UnderlyingConfigFilePath);

            if (IsAppConfigFile)
            {
                if (!configElement.ConfigFilePath.IsNullOrWhiteSpace())
                {
                    UnderlyingConfigFilePath = configElement.ConfigFilePath;
                    ConfigFilePath           = GetFullPath(UnderlyingConfigFilePath);
                }
            }

            if (node != null && !IsAppConfigFile)
            {
                if (configElement.Persistable)
                {
                    ChoXmlDocument.CreateXmlFileIfEmpty(ConfigFilePath);
                }

                using (ChoXmlDocument document = new ChoXmlDocument(ConfigFilePath))
                {
                    string nodeName = configElement.ConfigElementPath;
                    IncludeFilePaths = document.IncludeFiles;
                    if (document.XmlDocument != null && document.XmlDocument.DocumentElement != null)
                    {
                        ChoConfiguration configuration = document.XmlDocument.DocumentElement.ToObject <ChoConfiguration>();
                        if (configuration != null)
                        {
                            ConfigNode = ChoXmlDocument.GetXmlNode(nodeName, configuration.RestOfXmlDocumentElements);
                        }
                    }
                }
            }

            return(null);
        }
        public void OnConfigurationMetaDataChanged()
        {
            ChoConfigurationMetaDataChangeEventHandler configurationMetaDataChangeEvent = ConfigurationMetaDataChangedEvent;

            if (configurationMetaDataChangeEvent != null)
            {
                using (ChoXmlDocument document = new ChoXmlDocument(MetaDataFileName, false, true))
                {
                    configurationMetaDataChangeEvent(document.XmlDocument.SelectSingleNode(@"//{0}".FormatString(ConfigSectionName)));
                }
            }
        }
Ejemplo n.º 16
0
        public override void Persist(ChoConfigSectionObjectMap configSectionObjectMap)
        {
            StringBuilder config    = new StringBuilder();
            Hashtable     keyValues = configSectionObjectMap.ToHashtable();

            foreach (string key in keyValues.Keys)
            {
                if (keyValues[key] != null)
                {
                    config.AppendFormat("<add key=\"{0}\" value=\"{1}\" />{2}", key, keyValues[key].ToString(), Environment.NewLine);
                }
            }

            File.WriteAllText(_configPath, ChoXmlDocument.SetInnerXml(_configPath, ConfigSectionName, config.ToString()));
        }
Ejemplo n.º 17
0
        protected void PersistAsPlainXml(object data, ChoDictionaryService <string, object> stateInfo)
        {
            using (ChoXmlDocument xmlDocument = new ChoXmlDocument(stateInfo[UNDERLYING_CONFIG_PATH] as string))
            {
                PersistConfigSectionDefinition(xmlDocument.XmlDocument, data);
                XmlNode configNode = xmlDocument.XmlDocument.MakeXPath(ConfigElement.ConfigElementPath);
                if (configNode != null)
                {
                    string configXml = ToXml(data);
                    if (configXml.IsNullOrEmpty())
                    {
                        return;
                    }

                    ChoXmlDocument.SetOuterXml(configNode, configXml);
                }
            }
        }
Ejemplo n.º 18
0
        public static bool IsConfigXmlModified(string configSectionPath, XmlNode oldNode)
        {
            XmlNode node = ChoXmlDocument.GetXmlNode(configSectionPath, _configuration.RestOfXmlDocumentElements);

            if (object.ReferenceEquals(node, oldNode))
            {
                return(false);
            }
            if (object.ReferenceEquals(oldNode, null))
            {
                return(true);
            }
            if (object.ReferenceEquals(node, null))
            {
                return(true);
            }
            return(node.OuterXml != oldNode.OuterXml);
        }
        public static void SetMetaDataSection(ChoBaseConfigurationElement configElement)
        {
            if (configElement == null)
            {
                return;
            }
            if (ChoObject.Equals <ChoBaseConfigurationMetaDataInfo>(configElement.MetaDataInfo, GetMetaDataSection(configElement)))
            {
                return;
            }

            string configElementPath = configElement.ConfigElementPath;
            ChoBaseConfigurationMetaDataInfo configurationMetaDataInfo = configElement.MetaDataInfo;

            configurationMetaDataInfo = ChoObject.Merge <ChoBaseConfigurationMetaDataInfo>(configurationMetaDataInfo, GetDefaultMetaDataInfo(configElement));

            if (configurationMetaDataInfo == null)
            {
                return;
            }

            using (ChoXmlDocument xmlDocument = new ChoXmlDocument(_metaDataFilepath))
            {
                if (configElementPath.IsNullOrEmpty())
                {
                    return;
                }

                XmlNode node = xmlDocument.XmlDocument.SelectSingleNode(configElementPath);
                if (node == null)
                {
                    node = xmlDocument.XmlDocument.MakeXPath(configElementPath);
                }

                if (node != null)
                {
                    ChoXmlDocument.SetOuterXml(node, configurationMetaDataInfo.ToXml());
                    xmlDocument.XmlDocument.InnerXml = ChoXmlDocument.AppendToInnerXml(node, GetPropertyInfos(configElement).ToXml());

                    xmlDocument.Save();
                }
            }
        }
Ejemplo n.º 20
0
        private static void Refresh()
        {
            try
            {
                lock (_padLock)
                {
                    //_propDict.Clear();
                }

                if (ChoMetaDataFilePathSettings.Me != null)
                {
                    _pbsDataFilepath = ChoMetaDataFilePathSettings.Me.OverridenPBSDataFilePath;
                }

                if (!ChoAppFrxSettings.Me.DisableMetaDataConfig)
                {
                    ChoXmlDocument.CreateXmlFileIfEmpty(_pbsDataFilepath);
                    _pbsDataChangeWatcher = new ChoAppConfigurationChangeFileWatcher(PBS_DATA_KEY, _pbsDataFilepath, _includeFiles);
                    _pbsDataChangeWatcher.SetConfigurationChangedEventHandler(PBS_DATA_FILE_WATCHER_KEY,
                                                                              (sender, e) =>
                    {
                        Refresh();
                    });
                }

                if (!_pbsDataFilepath.IsNullOrWhiteSpace() && File.Exists(_pbsDataFilepath))
                {
                    using (ChoXmlDocument xmlDocument = new ChoXmlDocument(_pbsDataFilepath))
                    {
                        _rootNode     = xmlDocument.XmlDocument.DocumentElement;
                        _includeFiles = xmlDocument != null ? xmlDocument.IncludeFiles : null;
                    }
                }

                BuildProfileBackingStores();
            }
            catch (Exception ex)
            {
                ChoTrace.Error(ex.ToString());
                //throw;
            }
        }
Ejemplo n.º 21
0
        private static void StopGlobalQueuedExecutionService()
        {
            if (_configurationChangeWatcher != null)
            {
                _configurationChangeWatcher.Dispose();
            }
            _configurationChangeWatcher = null;

            if (_systemConfigurationChangeWatcher != null)
            {
                _systemConfigurationChangeWatcher.Dispose();
            }
            _systemConfigurationChangeWatcher = null;

            if (_appXmlDocument != null)
            {
                _appXmlDocument.Dispose();
                _appXmlDocument = null;
            }
        }
Ejemplo n.º 22
0
        internal static object GetConfig(Type configObjectType, string configSectionPath, Type defaultConfigSectionHandlerType,
                                         ChoBaseConfigurationElement configElement)
        {
            if (_configuration == null)
            {
                return(null);
            }

            ChoConfigurationSection configurationSection = ChoConfigurationManager.GetConfigSection(configSectionPath);

            XmlNode[] restOfXmlElements = null;

            Type configSectionHandlerType = defaultConfigSectionHandlerType;

            if (configurationSection != null)
            {
                restOfXmlElements = configurationSection.RestOfXmlElements;
                if (!configurationSection.Type.IsNullOrWhiteSpace())
                {
                    configSectionHandlerType = ChoType.GetType(configurationSection.Type);
                }
            }

            configElement.ConfigSectionHandlerType = configSectionHandlerType;
            if (!typeof(IChoConfigurationSectionHandler).IsAssignableFrom(configSectionHandlerType))
            {
                throw new ChoConfigurationException("Configuration section handler type should be of IChoConfigurationSectionHandler type.");
            }

            if (typeof(IChoNullConfigurationSectionableHandler).IsAssignableFrom(configSectionHandlerType))
            {
                return(ChoType.CreateInstance <IChoConfigurationSectionHandler>(configSectionHandlerType).Create(configObjectType, null, restOfXmlElements, configElement));
            }
            else
            {
                XmlNode node = ChoXmlDocument.GetXmlNode(configSectionPath, _configuration.RestOfXmlDocumentElements);
                //if (node != null)
                return(ChoType.CreateInstance <IChoConfigurationSectionHandler>(configSectionHandlerType).Create(configObjectType, node, restOfXmlElements, configElement));
            }
            //return new ChoDefaultApplicationConfigSection(configObjectType);
        }
Ejemplo n.º 23
0
        private static void Refresh()
        {
            try
            {
                lock (_padLock)
                {
                    _propDict.Clear();
                }

                if (ChoMetaDataFilePathSettings.Me != null)
                {
                    _metaDataFilepath = ChoMetaDataFilePathSettings.Me.OverridenConfigurationMetaDataFilePath;
                }

                if (!ChoAppFrxSettings.Me.DisableMetaDataConfig)
                {
                    ChoXmlDocument.CreateXmlFileIfEmpty(_metaDataFilepath);
                    _configurationChangeWatcher = new ChoAppConfigurationChangeFileWatcher("meta-data_configurations", _metaDataFilepath, _includeFiles);
                    _configurationChangeWatcher.SetConfigurationChangedEventHandler("ChoMetaDataManager_Watcher",
                                                                                    (sender, e) =>
                    {
                        Refresh();
                    });
                }

                if (!_metaDataFilepath.IsNullOrWhiteSpace() && File.Exists(_metaDataFilepath))
                {
                    using (ChoXmlDocument xmlDocument = new ChoXmlDocument(_metaDataFilepath))
                    {
                        _rootNode     = xmlDocument.XmlDocument.DocumentElement;
                        _includeFiles = xmlDocument != null ? xmlDocument.IncludeFiles : null;
                    }
                }
            }
            catch (Exception ex)
            {
                ChoTrace.Error(ex.ToString());
                //throw;
            }
        }
Ejemplo n.º 24
0
        private void Refresh()
        {
            if (ChoAppFrxSettings.Me.DisableMetaDataConfig)
            {
                return;
            }

            try
            {
                LoadFile();
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ChoTrace.Error(ex);

                try
                {
                    string newFile = ChoPath.AddExtension(MetaDataFilePath, ChoReservedFileExt.Err);
                    if (File.Exists(newFile))
                    {
                        File.Delete(newFile);
                    }
                    File.Move(MetaDataFilePath, newFile);

                    ChoXmlDocument.CreateXmlFileIfEmpty(MetaDataFilePath);
                    LoadFile();
                }
                catch (Exception innerEx)
                {
                    ChoTrace.Error(innerEx);
                }
            }
        }
Ejemplo n.º 25
0
        public override void Persist(object data, ChoDictionaryService <string, object> stateInfo)
        {
            ChoXmlDocument.CreateXmlFileIfEmpty(ConfigFilePath);

            string backupConfigFilePath = String.Format("{0}.{1}", ConfigFilePath, ChoReservedFileExt.Cho);

            try
            {
                //Write meta-data info
                ChoConfigurationMetaDataManager.SetMetaDataSection(ConfigElement);
                if (!IsAppConfigFile)
                {
                    if (File.Exists(backupConfigFilePath))
                    {
                        File.SetAttributes(backupConfigFilePath, FileAttributes.Archive);
                    }
                    File.Copy(ConfigFilePath, backupConfigFilePath, true);
                    if (File.Exists(backupConfigFilePath))
                    {
                        File.SetAttributes(backupConfigFilePath, FileAttributes.Hidden);
                    }
                }
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ConfigElement.Log(ex.ToString());
            }

            try
            {
                //if seperate config file maintained, make a link in the application configuration
                if (IsConfigReferenceDataChanged(stateInfo))
                {
                    using (ChoXmlDocument xmlDocument = new ChoXmlDocument(ChoGlobalApplicationSettings.Me.ApplicationConfigFilePath))
                    {
                        XmlNode configNode = xmlDocument.XmlDocument.MakeXPath(ConfigElement.ConfigElementPath);
                        if (configNode != null)
                        {
                            if (!IsAppConfigFile)
                            {
                                string configXml = @"<{0} {1}=""{2}"" {3}=""{4}"" />".FormatString(ConfigSectionName, ChoConfigurationManager.PathToken, UnderlyingConfigFilePath, ChoXmlDocument.CinchoNSToken, ChoXmlDocument.CinchooNSURI);
                                ChoXmlDocument.SetNamespaceAwareOuterXml(configNode, configXml, ChoXmlDocument.CinchooNSURI);
                            }
                        }
                    }
                }

                PersistConfigData(data, stateInfo);
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                ConfigElement.Log(ex.ToString());

                if (!IsAppConfigFile)
                {
                    File.Copy(backupConfigFilePath, ConfigFilePath, true);
                }
            }
        }
        internal static void OpenExeConfiguration()
        {
            //Expand Application configuration file
            string appConfigPath = AppConfigFilePath; // AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

            if (!File.Exists(appConfigPath))
            {
                return;
            }

            ChoFile.SetReadOnly(appConfigPath, false);

            if (_configurationChangeWatcher != null)
            {
                RestoreAppConfig();
                _configurationChangeWatcher.StopWatching();
                _configurationChangeWatcher = null;
            }

            //backup the current configuration
            string backupConfigFilePath = String.Format("{0}.cho", appConfigPath);

            File.Copy(appConfigPath, backupConfigFilePath, true);

            try
            {
                _appXmlDocument            = new ChoXmlDocument(appConfigPath, false, true);
                _appIncludeConfigFilePaths = _appXmlDocument.IncludeFiles;

                if (_appXmlDocument != null && _appIncludeConfigFilePaths != null && _appIncludeConfigFilePaths.Length > 0)
                {
                    _appXmlDocument.XmlDocument.Save(appConfigPath);
                }

                _configurationChangeWatcher = new ChoAppConfigurationChangeFileWatcher(ChoConfigurationManager.AppConfigFilePath, "Configurations");
                _configurationChangeWatcher.SetConfigurationChangedEventHandler(_key, new ChoConfigurationChangedEventHandler(_configurationChangeWatcher_ConfigurationChanged));

                //Remove namespaces
                if (_appXmlDocument != null && _appIncludeConfigFilePaths != null && _appIncludeConfigFilePaths.Length > 0)
                {
                    XDocument doc = XDocument.Load(appConfigPath, LoadOptions.PreserveWhitespace);
                    doc.Descendants().Attributes().Where(a => a.IsNamespaceDeclaration).Remove();
                    doc.Save(appConfigPath, SaveOptions.DisableFormatting);
                }
                _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                //TODO: use XmlDocument to expand
                if (ChoEnvironmentSettings.HasAppConfigPathSpecified())
                {
                    appConfigPath = ChoEnvironmentSettings.GetAppConfigPath();
                    if (!String.IsNullOrEmpty(appConfigPath))
                    {
                        _configuration = ConfigurationManager.OpenExeConfiguration(appConfigPath);
                    }
                }
            }
            catch
            {
                //Rollback the configuration file
                File.Copy(backupConfigFilePath, appConfigPath, true);

                throw;
            }
        }
 static void _configurationChangeWatcher_ConfigurationChanged(object sender, ChoConfigurationChangedEventArgs e)
 {
     _appXmlDocument            = new ChoXmlDocument(AppConfigFilePath, false, true);
     _appIncludeConfigFilePaths = _appXmlDocument.IncludeFiles;
 }
Ejemplo n.º 28
0
        private void Load(string sharedEnvironmentConfigFilePath)
        {
            BaseAppConfigDirectory = null;
            DefaultEnvironment     = null;
            EnvironmentDetails     = null;
            string backupSharedEnvironmentConfigFilePath = null;

            XmlDocument doc = new XmlDocument();

            if (!String.IsNullOrEmpty(sharedEnvironmentConfigFilePath) &&
                File.Exists(sharedEnvironmentConfigFilePath))
            {
                ChoApplication.Trace(true, "Using shared environment config file: {0}".FormatString(sharedEnvironmentConfigFilePath));
                backupSharedEnvironmentConfigFilePath = "{0}.{1}".FormatString(sharedEnvironmentConfigFilePath, ChoReservedFileExt.Cho);

                try
                {
                    if (File.Exists(backupSharedEnvironmentConfigFilePath))
                    {
                        File.SetAttributes(backupSharedEnvironmentConfigFilePath, FileAttributes.Archive);
                    }
                    using (ChoXmlDocument xmlDoc = new ChoXmlDocument(sharedEnvironmentConfigFilePath))
                    {
                        doc = xmlDoc.XmlDocument;
                    }
                    //doc.Load(sharedEnvironmentConfigFilePath);
                    doc.Save(backupSharedEnvironmentConfigFilePath);
                    if (File.Exists(backupSharedEnvironmentConfigFilePath))
                    {
                        File.SetAttributes(backupSharedEnvironmentConfigFilePath, FileAttributes.Hidden);
                    }
                }
                catch (Exception ex)
                {
                    ChoApplication.Trace(true, "Error loading shared environment config file: {0}.".FormatString(sharedEnvironmentConfigFilePath));
                    ChoApplication.Trace(true, ex.ToString());

                    doc = LoadBackupSharedEnvironmentConfigFile(backupSharedEnvironmentConfigFilePath);
                }
            }
            else if (ChoApplication.GetSharedEnvironmentConfigXml != null)
            {
                string xml = ChoApplication.GetSharedEnvironmentConfigXml();
                backupSharedEnvironmentConfigFilePath = "SharedEnvironments.{0}.{1}".FormatString(ChoReservedFileExt.Xml, ChoReservedFileExt.Cho);

                if (!xml.IsNullOrWhiteSpace())
                {
                    ChoApplication.Trace(true, "Using shared environment xml:");
                    ChoApplication.Trace(true, xml);

                    try
                    {
                        doc.LoadXml(xml);
                        doc.Save(backupSharedEnvironmentConfigFilePath);
                    }
                    catch (Exception ex)
                    {
                        ChoApplication.Trace(true, "Error loading shared environment config xml.");
                        ChoApplication.Trace(true, ex.ToString());

                        doc = LoadBackupSharedEnvironmentConfigFile(backupSharedEnvironmentConfigFilePath);
                    }
                }
            }
            LoadXml(doc);
        }
Ejemplo n.º 29
0
        private static void OpenConfiguration(string appConfigPath, bool doBackup)
        {
            if (_appConfigPath != appConfigPath)
            {
                Trace.TraceInformation("Using AppConfigPath: {0}".FormatString(appConfigPath));
            }

            _appConfigPath = appConfigPath;

            if (_appConfigPath.IsNullOrWhiteSpace())
            {
                Trace.TraceError("Empty AppConfigPath passed.");
                return;
            }

            ChoFile.SetReadOnly(_appConfigPath, false);

            if (_configurationChangeWatcher != null)
            {
                RestoreAppConfig();
                //_configurationChangeWatcher.StopWatching();
            }
            //if (_systemConfigurationChangeWatcher != null)
            //{
            //    _systemConfigurationChangeWatcher.StopWatching();
            //    _systemConfigurationChangeWatcher = null;
            //}

            ChoXmlDocument.CreateXmlFileIfEmpty(_appConfigPath);

            //backup the current configuration
            string backupConfigFilePath = String.Format("{0}.{1}", _appConfigPath, ChoReservedFileExt.Cho);

            try
            {
                LoadConfigurationFile();

                if (doBackup)
                {
                    if (File.Exists(backupConfigFilePath))
                    {
                        File.SetAttributes(backupConfigFilePath, FileAttributes.Archive);
                    }
                    File.Copy(_appConfigPath, backupConfigFilePath, true);
                    if (File.Exists(backupConfigFilePath))
                    {
                        File.SetAttributes(backupConfigFilePath, FileAttributes.Hidden);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
                if (_appXmlDocument != null)
                {
                    _appXmlDocument.Dispose();
                    _appXmlDocument = null;
                }

                try
                {
                    //Rollback the configuration file
                    if (doBackup)
                    {
                        File.Copy(backupConfigFilePath, _appConfigPath, true);
                    }

                    LoadConfigurationFile();
                }
                catch
                {
                }
            }
            finally
            {
                if (_configurationChangeWatcher != null)
                {
                    _configurationChangeWatcher.StartWatching();
                }
                if (_systemConfigurationChangeWatcher != null)
                {
                    _systemConfigurationChangeWatcher.StartWatching();
                }
            }
        }
Ejemplo n.º 30
0
 public override void Persist(ChoConfigSectionObjectMap configSectionObjectMap)
 {
     File.WriteAllText(_configPath, ChoXmlDocument.SetOuterXml(_configPath, ConfigSectionName,
                                                               ChoObject.ToXmlString(configSectionObjectMap.ConfigObject)));
 }