ConfigurationLoader.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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()
  63. {
  64. bool iniFileExists = false;
  65. List<string> sources = new List<string>();
  66. string iniFileName = "OpenSim.ini";
  67. string iniFilePath = Path.Combine(".", iniFileName);
  68. if (IsUri(iniFileName))
  69. {
  70. if (!sources.Contains(iniFileName))
  71. sources.Add(iniFileName);
  72. }
  73. else
  74. {
  75. if (File.Exists(iniFilePath))
  76. {
  77. if (!sources.Contains(iniFilePath))
  78. sources.Add(iniFilePath);
  79. }
  80. }
  81. m_config = new IniConfigSource();
  82. m_config.Merge(DefaultConfig());
  83. m_log.Info("[CONFIG] Reading configuration settings");
  84. if (sources.Count == 0)
  85. {
  86. m_log.FatalFormat("[CONFIG] Could not load any configuration");
  87. m_log.FatalFormat("[CONFIG] Did you copy the OpenSim.ini.example file to OpenSim.ini?");
  88. Environment.Exit(1);
  89. }
  90. for (int i = 0 ; i < sources.Count ; i++)
  91. {
  92. if (ReadConfig(sources[i]))
  93. iniFileExists = true;
  94. AddIncludes(sources);
  95. }
  96. if (!iniFileExists)
  97. {
  98. m_log.FatalFormat("[CONFIG] Could not load any configuration");
  99. m_log.FatalFormat("[CONFIG] Configuration exists, but there was an error loading it!");
  100. Environment.Exit(1);
  101. }
  102. return m_config;
  103. }
  104. /// <summary>
  105. /// Adds the included files as ini configuration files
  106. /// </summary>
  107. /// <param name="sources">List of URL strings or filename strings</param>
  108. private void AddIncludes(List<string> sources)
  109. {
  110. //loop over config sources
  111. foreach (IConfig config in m_config.Configs)
  112. {
  113. // Look for Include-* in the key name
  114. string[] keys = config.GetKeys();
  115. foreach (string k in keys)
  116. {
  117. if (k.StartsWith("Include-"))
  118. {
  119. // read the config file to be included.
  120. string file = config.GetString(k);
  121. if (IsUri(file))
  122. {
  123. if (!sources.Contains(file))
  124. sources.Add(file);
  125. }
  126. else
  127. {
  128. string basepath = Path.GetFullPath(".");
  129. // Resolve relative paths with wildcards
  130. string chunkWithoutWildcards = file;
  131. string chunkWithWildcards = string.Empty;
  132. int wildcardIndex = file.IndexOfAny(new char[] { '*', '?' });
  133. if (wildcardIndex != -1)
  134. {
  135. chunkWithoutWildcards = file.Substring(0, wildcardIndex);
  136. chunkWithWildcards = file.Substring(wildcardIndex);
  137. }
  138. string path = Path.Combine(basepath, chunkWithoutWildcards);
  139. path = Path.GetFullPath(path) + chunkWithWildcards;
  140. string[] paths = Util.Glob(path);
  141. foreach (string p in paths)
  142. {
  143. if (!sources.Contains(p))
  144. sources.Add(p);
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. /// <summary>
  152. /// Check if we can convert the string to a URI
  153. /// </summary>
  154. /// <param name="file">String uri to the remote resource</param>
  155. /// <returns>true if we can convert the string to a Uri object</returns>
  156. bool IsUri(string file)
  157. {
  158. Uri configUri;
  159. return Uri.TryCreate(file, UriKind.Absolute,
  160. out configUri) && configUri.Scheme == Uri.UriSchemeHttp;
  161. }
  162. /// <summary>
  163. /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http
  164. /// </summary>
  165. /// <param name="iniPath">Full path to the ini</param>
  166. /// <returns></returns>
  167. private bool ReadConfig(string iniPath)
  168. {
  169. bool success = false;
  170. if (!IsUri(iniPath))
  171. {
  172. m_log.InfoFormat("[CONFIG] Reading configuration file {0}",
  173. Path.GetFullPath(iniPath));
  174. m_config.Merge(new IniConfigSource(iniPath));
  175. success = true;
  176. }
  177. else
  178. {
  179. m_log.InfoFormat("[CONFIG] {0} is a http:// URI, fetching ...",
  180. iniPath);
  181. // The ini file path is a http URI
  182. // Try to read it
  183. //
  184. try
  185. {
  186. XmlReader r = XmlReader.Create(iniPath);
  187. XmlConfigSource cs = new XmlConfigSource(r);
  188. m_config.Merge(cs);
  189. success = true;
  190. }
  191. catch (Exception e)
  192. {
  193. m_log.FatalFormat("[CONFIG] Exception reading config from URI {0}\n" + e.ToString(), iniPath);
  194. Environment.Exit(1);
  195. }
  196. }
  197. return success;
  198. }
  199. /// <summary>
  200. /// Setup a default config values in case they aren't present in the ini file
  201. /// </summary>
  202. /// <returns>A Configuration source containing the default configuration</returns>
  203. private static IConfigSource DefaultConfig()
  204. {
  205. IConfigSource defaultConfig = new IniConfigSource();
  206. {
  207. IConfig config = defaultConfig.Configs["Startup"];
  208. if (null == config)
  209. config = defaultConfig.AddConfig("Startup");
  210. config.Set("region_info_source", "filesystem");
  211. config.Set("gridmode", false);
  212. config.Set("physics", "OpenDynamicsEngine");
  213. config.Set("meshing", "Meshmerizer");
  214. config.Set("physical_prim", true);
  215. config.Set("see_into_this_sim_from_neighbor", true);
  216. config.Set("serverside_object_permissions", false);
  217. config.Set("storage_plugin", "OpenSim.Data.SQLite.dll");
  218. config.Set("storage_connection_string", "URI=file:OpenSim.db,version=3");
  219. config.Set("storage_prim_inventories", true);
  220. config.Set("startup_console_commands_file", String.Empty);
  221. config.Set("shutdown_console_commands_file", String.Empty);
  222. config.Set("DefaultScriptEngine", "XEngine");
  223. config.Set("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
  224. // life doesn't really work without this
  225. config.Set("EventQueue", true);
  226. }
  227. return defaultConfig;
  228. }
  229. }
  230. }