OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
ConfigurationLoader.cs
Go to the documentation of this file.
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 
37 namespace OpenSim.Tools.Configger
38 {
42  public class ConfigurationLoader
43  {
47  protected IConfigSource m_config;
48 
52  private static readonly ILog m_log =
53  LogManager.GetLogger(
54  MethodBase.GetCurrentMethod().DeclaringType);
55 
57  {
58  }
59 
67  public IConfigSource LoadConfigSettings(IConfig startupConfig)
68  {
69  bool iniFileExists = false;
70 
71  List<string> sources = new List<string>();
72 
73  string masterFileName = startupConfig.GetString("inimaster", "OpenSimDefaults.ini");
74 
75  if (masterFileName == "none")
76  masterFileName = String.Empty;
77 
78  if (IsUri(masterFileName))
79  {
80  if (!sources.Contains(masterFileName))
81  sources.Add(masterFileName);
82  }
83  else
84  {
85  string masterFilePath = Path.GetFullPath(
86  Path.Combine(Util.configDir(), masterFileName));
87 
88  if (masterFileName != String.Empty)
89  {
90  if (File.Exists(masterFilePath))
91  {
92  if (!sources.Contains(masterFilePath))
93  sources.Add(masterFilePath);
94  }
95  else
96  {
97  m_log.ErrorFormat("Master ini file {0} not found", Path.GetFullPath(masterFilePath));
98  Environment.Exit(1);
99  }
100  }
101  }
102 
103  string iniFileName = startupConfig.GetString("inifile", Path.Combine(".", "OpenSim.ini"));
104 
105  if (IsUri(iniFileName))
106  {
107  if (!sources.Contains(iniFileName))
108  sources.Add(iniFileName);
109  }
110  else
111  {
112  if (File.Exists(iniFileName))
113  {
114  if (!sources.Contains(iniFileName))
115  sources.Add(iniFileName);
116  }
117  }
118 
119  m_config = new IniConfigSource();
120  m_config.Merge(DefaultConfig());
121 
122  m_log.Info("[CONFIG] Reading configuration settings");
123 
124  if (sources.Count == 0)
125  {
126  m_log.FatalFormat("[CONFIG] Could not load any configuration");
127  m_log.FatalFormat("[CONFIG] Did you copy the OpenSim.ini.example file to OpenSim.ini?");
128  Environment.Exit(1);
129  }
130 
131  for (int i = 0 ; i < sources.Count ; i++)
132  {
133  if (ReadConfig(sources[i]))
134  iniFileExists = true;
135  AddIncludes(sources);
136  }
137 
138  if (!iniFileExists)
139  {
140  m_log.FatalFormat("[CONFIG] Could not load any configuration");
141  m_log.FatalFormat("[CONFIG] Configuration exists, but there was an error loading it!");
142  Environment.Exit(1);
143  }
144 
145  return m_config;
146  }
147 
152  private void AddIncludes(List<string> sources)
153  {
154  //loop over config sources
155  foreach (IConfig config in m_config.Configs)
156  {
157  // Look for Include-* in the key name
158  string[] keys = config.GetKeys();
159  foreach (string k in keys)
160  {
161  if (k.StartsWith("Include-"))
162  {
163  // read the config file to be included.
164  string file = config.GetString(k);
165  if (IsUri(file))
166  {
167  if (!sources.Contains(file))
168  sources.Add(file);
169  }
170  else
171  {
172  string basepath = Path.GetFullPath(".");
173  // Resolve relative paths with wildcards
174  string chunkWithoutWildcards = file;
175  string chunkWithWildcards = string.Empty;
176  int wildcardIndex = file.IndexOfAny(new char[] { '*', '?' });
177  if (wildcardIndex != -1)
178  {
179  chunkWithoutWildcards = file.Substring(0, wildcardIndex);
180  chunkWithWildcards = file.Substring(wildcardIndex);
181  }
182  string path = Path.Combine(basepath, chunkWithoutWildcards);
183  path = Path.GetFullPath(path) + chunkWithWildcards;
184  string[] paths = Util.Glob(path);
185  foreach (string p in paths)
186  {
187  if (!sources.Contains(p))
188  sources.Add(p);
189  }
190  }
191  }
192  }
193  }
194  }
200  bool IsUri(string file)
201  {
202  Uri configUri;
203 
204  return Uri.TryCreate(file, UriKind.Absolute,
205  out configUri) && configUri.Scheme == Uri.UriSchemeHttp;
206  }
207 
213  private bool ReadConfig(string iniPath)
214  {
215  bool success = false;
216 
217  if (!IsUri(iniPath))
218  {
219  m_log.InfoFormat("[CONFIG] Reading configuration file {0}",
220  Path.GetFullPath(iniPath));
221 
222  m_config.Merge(new IniConfigSource(iniPath));
223  success = true;
224  }
225  else
226  {
227  m_log.InfoFormat("[CONFIG] {0} is a http:// URI, fetching ...",
228  iniPath);
229 
230  // The ini file path is a http URI
231  // Try to read it
232  //
233  try
234  {
235  XmlReader r = XmlReader.Create(iniPath);
236  XmlConfigSource cs = new XmlConfigSource(r);
237  m_config.Merge(cs);
238 
239  success = true;
240  }
241  catch (Exception e)
242  {
243  m_log.FatalFormat("[CONFIG] Exception reading config from URI {0}\n" + e.ToString(), iniPath);
244  Environment.Exit(1);
245  }
246  }
247  return success;
248  }
249 
254  private static IConfigSource DefaultConfig()
255  {
256  IConfigSource defaultConfig = new IniConfigSource();
257 
258  {
259  IConfig config = defaultConfig.Configs["Startup"];
260 
261  if (null == config)
262  config = defaultConfig.AddConfig("Startup");
263 
264  config.Set("region_info_source", "filesystem");
265  config.Set("allow_regionless", false);
266 
267  config.Set("gridmode", false);
268  config.Set("physics", "OpenDynamicsEngine");
269  config.Set("meshing", "Meshmerizer");
270  config.Set("physical_prim", true);
271  config.Set("serverside_object_permissions", true);
272  config.Set("storage_prim_inventories", true);
273  config.Set("startup_console_commands_file", String.Empty);
274  config.Set("shutdown_console_commands_file", String.Empty);
275  config.Set("DefaultScriptEngine", "XEngine");
276  config.Set("clientstack_plugin", "OpenSim.Region.ClientStack.LindenUDP.dll");
277  // life doesn't really work without this
278  config.Set("EventQueue", true);
279  }
280 
281  return defaultConfig;
282  }
283  }
284 }
IConfigSource m_config
A source of Configuration data
Loads the Configuration files into nIni
IConfigSource LoadConfigSettings(IConfig startupConfig)
Loads the region configuration
OpenSimConfigSource m_config
A source of Configuration data
Interactive OpenSim region server
Definition: OpenSim.cs:55