ServicesServerBase.cs 7.4 KB

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