ConfigurationLoader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. namespace OpenSim.Tools.Configger
  36. {
  37. /// <summary>
  38. /// Loads the Configuration files into nIni
  39. /// </summary>
  40. public class ConfigurationLoader
  41. {
  42. /// <summary>
  43. /// A source of Configuration data
  44. /// </summary>
  45. protected IConfigSource m_config;
  46. /// <summary>
  47. /// Console logger
  48. /// </summary>
  49. private static readonly ILog m_log =
  50. LogManager.GetLogger(
  51. MethodBase.GetCurrentMethod().DeclaringType);
  52. public ConfigurationLoader()
  53. {
  54. }
  55. /// <summary>
  56. /// Loads the region configuration
  57. /// </summary>
  58. /// <param name="argvSource">Parameters passed into the process when started</param>
  59. /// <param name="configSettings"></param>
  60. /// <param name="networkInfo"></param>
  61. /// <returns>A configuration that gets passed to modules</returns>
  62. public IConfigSource LoadConfigSettings(IConfig startupConfig)
  63. {
  64. bool iniFileExists = false;
  65. List<string> sources = new List<string>();
  66. string masterFileName = startupConfig.GetString("inimaster", "OpenSimDefaults.ini");
  67. if (masterFileName == "none")
  68. masterFileName = String.Empty;
  69. if (IsUri(masterFileName))
  70. {
  71. if (!sources.Contains(masterFileName))
  72. sources.Add(masterFileName);
  73. }
  74. else
  75. {
  76. string masterFilePath = Path.GetFullPath(
  77. Path.Combine(Util.configDir(), masterFileName));
  78. if (masterFileName != String.Empty)
  79. {
  80. if (File.Exists(masterFilePath))
  81. {
  82. if (!sources.Contains(masterFilePath))
  83. sources.Add(masterFilePath);
  84. }
  85. else
  86. {
  87. m_log.ErrorFormat("Master ini file {0} not found", Path.GetFullPath(masterFilePath));
  88. Environment.Exit(1);
  89. }
  90. }
  91. }
  92. string iniFileName = startupConfig.GetString("inifile", Path.Combine(".", "OpenSim.ini"));
  93. if (IsUri(iniFileName))
  94. {
  95. if (!sources.Contains(iniFileName))
  96. sources.Add(iniFileName);
  97. }
  98. else
  99. {
  100. if (File.Exists(iniFileName))
  101. {
  102. if (!sources.Contains(iniFileName))
  103. sources.Add(iniFileName);
  104. }
  105. }
  106. m_config = new IniConfigSource();
  107. m_config.Merge(DefaultConfig());
  108. m_log.Info("[CONFIG] Reading configuration settings");
  109. if (sources.Count == 0)
  110. {
  111. m_log.FatalFormat("[CONFIG] Could not load any configuration");
  112. m_log.FatalFormat("[CONFIG] Did you copy the OpenSim.ini.example file to OpenSim.ini?");
  113. Environment.Exit(1);
  114. }
  115. for (int i = 0 ; i < sources.Count ; i++)
  116. {
  117. if (ReadConfig(sources[i]))
  118. iniFileExists = true;
  119. AddIncludes(sources);
  120. }
  121. if (!iniFileExists)
  122. {
  123. m_log.FatalFormat("[CONFIG] Could not load any configuration");
  124. m_log.FatalFormat("[CONFIG] Configuration exists, but there was an error loading it!");
  125. Environment.Exit(1);
  126. }
  127. return m_config;
  128. }
  129. /// <summary>
  130. /// Adds the included files as ini configuration files
  131. /// </summary>
  132. /// <param name="sources">List of URL strings or filename strings</param>
  133. private void AddIncludes(List<string> sources)
  134. {
  135. //loop over config sources
  136. foreach (IConfig config in m_config.Configs)
  137. {
  138. // Look for Include-* in the key name
  139. string[] keys = config.GetKeys();
  140. foreach (string k in keys)
  141. {
  142. if (k.StartsWith("Include-"))
  143. {
  144. // read the config file to be included.
  145. string file = config.GetString(k);
  146. if (IsUri(file))
  147. {
  148. if (!sources.Contains(file))
  149. sources.Add(file);
  150. }
  151. else
  152. {
  153. string basepath = Path.GetFullPath(".");
  154. // Resolve relative paths with wildcards
  155. string chunkWithoutWildcards = file;
  156. string chunkWithWildcards = string.Empty;
  157. int wildcardIndex = file.IndexOfAny(new char[] { '*', '?' });
  158. if (wildcardIndex != -1)
  159. {
  160. chunkWithoutWildcards = file.Substring(0, wildcardIndex);
  161. chunkWithWildcards = file.Substring(wildcardIndex);
  162. }
  163. string path = Path.Combine(basepath, chunkWithoutWildcards);
  164. path = Path.GetFullPath(path) + chunkWithWildcards;
  165. string[] paths = Util.Glob(path);
  166. foreach (string p in paths)
  167. {
  168. if (!sources.Contains(p))
  169. sources.Add(p);
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. /// <summary>
  177. /// Check if we can convert the string to a URI
  178. /// </summary>
  179. /// <param name="file">String uri to the remote resource</param>
  180. /// <returns>true if we can convert the string to a Uri object</returns>
  181. bool IsUri(string file)
  182. {
  183. Uri configUri;
  184. return Uri.TryCreate(file, UriKind.Absolute,
  185. out configUri) && configUri.Scheme == Uri.UriSchemeHttp;
  186. }
  187. /// <summary>
  188. /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http
  189. /// </summary>
  190. /// <param name="iniPath">Full path to the ini</param>
  191. /// <returns></returns>
  192. private bool ReadConfig(string iniPath)
  193. {
  194. bool success = false;
  195. if (!IsUri(iniPath))
  196. {
  197. m_log.InfoFormat("[CONFIG] Reading configuration file {0}",
  198. Path.GetFullPath(iniPath));
  199. m_config.Merge(new IniConfigSource(iniPath));
  200. success = true;
  201. }
  202. else
  203. {
  204. m_log.InfoFormat("[CONFIG] {0} is a http:// URI, fetching ...",
  205. iniPath);
  206. // The ini file path is a http URI
  207. // Try to read it
  208. //
  209. try
  210. {
  211. XmlReader r = XmlReader.Create(iniPath);
  212. XmlConfigSource cs = new XmlConfigSource(r);
  213. m_config.Merge(cs);
  214. success = true;
  215. }
  216. catch (Exception e)
  217. {
  218. m_log.FatalFormat("[CONFIG] Exception reading config from URI {0}\n" + e.ToString(), iniPath);
  219. Environment.Exit(1);
  220. }
  221. }
  222. return success;
  223. }
  224. /// <summary>
  225. /// Setup a default config values in case they aren't present in the ini file
  226. /// </summary>
  227. /// <returns>A Configuration source containing the default configuration</returns>
  228. private static IConfigSource DefaultConfig()
  229. {
  230. IConfigSource defaultConfig = new IniConfigSource();
  231. {
  232. IConfig config = defaultConfig.Configs["Startup"];
  233. if (null == config)
  234. config = defaultConfig.AddConfig("Startup");
  235. config.Set("region_info_source", "filesystem");
  236. config.Set("allow_regionless", false);
  237. config.Set("physics", "OpenDynamicsEngine");
  238. config.Set("meshing", "Meshmerizer");
  239. config.Set("physical_prim", true);
  240. config.Set("serverside_object_permissions", true);
  241. config.Set("startup_console_commands_file", String.Empty);
  242. config.Set("shutdown_console_commands_file", String.Empty);
  243. config.Set("DefaultScriptEngine", "XEngine");
  244. config.Set("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  245. }
  246. return defaultConfig;
  247. }
  248. }
  249. }