30 using System.Reflection;
32 using System.Xml.Serialization;
34 using System.Collections.Generic;
37 using OpenSim.Framework;
40 using OpenSim.Framework.Servers.HttpServer;
41 using OpenSim.Framework.Servers;
44 [assembly:AddinRoot(
"Robust", OpenSim.VersionInfo.VersionNumber)]
45 namespace OpenSim.Server.Base
47 [TypeExtensionPoint(Path=
"/Robust/Connector", Name=
"RobustConnector")]
66 uint Configure(IConfigSource config);
73 static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
75 public AddinRegistry Registry
81 public IConfigSource Config
91 Registry =
new AddinRegistry(registryPath,
".");
93 AddinManager.Initialize(registryPath);
95 AddinManager.Registry.Update();
97 AddinManager.AddExtensionNodeHandler(
"/Robust/Connector", OnExtensionChanged);
100 private static TextWriter prev_console_;
115 prev_console_ = System.Console.Out;
116 System.Console.SetOut(
new StreamWriter(Stream.Null));
120 if (prev_console_ != null)
121 System.Console.SetOut(prev_console_);
125 private void OnExtensionChanged(
object s, ExtensionNodeEventArgs args)
128 Addin a = Registry.GetAddin(args.ExtensionNode.Addin.Id);
132 Registry.Rebuild(null);
133 a = Registry.GetAddin(args.ExtensionNode.Addin.Id);
138 case ExtensionChange.Add:
139 if (a.AddinFile.Contains(Registry.DefaultAddinsFolder))
141 m_log.InfoFormat(
"[SERVER UTILS]: Adding {0} from registry", a.Name);
142 connector.PluginPath = System.IO.Path.Combine(Registry.DefaultAddinsFolder,a.Name.Replace(
',',
'.')); }
145 m_log.InfoFormat(
"[SERVER UTILS]: Adding {0} from ./bin", a.Name);
146 connector.PluginPath = a.AddinFile;
148 LoadPlugin(connector);
150 case ExtensionChange.Remove:
151 m_log.InfoFormat(
"[SERVER UTILS]: Removing {0}", a.Name);
152 UnloadPlugin(connector);
157 private void LoadPlugin(IRobustConnector connector)
160 uint port = connector.Configure(Config);
162 if(connector.Enabled)
164 server = GetServer(connector, port);
165 connector.Initialize(server);
169 m_log.InfoFormat(
"[SERVER UTILS]: {0} Disabled.", connector.ConfigName);
173 private void UnloadPlugin(IRobustConnector connector)
175 m_log.InfoFormat(
"[SERVER UTILS]: Unloading {0}", connector.ConfigName);
180 private IHttpServer GetServer(IRobustConnector connector, uint port)
185 server = MainServer.GetHttpServer(port);
187 server = MainServer.Instance;
193 public static class ServerUtils
195 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
197 public static byte[] SerializeResult(XmlSerializer xs,
object data)
199 using (MemoryStream ms =
new MemoryStream())
200 using (XmlTextWriter xw =
new XmlTextWriter(ms, Util.UTF8))
202 xw.Formatting = Formatting.Indented;
203 xs.Serialize(xw, data);
206 ms.Seek(0, SeekOrigin.Begin);
207 byte[] ret = ms.GetBuffer();
208 Array.Resize(ref ret, (int)ms.Length);
220 public static T LoadPlugin<T> (
string dllName,
Object[] args) where T:
class
226 string className = String.Empty;
229 string[] parts = dllName.Split (
new char[] {
':'});
231 if (parts [0].Length > 1)
234 if (parts.Length > 1)
235 className = parts[1];
240 dllName = String.Format (
"{0}:{1}", parts [0], parts [1]);
241 if (parts.Length > 2)
242 className = parts[2];
245 return LoadPlugin<T>(dllName, className, args);
255 public static T LoadPlugin<T>(
string dllName,
string className,
Object[] args) where T:
class
257 string interfaceName = typeof(T).ToString();
261 Assembly pluginAssembly = Assembly.LoadFrom(dllName);
263 foreach (Type pluginType
in pluginAssembly.GetTypes())
265 if (pluginType.IsPublic)
267 if (className !=
String.Empty
268 && pluginType.ToString() != pluginType.Namespace +
"." + className)
271 Type typeInterface = pluginType.GetInterface(interfaceName);
273 if (typeInterface != null)
278 plug = (T)Activator.CreateInstance(pluginType,
283 if (!(e is System.MissingMethodException))
285 m_log.Error(string.Format(
"[SERVER UTILS]: Error loading plugin {0} from {1}. Exception: {2}",
288 e.InnerException == null ? e.Message : e.InnerException.Message),
291 m_log.ErrorFormat(
"[SERVER UTILS]: Error loading plugin {0}: {1} args.Length {2}", dllName, e.Message, args.Length);
302 catch (ReflectionTypeLoadException rtle)
304 m_log.Error(string.Format(
"[SERVER UTILS]: Error loading plugin from {0}:\n{1}", dllName,
305 String.Join(
"\n", Array.ConvertAll(rtle.LoaderExceptions, e => e.ToString()))),
311 m_log.Error(string.Format(
"[SERVER UTILS]: Error loading plugin from {0}", dllName), e);
316 public static Dictionary<string, object> ParseQueryString(
string query)
318 Dictionary<string, object> result =
new Dictionary<string, object>();
319 string[] terms = query.Split(
new char[] {
'&'});
321 if (terms.Length == 0)
324 foreach (
string t
in terms)
326 string[] elems = t.Split(
new char[] {
'='});
327 if (elems.Length == 0)
330 string name = System.Web.HttpUtility.UrlDecode(elems[0]);
331 string value = String.Empty;
333 if (elems.Length > 1)
334 value = System.Web.HttpUtility.UrlDecode(elems[1]);
336 if (name.EndsWith(
"[]"))
338 string cleanName = name.Substring(0, name.Length - 2);
339 if (result.ContainsKey(cleanName))
341 if (!(result[cleanName] is List<string>))
344 List<string> l = (List<string>)result[cleanName];
350 List<string> newList =
new List<string>();
354 result[cleanName] = newList;
359 if (!result.ContainsKey(name))
360 result[name] = value;
367 public static string BuildQueryString(Dictionary<string, object> data)
369 string qstring = String.Empty;
373 foreach (KeyValuePair<string, object> kvp
in data)
375 if (kvp.Value is List<string>)
377 List<string> l = (List<String>)kvp.Value;
379 foreach (
string s in l)
381 part = System.Web.HttpUtility.UrlEncode(kvp.Key) +
382 "[]=" + System.Web.HttpUtility.UrlEncode(s);
384 if (qstring !=
String.Empty)
392 if (kvp.Value.ToString() != String.Empty)
394 part = System.Web.HttpUtility.UrlEncode(kvp.Key) +
395 "=" + System.Web.HttpUtility.UrlEncode(kvp.Value.ToString());
399 part = System.Web.HttpUtility.UrlEncode(kvp.Key);
402 if (qstring !=
String.Empty)
412 public static string BuildXmlResponse(Dictionary<string, object> data)
414 XmlDocument doc =
new XmlDocument();
416 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
419 doc.AppendChild(xmlnode);
421 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
424 doc.AppendChild(rootElement);
426 BuildXmlData(rootElement, data);
431 private static void BuildXmlData(XmlElement parent, Dictionary<string, object> data)
433 foreach (KeyValuePair<string, object> kvp
in data)
435 if (kvp.Value == null)
438 XmlElement elem = parent.OwnerDocument.CreateElement(
"",
439 XmlConvert.EncodeLocalName(kvp.Key),
"");
441 if (kvp.Value is Dictionary<string, object>)
443 XmlAttribute type = parent.OwnerDocument.CreateAttribute(
"",
447 elem.Attributes.Append(type);
449 BuildXmlData(elem, (Dictionary<string, object>)kvp.Value);
453 elem.AppendChild(parent.OwnerDocument.CreateTextNode(
454 kvp.Value.ToString()));
457 parent.AppendChild(elem);
461 public static Dictionary<string, object> ParseXmlResponse(
string data)
465 Dictionary<string, object> ret =
new Dictionary<string, object>();
467 XmlDocument doc =
new XmlDocument();
471 XmlNodeList rootL = doc.GetElementsByTagName(
"ServerResponse");
473 if (rootL.Count != 1)
476 XmlNode rootNode = rootL[0];
478 ret = ParseElement(rootNode);
483 private static Dictionary<string, object> ParseElement(XmlNode element)
485 Dictionary<string, object> ret =
new Dictionary<string, object>();
487 XmlNodeList partL = element.ChildNodes;
489 foreach (XmlNode part
in partL)
491 XmlNode type = part.Attributes.GetNamedItem(
"type");
492 if (type == null || type.Value !=
"List")
494 ret[XmlConvert.DecodeName(part.Name)] = part.InnerText;
498 ret[XmlConvert.DecodeName(part.Name)] = ParseElement(part);
505 public static IConfig GetConfig(
string configFile,
string configName)
509 if (
File.Exists(configFile))
511 IConfigSource configsource =
new IniConfigSource(configFile);
512 config = configsource.Configs[configName];
520 public static IConfigSource LoadInitialConfig(
string url)
522 IConfigSource source =
new XmlConfigSource();
523 m_log.InfoFormat(
"[SERVER UTILS]: {0} is a http:// URI, fetching ...", url);
529 XmlReader r = XmlReader.Create(url);
530 IConfigSource cs =
new XmlConfigSource(r);
535 m_log.FatalFormat(
"[SERVER UTILS]: Exception reading config from URI {0}\n" + e.ToString(), url);
Command manager - Wrapper for OpenSim.Framework.PluginManager to allow us to add commands to the cons...
Interface to OpenSimulator's built in HTTP server. Use this to register handlers (http, llsd, xmlrpc, etc.) for given URLs.
void suppress_console_output_(bool save)
PluginLoader(IConfigSource config, string registryPath)