DAMap.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.Collections;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Text;
  33. using System.Xml;
  34. using System.Xml.Schema;
  35. using System.Xml.Serialization;
  36. using log4net;
  37. using OpenMetaverse;
  38. using OpenMetaverse.StructuredData;
  39. namespace OpenSim.Framework
  40. {
  41. /// <summary>
  42. /// This class stores and retrieves dynamic attributes.
  43. /// </summary>
  44. /// <remarks>
  45. /// Modules that want to use dynamic attributes need to do so in a private data store
  46. /// which is accessed using a unique name. DAMap provides access to the data stores,
  47. /// each of which is an OSDMap. Modules are free to store any type of data they want
  48. /// within their data store. However, avoid storing large amounts of data because that
  49. /// would slow down database access.
  50. /// </remarks>
  51. public class DAMap : IXmlSerializable
  52. {
  53. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  54. private static readonly int MIN_NAMESPACE_LENGTH = 4;
  55. private OSDMap m_map = new OSDMap();
  56. // WARNING: this is temporary for experimentation only, it will be removed!!!!
  57. public OSDMap TopLevelMap
  58. {
  59. get { return m_map; }
  60. set { m_map = value; }
  61. }
  62. public XmlSchema GetSchema() { return null; }
  63. public static DAMap FromXml(string rawXml)
  64. {
  65. DAMap map = new DAMap();
  66. map.ReadXml(rawXml);
  67. return map;
  68. }
  69. public void ReadXml(XmlReader reader)
  70. {
  71. ReadXml(reader.ReadInnerXml());
  72. }
  73. public void ReadXml(string rawXml)
  74. {
  75. // System.Console.WriteLine("Trying to deserialize [{0}]", rawXml);
  76. lock (this)
  77. {
  78. m_map = (OSDMap)OSDParser.DeserializeLLSDXml(rawXml);
  79. SanitiseMap(this);
  80. }
  81. }
  82. public void WriteXml(XmlWriter writer)
  83. {
  84. writer.WriteRaw(ToXml());
  85. }
  86. public string ToXml()
  87. {
  88. lock (this)
  89. return OSDParser.SerializeLLSDXmlString(m_map);
  90. }
  91. public void CopyFrom(DAMap other)
  92. {
  93. // Deep copy
  94. string data = null;
  95. lock (other)
  96. {
  97. if (other.CountNamespaces > 0)
  98. {
  99. data = OSDParser.SerializeLLSDXmlString(other.m_map);
  100. }
  101. }
  102. lock (this)
  103. {
  104. if (data == null)
  105. Clear();
  106. else
  107. m_map = (OSDMap)OSDParser.DeserializeLLSDXml(data);
  108. }
  109. }
  110. /// <summary>
  111. /// Sanitise the map to remove any namespaces or stores that are not OSDMap.
  112. /// </summary>
  113. /// <param name='map'>
  114. /// </param>
  115. public static void SanitiseMap(DAMap daMap)
  116. {
  117. List<string> keysToRemove = null;
  118. OSDMap namespacesMap = daMap.m_map;
  119. foreach (string key in namespacesMap.Keys)
  120. {
  121. // Console.WriteLine("Processing ns {0}", key);
  122. if (!(namespacesMap[key] is OSDMap))
  123. {
  124. if (keysToRemove == null)
  125. keysToRemove = new List<string>();
  126. keysToRemove.Add(key);
  127. }
  128. }
  129. if (keysToRemove != null)
  130. {
  131. foreach (string key in keysToRemove)
  132. {
  133. // Console.WriteLine ("Removing bad ns {0}", key);
  134. namespacesMap.Remove(key);
  135. }
  136. }
  137. foreach (OSD nsOsd in namespacesMap.Values)
  138. {
  139. OSDMap nsOsdMap = (OSDMap)nsOsd;
  140. keysToRemove = null;
  141. foreach (string key in nsOsdMap.Keys)
  142. {
  143. if (!(nsOsdMap[key] is OSDMap))
  144. {
  145. if (keysToRemove == null)
  146. keysToRemove = new List<string>();
  147. keysToRemove.Add(key);
  148. }
  149. }
  150. if (keysToRemove != null)
  151. foreach (string key in keysToRemove)
  152. nsOsdMap.Remove(key);
  153. }
  154. }
  155. /// <summary>
  156. /// Get the number of namespaces
  157. /// </summary>
  158. public int CountNamespaces { get { lock (this) { return m_map.Count; } } }
  159. /// <summary>
  160. /// Get the number of stores.
  161. /// </summary>
  162. public int CountStores
  163. {
  164. get
  165. {
  166. int count = 0;
  167. lock (this)
  168. {
  169. foreach (OSD osdNamespace in m_map)
  170. {
  171. count += ((OSDMap)osdNamespace).Count;
  172. }
  173. }
  174. return count;
  175. }
  176. }
  177. /// <summary>
  178. /// Retrieve a Dynamic Attribute store
  179. /// </summary>
  180. /// <param name="ns">namespace for the store - use "OpenSim" for in-core modules</param>
  181. /// <param name="storeName">name of the store within the namespace</param>
  182. /// <returns>an OSDMap representing the stored data, or null if not found</returns>
  183. public OSDMap GetStore(string ns, string storeName)
  184. {
  185. OSD namespaceOsd;
  186. lock (this)
  187. {
  188. if (m_map.TryGetValue(ns, out namespaceOsd))
  189. {
  190. OSD store;
  191. if (((OSDMap)namespaceOsd).TryGetValue(storeName, out store))
  192. return (OSDMap)store;
  193. }
  194. }
  195. return null;
  196. }
  197. /// <summary>
  198. /// Saves a Dynamic attribute store
  199. /// </summary>
  200. /// <param name="ns">namespace for the store - use "OpenSim" for in-core modules</param>
  201. /// <param name="storeName">name of the store within the namespace</param>
  202. /// <param name="store">an OSDMap representing the data to store</param>
  203. public void SetStore(string ns, string storeName, OSDMap store)
  204. {
  205. ValidateNamespace(ns);
  206. OSDMap nsMap;
  207. lock (this)
  208. {
  209. if (!m_map.ContainsKey(ns))
  210. {
  211. nsMap = new OSDMap();
  212. m_map[ns] = nsMap;
  213. }
  214. nsMap = (OSDMap)m_map[ns];
  215. // m_log.DebugFormat("[DA MAP]: Setting store to {0}:{1}", ns, storeName);
  216. nsMap[storeName] = store;
  217. }
  218. }
  219. /// <summary>
  220. /// Validate the key used for storing separate data stores.
  221. /// </summary>
  222. /// <param name='key'></param>
  223. public static void ValidateNamespace(string ns)
  224. {
  225. if (ns.Length < MIN_NAMESPACE_LENGTH)
  226. throw new Exception("Minimum namespace length is " + MIN_NAMESPACE_LENGTH);
  227. }
  228. public bool ContainsStore(string ns, string storeName)
  229. {
  230. OSD namespaceOsd;
  231. lock (this)
  232. {
  233. if (m_map.TryGetValue(ns, out namespaceOsd))
  234. {
  235. return ((OSDMap)namespaceOsd).ContainsKey(storeName);
  236. }
  237. }
  238. return false;
  239. }
  240. public bool TryGetStore(string ns, string storeName, out OSDMap store)
  241. {
  242. OSD namespaceOsd;
  243. lock (this)
  244. {
  245. if (m_map.TryGetValue(ns, out namespaceOsd))
  246. {
  247. OSD storeOsd;
  248. bool result = ((OSDMap)namespaceOsd).TryGetValue(storeName, out storeOsd);
  249. store = (OSDMap)storeOsd;
  250. return result;
  251. }
  252. }
  253. store = null;
  254. return false;
  255. }
  256. public void Clear()
  257. {
  258. lock (this)
  259. m_map.Clear();
  260. }
  261. public bool RemoveStore(string ns, string storeName)
  262. {
  263. OSD namespaceOsd;
  264. lock (this)
  265. {
  266. if (m_map.TryGetValue(ns, out namespaceOsd))
  267. {
  268. OSDMap namespaceOsdMap = (OSDMap)namespaceOsd;
  269. namespaceOsdMap.Remove(storeName);
  270. // Don't keep empty namespaces around
  271. if (namespaceOsdMap.Count <= 0)
  272. m_map.Remove(ns);
  273. }
  274. }
  275. return false;
  276. }
  277. }
  278. }