1
0

ConfigurationLoader.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_log.Info("[CONFIG] Reading configuration settings");
  108. if (sources.Count == 0)
  109. {
  110. m_log.FatalFormat("[CONFIG] Could not load any configuration");
  111. m_log.FatalFormat("[CONFIG] Did you copy the OpenSim.ini.example file to OpenSim.ini?");
  112. Environment.Exit(1);
  113. }
  114. for (int i = 0 ; i < sources.Count ; i++)
  115. {
  116. if (ReadConfig(sources[i]))
  117. iniFileExists = true;
  118. AddIncludes(sources);
  119. }
  120. if (!iniFileExists)
  121. {
  122. m_log.FatalFormat("[CONFIG] Could not load any configuration");
  123. m_log.FatalFormat("[CONFIG] Configuration exists, but there was an error loading it!");
  124. Environment.Exit(1);
  125. }
  126. return m_config;
  127. }
  128. /// <summary>
  129. /// Adds the included files as ini configuration files
  130. /// </summary>
  131. /// <param name="sources">List of URL strings or filename strings</param>
  132. private void AddIncludes(List<string> sources)
  133. {
  134. //loop over config sources
  135. foreach (IConfig config in m_config.Configs)
  136. {
  137. // Look for Include-* in the key name
  138. string[] keys = config.GetKeys();
  139. foreach (string k in keys)
  140. {
  141. if (k.StartsWith("Include-"))
  142. {
  143. // read the config file to be included.
  144. string file = config.GetString(k);
  145. if (IsUri(file))
  146. {
  147. if (!sources.Contains(file))
  148. sources.Add(file);
  149. }
  150. else
  151. {
  152. string basepath = Path.GetFullPath(".");
  153. // Resolve relative paths with wildcards
  154. string chunkWithoutWildcards = file;
  155. string chunkWithWildcards = string.Empty;
  156. int wildcardIndex = file.IndexOfAny(new char[] { '*', '?' });
  157. if (wildcardIndex != -1)
  158. {
  159. chunkWithoutWildcards = file.Substring(0, wildcardIndex);
  160. chunkWithWildcards = file.Substring(wildcardIndex);
  161. }
  162. string path = Path.Combine(basepath, chunkWithoutWildcards);
  163. path = Path.GetFullPath(path) + chunkWithWildcards;
  164. string[] paths = Util.Glob(path);
  165. foreach (string p in paths)
  166. {
  167. if (!sources.Contains(p))
  168. sources.Add(p);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// Check if we can convert the string to a URI
  177. /// </summary>
  178. /// <param name="file">String uri to the remote resource</param>
  179. /// <returns>true if we can convert the string to a Uri object</returns>
  180. bool IsUri(string file)
  181. {
  182. Uri configUri;
  183. return Uri.TryCreate(file, UriKind.Absolute,
  184. out configUri) && configUri.Scheme == Uri.UriSchemeHttp;
  185. }
  186. /// <summary>
  187. /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http
  188. /// </summary>
  189. /// <param name="iniPath">Full path to the ini</param>
  190. /// <returns></returns>
  191. private bool ReadConfig(string iniPath)
  192. {
  193. bool success = false;
  194. if (!IsUri(iniPath))
  195. {
  196. m_log.InfoFormat("[CONFIG] Reading configuration file {0}",
  197. Path.GetFullPath(iniPath));
  198. m_config.Merge(new IniConfigSource(iniPath));
  199. success = true;
  200. }
  201. else
  202. {
  203. m_log.InfoFormat("[CONFIG] {0} is a http:// URI, fetching ...",
  204. iniPath);
  205. // The ini file path is a http URI
  206. // Try to read it
  207. //
  208. try
  209. {
  210. XmlReader r = XmlReader.Create(iniPath);
  211. XmlConfigSource cs = new XmlConfigSource(r);
  212. m_config.Merge(cs);
  213. success = true;
  214. }
  215. catch (Exception e)
  216. {
  217. m_log.FatalFormat("[CONFIG] Exception reading config from URI {0}\n" + e.ToString(), iniPath);
  218. Environment.Exit(1);
  219. }
  220. }
  221. return success;
  222. }
  223. }
  224. }