internal static RelayNodeConfig GetRelayNodeConfig(XmlNode section) { RelayNodeConfig mainConfig; if (string.IsNullOrWhiteSpace(section.InnerXml)) { mainConfig = new RelayNodeConfig(); mainConfig.UseConfigurationServer = true; mainConfig.SectionXml = section; } else { mainConfig = DeserializeMainConfig(section); } if (mainConfig.UseConfigurationServer) //override with remotely hosted section { mainConfig = GetMainConfigFromServer(mainConfig.ConfigurationServerSectionName); } WatchConfig(mainConfig); FillSubConfigs(mainConfig); return(mainConfig); }
private static void ReloadDefaultConfig(object state, EventArgs args) { RelayNodeConfig newConfig = state as RelayNodeConfig; if (newConfig != null) { DefaultConfiguration = newConfig; } }
private static RelayNodeConfig GetMainConfigFromServer(string sectionName) { XmlNode remoteSectionNode = ConfigurationClient.GetSectionXml(sectionName); RelayNodeConfig remoteConfig = DeserializeMainConfig(remoteSectionNode); //no matter what config server says we need to keep these two properties the same remoteConfig.UseConfigurationServer = true; remoteConfig.ConfigurationServerSectionName = sectionName; return(remoteConfig); }
private static RelayNodeConfig GetDefaultConfig() { if (DefaultConfiguration == null) { log.Info("No Data Relay configuration found. Getting default client configuration from configuration server."); DefaultConfiguration = RelayNodeSectionHandler.GetRelayNodeConfig(GetDefaultConfigNode()); AddReloadEventHandler(ReloadDefaultConfig); } return(DefaultConfiguration); }
public static RelayNodeConfig GetRelayNodeConfig(EventHandler reloadEventHandler) { AddReloadEventHandler(reloadEventHandler); RelayNodeConfig config = ConfigurationManager.GetSection(RelayNodeSectionHandler.ConfigSectionName) as RelayNodeConfig; if (config == null) { config = GetDefaultConfig(); } return(config); }
private static RelayNodeConfig DeserializeMainConfig(XmlNode section) { XmlSerializer ser = new XmlSerializer(typeof(RelayNodeConfig)); object configurationObject = ser.Deserialize(new XmlNodeReader(section)); if (!(configurationObject is RelayNodeConfig)) { throw new ConfigurationErrorsException("Relay Node config with xml " + section.OuterXml + " could not be deserialzed"); } RelayNodeConfig mainConfig = configurationObject as RelayNodeConfig; mainConfig.SectionXml = section; return(mainConfig); }
private static void ReloadConfig(string name) { try { lock (ReloadEventHandlers) { SetConfigurationFile(); ConfigurationSection relayNodeConfigSection = ConfigurationFile.GetSection(ConfigSectionName); XmlDocument configDoc = new XmlDocument(); XmlNode configNode; if (relayNodeConfigSection != null) { string configSource = relayNodeConfigSection.SectionInformation.ConfigSource; if (configSource == String.Empty) { configDoc.Load(ConfigurationFile.FilePath); configNode = configDoc.SelectSingleNode("*/*[local-name()='RelayNodeConfig']"); } else { configDoc.Load(GetFilePath(configSource)); configNode = configDoc.DocumentElement; } } else { configNode = RelayNodeConfig.GetDefaultConfigNode(); } RelayNodeConfig newConfig = GetRelayNodeConfig(configNode); foreach (EventHandler handler in ReloadEventHandlers) { handler(newConfig, EventArgs.Empty); } } } catch (Exception e) { //since this happens on a background thread we really want to swallow it, because otherwise without legacy exception handling //enabled it'll tank the app pool. log.ErrorFormat("Exception processing config reload: {0}", e); } }
public object Create(object parent, object configContext, XmlNode section) { try { RelayNodeConfig config = GetRelayNodeConfig(section); return(config); } catch (Exception ex) { if (log.IsErrorEnabled) { log.ErrorFormat("Exception getting relay node config: {0}", ex); } throw; // we want callers to know there was a problem } }
private static void FillSubConfigs(RelayNodeConfig mainConfig) { if (mainConfig.SectionXml == null) { throw new ConfigurationErrorsException("Section Xml on RelayNodeConfig was not available; can't fill sub configs!"); } //TODO: implement defaults for subsections. foreach (XmlNode node in mainConfig.SectionXml.ChildNodes) { switch (node.Name) { case "RelayComponents": RelayComponents comps = GetSubConfig <RelayComponents>(node); if (comps != null) { mainConfig.RelayComponents = comps.RelayComponentCollection; } else { if (log.IsErrorEnabled) { log.Error("No relay component config found."); } } break; case "TypeSettings": mainConfig.TypeSettings = GetSubConfig <TypeSettings>(node); break; case "RelayNodeMapping": mainConfig.RelayNodeMapping = GetSubConfig <RelayNodeMapping>(node); break; case "TransportSettings": mainConfig.TransportSettings = GetSubConfig <TransportSettings>(node); break; } } }
public static RelayNodeConfig GetRelayNodeConfig(EventHandler reloadEventHandler) { RelayNodeConfig config = null; try { AddReloadEventHandler(reloadEventHandler); config = System.Configuration.ConfigurationManager.GetSection(RelayNodeSectionHandler.ConfigSectionName) as RelayNodeConfig; } catch (Exception ex) { if (log.IsErrorEnabled) { log.ErrorFormat("Exception loading Relay Node Config Section: {0}", ex); } throw; // we want callers to know something went wrong. } return(config); }
private static void WatchConfig(RelayNodeConfig mainConfig) { //either way, watch the file that represents it, since it might switch between local and not ConfigurationSection relayNodeConfigSection = ConfigurationFile.GetSection(ConfigSectionName); string configSource = String.Empty; if (relayNodeConfigSection != null) { configSource = relayNodeConfigSection.SectionInformation.ConfigSource; } string configPath = String.Empty; if (!String.IsNullOrEmpty((configSource))) { //if there's no config source, then the info is embedded in the app config, and that's the file we need to watch if (HttpContext.Current == null) { //but if there's an httpcontext, then we're in a web context, and you can't update an IIS config file without bouncing //the appdomain, so there's no point in watching it. configPath = Path.Combine(BasePath, configSource); } } else { configPath = Path.GetFullPath(ConfigurationFile.FilePath); } if (!string.IsNullOrEmpty(configPath)) { ConfigurationWatcher.WatchFile(configPath, ReloadConfig); } //if it came from config server, then register reload notification there if (mainConfig.UseConfigurationServer) { ConfigurationWatcher.WatchRemoteSection(mainConfig.ConfigurationServerSectionName, ReloadConfig); } }
static void DelayProcessConfigChange(object ar) { lock (configLoadLock) { pendingConfigReloads.Clear(); try { System.Configuration.Configuration conf = GetConfigurationFile(); ConfigurationSection confSection = conf.GetSection(ConfigSectionName); string configFilePath = GetConfigFilePath(conf, confSection); XmlDocument configDoc = new XmlDocument(); configDoc.Load(configFilePath); XmlNode configNode; if (confSection.SectionInformation.ConfigSource == String.Empty) { configNode = configDoc.SelectSingleNode("*/*[local-name()='RelayNodeConfig']"); } else { configNode = configDoc.DocumentElement; } RelayNodeConfig relayNodeConfig = GetRelayNodeConfig(configNode); //object configObject = System.Configuration.ConfigurationManager.GetSection(RelayNodeSectionHandler.ConfigSectionName); System.Configuration.ConfigurationManager.RefreshSection(RelayNodeSectionHandler.ConfigSectionName); //this will retrigger the watch foreach (EventHandler handler in ReloadEventHandlers) { handler(relayNodeConfig, EventArgs.Empty); } } catch (Exception ex) { if (log.IsErrorEnabled) { log.ErrorFormat("Exception processing relay system config reload: {0}", ex); } } } }
internal static RelayNodeConfig GetRelayNodeConfig(XmlNode section) { //XPathNavigator nav = section.CreateNavigator(); XmlSerializer ser = new XmlSerializer(typeof(RelayNodeConfig)); object configurationObject; configurationObject = ser.Deserialize(new XmlNodeReader(section)); RelayNodeConfig typedConfig = configurationObject as RelayNodeConfig; System.Configuration.Configuration confFile = GetConfigurationFile(); string basePath = confFile.FilePath; if (typedConfig != null) { #region Get Sub Configs foreach (XmlNode node in section.ChildNodes) { switch (node.Name) { case "RelayComponents": RelayComponents comps = GetSourcedObject <RelayComponents>(basePath, node); if (comps != null) { typedConfig.RelayComponents = comps.RelayComponentCollection; } else { if (log.IsErrorEnabled) { log.Error("No relay component config found."); } } break; case "TypeSettings": TypeSettings typeSettings = GetSourcedObject <TypeSettings>(basePath, node); if (typeSettings.TypeSettingCollection.Count == 0) { // attempt custom load typeSettings = TypeSettingsConfigLoader.Load(basePath, node); } typedConfig.TypeSettings = typeSettings; break; case "RelayNodeMapping": RelayNodeMapping nodeMapping = GetSourcedObject <RelayNodeMapping>(basePath, node); typedConfig.RelayNodeMapping = nodeMapping; break; case "TransportSettings": TransportSettings transportSettings = GetSourcedObject <TransportSettings>(basePath, node); typedConfig.TransportSettings = transportSettings; break; } } #endregion if (System.Web.HttpContext.Current == null) //not a web project, doesn't apply { WatchConfigFiles(); } else { SectionInformation info = confFile.GetSection(RelayNodeSectionHandler.ConfigSectionName).SectionInformation; if (!info.RestartOnExternalChanges) { WatchConfigFiles(); } } } return(typedConfig); }