From 6955190c7db2563927bc53fd47ed0b47094be45c Mon Sep 17 00:00:00 2001 From: Dev Random Date: Tue, 25 Feb 2014 08:24:22 -0500 Subject: Add Util method to load OpSys env vars --- OpenSim/Framework/Util.cs | 170 +++++++++++----------- OpenSim/Region/Application/ConfigurationLoader.cs | 24 +-- OpenSim/Server/Base/ServicesServerBase.cs | 34 ++--- 3 files changed, 104 insertions(+), 124 deletions(-) diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index c7377b8..65cf177 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -946,11 +946,12 @@ namespace OpenSim.Framework } #region Nini (config) related Methods + public static IConfigSource ConvertDataRowToXMLConfig(DataRow row, string fileName) { if (!File.Exists(fileName)) { - //create new file + // create new file } XmlConfigSource config = new XmlConfigSource(fileName); AddDataRowToConfig(config, row); @@ -968,25 +969,6 @@ namespace OpenSim.Framework } } - public static string GetConfigVarWithDefaultSection(IConfigSource config, string varname, string section) - { - // First, check the Startup section, the default section - IConfig cnf = config.Configs["Startup"]; - if (cnf == null) - return string.Empty; - string val = cnf.GetString(varname, string.Empty); - - // Then check for an overwrite of the default in the given section - if (!string.IsNullOrEmpty(section)) - { - cnf = config.Configs[section]; - if (cnf != null) - val = cnf.GetString(varname, val); - } - - return val; - } - /// /// Gets the value of a configuration variable by looking into /// multiple sections in order. The latter sections overwrite @@ -1039,6 +1021,91 @@ namespace OpenSim.Framework return (T)val; } + public static void MergeEnvironmentToConfig(IConfigSource ConfigSource) + { + IConfig enVars = ConfigSource.Configs["Environment"]; + // if section does not exist then user isn't expecting them, so don't bother. + if( enVars != null ) + { + // load the values from the environment + EnvConfigSource envConfigSource = new EnvConfigSource(); + // add the requested keys + string[] env_keys = enVars.GetKeys(); + foreach ( string key in env_keys ) + { + envConfigSource.AddEnv(key, string.Empty); + } + // load the values from environment + envConfigSource.LoadEnv(); + // add them in to the master + ConfigSource.Merge(envConfigSource); + ConfigSource.ExpandKeyValues(); + } + } + + public static T ReadSettingsFromIniFile(IConfig config, T settingsClass) + { + Type settingsType = settingsClass.GetType(); + + FieldInfo[] fieldInfos = settingsType.GetFields(); + foreach (FieldInfo fieldInfo in fieldInfos) + { + if (!fieldInfo.IsStatic) + { + if (fieldInfo.FieldType == typeof(System.String)) + { + fieldInfo.SetValue(settingsClass, config.Get(fieldInfo.Name, (string)fieldInfo.GetValue(settingsClass))); + } + else if (fieldInfo.FieldType == typeof(System.Boolean)) + { + fieldInfo.SetValue(settingsClass, config.GetBoolean(fieldInfo.Name, (bool)fieldInfo.GetValue(settingsClass))); + } + else if (fieldInfo.FieldType == typeof(System.Int32)) + { + fieldInfo.SetValue(settingsClass, config.GetInt(fieldInfo.Name, (int)fieldInfo.GetValue(settingsClass))); + } + else if (fieldInfo.FieldType == typeof(System.Single)) + { + fieldInfo.SetValue(settingsClass, config.GetFloat(fieldInfo.Name, (float)fieldInfo.GetValue(settingsClass))); + } + else if (fieldInfo.FieldType == typeof(System.UInt32)) + { + fieldInfo.SetValue(settingsClass, Convert.ToUInt32(config.Get(fieldInfo.Name, ((uint)fieldInfo.GetValue(settingsClass)).ToString()))); + } + } + } + + PropertyInfo[] propertyInfos = settingsType.GetProperties(); + foreach (PropertyInfo propInfo in propertyInfos) + { + if ((propInfo.CanRead) && (propInfo.CanWrite)) + { + if (propInfo.PropertyType == typeof(System.String)) + { + propInfo.SetValue(settingsClass, config.Get(propInfo.Name, (string)propInfo.GetValue(settingsClass, null)), null); + } + else if (propInfo.PropertyType == typeof(System.Boolean)) + { + propInfo.SetValue(settingsClass, config.GetBoolean(propInfo.Name, (bool)propInfo.GetValue(settingsClass, null)), null); + } + else if (propInfo.PropertyType == typeof(System.Int32)) + { + propInfo.SetValue(settingsClass, config.GetInt(propInfo.Name, (int)propInfo.GetValue(settingsClass, null)), null); + } + else if (propInfo.PropertyType == typeof(System.Single)) + { + propInfo.SetValue(settingsClass, config.GetFloat(propInfo.Name, (float)propInfo.GetValue(settingsClass, null)), null); + } + if (propInfo.PropertyType == typeof(System.UInt32)) + { + propInfo.SetValue(settingsClass, Convert.ToUInt32(config.Get(propInfo.Name, ((uint)propInfo.GetValue(settingsClass, null)).ToString())), null); + } + } + } + + return settingsClass; + } + #endregion public static float Clip(float x, float min, float max) @@ -1411,69 +1478,6 @@ namespace OpenSim.Framework return displayConnectionString; } - public static T ReadSettingsFromIniFile(IConfig config, T settingsClass) - { - Type settingsType = settingsClass.GetType(); - - FieldInfo[] fieldInfos = settingsType.GetFields(); - foreach (FieldInfo fieldInfo in fieldInfos) - { - if (!fieldInfo.IsStatic) - { - if (fieldInfo.FieldType == typeof(System.String)) - { - fieldInfo.SetValue(settingsClass, config.Get(fieldInfo.Name, (string)fieldInfo.GetValue(settingsClass))); - } - else if (fieldInfo.FieldType == typeof(System.Boolean)) - { - fieldInfo.SetValue(settingsClass, config.GetBoolean(fieldInfo.Name, (bool)fieldInfo.GetValue(settingsClass))); - } - else if (fieldInfo.FieldType == typeof(System.Int32)) - { - fieldInfo.SetValue(settingsClass, config.GetInt(fieldInfo.Name, (int)fieldInfo.GetValue(settingsClass))); - } - else if (fieldInfo.FieldType == typeof(System.Single)) - { - fieldInfo.SetValue(settingsClass, config.GetFloat(fieldInfo.Name, (float)fieldInfo.GetValue(settingsClass))); - } - else if (fieldInfo.FieldType == typeof(System.UInt32)) - { - fieldInfo.SetValue(settingsClass, Convert.ToUInt32(config.Get(fieldInfo.Name, ((uint)fieldInfo.GetValue(settingsClass)).ToString()))); - } - } - } - - PropertyInfo[] propertyInfos = settingsType.GetProperties(); - foreach (PropertyInfo propInfo in propertyInfos) - { - if ((propInfo.CanRead) && (propInfo.CanWrite)) - { - if (propInfo.PropertyType == typeof(System.String)) - { - propInfo.SetValue(settingsClass, config.Get(propInfo.Name, (string)propInfo.GetValue(settingsClass, null)), null); - } - else if (propInfo.PropertyType == typeof(System.Boolean)) - { - propInfo.SetValue(settingsClass, config.GetBoolean(propInfo.Name, (bool)propInfo.GetValue(settingsClass, null)), null); - } - else if (propInfo.PropertyType == typeof(System.Int32)) - { - propInfo.SetValue(settingsClass, config.GetInt(propInfo.Name, (int)propInfo.GetValue(settingsClass, null)), null); - } - else if (propInfo.PropertyType == typeof(System.Single)) - { - propInfo.SetValue(settingsClass, config.GetFloat(propInfo.Name, (float)propInfo.GetValue(settingsClass, null)), null); - } - if (propInfo.PropertyType == typeof(System.UInt32)) - { - propInfo.SetValue(settingsClass, Convert.ToUInt32(config.Get(propInfo.Name, ((uint)propInfo.GetValue(settingsClass, null)).ToString())), null); - } - } - } - - return settingsClass; - } - public static string Base64ToString(string str) { Decoder utf8Decode = Encoding.UTF8.GetDecoder(); diff --git a/OpenSim/Region/Application/ConfigurationLoader.cs b/OpenSim/Region/Application/ConfigurationLoader.cs index 52e520c..06ce145 100644 --- a/OpenSim/Region/Application/ConfigurationLoader.cs +++ b/OpenSim/Region/Application/ConfigurationLoader.cs @@ -82,8 +82,7 @@ namespace OpenSim List sources = new List(); - string masterFileName = - startupConfig.GetString("inimaster", "OpenSimDefaults.ini"); + string masterFileName = startupConfig.GetString("inimaster", "OpenSimDefaults.ini"); if (masterFileName == "none") masterFileName = String.Empty; @@ -207,26 +206,13 @@ namespace OpenSim Environment.Exit(1); } + // Merge OpSys env vars + m_log.Info("[CONFIG]: Loading environment variables for Config"); + Util.MergeEnvironmentToConfig(m_config.Source); + // Make sure command line options take precedence m_config.Source.Merge(argvSource); - IConfig enVars = m_config.Source.Configs["Environment"]; - - if( enVars != null ) - { - string[] env_keys = enVars.GetKeys(); - - foreach ( string key in env_keys ) - { - envConfigSource.AddEnv(key, string.Empty); - } - - envConfigSource.LoadEnv(); - m_config.Source.Merge(envConfigSource); - } - - m_config.Source.ExpandKeyValues(); - ReadConfigSettings(); return m_config; diff --git a/OpenSim/Server/Base/ServicesServerBase.cs b/OpenSim/Server/Base/ServicesServerBase.cs index 667cef8..8352ee2 100644 --- a/OpenSim/Server/Base/ServicesServerBase.cs +++ b/OpenSim/Server/Base/ServicesServerBase.cs @@ -49,9 +49,7 @@ namespace OpenSim.Server.Base { // Logger // - private static readonly ILog m_log = - LogManager.GetLogger( - MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); // Command line args // @@ -72,11 +70,9 @@ namespace OpenSim.Server.Base public ServicesServerBase(string prompt, string[] args) : base() { // Save raw arguments - // m_Arguments = args; // Read command line - // ArgvConfigSource argvConfig = new ArgvConfigSource(args); argvConfig.AddSwitch("Startup", "console", "c"); @@ -86,7 +82,6 @@ namespace OpenSim.Server.Base argvConfig.AddSwitch("Startup", "logconfig", "g"); // Automagically create the ini file name - // string fileName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location); string iniFile = fileName + ".ini"; string logConfig = null; @@ -95,19 +90,17 @@ namespace OpenSim.Server.Base if (startupConfig != null) { // Check if a file name was given on the command line - // iniFile = startupConfig.GetString("inifile", iniFile); - // + // Check if a prompt was given on the command line prompt = startupConfig.GetString("prompt", prompt); - // + // Check for a Log4Net config file on the command line - logConfig =startupConfig.GetString("logconfig",logConfig); + logConfig =startupConfig.GetString("logconfig", logConfig); } - // Find out of the file name is a URI and remote load it - // if it's possible. Load it as a local file otherwise. - // + // Find out of the file name is a URI and remote load it if possible. + // Load it as a local file otherwise. Uri configUri; try @@ -129,13 +122,14 @@ namespace OpenSim.Server.Base Environment.Exit(1); } - // Merge the configuration from the command line into the - // loaded file - // + // Merge OpSys env vars + m_log.Info("[CONFIG]: Loading environment variables for Config"); + Util.MergeEnvironmentToConfig(Config); + + // Merge the configuration from the command line into the loaded file Config.Merge(argvConfig); // Refresh the startupConfig post merge - // if (Config.Configs["Startup"] != null) { startupConfig = Config.Configs["Startup"]; @@ -145,13 +139,10 @@ namespace OpenSim.Server.Base prompt = startupConfig.GetString("Prompt", prompt); - // Allow derived classes to load config before the console is - // opened. - // + // Allow derived classes to load config before the console is opened. ReadConfig(); // Create main console - // string consoleType = "local"; if (startupConfig != null) consoleType = startupConfig.GetString("console", consoleType); @@ -195,7 +186,6 @@ namespace OpenSim.Server.Base // Allow derived classes to perform initialization that // needs to be done after the console has opened - // Initialise(); } -- cgit v1.1