clockwerk-opensim – Blame information for rev 1
?pathlinks?
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.Xml; |
||
34 | using log4net; |
||
35 | using Nini.Config; |
||
36 | using OpenSim.Framework; |
||
37 | |||
38 | namespace OpenSim |
||
39 | { |
||
40 | /// <summary> |
||
41 | /// Loads the Configuration files into nIni |
||
42 | /// </summary> |
||
43 | public class ConfigurationLoader |
||
44 | { |
||
45 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
46 | |||
47 | /// <summary> |
||
48 | /// Various Config settings the region needs to start |
||
49 | /// Physics Engine, Mesh Engine, GridMode, PhysicsPrim allowed, Neighbor, |
||
50 | /// StorageDLL, Storage Connection String, Estate connection String, Client Stack |
||
51 | /// Standalone settings. |
||
52 | /// </summary> |
||
53 | protected ConfigSettings m_configSettings; |
||
54 | |||
55 | /// <summary> |
||
56 | /// A source of Configuration data |
||
57 | /// </summary> |
||
58 | protected OpenSimConfigSource m_config; |
||
59 | |||
60 | /// <summary> |
||
61 | /// Grid Service Information. This refers to classes and addresses of the grid service |
||
62 | /// </summary> |
||
63 | protected NetworkServersInfo m_networkServersInfo; |
||
64 | |||
65 | /// <summary> |
||
66 | /// Loads the region configuration |
||
67 | /// </summary> |
||
68 | /// <param name="argvSource">Parameters passed into the process when started</param> |
||
69 | /// <param name="configSettings"></param> |
||
70 | /// <param name="networkInfo"></param> |
||
71 | /// <returns>A configuration that gets passed to modules</returns> |
||
72 | public OpenSimConfigSource LoadConfigSettings( |
||
73 | IConfigSource argvSource, EnvConfigSource envConfigSource, out ConfigSettings configSettings, |
||
74 | out NetworkServersInfo networkInfo) |
||
75 | { |
||
76 | m_configSettings = configSettings = new ConfigSettings(); |
||
77 | m_networkServersInfo = networkInfo = new NetworkServersInfo(); |
||
78 | |||
79 | bool iniFileExists = false; |
||
80 | |||
81 | IConfig startupConfig = argvSource.Configs["Startup"]; |
||
82 | |||
83 | List<string> sources = new List<string>(); |
||
84 | |||
85 | string masterFileName = startupConfig.GetString("inimaster", "OpenSimDefaults.ini"); |
||
86 | |||
87 | if (masterFileName == "none") |
||
88 | masterFileName = String.Empty; |
||
89 | |||
90 | if (IsUri(masterFileName)) |
||
91 | { |
||
92 | if (!sources.Contains(masterFileName)) |
||
93 | sources.Add(masterFileName); |
||
94 | } |
||
95 | else |
||
96 | { |
||
97 | string masterFilePath = Path.GetFullPath( |
||
98 | Path.Combine(Util.configDir(), masterFileName)); |
||
99 | |||
100 | if (masterFileName != String.Empty) |
||
101 | { |
||
102 | if (File.Exists(masterFilePath)) |
||
103 | { |
||
104 | if (!sources.Contains(masterFilePath)) |
||
105 | sources.Add(masterFilePath); |
||
106 | } |
||
107 | else |
||
108 | { |
||
109 | m_log.ErrorFormat("Master ini file {0} not found", Path.GetFullPath(masterFilePath)); |
||
110 | Environment.Exit(1); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | |||
115 | string iniFileName = startupConfig.GetString("inifile", "OpenSim.ini"); |
||
116 | |||
117 | if (IsUri(iniFileName)) |
||
118 | { |
||
119 | if (!sources.Contains(iniFileName)) |
||
120 | sources.Add(iniFileName); |
||
121 | Application.iniFilePath = iniFileName; |
||
122 | } |
||
123 | else |
||
124 | { |
||
125 | Application.iniFilePath = Path.GetFullPath( |
||
126 | Path.Combine(Util.configDir(), iniFileName)); |
||
127 | |||
128 | if (!File.Exists(Application.iniFilePath)) |
||
129 | { |
||
130 | iniFileName = "OpenSim.xml"; |
||
131 | Application.iniFilePath = Path.GetFullPath(Path.Combine(Util.configDir(), iniFileName)); |
||
132 | } |
||
133 | |||
134 | if (File.Exists(Application.iniFilePath)) |
||
135 | { |
||
136 | if (!sources.Contains(Application.iniFilePath)) |
||
137 | sources.Add(Application.iniFilePath); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | m_config = new OpenSimConfigSource(); |
||
142 | m_config.Source = new IniConfigSource(); |
||
143 | m_config.Source.Merge(DefaultConfig()); |
||
144 | |||
145 | m_log.Info("[CONFIG]: Reading configuration settings"); |
||
146 | |||
147 | for (int i = 0 ; i < sources.Count ; i++) |
||
148 | { |
||
149 | if (ReadConfig(m_config, sources[i])) |
||
150 | { |
||
151 | iniFileExists = true; |
||
152 | AddIncludes(m_config, sources); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | // Override distro settings with contents of inidirectory |
||
157 | string iniDirName = startupConfig.GetString("inidirectory", "config"); |
||
158 | string iniDirPath = Path.Combine(Util.configDir(), iniDirName); |
||
159 | |||
160 | if (Directory.Exists(iniDirPath)) |
||
161 | { |
||
162 | m_log.InfoFormat("[CONFIG]: Searching folder {0} for config ini files", iniDirPath); |
||
163 | List<string> overrideSources = new List<string>(); |
||
164 | |||
165 | string[] fileEntries = Directory.GetFiles(iniDirName); |
||
166 | foreach (string filePath in fileEntries) |
||
167 | { |
||
168 | if (Path.GetExtension(filePath).ToLower() == ".ini") |
||
169 | { |
||
170 | if (!sources.Contains(Path.GetFullPath(filePath))) |
||
171 | { |
||
172 | overrideSources.Add(Path.GetFullPath(filePath)); |
||
173 | // put it in sources too, to avoid circularity |
||
174 | sources.Add(Path.GetFullPath(filePath)); |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | |||
179 | |||
180 | if (overrideSources.Count > 0) |
||
181 | { |
||
182 | OpenSimConfigSource overrideConfig = new OpenSimConfigSource(); |
||
183 | overrideConfig.Source = new IniConfigSource(); |
||
184 | |||
185 | for (int i = 0 ; i < overrideSources.Count ; i++) |
||
186 | { |
||
187 | if (ReadConfig(overrideConfig, overrideSources[i])) |
||
188 | { |
||
189 | iniFileExists = true; |
||
190 | AddIncludes(overrideConfig, overrideSources); |
||
191 | } |
||
192 | } |
||
193 | m_config.Source.Merge(overrideConfig.Source); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | if (sources.Count == 0) |
||
198 | { |
||
199 | m_log.FatalFormat("[CONFIG]: Could not load any configuration"); |
||
200 | Environment.Exit(1); |
||
201 | } |
||
202 | else if (!iniFileExists) |
||
203 | { |
||
204 | m_log.FatalFormat("[CONFIG]: Could not load any configuration"); |
||
205 | m_log.FatalFormat("[CONFIG]: Configuration exists, but there was an error loading it!"); |
||
206 | Environment.Exit(1); |
||
207 | } |
||
208 | |||
209 | // Merge OpSys env vars |
||
210 | m_log.Info("[CONFIG]: Loading environment variables for Config"); |
||
211 | Util.MergeEnvironmentToConfig(m_config.Source); |
||
212 | |||
213 | // Make sure command line options take precedence |
||
214 | m_config.Source.Merge(argvSource); |
||
215 | |||
216 | ReadConfigSettings(); |
||
217 | |||
218 | return m_config; |
||
219 | } |
||
220 | |||
221 | /// <summary> |
||
222 | /// Adds the included files as ini configuration files |
||
223 | /// </summary> |
||
224 | /// <param name="sources">List of URL strings or filename strings</param> |
||
225 | private void AddIncludes(OpenSimConfigSource configSource, List<string> sources) |
||
226 | { |
||
227 | //loop over config sources |
||
228 | foreach (IConfig config in configSource.Source.Configs) |
||
229 | { |
||
230 | // Look for Include-* in the key name |
||
231 | string[] keys = config.GetKeys(); |
||
232 | foreach (string k in keys) |
||
233 | { |
||
234 | if (k.StartsWith("Include-")) |
||
235 | { |
||
236 | // read the config file to be included. |
||
237 | string file = config.GetString(k); |
||
238 | if (IsUri(file)) |
||
239 | { |
||
240 | if (!sources.Contains(file)) |
||
241 | sources.Add(file); |
||
242 | } |
||
243 | else |
||
244 | { |
||
245 | string basepath = Path.GetFullPath(Util.configDir()); |
||
246 | // Resolve relative paths with wildcards |
||
247 | string chunkWithoutWildcards = file; |
||
248 | string chunkWithWildcards = string.Empty; |
||
249 | int wildcardIndex = file.IndexOfAny(new char[] { '*', '?' }); |
||
250 | if (wildcardIndex != -1) |
||
251 | { |
||
252 | chunkWithoutWildcards = file.Substring(0, wildcardIndex); |
||
253 | chunkWithWildcards = file.Substring(wildcardIndex); |
||
254 | } |
||
255 | string path = Path.Combine(basepath, chunkWithoutWildcards); |
||
256 | path = Path.GetFullPath(path) + chunkWithWildcards; |
||
257 | string[] paths = Util.Glob(path); |
||
258 | |||
259 | // If the include path contains no wildcards, then warn the user that it wasn't found. |
||
260 | if (wildcardIndex == -1 && paths.Length == 0) |
||
261 | { |
||
262 | m_log.WarnFormat("[CONFIG]: Could not find include file {0}", path); |
||
263 | } |
||
264 | else |
||
265 | { |
||
266 | foreach (string p in paths) |
||
267 | { |
||
268 | if (!sources.Contains(p)) |
||
269 | sources.Add(p); |
||
270 | } |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | } |
||
276 | } |
||
277 | /// <summary> |
||
278 | /// Check if we can convert the string to a URI |
||
279 | /// </summary> |
||
280 | /// <param name="file">String uri to the remote resource</param> |
||
281 | /// <returns>true if we can convert the string to a Uri object</returns> |
||
282 | bool IsUri(string file) |
||
283 | { |
||
284 | Uri configUri; |
||
285 | |||
286 | return Uri.TryCreate(file, UriKind.Absolute, |
||
287 | out configUri) && configUri.Scheme == Uri.UriSchemeHttp; |
||
288 | } |
||
289 | |||
290 | /// <summary> |
||
291 | /// Provide same ini loader functionality for standard ini and master ini - file system or XML over http |
||
292 | /// </summary> |
||
293 | /// <param name="iniPath">Full path to the ini</param> |
||
294 | /// <returns></returns> |
||
295 | private bool ReadConfig(OpenSimConfigSource configSource, string iniPath) |
||
296 | { |
||
297 | bool success = false; |
||
298 | |||
299 | if (!IsUri(iniPath)) |
||
300 | { |
||
301 | m_log.InfoFormat("[CONFIG]: Reading configuration file {0}", Path.GetFullPath(iniPath)); |
||
302 | |||
303 | configSource.Source.Merge(new IniConfigSource(iniPath)); |
||
304 | success = true; |
||
305 | } |
||
306 | else |
||
307 | { |
||
308 | m_log.InfoFormat("[CONFIG]: {0} is a http:// URI, fetching ...", iniPath); |
||
309 | |||
310 | // The ini file path is a http URI |
||
311 | // Try to read it |
||
312 | try |
||
313 | { |
||
314 | XmlReader r = XmlReader.Create(iniPath); |
||
315 | XmlConfigSource cs = new XmlConfigSource(r); |
||
316 | configSource.Source.Merge(cs); |
||
317 | |||
318 | success = true; |
||
319 | } |
||
320 | catch (Exception e) |
||
321 | { |
||
322 | m_log.FatalFormat("[CONFIG]: Exception reading config from URI {0}\n" + e.ToString(), iniPath); |
||
323 | Environment.Exit(1); |
||
324 | } |
||
325 | } |
||
326 | return success; |
||
327 | } |
||
328 | |||
329 | /// <summary> |
||
330 | /// Setup a default config values in case they aren't present in the ini file |
||
331 | /// </summary> |
||
332 | /// <returns>A Configuration source containing the default configuration</returns> |
||
333 | private static IConfigSource DefaultConfig() |
||
334 | { |
||
335 | IConfigSource defaultConfig = new IniConfigSource(); |
||
336 | |||
337 | { |
||
338 | IConfig config = defaultConfig.Configs["Startup"]; |
||
339 | |||
340 | if (null == config) |
||
341 | config = defaultConfig.AddConfig("Startup"); |
||
342 | |||
343 | config.Set("region_info_source", "filesystem"); |
||
344 | |||
345 | config.Set("physics", "OpenDynamicsEngine"); |
||
346 | config.Set("meshing", "Meshmerizer"); |
||
347 | config.Set("physical_prim", true); |
||
348 | config.Set("serverside_object_permissions", true); |
||
349 | config.Set("storage_prim_inventories", true); |
||
350 | config.Set("startup_console_commands_file", String.Empty); |
||
351 | config.Set("shutdown_console_commands_file", String.Empty); |
||
352 | config.Set("DefaultScriptEngine", "XEngine"); |
||
353 | config.Set("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll"); |
||
354 | // life doesn't really work without this |
||
355 | config.Set("EventQueue", true); |
||
356 | } |
||
357 | |||
358 | { |
||
359 | IConfig config = defaultConfig.Configs["Network"]; |
||
360 | |||
361 | if (null == config) |
||
362 | config = defaultConfig.AddConfig("Network"); |
||
363 | |||
364 | config.Set("http_listener_port", ConfigSettings.DefaultRegionHttpPort); |
||
365 | } |
||
366 | |||
367 | return defaultConfig; |
||
368 | } |
||
369 | |||
370 | /// <summary> |
||
371 | /// Read initial region settings from the ConfigSource |
||
372 | /// </summary> |
||
373 | protected virtual void ReadConfigSettings() |
||
374 | { |
||
375 | IConfig startupConfig = m_config.Source.Configs["Startup"]; |
||
376 | if (startupConfig != null) |
||
377 | { |
||
378 | m_configSettings.PhysicsEngine = startupConfig.GetString("physics"); |
||
379 | m_configSettings.MeshEngineName = startupConfig.GetString("meshing"); |
||
380 | |||
381 | m_configSettings.ClientstackDll |
||
382 | = startupConfig.GetString("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll"); |
||
383 | } |
||
384 | |||
385 | m_networkServersInfo.loadFromConfiguration(m_config.Source); |
||
386 | } |
||
387 | } |
||
388 | } |