From e5f3337c3fd4266d06e6e75ad3bf7cbcd68163ca Mon Sep 17 00:00:00 2001 From: Melanie Thielker Date: Thu, 21 May 2009 23:06:10 +0000 Subject: Implement .ini file includes. Anything that begins with "Include-" will be treated as another ini source to load. For example: Include-Asset = AssetSetup.ini will load AssetSetup.ini after all other ini files are done. This works recursively, too --- OpenSim/Region/Application/ConfigurationLoader.cs | 208 ++++++++++++++-------- 1 file changed, 131 insertions(+), 77 deletions(-) (limited to 'OpenSim/Region/Application/ConfigurationLoader.cs') diff --git a/OpenSim/Region/Application/ConfigurationLoader.cs b/OpenSim/Region/Application/ConfigurationLoader.cs index 467b099..b317db5 100644 --- a/OpenSim/Region/Application/ConfigurationLoader.cs +++ b/OpenSim/Region/Application/ConfigurationLoader.cs @@ -26,6 +26,7 @@ */ using System; +using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading; @@ -42,100 +43,120 @@ namespace OpenSim protected OpenSimConfigSource m_config; protected NetworkServersInfo m_networkServersInfo; - private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog m_log = + LogManager.GetLogger( + MethodBase.GetCurrentMethod().DeclaringType); public ConfigurationLoader() { } - public OpenSimConfigSource LoadConfigSettings(IConfigSource configSource, out ConfigSettings configSettings, - out NetworkServersInfo networkInfo) + public OpenSimConfigSource LoadConfigSettings( + IConfigSource argvSource, out ConfigSettings configSettings, + out NetworkServersInfo networkInfo) { m_configSettings = configSettings = new ConfigSettings(); m_networkServersInfo = networkInfo = new NetworkServersInfo(); + bool iniFileExists = false; - IConfig startupConfig = configSource.Configs["Startup"]; + IConfig startupConfig = argvSource.Configs["Startup"]; - string iniFileName = startupConfig.GetString("inifile", "OpenSim.ini"); - Application.iniFilePath = Path.Combine(Util.configDir(), iniFileName); + List sources = new List(); - string masterFileName = startupConfig.GetString("inimaster", ""); - string masterfilePath = Path.Combine(Util.configDir(), masterFileName); + string masterFileName = + startupConfig.GetString("inimaster", String.Empty); - string iniDirName = startupConfig.GetString("inidirectory", "config"); - //string iniDirPath = Path.Combine(Util.configDir(), iniDirName); + if (IsUri(masterFileName)) + { + if (!sources.Contains(masterFileName)) + sources.Add(masterFileName); + } + else + { + string masterFilePath = Path.GetFullPath( + Path.Combine(Util.configDir(), masterFileName)); - m_config = new OpenSimConfigSource(); - m_config.Source = new IniConfigSource(); - m_config.Source.Merge(DefaultConfig()); + if (masterFileName != String.Empty && + File.Exists(masterFilePath) && + (!sources.Contains(masterFilePath))) + sources.Add(masterFilePath); + } - m_log.Info("[CONFIG] Reading configuration settings"); - Uri configUri; - String xmlPath = Path.Combine(Util.configDir(), "OpenSim.xml"); + string iniFileName = + startupConfig.GetString("inifile", "OpenSim.ini"); - //check for master .INI file (name passed in command line, no default), or XML over http - if (masterFileName.Length > 0) // If a master file name is given ... + if (IsUri(iniFileName)) { - m_log.InfoFormat("[CONFIG] Reading config master file {0}", masterfilePath); + if (!sources.Contains(iniFileName)) + sources.Add(iniFileName); + Application.iniFilePath = iniFileName; + } + else + { + Application.iniFilePath = Path.GetFullPath( + Path.Combine(Util.configDir(), iniFileName)); - bool isMasterUri = Uri.TryCreate(masterFileName, UriKind.Absolute, out configUri) && - configUri.Scheme == Uri.UriSchemeHttp; + if (!File.Exists(Application.iniFilePath)) + { + iniFileName = "OpenSim.xml"; + Application.iniFilePath = Path.GetFullPath( + Path.Combine(Util.configDir(), iniFileName)); + } - if (!ReadConfig(masterFileName, masterfilePath, m_config, isMasterUri)) + if (File.Exists(Application.iniFilePath)) { - m_log.FatalFormat("[CONFIG] Could not open master config file {0}", masterfilePath); + if (!sources.Contains(Application.iniFilePath)) + sources.Add(Application.iniFilePath); } } - if (Directory.Exists(iniDirName)) + string iniDirName = + startupConfig.GetString("inidirectory", "config"); + string iniDirPath = + Path.Combine(Util.configDir(), iniDirName); + + if (Directory.Exists(iniDirPath)) { - m_log.InfoFormat("Searching folder: {0} , for config ini files", iniDirName); + m_log.InfoFormat("Searching folder {0} for config ini files", + iniDirPath); + string[] fileEntries = Directory.GetFiles(iniDirName); foreach (string filePath in fileEntries) { if (Path.GetExtension(filePath).ToLower() == ".ini") { - // m_log.InfoFormat("reading ini file < {0} > from config dir", filePath); - ReadConfig(Path.GetFileName(filePath), filePath, m_config, false); + if (!sources.Contains(Path.GetFullPath(filePath))) + sources.Add(Path.GetFullPath(filePath)); } } } - // Check for .INI file (either default or name passed on command - // line) or XML config source over http - bool isIniUri = Uri.TryCreate(iniFileName, UriKind.Absolute, out configUri) && - configUri.Scheme == Uri.UriSchemeHttp; - iniFileExists = ReadConfig(iniFileName, Application.iniFilePath, m_config, isIniUri); + m_config = new OpenSimConfigSource(); + m_config.Source = new IniConfigSource(); + m_config.Source.Merge(DefaultConfig()); - if (!iniFileExists) + m_log.Info("[CONFIG] Reading configuration settings"); + + if (sources.Count == 0) { - // check for a xml config file - if (File.Exists(xmlPath)) - { - Application.iniFilePath = xmlPath; + m_log.FatalFormat("[CONFIG] Could not load any configuration"); + m_log.FatalFormat("[CONFIG] Did you copy the OpenSim.ini.example file to OpenSim.ini?"); + Environment.Exit(1); + } - m_log.InfoFormat("Reading XML configuration from {0}", Path.GetFullPath(xmlPath)); + for (int i = 0 ; i < sources.Count ; i++) + { + if (ReadConfig(sources[i])) iniFileExists = true; - - m_config.Source = new XmlConfigSource(); - m_config.Source.Merge(new XmlConfigSource(Application.iniFilePath)); - } + AddIncludes(sources); } - m_config.Source.Merge(configSource); - if (!iniFileExists) { m_log.FatalFormat("[CONFIG] Could not load any configuration"); - if (!isIniUri) - m_log.FatalFormat("[CONFIG] Tried to load {0}, ", Path.GetFullPath(Application.iniFilePath)); - else - m_log.FatalFormat("[CONFIG] Tried to load from URI {0}, ", iniFileName); - m_log.FatalFormat("[CONFIG] and XML source {0}", Path.GetFullPath(xmlPath)); - - m_log.FatalFormat("[CONFIG] Did you copy the OpenSim.ini.example file to OpenSim.ini?"); + m_log.FatalFormat("[CONFIG] Configuration exists, but there was an error loading it!"); Environment.Exit(1); } @@ -144,48 +165,81 @@ namespace OpenSim return m_config; } + private void AddIncludes(List sources) + { + foreach (IConfig config in m_config.Source.Configs) + { + string[] keys = config.GetKeys(); + foreach (string k in keys) + { + if (k.StartsWith("Include-")) + { + string file = config.GetString(k); + if (IsUri(file)) + { + if (!sources.Contains(file)) + sources.Add(file); + } + else + { + string path = Path.GetFullPath( + Path.Combine(Util.configDir(), file)); + if (File.Exists(path)) + { + if (!sources.Contains(path)) + sources.Add(path); + } + } + } + } + } + } + + bool IsUri(string file) + { + Uri configUri; + + return Uri.TryCreate(file, UriKind.Absolute, + out configUri) && configUri.Scheme == Uri.UriSchemeHttp; + } + /// /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http /// - /// The name of the ini to load /// Full path to the ini - /// The current configuration source - /// Boolean representing whether the ini source is a URI path over http or a file on the system /// - private bool ReadConfig(string iniName, string iniPath, OpenSimConfigSource m_config, bool isUri) + private bool ReadConfig(string iniPath) { bool success = false; - if (!isUri && File.Exists(iniPath)) + if (!IsUri(iniPath)) { - m_log.InfoFormat("[CONFIG] Reading configuration file {0}", Path.GetFullPath(iniPath)); + m_log.InfoFormat("[CONFIG] Reading configuration file {0}", + Path.GetFullPath(iniPath)); - // From reading Nini's code, it seems that later merged keys replace earlier ones. m_config.Source.Merge(new IniConfigSource(iniPath)); success = true; } else { - if (isUri) - { - m_log.InfoFormat("[CONFIG] {0} is a http:// URI, fetching ...", iniName); + m_log.InfoFormat("[CONFIG] {0} is a http:// URI, fetching ...", + iniPath); - // The ini file path is a http URI - // Try to read it - try - { - XmlReader r = XmlReader.Create(iniName); - XmlConfigSource cs = new XmlConfigSource(r); - m_config.Source.Merge(cs); + // The ini file path is a http URI + // Try to read it + // + try + { + XmlReader r = XmlReader.Create(iniPath); + XmlConfigSource cs = new XmlConfigSource(r); + m_config.Source.Merge(cs); - success = true; - m_log.InfoFormat("[CONFIG] Loaded config from {0}", iniName); - } - catch (Exception e) - { - m_log.FatalFormat("[CONFIG] Exception reading config from URI {0}\n" + e.ToString(), iniName); - Environment.Exit(1); - } + success = true; + } + catch (Exception e) + { + m_log.FatalFormat("[CONFIG] Exception reading config from URI {0}\n" + e.ToString(), iniPath); + Environment.Exit(1); } } return success; @@ -195,7 +249,7 @@ namespace OpenSim /// Setup a default config values in case they aren't present in the ini file /// /// - public static IConfigSource DefaultConfig() + private static IConfigSource DefaultConfig() { IConfigSource defaultConfig = new IniConfigSource(); -- cgit v1.1