ServicesServerBase.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.Text;
  33. using System.Xml;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Monitoring;
  37. using OpenSim.Framework.Servers;
  38. using log4net;
  39. using log4net.Config;
  40. using log4net.Appender;
  41. using log4net.Core;
  42. using log4net.Repository;
  43. using Nini.Config;
  44. namespace OpenSim.Server.Base
  45. {
  46. public class ServicesServerBase : ServerBase
  47. {
  48. // Logger
  49. //
  50. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. // Command line args
  52. //
  53. protected string[] m_Arguments;
  54. public string ConfigDirectory
  55. {
  56. get;
  57. private set;
  58. }
  59. // Run flag
  60. //
  61. private bool m_Running = true;
  62. // Handle all the automagical stuff
  63. //
  64. public ServicesServerBase(string prompt, string[] args) : base()
  65. {
  66. // Save raw arguments
  67. m_Arguments = args;
  68. // Read command line
  69. ArgvConfigSource argvConfig = new ArgvConfigSource(args);
  70. argvConfig.AddSwitch("Startup", "console", "c");
  71. argvConfig.AddSwitch("Startup", "logfile", "l");
  72. argvConfig.AddSwitch("Startup", "inifile", "i");
  73. argvConfig.AddSwitch("Startup", "prompt", "p");
  74. argvConfig.AddSwitch("Startup", "logconfig", "g");
  75. // Automagically create the ini file name
  76. string fileName = "";
  77. if (Assembly.GetEntryAssembly() != null)
  78. fileName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
  79. string iniFile = fileName + ".ini";
  80. string logConfig = null;
  81. IConfig startupConfig = argvConfig.Configs["Startup"];
  82. if (startupConfig != null)
  83. {
  84. // Check if a file name was given on the command line
  85. iniFile = startupConfig.GetString("inifile", iniFile);
  86. // Check if a prompt was given on the command line
  87. prompt = startupConfig.GetString("prompt", prompt);
  88. // Check for a Log4Net config file on the command line
  89. logConfig =startupConfig.GetString("logconfig", logConfig);
  90. }
  91. // Find out of the file name is a URI and remote load it if possible.
  92. // Load it as a local file otherwise.
  93. Uri configUri;
  94. try
  95. {
  96. if (Uri.TryCreate(iniFile, UriKind.Absolute, out configUri) &&
  97. configUri.Scheme == Uri.UriSchemeHttp)
  98. {
  99. XmlReader r = XmlReader.Create(iniFile);
  100. Config = new XmlConfigSource(r);
  101. }
  102. else
  103. {
  104. Config = new IniConfigSource(iniFile);
  105. }
  106. }
  107. catch (Exception e)
  108. {
  109. System.Console.WriteLine("Error reading from config source. {0}", e.Message);
  110. Environment.Exit(1);
  111. }
  112. // Merge OpSys env vars
  113. m_log.Info("[CONFIG]: Loading environment variables for Config");
  114. Util.MergeEnvironmentToConfig(Config);
  115. // Merge the configuration from the command line into the loaded file
  116. Config.Merge(argvConfig);
  117. Config.ReplaceKeyValues();
  118. // Refresh the startupConfig post merge
  119. if (Config.Configs["Startup"] != null)
  120. {
  121. startupConfig = Config.Configs["Startup"];
  122. }
  123. ConfigDirectory = startupConfig.GetString("ConfigDirectory", ".");
  124. prompt = startupConfig.GetString("Prompt", prompt);
  125. // Allow derived classes to load config before the console is opened.
  126. ReadConfig();
  127. // Create main console
  128. string consoleType = "local";
  129. if (startupConfig != null)
  130. consoleType = startupConfig.GetString("console", consoleType);
  131. if (consoleType == "basic")
  132. {
  133. MainConsole.Instance = new CommandConsole(prompt);
  134. }
  135. else if (consoleType == "rest")
  136. {
  137. MainConsole.Instance = new RemoteConsole(prompt);
  138. ((RemoteConsole)MainConsole.Instance).ReadConfig(Config);
  139. }
  140. else if (consoleType == "mock")
  141. {
  142. MainConsole.Instance = new MockConsole();
  143. }
  144. else if (consoleType == "local")
  145. {
  146. MainConsole.Instance = new LocalConsole(prompt, startupConfig);
  147. }
  148. m_console = MainConsole.Instance;
  149. if (logConfig != null)
  150. {
  151. FileInfo cfg = new FileInfo(logConfig);
  152. XmlConfigurator.Configure(cfg);
  153. }
  154. else
  155. {
  156. XmlConfigurator.Configure();
  157. }
  158. LogEnvironmentInformation();
  159. RegisterCommonAppenders(startupConfig);
  160. if (startupConfig.GetString("PIDFile", String.Empty) != String.Empty)
  161. {
  162. CreatePIDFile(startupConfig.GetString("PIDFile"));
  163. }
  164. RegisterCommonCommands();
  165. RegisterCommonComponents(Config);
  166. // Allow derived classes to perform initialization that
  167. // needs to be done after the console has opened
  168. Initialise();
  169. }
  170. public bool Running
  171. {
  172. get { return m_Running; }
  173. }
  174. public virtual int Run()
  175. {
  176. Watchdog.Enabled = true;
  177. MemoryWatchdog.Enabled = true;
  178. while (m_Running)
  179. {
  180. try
  181. {
  182. MainConsole.Instance.Prompt();
  183. }
  184. catch (Exception e)
  185. {
  186. m_log.ErrorFormat("Command error: {0}", e);
  187. }
  188. }
  189. RemovePIDFile();
  190. return 0;
  191. }
  192. protected override void ShutdownSpecific()
  193. {
  194. m_Running = false;
  195. m_log.Info("[CONSOLE] Quitting");
  196. base.ShutdownSpecific();
  197. }
  198. protected virtual void ReadConfig()
  199. {
  200. }
  201. protected virtual void Initialise()
  202. {
  203. }
  204. }
  205. }