ConfigurationLoader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Threading;
  32. using System.Xml;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenSim.Framework;
  36. namespace OpenSim
  37. {
  38. /// <summary>
  39. /// Loads the Configuration files into nIni
  40. /// </summary>
  41. public class ConfigurationLoader
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. /// <summary>
  45. /// Various Config settings the region needs to start
  46. /// Physics Engine, Mesh Engine, GridMode, PhysicsPrim allowed, Neighbor,
  47. /// StorageDLL, Storage Connection String, Estate connection String, Client Stack
  48. /// Standalone settings.
  49. /// </summary>
  50. protected ConfigSettings m_configSettings;
  51. /// <summary>
  52. /// A source of Configuration data
  53. /// </summary>
  54. protected OpenSimConfigSource m_config;
  55. /// <summary>
  56. /// Grid Service Information. This refers to classes and addresses of the grid service
  57. /// </summary>
  58. protected NetworkServersInfo m_networkServersInfo;
  59. /// <summary>
  60. /// Loads the region configuration
  61. /// </summary>
  62. /// <param name="argvSource">Parameters passed into the process when started</param>
  63. /// <param name="configSettings"></param>
  64. /// <param name="networkInfo"></param>
  65. /// <returns>A configuration that gets passed to modules</returns>
  66. public OpenSimConfigSource LoadConfigSettings(
  67. IConfigSource argvSource, EnvConfigSource envConfigSource, out ConfigSettings configSettings,
  68. out NetworkServersInfo networkInfo)
  69. {
  70. m_configSettings = configSettings = new ConfigSettings();
  71. m_networkServersInfo = networkInfo = new NetworkServersInfo();
  72. bool iniFileExists = false;
  73. IConfig startupConfig = argvSource.Configs["Startup"];
  74. List<string> sources = new List<string>();
  75. string masterFileName = startupConfig.GetString("inimaster", "OpenSimDefaults.ini");
  76. if (masterFileName == "none")
  77. masterFileName = String.Empty;
  78. if (IsUri(masterFileName))
  79. {
  80. if (!sources.Contains(masterFileName))
  81. sources.Add(masterFileName);
  82. }
  83. else
  84. {
  85. string masterFilePath = Path.GetFullPath(
  86. Path.Combine(Util.configDir(), masterFileName));
  87. if (masterFileName != String.Empty)
  88. {
  89. if (File.Exists(masterFilePath))
  90. {
  91. if (!sources.Contains(masterFilePath))
  92. sources.Add(masterFilePath);
  93. }
  94. else
  95. {
  96. m_log.ErrorFormat("Master ini file {0} not found", Path.GetFullPath(masterFilePath));
  97. Environment.Exit(1);
  98. }
  99. }
  100. }
  101. string iniFileName = startupConfig.GetString("inifile", "OpenSim.ini");
  102. if (IsUri(iniFileName))
  103. {
  104. if (!sources.Contains(iniFileName))
  105. sources.Add(iniFileName);
  106. Application.iniFilePath = iniFileName;
  107. }
  108. else
  109. {
  110. Application.iniFilePath = Path.GetFullPath(
  111. Path.Combine(Util.configDir(), iniFileName));
  112. if (!File.Exists(Application.iniFilePath))
  113. {
  114. iniFileName = "OpenSim.xml";
  115. Application.iniFilePath = Path.GetFullPath(Path.Combine(Util.configDir(), iniFileName));
  116. }
  117. if (File.Exists(Application.iniFilePath))
  118. {
  119. if (!sources.Contains(Application.iniFilePath))
  120. sources.Add(Application.iniFilePath);
  121. }
  122. }
  123. m_config = new OpenSimConfigSource();
  124. m_config.Source = new IniConfigSource();
  125. m_config.Source.Merge(DefaultConfig());
  126. m_log.Info("[CONFIG]: Reading configuration settings");
  127. for (int i = 0 ; i < sources.Count ; i++)
  128. {
  129. if (ReadConfig(m_config, sources[i]))
  130. {
  131. iniFileExists = true;
  132. AddIncludes(m_config, sources);
  133. }
  134. }
  135. // Override distro settings with contents of inidirectory
  136. string iniDirName = startupConfig.GetString("inidirectory", "config");
  137. string iniDirPath = Path.Combine(Util.configDir(), iniDirName);
  138. if (Directory.Exists(iniDirPath))
  139. {
  140. m_log.InfoFormat("[CONFIG]: Searching folder {0} for config ini files", iniDirPath);
  141. List<string> overrideSources = new List<string>();
  142. string[] fileEntries = Directory.GetFiles(iniDirName);
  143. foreach (string filePath in fileEntries)
  144. {
  145. if (Path.GetExtension(filePath).ToLower() == ".ini")
  146. {
  147. if (!sources.Contains(Path.GetFullPath(filePath)))
  148. {
  149. overrideSources.Add(Path.GetFullPath(filePath));
  150. // put it in sources too, to avoid circularity
  151. sources.Add(Path.GetFullPath(filePath));
  152. }
  153. }
  154. }
  155. if (overrideSources.Count > 0)
  156. {
  157. OpenSimConfigSource overrideConfig = new OpenSimConfigSource();
  158. overrideConfig.Source = new IniConfigSource();
  159. for (int i = 0 ; i < overrideSources.Count ; i++)
  160. {
  161. if (ReadConfig(overrideConfig, overrideSources[i]))
  162. {
  163. iniFileExists = true;
  164. AddIncludes(overrideConfig, overrideSources);
  165. }
  166. }
  167. m_config.Source.Merge(overrideConfig.Source);
  168. }
  169. }
  170. if (sources.Count == 0)
  171. {
  172. m_log.FatalFormat("[CONFIG]: Could not load any configuration");
  173. Environment.Exit(1);
  174. }
  175. else if (!iniFileExists)
  176. {
  177. m_log.FatalFormat("[CONFIG]: Could not load any configuration");
  178. m_log.FatalFormat("[CONFIG]: Configuration exists, but there was an error loading it!");
  179. Environment.Exit(1);
  180. }
  181. // Merge OpSys env vars
  182. m_log.Info("[CONFIG]: Loading environment variables for Config");
  183. Util.MergeEnvironmentToConfig(m_config.Source);
  184. // Make sure command line options take precedence
  185. m_config.Source.Merge(argvSource);
  186. m_config.Source.ReplaceKeyValues();
  187. ReadConfigSettings();
  188. return m_config;
  189. }
  190. /// <summary>
  191. /// Adds the included files as ini configuration files
  192. /// </summary>
  193. /// <param name="sources">List of URL strings or filename strings</param>
  194. private void AddIncludes(OpenSimConfigSource configSource, List<string> sources)
  195. {
  196. //loop over config sources
  197. foreach (IConfig config in configSource.Source.Configs)
  198. {
  199. // Look for Include-* in the key name
  200. string[] keys = config.GetKeys();
  201. foreach (string k in keys)
  202. {
  203. if (k.StartsWith("Include-"))
  204. {
  205. // read the config file to be included.
  206. string file = config.GetString(k);
  207. if (IsUri(file))
  208. {
  209. if (!sources.Contains(file))
  210. sources.Add(file);
  211. }
  212. else
  213. {
  214. string basepath = Path.GetFullPath(Util.configDir());
  215. // Resolve relative paths with wildcards
  216. string chunkWithoutWildcards = file;
  217. string chunkWithWildcards = string.Empty;
  218. int wildcardIndex = file.IndexOfAny(new char[] { '*', '?' });
  219. if (wildcardIndex != -1)
  220. {
  221. chunkWithoutWildcards = file.Substring(0, wildcardIndex);
  222. chunkWithWildcards = file.Substring(wildcardIndex);
  223. }
  224. string path = Path.Combine(basepath, chunkWithoutWildcards);
  225. path = Path.GetFullPath(path) + chunkWithWildcards;
  226. string[] paths = Util.Glob(path);
  227. // If the include path contains no wildcards, then warn the user that it wasn't found.
  228. if (wildcardIndex == -1 && paths.Length == 0)
  229. {
  230. m_log.WarnFormat("[CONFIG]: Could not find include file {0}", path);
  231. }
  232. else
  233. {
  234. foreach (string p in paths)
  235. {
  236. if (!sources.Contains(p))
  237. sources.Add(p);
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// Check if we can convert the string to a URI
  247. /// </summary>
  248. /// <param name="file">String uri to the remote resource</param>
  249. /// <returns>true if we can convert the string to a Uri object</returns>
  250. bool IsUri(string file)
  251. {
  252. Uri configUri;
  253. return Uri.TryCreate(file, UriKind.Absolute,
  254. out configUri) && configUri.Scheme == Uri.UriSchemeHttp;
  255. }
  256. /// <summary>
  257. /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http
  258. /// </summary>
  259. /// <param name="iniPath">Full path to the ini</param>
  260. /// <returns></returns>
  261. private bool ReadConfig(OpenSimConfigSource configSource, string iniPath)
  262. {
  263. bool success = false;
  264. if (!IsUri(iniPath))
  265. {
  266. m_log.InfoFormat("[CONFIG]: Reading configuration file {0}", Path.GetFullPath(iniPath));
  267. configSource.Source.Merge(new IniConfigSource(iniPath));
  268. success = true;
  269. }
  270. else
  271. {
  272. m_log.InfoFormat("[CONFIG]: {0} is a http:// URI, fetching ...", iniPath);
  273. // The ini file path is a http URI
  274. // Try to read it
  275. try
  276. {
  277. XmlReader r = XmlReader.Create(iniPath);
  278. XmlConfigSource cs = new XmlConfigSource(r);
  279. configSource.Source.Merge(cs);
  280. success = true;
  281. }
  282. catch (Exception e)
  283. {
  284. m_log.FatalFormat("[CONFIG]: Exception reading config from URI {0}\n" + e.ToString(), iniPath);
  285. Environment.Exit(1);
  286. }
  287. }
  288. return success;
  289. }
  290. /// <summary>
  291. /// Setup a default config values in case they aren't present in the ini file
  292. /// </summary>
  293. /// <returns>A Configuration source containing the default configuration</returns>
  294. private static IConfigSource DefaultConfig()
  295. {
  296. IConfigSource defaultConfig = new IniConfigSource();
  297. {
  298. IConfig config = defaultConfig.Configs["Startup"];
  299. if (null == config)
  300. config = defaultConfig.AddConfig("Startup");
  301. config.Set("region_info_source", "filesystem");
  302. config.Set("physics", "OpenDynamicsEngine");
  303. config.Set("meshing", "Meshmerizer");
  304. config.Set("physical_prim", true);
  305. config.Set("serverside_object_permissions", true);
  306. config.Set("storage_prim_inventories", true);
  307. config.Set("startup_console_commands_file", String.Empty);
  308. config.Set("shutdown_console_commands_file", String.Empty);
  309. config.Set("DefaultScriptEngine", "XEngine");
  310. config.Set("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  311. // life doesn't really work without this
  312. config.Set("EventQueue", true);
  313. }
  314. {
  315. IConfig config = defaultConfig.Configs["Network"];
  316. if (null == config)
  317. config = defaultConfig.AddConfig("Network");
  318. config.Set("http_listener_port", ConfigSettings.DefaultRegionHttpPort);
  319. }
  320. return defaultConfig;
  321. }
  322. /// <summary>
  323. /// Read initial region settings from the ConfigSource
  324. /// </summary>
  325. protected virtual void ReadConfigSettings()
  326. {
  327. IConfig startupConfig = m_config.Source.Configs["Startup"];
  328. if (startupConfig != null)
  329. {
  330. m_configSettings.PhysicsEngine = startupConfig.GetString("physics");
  331. m_configSettings.MeshEngineName = startupConfig.GetString("meshing");
  332. m_configSettings.ClientstackDll
  333. = startupConfig.GetString("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  334. }
  335. m_networkServersInfo.loadFromConfiguration(m_config.Source);
  336. }
  337. }
  338. }