ServicesServerBase.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.IO;
  29. using System.Xml;
  30. using System.Threading;
  31. using System.Reflection;
  32. using OpenSim.Framework.Console;
  33. using log4net;
  34. using log4net.Config;
  35. using log4net.Appender;
  36. using log4net.Core;
  37. using log4net.Repository;
  38. using Nini.Config;
  39. namespace OpenSim.Server.Base
  40. {
  41. public class ServicesServerBase
  42. {
  43. // Logger
  44. //
  45. private static readonly ILog m_log =
  46. LogManager.GetLogger(
  47. MethodBase.GetCurrentMethod().DeclaringType);
  48. // Command line args
  49. //
  50. protected string[] m_Arguments;
  51. // Configuration
  52. //
  53. protected IConfigSource m_Config = null;
  54. public IConfigSource Config
  55. {
  56. get { return m_Config; }
  57. }
  58. // Run flag
  59. //
  60. private bool m_Running = true;
  61. // Handle all the automagical stuff
  62. //
  63. public ServicesServerBase(string prompt, string[] args)
  64. {
  65. // Save raw arguments
  66. //
  67. m_Arguments = args;
  68. // Read command line
  69. //
  70. ArgvConfigSource argvConfig = new ArgvConfigSource(args);
  71. argvConfig.AddSwitch("Startup", "console", "c");
  72. argvConfig.AddSwitch("Startup", "logfile", "l");
  73. argvConfig.AddSwitch("Startup", "inifile", "i");
  74. argvConfig.AddSwitch("Startup", "prompt", "p");
  75. // Automagically create the ini file name
  76. //
  77. string fileName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
  78. string iniFile = fileName + ".ini";
  79. IConfig startupConfig = argvConfig.Configs["Startup"];
  80. if (startupConfig != null)
  81. {
  82. // Check if a file name was given on the command line
  83. //
  84. iniFile = startupConfig.GetString("inifile", iniFile);
  85. //
  86. // Check if a prompt was given on the command line
  87. prompt = startupConfig.GetString("prompt", prompt);
  88. }
  89. // Find out of the file name is a URI and remote load it
  90. // if it's possible. Load it as a local file otherwise.
  91. //
  92. Uri configUri;
  93. try
  94. {
  95. if (Uri.TryCreate(iniFile, UriKind.Absolute, out configUri) &&
  96. configUri.Scheme == Uri.UriSchemeHttp)
  97. {
  98. XmlReader r = XmlReader.Create(iniFile);
  99. m_Config = new XmlConfigSource(r);
  100. }
  101. else
  102. {
  103. m_Config = new IniConfigSource(iniFile);
  104. }
  105. }
  106. catch (Exception)
  107. {
  108. System.Console.WriteLine("Error reading from config source {0}",
  109. iniFile);
  110. Thread.CurrentThread.Abort();
  111. }
  112. // Merge the configuration from the command line into the
  113. // loaded file
  114. //
  115. m_Config.Merge(argvConfig);
  116. // Refresh the startupConfig post merge
  117. //
  118. if (m_Config.Configs["Startup"] != null)
  119. {
  120. startupConfig = m_Config.Configs["Startup"];
  121. }
  122. prompt = startupConfig.GetString("Prompt", prompt);
  123. // Allow derived classes to load config before the console is
  124. // opened.
  125. //
  126. ReadConfig();
  127. // Create main console
  128. //
  129. string consoleType = "local";
  130. if (startupConfig != null)
  131. consoleType = startupConfig.GetString("console", consoleType);
  132. if (consoleType == "basic")
  133. {
  134. MainConsole.Instance = new CommandConsole(prompt);
  135. }
  136. else if (consoleType == "rest")
  137. {
  138. MainConsole.Instance = new RemoteConsole(prompt);
  139. ((RemoteConsole)MainConsole.Instance).ReadConfig(Config);
  140. }
  141. else
  142. {
  143. MainConsole.Instance = new LocalConsole(prompt);
  144. }
  145. // Configure the appenders for log4net
  146. //
  147. OpenSimAppender consoleAppender = null;
  148. FileAppender fileAppender = null;
  149. XmlConfigurator.Configure();
  150. ILoggerRepository repository = LogManager.GetRepository();
  151. IAppender[] appenders = repository.GetAppenders();
  152. foreach (IAppender appender in appenders)
  153. {
  154. if (appender.Name == "Console")
  155. {
  156. consoleAppender = (OpenSimAppender)appender;
  157. }
  158. if (appender.Name == "LogFileAppender")
  159. {
  160. fileAppender = (FileAppender)appender;
  161. }
  162. }
  163. if (consoleAppender == null)
  164. {
  165. System.Console.WriteLine("No console appender found. Server can't start");
  166. Thread.CurrentThread.Abort();
  167. }
  168. else
  169. {
  170. consoleAppender.Console = MainConsole.Instance;
  171. if (null == consoleAppender.Threshold)
  172. consoleAppender.Threshold = Level.All;
  173. }
  174. // Set log file
  175. //
  176. if (fileAppender != null)
  177. {
  178. if (startupConfig != null)
  179. {
  180. fileName = startupConfig.GetString("logfile", fileName+".log");
  181. fileName = Path.GetFullPath(Path.Combine(".", fileName));
  182. fileAppender.File = fileName;
  183. fileAppender.ActivateOptions();
  184. }
  185. }
  186. // Register the quit command
  187. //
  188. MainConsole.Instance.Commands.AddCommand("base", false, "quit",
  189. "quit",
  190. "Quit the application", HandleQuit);
  191. // Allow derived classes to perform initialization that
  192. // needs to be done after the console has opened
  193. //
  194. Initialise();
  195. }
  196. public virtual int Run()
  197. {
  198. while (m_Running)
  199. {
  200. MainConsole.Instance.Prompt();
  201. }
  202. return 0;
  203. }
  204. protected virtual void HandleQuit(string module, string[] args)
  205. {
  206. m_Running = false;
  207. m_log.Info("[CONSOLE] Quitting");
  208. }
  209. protected virtual void ReadConfig()
  210. {
  211. }
  212. protected virtual void Initialise()
  213. {
  214. }
  215. }
  216. }