ConfigurationMember.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 OpenSim 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.Globalization;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Xml;
  33. using libsecondlife;
  34. using OpenSim.Framework.Console;
  35. namespace OpenSim.Framework
  36. {
  37. public class ConfigurationMember
  38. {
  39. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  40. public delegate bool ConfigurationOptionResult(string configuration_key, object configuration_result);
  41. public delegate void ConfigurationOptionsLoad();
  42. private List<ConfigurationOption> configurationOptions = new List<ConfigurationOption>();
  43. private string configurationFilename = String.Empty;
  44. private string configurationDescription = String.Empty;
  45. private XmlNode configurationFromXMLNode = null;
  46. private ConfigurationOptionsLoad loadFunction;
  47. private ConfigurationOptionResult resultFunction;
  48. private IGenericConfig configurationPlugin = null;
  49. private bool useConsoleToPromptOnError = true;
  50. /// <summary>
  51. /// This is the default configuration DLL loaded
  52. /// </summary>
  53. private string configurationPluginFilename = "OpenSim.Framework.Configuration.XML.dll";
  54. public ConfigurationMember(string configuration_filename, string configuration_description,
  55. ConfigurationOptionsLoad load_function, ConfigurationOptionResult result_function, bool use_console_to_prompt_on_error)
  56. {
  57. configurationFilename = configuration_filename;
  58. configurationDescription = configuration_description;
  59. loadFunction = load_function;
  60. resultFunction = result_function;
  61. useConsoleToPromptOnError = use_console_to_prompt_on_error;
  62. }
  63. public ConfigurationMember(XmlNode configuration_xml, string configuration_description,
  64. ConfigurationOptionsLoad load_function, ConfigurationOptionResult result_function, bool use_console_to_prompt_on_error)
  65. {
  66. configurationFilename = String.Empty;
  67. configurationFromXMLNode = configuration_xml;
  68. configurationDescription = configuration_description;
  69. loadFunction = load_function;
  70. resultFunction = result_function;
  71. useConsoleToPromptOnError = use_console_to_prompt_on_error;
  72. }
  73. public void setConfigurationFilename(string filename)
  74. {
  75. configurationFilename = filename;
  76. }
  77. public void setConfigurationDescription(string desc)
  78. {
  79. configurationDescription = desc;
  80. }
  81. public void setConfigurationResultFunction(ConfigurationOptionResult result)
  82. {
  83. resultFunction = result;
  84. }
  85. public void forceConfigurationPluginLibrary(string dll_filename)
  86. {
  87. configurationPluginFilename = dll_filename;
  88. }
  89. private void checkAndAddConfigOption(ConfigurationOption option)
  90. {
  91. if ((option.configurationKey != String.Empty && option.configurationQuestion != String.Empty) ||
  92. (option.configurationKey != String.Empty && option.configurationUseDefaultNoPrompt))
  93. {
  94. if (!configurationOptions.Contains(option))
  95. {
  96. configurationOptions.Add(option);
  97. }
  98. }
  99. else
  100. {
  101. m_log.Info(
  102. "Required fields for adding a configuration option is invalid. Will not add this option (" +
  103. option.configurationKey + ")");
  104. }
  105. }
  106. public void addConfigurationOption(string configuration_key,
  107. ConfigurationOption.ConfigurationTypes configuration_type,
  108. string configuration_question, string configuration_default,
  109. bool use_default_no_prompt)
  110. {
  111. ConfigurationOption configOption = new ConfigurationOption();
  112. configOption.configurationKey = configuration_key;
  113. configOption.configurationQuestion = configuration_question;
  114. configOption.configurationDefault = configuration_default;
  115. configOption.configurationType = configuration_type;
  116. configOption.configurationUseDefaultNoPrompt = use_default_no_prompt;
  117. configOption.shouldIBeAsked = null; //Assumes true, I can ask whenever
  118. checkAndAddConfigOption(configOption);
  119. }
  120. public void addConfigurationOption(string configuration_key,
  121. ConfigurationOption.ConfigurationTypes configuration_type,
  122. string configuration_question, string configuration_default,
  123. bool use_default_no_prompt,
  124. ConfigurationOption.ConfigurationOptionShouldBeAsked shouldIBeAskedDelegate)
  125. {
  126. ConfigurationOption configOption = new ConfigurationOption();
  127. configOption.configurationKey = configuration_key;
  128. configOption.configurationQuestion = configuration_question;
  129. configOption.configurationDefault = configuration_default;
  130. configOption.configurationType = configuration_type;
  131. configOption.configurationUseDefaultNoPrompt = use_default_no_prompt;
  132. configOption.shouldIBeAsked = shouldIBeAskedDelegate;
  133. checkAndAddConfigOption(configOption);
  134. }
  135. // TEMP - REMOVE
  136. private int cE = 0;
  137. public void performConfigurationRetrieve()
  138. {
  139. if (cE > 1)
  140. m_log.Error("READING CONFIGURATION COUT: " + cE.ToString());
  141. configurationPlugin = LoadConfigDll(configurationPluginFilename);
  142. configurationOptions.Clear();
  143. if (loadFunction == null)
  144. {
  145. m_log.Error("Load Function for '" + configurationDescription +
  146. "' is null. Refusing to run configuration.");
  147. return;
  148. }
  149. if (resultFunction == null)
  150. {
  151. m_log.Error("Result Function for '" + configurationDescription +
  152. "' is null. Refusing to run configuration.");
  153. return;
  154. }
  155. m_log.Info("[CONFIG]: Calling Configuration Load Function...");
  156. loadFunction();
  157. if (configurationOptions.Count <= 0)
  158. {
  159. m_log.Error("[CONFIG]: No configuration options were specified for '" + configurationOptions +
  160. "'. Refusing to continue configuration.");
  161. return;
  162. }
  163. bool useFile = true;
  164. if (configurationPlugin == null)
  165. {
  166. m_log.Error("[CONFIG]: Configuration Plugin NOT LOADED!");
  167. return;
  168. }
  169. if (configurationFilename.Trim() != String.Empty)
  170. {
  171. configurationPlugin.SetFileName(configurationFilename);
  172. try
  173. {
  174. configurationPlugin.LoadData();
  175. useFile = true;
  176. }
  177. catch (XmlException e)
  178. {
  179. m_log.Error("Error loading " + configurationFilename + ": " + e.ToString());
  180. useFile = false;
  181. }
  182. }
  183. else
  184. {
  185. if (configurationFromXMLNode != null)
  186. {
  187. m_log.Info("Loading from XML Node, will not save to the file");
  188. configurationPlugin.LoadDataFromString(configurationFromXMLNode.OuterXml);
  189. }
  190. m_log.Info("XML Configuration Filename is not valid; will not save to the file.");
  191. useFile = false;
  192. }
  193. foreach (ConfigurationOption configOption in configurationOptions)
  194. {
  195. bool convertSuccess = false;
  196. object return_result = null;
  197. string errorMessage = String.Empty;
  198. bool ignoreNextFromConfig = false;
  199. while (convertSuccess == false)
  200. {
  201. string console_result = String.Empty;
  202. string attribute = null;
  203. if (useFile || configurationFromXMLNode != null)
  204. {
  205. if (!ignoreNextFromConfig)
  206. {
  207. attribute = configurationPlugin.GetAttribute(configOption.configurationKey);
  208. }
  209. else
  210. {
  211. ignoreNextFromConfig = false;
  212. }
  213. }
  214. if (attribute == null)
  215. {
  216. if (configOption.configurationUseDefaultNoPrompt || useConsoleToPromptOnError == false)
  217. {
  218. console_result = configOption.configurationDefault;
  219. }
  220. else
  221. {
  222. if ((configOption.shouldIBeAsked != null &&
  223. configOption.shouldIBeAsked(configOption.configurationKey)) ||
  224. configOption.shouldIBeAsked == null)
  225. {
  226. if (configurationDescription.Trim() != String.Empty)
  227. {
  228. console_result =
  229. MainConsole.Instance.CmdPrompt(
  230. configurationDescription + ": " + configOption.configurationQuestion,
  231. configOption.configurationDefault);
  232. }
  233. else
  234. {
  235. console_result =
  236. MainConsole.Instance.CmdPrompt(configOption.configurationQuestion,
  237. configOption.configurationDefault);
  238. }
  239. }
  240. else
  241. {
  242. //Dont Ask! Just use default
  243. console_result = configOption.configurationDefault;
  244. }
  245. }
  246. }
  247. else
  248. {
  249. console_result = attribute;
  250. }
  251. switch (configOption.configurationType)
  252. {
  253. case ConfigurationOption.ConfigurationTypes.TYPE_STRING:
  254. return_result = console_result;
  255. convertSuccess = true;
  256. break;
  257. case ConfigurationOption.ConfigurationTypes.TYPE_STRING_NOT_EMPTY:
  258. if (console_result.Length > 0)
  259. {
  260. return_result = console_result;
  261. convertSuccess = true;
  262. }
  263. errorMessage = "a string that is not empty";
  264. break;
  265. case ConfigurationOption.ConfigurationTypes.TYPE_BOOLEAN:
  266. bool boolResult;
  267. if (Boolean.TryParse(console_result, out boolResult))
  268. {
  269. convertSuccess = true;
  270. return_result = boolResult;
  271. }
  272. errorMessage = "'true' or 'false' (Boolean)";
  273. break;
  274. case ConfigurationOption.ConfigurationTypes.TYPE_BYTE:
  275. byte byteResult;
  276. if (Byte.TryParse(console_result, out byteResult))
  277. {
  278. convertSuccess = true;
  279. return_result = byteResult;
  280. }
  281. errorMessage = "a byte (Byte)";
  282. break;
  283. case ConfigurationOption.ConfigurationTypes.TYPE_CHARACTER:
  284. char charResult;
  285. if (Char.TryParse(console_result, out charResult))
  286. {
  287. convertSuccess = true;
  288. return_result = charResult;
  289. }
  290. errorMessage = "a character (Char)";
  291. break;
  292. case ConfigurationOption.ConfigurationTypes.TYPE_INT16:
  293. short shortResult;
  294. if (Int16.TryParse(console_result, out shortResult))
  295. {
  296. convertSuccess = true;
  297. return_result = shortResult;
  298. }
  299. errorMessage = "a signed 32 bit integer (short)";
  300. break;
  301. case ConfigurationOption.ConfigurationTypes.TYPE_INT32:
  302. int intResult;
  303. if (Int32.TryParse(console_result, out intResult))
  304. {
  305. convertSuccess = true;
  306. return_result = intResult;
  307. }
  308. errorMessage = "a signed 32 bit integer (int)";
  309. break;
  310. case ConfigurationOption.ConfigurationTypes.TYPE_INT64:
  311. long longResult;
  312. if (Int64.TryParse(console_result, out longResult))
  313. {
  314. convertSuccess = true;
  315. return_result = longResult;
  316. }
  317. errorMessage = "a signed 32 bit integer (long)";
  318. break;
  319. case ConfigurationOption.ConfigurationTypes.TYPE_IP_ADDRESS:
  320. IPAddress ipAddressResult;
  321. if (IPAddress.TryParse(console_result, out ipAddressResult))
  322. {
  323. convertSuccess = true;
  324. return_result = ipAddressResult;
  325. }
  326. errorMessage = "an IP Address (IPAddress)";
  327. break;
  328. case ConfigurationOption.ConfigurationTypes.TYPE_LLUUID:
  329. LLUUID uuidResult;
  330. if (LLUUID.TryParse(console_result, out uuidResult))
  331. {
  332. convertSuccess = true;
  333. return_result = uuidResult;
  334. }
  335. errorMessage = "a UUID (LLUUID)";
  336. break;
  337. case ConfigurationOption.ConfigurationTypes.TYPE_LLVECTOR3:
  338. LLVector3 vectorResult;
  339. if (LLVector3.TryParse(console_result, out vectorResult))
  340. {
  341. convertSuccess = true;
  342. return_result = vectorResult;
  343. }
  344. errorMessage = "a vector (LLVector3)";
  345. break;
  346. case ConfigurationOption.ConfigurationTypes.TYPE_UINT16:
  347. ushort ushortResult;
  348. if (UInt16.TryParse(console_result, out ushortResult))
  349. {
  350. convertSuccess = true;
  351. return_result = ushortResult;
  352. }
  353. errorMessage = "an unsigned 16 bit integer (ushort)";
  354. break;
  355. case ConfigurationOption.ConfigurationTypes.TYPE_UINT32:
  356. uint uintResult;
  357. if (UInt32.TryParse(console_result, out uintResult))
  358. {
  359. convertSuccess = true;
  360. return_result = uintResult;
  361. }
  362. errorMessage = "an unsigned 32 bit integer (uint)";
  363. break;
  364. case ConfigurationOption.ConfigurationTypes.TYPE_UINT64:
  365. ulong ulongResult;
  366. if (UInt64.TryParse(console_result, out ulongResult))
  367. {
  368. convertSuccess = true;
  369. return_result = ulongResult;
  370. }
  371. errorMessage = "an unsigned 64 bit integer (ulong)";
  372. break;
  373. case ConfigurationOption.ConfigurationTypes.TYPE_FLOAT:
  374. float floatResult;
  375. if (
  376. float.TryParse(console_result, NumberStyles.AllowDecimalPoint, Culture.NumberFormatInfo,
  377. out floatResult))
  378. {
  379. convertSuccess = true;
  380. return_result = floatResult;
  381. }
  382. errorMessage = "a single-precision floating point number (float)";
  383. break;
  384. case ConfigurationOption.ConfigurationTypes.TYPE_DOUBLE:
  385. double doubleResult;
  386. if (
  387. Double.TryParse(console_result, NumberStyles.AllowDecimalPoint, Culture.NumberFormatInfo,
  388. out doubleResult))
  389. {
  390. convertSuccess = true;
  391. return_result = doubleResult;
  392. }
  393. errorMessage = "an double-precision floating point number (double)";
  394. break;
  395. }
  396. if (convertSuccess)
  397. {
  398. if (useFile)
  399. {
  400. configurationPlugin.SetAttribute(configOption.configurationKey, console_result);
  401. }
  402. if (!resultFunction(configOption.configurationKey, return_result))
  403. {
  404. m_log.Info(
  405. "The handler for the last configuration option denied that input, please try again.");
  406. convertSuccess = false;
  407. ignoreNextFromConfig = true;
  408. }
  409. }
  410. else
  411. {
  412. if (configOption.configurationUseDefaultNoPrompt)
  413. {
  414. m_log.Error(string.Format(
  415. "[CONFIG]: [{3}]:[{1}] is not valid default for parameter [{0}].\nThe configuration result must be parsable to {2}.\n",
  416. configOption.configurationKey, console_result, errorMessage,
  417. configurationFilename));
  418. convertSuccess = true;
  419. }
  420. else
  421. {
  422. m_log.Warn(string.Format(
  423. "[CONFIG]: [{3}]:[{1}] is not a valid value [{0}].\nThe configuration result must be parsable to {2}.\n",
  424. configOption.configurationKey, console_result, errorMessage,
  425. configurationFilename));
  426. ignoreNextFromConfig = true;
  427. }
  428. }
  429. }
  430. }
  431. if (useFile)
  432. {
  433. configurationPlugin.Commit();
  434. configurationPlugin.Close();
  435. }
  436. }
  437. private IGenericConfig LoadConfigDll(string dllName)
  438. {
  439. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  440. IGenericConfig plug = null;
  441. foreach (Type pluginType in pluginAssembly.GetTypes())
  442. {
  443. if (pluginType.IsPublic)
  444. {
  445. if (!pluginType.IsAbstract)
  446. {
  447. Type typeInterface = pluginType.GetInterface("IGenericConfig", true);
  448. if (typeInterface != null)
  449. {
  450. plug =
  451. (IGenericConfig) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  452. }
  453. }
  454. }
  455. }
  456. pluginAssembly = null;
  457. return plug;
  458. }
  459. public void forceSetConfigurationOption(string configuration_key, string configuration_value)
  460. {
  461. configurationPlugin.LoadData();
  462. configurationPlugin.SetAttribute(configuration_key, configuration_value);
  463. configurationPlugin.Commit();
  464. configurationPlugin.Close();
  465. }
  466. }
  467. }