ConfigurationLoader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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, 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 =
  76. startupConfig.GetString("inimaster", "OpenSimDefaults.ini");
  77. if (masterFileName == "none")
  78. masterFileName = String.Empty;
  79. if (IsUri(masterFileName))
  80. {
  81. if (!sources.Contains(masterFileName))
  82. sources.Add(masterFileName);
  83. }
  84. else
  85. {
  86. string masterFilePath = Path.GetFullPath(
  87. Path.Combine(Util.configDir(), masterFileName));
  88. if (masterFileName != String.Empty)
  89. {
  90. if (File.Exists(masterFilePath))
  91. {
  92. if (!sources.Contains(masterFilePath))
  93. sources.Add(masterFilePath);
  94. }
  95. else
  96. {
  97. m_log.ErrorFormat("Master ini file {0} not found", masterFilePath);
  98. Environment.Exit(1);
  99. }
  100. }
  101. }
  102. string iniFileName =
  103. startupConfig.GetString("inifile", "OpenSim.ini");
  104. if (IsUri(iniFileName))
  105. {
  106. if (!sources.Contains(iniFileName))
  107. sources.Add(iniFileName);
  108. Application.iniFilePath = iniFileName;
  109. }
  110. else
  111. {
  112. Application.iniFilePath = Path.GetFullPath(
  113. Path.Combine(Util.configDir(), iniFileName));
  114. if (!File.Exists(Application.iniFilePath))
  115. {
  116. iniFileName = "OpenSim.xml";
  117. Application.iniFilePath = Path.GetFullPath(
  118. Path.Combine(Util.configDir(), iniFileName));
  119. }
  120. if (File.Exists(Application.iniFilePath))
  121. {
  122. if (!sources.Contains(Application.iniFilePath))
  123. sources.Add(Application.iniFilePath);
  124. }
  125. }
  126. string iniDirName =
  127. startupConfig.GetString("inidirectory", "config");
  128. string iniDirPath =
  129. Path.Combine(Util.configDir(), iniDirName);
  130. if (Directory.Exists(iniDirPath))
  131. {
  132. m_log.InfoFormat("Searching folder {0} for config ini files",
  133. iniDirPath);
  134. string[] fileEntries = Directory.GetFiles(iniDirName);
  135. foreach (string filePath in fileEntries)
  136. {
  137. if (Path.GetExtension(filePath).ToLower() == ".ini")
  138. {
  139. if (!sources.Contains(Path.GetFullPath(filePath)))
  140. sources.Add(Path.GetFullPath(filePath));
  141. }
  142. }
  143. }
  144. m_config = new OpenSimConfigSource();
  145. m_config.Source = new IniConfigSource();
  146. m_config.Source.Merge(DefaultConfig());
  147. m_log.Info("[CONFIG]: Reading configuration settings");
  148. if (sources.Count == 0)
  149. {
  150. m_log.FatalFormat("[CONFIG]: Could not load any configuration");
  151. m_log.FatalFormat("[CONFIG]: Did you copy the OpenSimDefaults.ini.example file to OpenSimDefaults.ini?");
  152. Environment.Exit(1);
  153. }
  154. for (int i = 0 ; i < sources.Count ; i++)
  155. {
  156. if (ReadConfig(sources[i]))
  157. {
  158. iniFileExists = true;
  159. AddIncludes(sources);
  160. }
  161. }
  162. if (!iniFileExists)
  163. {
  164. m_log.FatalFormat("[CONFIG]: Could not load any configuration");
  165. m_log.FatalFormat("[CONFIG]: Configuration exists, but there was an error loading it!");
  166. Environment.Exit(1);
  167. }
  168. // Make sure command line options take precedence
  169. m_config.Source.Merge(argvSource);
  170. ReadConfigSettings();
  171. return m_config;
  172. }
  173. /// <summary>
  174. /// Adds the included files as ini configuration files
  175. /// </summary>
  176. /// <param name="sources">List of URL strings or filename strings</param>
  177. private void AddIncludes(List<string> sources)
  178. {
  179. //loop over config sources
  180. foreach (IConfig config in m_config.Source.Configs)
  181. {
  182. // Look for Include-* in the key name
  183. string[] keys = config.GetKeys();
  184. foreach (string k in keys)
  185. {
  186. if (k.StartsWith("Include-"))
  187. {
  188. // read the config file to be included.
  189. string file = config.GetString(k);
  190. if (IsUri(file))
  191. {
  192. if (!sources.Contains(file))
  193. sources.Add(file);
  194. }
  195. else
  196. {
  197. string basepath = Path.GetFullPath(Util.configDir());
  198. // Resolve relative paths with wildcards
  199. string chunkWithoutWildcards = file;
  200. string chunkWithWildcards = string.Empty;
  201. int wildcardIndex = file.IndexOfAny(new char[] { '*', '?' });
  202. if (wildcardIndex != -1)
  203. {
  204. chunkWithoutWildcards = file.Substring(0, wildcardIndex);
  205. chunkWithWildcards = file.Substring(wildcardIndex);
  206. }
  207. string path = Path.Combine(basepath, chunkWithoutWildcards);
  208. path = Path.GetFullPath(path) + chunkWithWildcards;
  209. string[] paths = Util.Glob(path);
  210. // If the include path contains no wildcards, then warn the user that it wasn't found.
  211. if (wildcardIndex == -1 && paths.Length == 0)
  212. {
  213. m_log.WarnFormat("[CONFIG]: Could not find include file {0}", path);
  214. }
  215. else
  216. {
  217. foreach (string p in paths)
  218. {
  219. if (!sources.Contains(p))
  220. sources.Add(p);
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. /// <summary>
  229. /// Check if we can convert the string to a URI
  230. /// </summary>
  231. /// <param name="file">String uri to the remote resource</param>
  232. /// <returns>true if we can convert the string to a Uri object</returns>
  233. bool IsUri(string file)
  234. {
  235. Uri configUri;
  236. return Uri.TryCreate(file, UriKind.Absolute,
  237. out configUri) && configUri.Scheme == Uri.UriSchemeHttp;
  238. }
  239. /// <summary>
  240. /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http
  241. /// </summary>
  242. /// <param name="iniPath">Full path to the ini</param>
  243. /// <returns></returns>
  244. private bool ReadConfig(string iniPath)
  245. {
  246. bool success = false;
  247. if (!IsUri(iniPath))
  248. {
  249. m_log.InfoFormat("[CONFIG]: Reading configuration file {0}", Path.GetFullPath(iniPath));
  250. m_config.Source.Merge(new IniConfigSource(iniPath));
  251. success = true;
  252. }
  253. else
  254. {
  255. m_log.InfoFormat("[CONFIG]: {0} is a http:// URI, fetching ...", iniPath);
  256. // The ini file path is a http URI
  257. // Try to read it
  258. try
  259. {
  260. XmlReader r = XmlReader.Create(iniPath);
  261. XmlConfigSource cs = new XmlConfigSource(r);
  262. m_config.Source.Merge(cs);
  263. success = true;
  264. }
  265. catch (Exception e)
  266. {
  267. m_log.FatalFormat("[CONFIG]: Exception reading config from URI {0}\n" + e.ToString(), iniPath);
  268. Environment.Exit(1);
  269. }
  270. }
  271. return success;
  272. }
  273. /// <summary>
  274. /// Setup a default config values in case they aren't present in the ini file
  275. /// </summary>
  276. /// <returns>A Configuration source containing the default configuration</returns>
  277. private static IConfigSource DefaultConfig()
  278. {
  279. IConfigSource defaultConfig = new IniConfigSource();
  280. {
  281. IConfig config = defaultConfig.Configs["Startup"];
  282. if (null == config)
  283. config = defaultConfig.AddConfig("Startup");
  284. config.Set("region_info_source", "filesystem");
  285. config.Set("physics", "OpenDynamicsEngine");
  286. config.Set("meshing", "Meshmerizer");
  287. config.Set("physical_prim", true);
  288. config.Set("see_into_this_sim_from_neighbor", true);
  289. config.Set("serverside_object_permissions", false);
  290. config.Set("storage_plugin", "OpenSim.Data.SQLite.dll");
  291. config.Set("storage_connection_string", "URI=file:OpenSim.db,version=3");
  292. config.Set("storage_prim_inventories", true);
  293. config.Set("startup_console_commands_file", String.Empty);
  294. config.Set("shutdown_console_commands_file", String.Empty);
  295. config.Set("DefaultScriptEngine", "XEngine");
  296. config.Set("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  297. // life doesn't really work without this
  298. config.Set("EventQueue", true);
  299. }
  300. {
  301. IConfig config = defaultConfig.Configs["Network"];
  302. if (null == config)
  303. config = defaultConfig.AddConfig("Network");
  304. config.Set("http_listener_port", ConfigSettings.DefaultRegionHttpPort);
  305. }
  306. return defaultConfig;
  307. }
  308. /// <summary>
  309. /// Read initial region settings from the ConfigSource
  310. /// </summary>
  311. protected virtual void ReadConfigSettings()
  312. {
  313. IConfig startupConfig = m_config.Source.Configs["Startup"];
  314. if (startupConfig != null)
  315. {
  316. m_configSettings.PhysicsEngine = startupConfig.GetString("physics");
  317. m_configSettings.MeshEngineName = startupConfig.GetString("meshing");
  318. m_configSettings.PhysicalPrim = startupConfig.GetBoolean("physical_prim", true);
  319. m_configSettings.See_into_region_from_neighbor = startupConfig.GetBoolean("see_into_this_sim_from_neighbor", true);
  320. m_configSettings.StorageDll = startupConfig.GetString("storage_plugin");
  321. m_configSettings.ClientstackDll
  322. = startupConfig.GetString("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  323. }
  324. m_networkServersInfo.loadFromConfiguration(m_config.Source);
  325. }
  326. }
  327. }