ServicesServerBase.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.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.Server.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. argvConfig.AddSwitch("Startup", "prompt", "p");
  74. // Automagically create the ini file name
  75. //
  76. string fullName = Assembly.GetEntryAssembly().FullName;
  77. AssemblyName assemblyName = new AssemblyName(fullName);
  78. string iniFile = assemblyName.Name + ".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. // Allow derived classes to load config before the console is
  123. // opened.
  124. //
  125. ReadConfig();
  126. // Create main console
  127. //
  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
  141. {
  142. MainConsole.Instance = new LocalConsole(prompt);
  143. }
  144. // Configure the appenders for log4net
  145. //
  146. OpenSimAppender consoleAppender = null;
  147. FileAppender fileAppender = null;
  148. XmlConfigurator.Configure();
  149. ILoggerRepository repository = LogManager.GetRepository();
  150. IAppender[] appenders = repository.GetAppenders();
  151. foreach (IAppender appender in appenders)
  152. {
  153. if (appender.Name == "Console")
  154. {
  155. consoleAppender = (OpenSimAppender)appender;
  156. }
  157. if (appender.Name == "LogFileAppender")
  158. {
  159. fileAppender = (FileAppender)appender;
  160. }
  161. }
  162. if (consoleAppender == null)
  163. {
  164. System.Console.WriteLine("No console appender found. Server can't start");
  165. Thread.CurrentThread.Abort();
  166. }
  167. else
  168. {
  169. consoleAppender.Console = MainConsole.Instance;
  170. if (null == consoleAppender.Threshold)
  171. consoleAppender.Threshold = Level.All;
  172. }
  173. // Set log file
  174. //
  175. if (fileAppender != null)
  176. {
  177. if (startupConfig != null)
  178. fileAppender.File = startupConfig.GetString("logfile",
  179. assemblyName.Name + ".log");
  180. }
  181. // Register the quit command
  182. //
  183. MainConsole.Instance.Commands.AddCommand("base", false, "quit",
  184. "quit",
  185. "Quit the application", HandleQuit);
  186. // Allow derived classes to perform initialization that
  187. // needs to be done after the console has opened
  188. //
  189. Initialise();
  190. }
  191. public virtual int Run()
  192. {
  193. while (m_Running)
  194. {
  195. MainConsole.Instance.Prompt();
  196. }
  197. return 0;
  198. }
  199. protected virtual void HandleQuit(string module, string[] args)
  200. {
  201. m_Running = false;
  202. m_log.Info("[CONSOLE] Quitting");
  203. }
  204. protected virtual void ReadConfig()
  205. {
  206. }
  207. protected virtual void Initialise()
  208. {
  209. }
  210. }
  211. }