diff options
Diffstat (limited to 'OpenSim/Framework')
-rw-r--r-- | OpenSim/Framework/DAMap.cs | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/OpenSim/Framework/DAMap.cs b/OpenSim/Framework/DAMap.cs new file mode 100644 index 0000000..291c8b8 --- /dev/null +++ b/OpenSim/Framework/DAMap.cs | |||
@@ -0,0 +1,260 @@ | |||
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; | ||
30 | using System.Collections.Generic; | ||
31 | using System.IO; | ||
32 | using System.Text; | ||
33 | using System.Xml; | ||
34 | using System.Xml.Schema; | ||
35 | using System.Xml.Serialization; | ||
36 | using OpenMetaverse; | ||
37 | using OpenMetaverse.StructuredData; | ||
38 | |||
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 : IDictionary<string, OSDMap>, IXmlSerializable | ||
52 | { | ||
53 | private static readonly int MIN_STORE_NAME_LENGTH = 4; | ||
54 | |||
55 | protected OSDMap m_map; | ||
56 | |||
57 | public DAMap() { m_map = new OSDMap(); } | ||
58 | |||
59 | public XmlSchema GetSchema() { return null; } | ||
60 | |||
61 | public static DAMap FromXml(string rawXml) | ||
62 | { | ||
63 | DAMap map = new DAMap(); | ||
64 | map.ReadXml(rawXml); | ||
65 | return map; | ||
66 | } | ||
67 | |||
68 | public void ReadXml(string rawXml) | ||
69 | { | ||
70 | // System.Console.WriteLine("Trying to deserialize [{0}]", rawXml); | ||
71 | |||
72 | lock (this) | ||
73 | m_map = (OSDMap)OSDParser.DeserializeLLSDXml(rawXml); | ||
74 | } | ||
75 | |||
76 | public void ReadXml(XmlReader reader) | ||
77 | { | ||
78 | ReadXml(reader.ReadInnerXml()); | ||
79 | } | ||
80 | |||
81 | public string ToXml() | ||
82 | { | ||
83 | lock (this) | ||
84 | return OSDParser.SerializeLLSDXmlString(m_map); | ||
85 | } | ||
86 | |||
87 | public void WriteXml(XmlWriter writer) | ||
88 | { | ||
89 | writer.WriteRaw(ToXml()); | ||
90 | } | ||
91 | |||
92 | public void CopyFrom(DAMap other) | ||
93 | { | ||
94 | // Deep copy | ||
95 | |||
96 | string data = null; | ||
97 | lock (other) | ||
98 | { | ||
99 | if (other.Count > 0) | ||
100 | { | ||
101 | data = OSDParser.SerializeLLSDXmlString(other.m_map); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | lock (this) | ||
106 | { | ||
107 | if (data == null) | ||
108 | Clear(); | ||
109 | else | ||
110 | m_map = (OSDMap)OSDParser.DeserializeLLSDXml(data); | ||
111 | } | ||
112 | } | ||
113 | |||
114 | /// <summary> | ||
115 | /// Returns the number of data stores. | ||
116 | /// </summary> | ||
117 | public int Count { get { lock (this) { return m_map.Count; } } } | ||
118 | |||
119 | public bool IsReadOnly { get { return false; } } | ||
120 | |||
121 | /// <summary> | ||
122 | /// Returns the names of the data stores. | ||
123 | /// </summary> | ||
124 | public ICollection<string> Keys { get { lock (this) { return m_map.Keys; } } } | ||
125 | |||
126 | /// <summary> | ||
127 | /// Returns all the data stores. | ||
128 | /// </summary> | ||
129 | public ICollection<OSDMap> Values | ||
130 | { | ||
131 | get | ||
132 | { | ||
133 | lock (this) | ||
134 | { | ||
135 | List<OSDMap> stores = new List<OSDMap>(m_map.Count); | ||
136 | foreach (OSD llsd in m_map.Values) | ||
137 | stores.Add((OSDMap)llsd); | ||
138 | return stores; | ||
139 | } | ||
140 | } | ||
141 | } | ||
142 | |||
143 | /// <summary> | ||
144 | /// Gets or sets one data store. | ||
145 | /// </summary> | ||
146 | /// <param name="key">Store name</param> | ||
147 | /// <returns></returns> | ||
148 | public OSDMap this[string key] | ||
149 | { | ||
150 | get | ||
151 | { | ||
152 | OSD llsd; | ||
153 | |||
154 | lock (this) | ||
155 | { | ||
156 | if (m_map.TryGetValue(key, out llsd)) | ||
157 | return (OSDMap)llsd; | ||
158 | else | ||
159 | return null; | ||
160 | } | ||
161 | } | ||
162 | |||
163 | set | ||
164 | { | ||
165 | ValidateKey(key); | ||
166 | lock (this) | ||
167 | m_map[key] = value; | ||
168 | } | ||
169 | } | ||
170 | |||
171 | private static void ValidateKey(string key) | ||
172 | { | ||
173 | if (key.Length < MIN_STORE_NAME_LENGTH) | ||
174 | throw new Exception("Minimum store name length is " + MIN_STORE_NAME_LENGTH); | ||
175 | } | ||
176 | |||
177 | public bool ContainsKey(string key) | ||
178 | { | ||
179 | lock (this) | ||
180 | return m_map.ContainsKey(key); | ||
181 | } | ||
182 | |||
183 | public void Add(string key, OSDMap store) | ||
184 | { | ||
185 | ValidateKey(key); | ||
186 | lock (this) | ||
187 | m_map.Add(key, store); | ||
188 | } | ||
189 | |||
190 | public void Add(KeyValuePair<string, OSDMap> kvp) | ||
191 | { | ||
192 | lock (this) | ||
193 | m_map.Add(kvp.Key, kvp.Value); | ||
194 | } | ||
195 | |||
196 | public bool Remove(string key) | ||
197 | { | ||
198 | lock (this) | ||
199 | return m_map.Remove(key); | ||
200 | } | ||
201 | |||
202 | public bool TryGetValue(string key, out OSDMap store) | ||
203 | { | ||
204 | lock (this) | ||
205 | { | ||
206 | OSD llsd; | ||
207 | if (m_map.TryGetValue(key, out llsd)) | ||
208 | { | ||
209 | store = (OSDMap)llsd; | ||
210 | return true; | ||
211 | } | ||
212 | else | ||
213 | { | ||
214 | store = null; | ||
215 | return false; | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | |||
220 | public void Clear() | ||
221 | { | ||
222 | lock (this) | ||
223 | m_map.Clear(); | ||
224 | } | ||
225 | |||
226 | public bool Contains(KeyValuePair<string, OSDMap> kvp) | ||
227 | { | ||
228 | lock (this) | ||
229 | return m_map.ContainsKey(kvp.Key); | ||
230 | } | ||
231 | |||
232 | public void CopyTo(KeyValuePair<string, OSDMap>[] array, int index) | ||
233 | { | ||
234 | throw new NotImplementedException(); | ||
235 | } | ||
236 | |||
237 | public bool Remove(KeyValuePair<string, OSDMap> kvp) | ||
238 | { | ||
239 | lock (this) | ||
240 | return m_map.Remove(kvp.Key); | ||
241 | } | ||
242 | |||
243 | public System.Collections.IDictionaryEnumerator GetEnumerator() | ||
244 | { | ||
245 | lock (this) | ||
246 | return m_map.GetEnumerator(); | ||
247 | } | ||
248 | |||
249 | IEnumerator<KeyValuePair<string, OSDMap>> IEnumerable<KeyValuePair<string, OSDMap>>.GetEnumerator() | ||
250 | { | ||
251 | return null; | ||
252 | } | ||
253 | |||
254 | IEnumerator IEnumerable.GetEnumerator() | ||
255 | { | ||
256 | lock (this) | ||
257 | return m_map.GetEnumerator(); | ||
258 | } | ||
259 | } | ||
260 | } \ No newline at end of file | ||