Add Util method to load OpSys env vars

0.8.0.3
Dev Random 2014-02-25 08:24:22 -05:00 committed by Justin Clark-Casey (justincc)
parent ce5d308d23
commit 6955190c7d
3 changed files with 104 additions and 124 deletions

View File

@ -946,6 +946,7 @@ namespace OpenSim.Framework
} }
#region Nini (config) related Methods #region Nini (config) related Methods
public static IConfigSource ConvertDataRowToXMLConfig(DataRow row, string fileName) public static IConfigSource ConvertDataRowToXMLConfig(DataRow row, string fileName)
{ {
if (!File.Exists(fileName)) if (!File.Exists(fileName))
@ -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;
}
/// <summary> /// <summary>
/// Gets the value of a configuration variable by looking into /// Gets the value of a configuration variable by looking into
/// multiple sections in order. The latter sections overwrite /// multiple sections in order. The latter sections overwrite
@ -1039,6 +1021,91 @@ namespace OpenSim.Framework
return (T)val; 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<T>(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 #endregion
public static float Clip(float x, float min, float max) public static float Clip(float x, float min, float max)
@ -1411,69 +1478,6 @@ namespace OpenSim.Framework
return displayConnectionString; return displayConnectionString;
} }
public static T ReadSettingsFromIniFile<T>(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) public static string Base64ToString(string str)
{ {
Decoder utf8Decode = Encoding.UTF8.GetDecoder(); Decoder utf8Decode = Encoding.UTF8.GetDecoder();

View File

@ -82,8 +82,7 @@ namespace OpenSim
List<string> sources = new List<string>(); List<string> sources = new List<string>();
string masterFileName = string masterFileName = startupConfig.GetString("inimaster", "OpenSimDefaults.ini");
startupConfig.GetString("inimaster", "OpenSimDefaults.ini");
if (masterFileName == "none") if (masterFileName == "none")
masterFileName = String.Empty; masterFileName = String.Empty;
@ -207,26 +206,13 @@ namespace OpenSim
Environment.Exit(1); 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 // Make sure command line options take precedence
m_config.Source.Merge(argvSource); 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(); ReadConfigSettings();
return m_config; return m_config;

View File

@ -49,9 +49,7 @@ namespace OpenSim.Server.Base
{ {
// Logger // Logger
// //
private static readonly ILog m_log = private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
LogManager.GetLogger(
MethodBase.GetCurrentMethod().DeclaringType);
// Command line args // Command line args
// //
@ -72,11 +70,9 @@ namespace OpenSim.Server.Base
public ServicesServerBase(string prompt, string[] args) : base() public ServicesServerBase(string prompt, string[] args) : base()
{ {
// Save raw arguments // Save raw arguments
//
m_Arguments = args; m_Arguments = args;
// Read command line // Read command line
//
ArgvConfigSource argvConfig = new ArgvConfigSource(args); ArgvConfigSource argvConfig = new ArgvConfigSource(args);
argvConfig.AddSwitch("Startup", "console", "c"); argvConfig.AddSwitch("Startup", "console", "c");
@ -86,7 +82,6 @@ namespace OpenSim.Server.Base
argvConfig.AddSwitch("Startup", "logconfig", "g"); argvConfig.AddSwitch("Startup", "logconfig", "g");
// Automagically create the ini file name // Automagically create the ini file name
//
string fileName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location); string fileName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
string iniFile = fileName + ".ini"; string iniFile = fileName + ".ini";
string logConfig = null; string logConfig = null;
@ -95,19 +90,17 @@ namespace OpenSim.Server.Base
if (startupConfig != null) if (startupConfig != null)
{ {
// Check if a file name was given on the command line // Check if a file name was given on the command line
//
iniFile = startupConfig.GetString("inifile", iniFile); iniFile = startupConfig.GetString("inifile", iniFile);
//
// Check if a prompt was given on the command line // Check if a prompt was given on the command line
prompt = startupConfig.GetString("prompt", prompt); prompt = startupConfig.GetString("prompt", prompt);
//
// Check for a Log4Net config file on the command line // 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 // Find out of the file name is a URI and remote load it if possible.
// if it's possible. Load it as a local file otherwise. // Load it as a local file otherwise.
//
Uri configUri; Uri configUri;
try try
@ -129,13 +122,14 @@ namespace OpenSim.Server.Base
Environment.Exit(1); Environment.Exit(1);
} }
// Merge the configuration from the command line into the // Merge OpSys env vars
// loaded file 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); Config.Merge(argvConfig);
// Refresh the startupConfig post merge // Refresh the startupConfig post merge
//
if (Config.Configs["Startup"] != null) if (Config.Configs["Startup"] != null)
{ {
startupConfig = Config.Configs["Startup"]; startupConfig = Config.Configs["Startup"];
@ -145,13 +139,10 @@ namespace OpenSim.Server.Base
prompt = startupConfig.GetString("Prompt", prompt); prompt = startupConfig.GetString("Prompt", prompt);
// Allow derived classes to load config before the console is // Allow derived classes to load config before the console is opened.
// opened.
//
ReadConfig(); ReadConfig();
// Create main console // Create main console
//
string consoleType = "local"; string consoleType = "local";
if (startupConfig != null) if (startupConfig != null)
consoleType = startupConfig.GetString("console", consoleType); consoleType = startupConfig.GetString("console", consoleType);
@ -195,7 +186,6 @@ namespace OpenSim.Server.Base
// Allow derived classes to perform initialization that // Allow derived classes to perform initialization that
// needs to be done after the console has opened // needs to be done after the console has opened
//
Initialise(); Initialise();
} }