1
0

ConfigurationLoader.cs 9.7 KB

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