clockwerk-opensim – Blame information for rev 1

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