diff options
Diffstat (limited to 'OpenSim')
4 files changed, 637 insertions, 2 deletions
diff --git a/OpenSim/Data/MySQL/MySQLXAssetData.cs b/OpenSim/Data/MySQL/MySQLXAssetData.cs new file mode 100644 index 0000000..0dadf5e --- /dev/null +++ b/OpenSim/Data/MySQL/MySQLXAssetData.cs | |||
@@ -0,0 +1,392 @@ | |||
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.Data; | ||
30 | using System.Reflection; | ||
31 | using System.Collections.Generic; | ||
32 | using System.Text; | ||
33 | using log4net; | ||
34 | using MySql.Data.MySqlClient; | ||
35 | using OpenMetaverse; | ||
36 | using OpenSim.Framework; | ||
37 | using OpenSim.Data; | ||
38 | |||
39 | namespace OpenSim.Data.MySQL | ||
40 | { | ||
41 | public class MySQLXAssetData : AssetDataBase | ||
42 | { | ||
43 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
44 | |||
45 | private string m_connectionString; | ||
46 | private object m_dbLock = new object(); | ||
47 | |||
48 | protected virtual Assembly Assembly | ||
49 | { | ||
50 | get { return GetType().Assembly; } | ||
51 | } | ||
52 | |||
53 | #region IPlugin Members | ||
54 | |||
55 | public override string Version { get { return "1.0.0.0"; } } | ||
56 | |||
57 | /// <summary> | ||
58 | /// <para>Initialises Asset interface</para> | ||
59 | /// <para> | ||
60 | /// <list type="bullet"> | ||
61 | /// <item>Loads and initialises the MySQL storage plugin.</item> | ||
62 | /// <item>Warns and uses the obsolete mysql_connection.ini if connect string is empty.</item> | ||
63 | /// <item>Check for migration</item> | ||
64 | /// </list> | ||
65 | /// </para> | ||
66 | /// </summary> | ||
67 | /// <param name="connect">connect string</param> | ||
68 | public override void Initialise(string connect) | ||
69 | { | ||
70 | m_connectionString = connect; | ||
71 | |||
72 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
73 | { | ||
74 | dbcon.Open(); | ||
75 | Migration m = new Migration(dbcon, Assembly, "XAssetStore"); | ||
76 | m.Update(); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | public override void Initialise() | ||
81 | { | ||
82 | throw new NotImplementedException(); | ||
83 | } | ||
84 | |||
85 | public override void Dispose() { } | ||
86 | |||
87 | /// <summary> | ||
88 | /// The name of this DB provider | ||
89 | /// </summary> | ||
90 | override public string Name | ||
91 | { | ||
92 | get { return "MySQL XAsset storage engine"; } | ||
93 | } | ||
94 | |||
95 | #endregion | ||
96 | |||
97 | #region IAssetDataPlugin Members | ||
98 | |||
99 | /// <summary> | ||
100 | /// Fetch Asset <paramref name="assetID"/> from database | ||
101 | /// </summary> | ||
102 | /// <param name="assetID">Asset UUID to fetch</param> | ||
103 | /// <returns>Return the asset</returns> | ||
104 | /// <remarks>On failure : throw an exception and attempt to reconnect to database</remarks> | ||
105 | override public AssetBase GetAsset(UUID assetID) | ||
106 | { | ||
107 | // m_log.DebugFormat("[MYSQL XASSET DATA]: Looking for asset {0}", assetID); | ||
108 | |||
109 | AssetBase asset = null; | ||
110 | lock (m_dbLock) | ||
111 | { | ||
112 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
113 | { | ||
114 | dbcon.Open(); | ||
115 | |||
116 | string hash = null; | ||
117 | |||
118 | using (MySqlCommand cmd = new MySqlCommand( | ||
119 | "SELECT name, description, asset_type, local, temporary, asset_flags, creator_id, data FROM xassetsmeta JOIN xassetsdata ON xassetsmeta.hash = xassetsdata.hash WHERE id=?id", | ||
120 | dbcon)) | ||
121 | { | ||
122 | cmd.Parameters.AddWithValue("?id", assetID.ToString()); | ||
123 | |||
124 | try | ||
125 | { | ||
126 | using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow)) | ||
127 | { | ||
128 | if (dbReader.Read()) | ||
129 | { | ||
130 | asset = new AssetBase(assetID, (string)dbReader["name"], (sbyte)dbReader["asset_type"], dbReader["creator_id"].ToString()); | ||
131 | asset.Data = (byte[])dbReader["data"]; | ||
132 | asset.Description = (string)dbReader["description"]; | ||
133 | |||
134 | string local = dbReader["local"].ToString(); | ||
135 | if (local.Equals("1") || local.Equals("true", StringComparison.InvariantCultureIgnoreCase)) | ||
136 | asset.Local = true; | ||
137 | else | ||
138 | asset.Local = false; | ||
139 | |||
140 | asset.Temporary = Convert.ToBoolean(dbReader["temporary"]); | ||
141 | asset.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]); | ||
142 | } | ||
143 | } | ||
144 | } | ||
145 | catch (Exception e) | ||
146 | { | ||
147 | m_log.Error("[MYSQL XASSET DATA]: MySql failure fetching asset " + assetID + ": " + e.Message); | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | |||
153 | return asset; | ||
154 | } | ||
155 | |||
156 | /// <summary> | ||
157 | /// Create an asset in database, or update it if existing. | ||
158 | /// </summary> | ||
159 | /// <param name="asset">Asset UUID to create</param> | ||
160 | /// <remarks>On failure : Throw an exception and attempt to reconnect to database</remarks> | ||
161 | override public void StoreAsset(AssetBase asset) | ||
162 | { | ||
163 | lock (m_dbLock) | ||
164 | { | ||
165 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
166 | { | ||
167 | dbcon.Open(); | ||
168 | |||
169 | string assetName = asset.Name; | ||
170 | if (asset.Name.Length > 64) | ||
171 | { | ||
172 | assetName = asset.Name.Substring(0, 64); | ||
173 | m_log.Warn("[XASSET DB]: Name field truncated from " + asset.Name.Length + " to " + assetName.Length + " characters on add"); | ||
174 | } | ||
175 | |||
176 | string assetDescription = asset.Description; | ||
177 | if (asset.Description.Length > 64) | ||
178 | { | ||
179 | assetDescription = asset.Description.Substring(0, 64); | ||
180 | m_log.Warn("[XASSET DB]: Description field truncated from " + asset.Description.Length + " to " + assetDescription.Length + " characters on add"); | ||
181 | } | ||
182 | |||
183 | string hash = Util.SHA1Hash(asset.Data); | ||
184 | |||
185 | try | ||
186 | { | ||
187 | using (MySqlCommand cmd = | ||
188 | new MySqlCommand( | ||
189 | "replace INTO xassetsmeta(id, hash, name, description, asset_type, local, temporary, create_time, access_time, asset_flags, creator_id)" + | ||
190 | "VALUES(?id, ?hash, ?name, ?description, ?asset_type, ?local, ?temporary, ?create_time, ?access_time, ?asset_flags, ?creator_id)", | ||
191 | dbcon)) | ||
192 | { | ||
193 | // create unix epoch time | ||
194 | int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow); | ||
195 | cmd.Parameters.AddWithValue("?id", asset.ID); | ||
196 | cmd.Parameters.AddWithValue("?hash", hash); | ||
197 | cmd.Parameters.AddWithValue("?name", assetName); | ||
198 | cmd.Parameters.AddWithValue("?description", assetDescription); | ||
199 | cmd.Parameters.AddWithValue("?asset_type", asset.Type); | ||
200 | cmd.Parameters.AddWithValue("?local", asset.Local); | ||
201 | cmd.Parameters.AddWithValue("?temporary", asset.Temporary); | ||
202 | cmd.Parameters.AddWithValue("?create_time", now); | ||
203 | cmd.Parameters.AddWithValue("?access_time", now); | ||
204 | cmd.Parameters.AddWithValue("?creator_id", asset.Metadata.CreatorID); | ||
205 | cmd.Parameters.AddWithValue("?asset_flags", (int)asset.Flags); | ||
206 | cmd.Parameters.AddWithValue("?data", asset.Data); | ||
207 | cmd.ExecuteNonQuery(); | ||
208 | cmd.Dispose(); | ||
209 | } | ||
210 | } | ||
211 | catch (Exception e) | ||
212 | { | ||
213 | m_log.ErrorFormat("[ASSET DB]: MySQL failure creating asset metadata {0} with name \"{1}\". Error: {2}", | ||
214 | asset.FullID, asset.Name, e.Message); | ||
215 | } | ||
216 | |||
217 | try | ||
218 | { | ||
219 | using (MySqlCommand cmd = | ||
220 | new MySqlCommand( | ||
221 | "replace INTO xassetsdata(hash, data) VALUES(?hash, ?data)", | ||
222 | dbcon)) | ||
223 | { | ||
224 | cmd.Parameters.AddWithValue("?hash", hash); | ||
225 | cmd.Parameters.AddWithValue("?data", asset.Data); | ||
226 | cmd.ExecuteNonQuery(); | ||
227 | cmd.Dispose(); | ||
228 | } | ||
229 | } | ||
230 | catch (Exception e) | ||
231 | { | ||
232 | m_log.ErrorFormat("[XASSET DB]: MySQL failure creating asset data {0} with name \"{1}\". Error: {2}", | ||
233 | asset.FullID, asset.Name, e.Message); | ||
234 | } | ||
235 | } | ||
236 | } | ||
237 | } | ||
238 | |||
239 | // private void UpdateAccessTime(AssetBase asset) | ||
240 | // { | ||
241 | // lock (m_dbLock) | ||
242 | // { | ||
243 | // using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
244 | // { | ||
245 | // dbcon.Open(); | ||
246 | // MySqlCommand cmd = | ||
247 | // new MySqlCommand("update assets set access_time=?access_time where id=?id", | ||
248 | // dbcon); | ||
249 | // | ||
250 | // // need to ensure we dispose | ||
251 | // try | ||
252 | // { | ||
253 | // using (cmd) | ||
254 | // { | ||
255 | // // create unix epoch time | ||
256 | // int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow); | ||
257 | // cmd.Parameters.AddWithValue("?id", asset.ID); | ||
258 | // cmd.Parameters.AddWithValue("?access_time", now); | ||
259 | // cmd.ExecuteNonQuery(); | ||
260 | // cmd.Dispose(); | ||
261 | // } | ||
262 | // } | ||
263 | // catch (Exception e) | ||
264 | // { | ||
265 | // m_log.ErrorFormat( | ||
266 | // "[ASSETS DB]: " + | ||
267 | // "MySql failure updating access_time for asset {0} with name {1}" + Environment.NewLine + e.ToString() | ||
268 | // + Environment.NewLine + "Attempting reconnection", asset.FullID, asset.Name); | ||
269 | // } | ||
270 | // } | ||
271 | // } | ||
272 | // | ||
273 | // } | ||
274 | |||
275 | /// <summary> | ||
276 | /// Check if the asset exists in the database | ||
277 | /// </summary> | ||
278 | /// <param name="uuid">The asset UUID</param> | ||
279 | /// <returns>true if it exists, false otherwise.</returns> | ||
280 | override public bool ExistsAsset(UUID uuid) | ||
281 | { | ||
282 | // m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid); | ||
283 | |||
284 | bool assetExists = false; | ||
285 | |||
286 | lock (m_dbLock) | ||
287 | { | ||
288 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
289 | { | ||
290 | dbcon.Open(); | ||
291 | using (MySqlCommand cmd = new MySqlCommand("SELECT id FROM xassetsmeta WHERE id=?id", dbcon)) | ||
292 | { | ||
293 | cmd.Parameters.AddWithValue("?id", uuid.ToString()); | ||
294 | |||
295 | try | ||
296 | { | ||
297 | using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow)) | ||
298 | { | ||
299 | if (dbReader.Read()) | ||
300 | { | ||
301 | // m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid); | ||
302 | assetExists = true; | ||
303 | } | ||
304 | } | ||
305 | } | ||
306 | catch (Exception e) | ||
307 | { | ||
308 | m_log.ErrorFormat( | ||
309 | "[XASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString(), uuid); | ||
310 | } | ||
311 | } | ||
312 | } | ||
313 | } | ||
314 | |||
315 | return assetExists; | ||
316 | } | ||
317 | |||
318 | /// <summary> | ||
319 | /// Returns a list of AssetMetadata objects. The list is a subset of | ||
320 | /// the entire data set offset by <paramref name="start" /> containing | ||
321 | /// <paramref name="count" /> elements. | ||
322 | /// </summary> | ||
323 | /// <param name="start">The number of results to discard from the total data set.</param> | ||
324 | /// <param name="count">The number of rows the returned list should contain.</param> | ||
325 | /// <returns>A list of AssetMetadata objects.</returns> | ||
326 | public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count) | ||
327 | { | ||
328 | List<AssetMetadata> retList = new List<AssetMetadata>(count); | ||
329 | |||
330 | lock (m_dbLock) | ||
331 | { | ||
332 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
333 | { | ||
334 | dbcon.Open(); | ||
335 | MySqlCommand cmd = new MySqlCommand("SELECT name,description,asset_type,temporary,id,asset_flags,creator_id FROM xassetsmeta LIMIT ?start, ?count", dbcon); | ||
336 | cmd.Parameters.AddWithValue("?start", start); | ||
337 | cmd.Parameters.AddWithValue("?count", count); | ||
338 | |||
339 | try | ||
340 | { | ||
341 | using (MySqlDataReader dbReader = cmd.ExecuteReader()) | ||
342 | { | ||
343 | while (dbReader.Read()) | ||
344 | { | ||
345 | AssetMetadata metadata = new AssetMetadata(); | ||
346 | metadata.Name = (string)dbReader["name"]; | ||
347 | metadata.Description = (string)dbReader["description"]; | ||
348 | metadata.Type = (sbyte)dbReader["asset_type"]; | ||
349 | metadata.Temporary = Convert.ToBoolean(dbReader["temporary"]); // Not sure if this is correct. | ||
350 | metadata.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]); | ||
351 | metadata.FullID = DBGuid.FromDB(dbReader["id"]); | ||
352 | metadata.CreatorID = dbReader["creator_id"].ToString(); | ||
353 | metadata.SHA1 = Encoding.Default.GetBytes((string)dbReader["hash"]); | ||
354 | |||
355 | retList.Add(metadata); | ||
356 | } | ||
357 | } | ||
358 | } | ||
359 | catch (Exception e) | ||
360 | { | ||
361 | m_log.Error("[XASSETS DB]: MySql failure fetching asset set" + Environment.NewLine + e.ToString()); | ||
362 | } | ||
363 | } | ||
364 | } | ||
365 | |||
366 | return retList; | ||
367 | } | ||
368 | |||
369 | public override bool Delete(string id) | ||
370 | { | ||
371 | lock (m_dbLock) | ||
372 | { | ||
373 | using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) | ||
374 | { | ||
375 | dbcon.Open(); | ||
376 | MySqlCommand cmd = new MySqlCommand("delete from xassetsmeta where id=?id", dbcon); | ||
377 | cmd.Parameters.AddWithValue("?id", id); | ||
378 | cmd.ExecuteNonQuery(); | ||
379 | |||
380 | cmd.Dispose(); | ||
381 | |||
382 | // TODO: How do we deal with data from deleted assets? Probably not easily reapable unless we | ||
383 | // keep a reference count (?) | ||
384 | } | ||
385 | } | ||
386 | |||
387 | return true; | ||
388 | } | ||
389 | |||
390 | #endregion | ||
391 | } | ||
392 | } \ No newline at end of file | ||
diff --git a/OpenSim/Data/MySQL/Resources/XAssetStore.migrations b/OpenSim/Data/MySQL/Resources/XAssetStore.migrations new file mode 100644 index 0000000..b89eab2 --- /dev/null +++ b/OpenSim/Data/MySQL/Resources/XAssetStore.migrations | |||
@@ -0,0 +1,27 @@ | |||
1 | # ----------------- | ||
2 | :VERSION 1 | ||
3 | |||
4 | BEGIN; | ||
5 | |||
6 | CREATE TABLE `xassetsmeta` ( | ||
7 | `id` char(36) NOT NULL, | ||
8 | `hash` char(64) NOT NULL, | ||
9 | `name` varchar(64) NOT NULL, | ||
10 | `description` varchar(64) NOT NULL, | ||
11 | `asset_type` tinyint(4) NOT NULL, | ||
12 | `local` tinyint(1) NOT NULL, | ||
13 | `temporary` tinyint(1) NOT NULL, | ||
14 | `create_time` int(11) NOT NULL, | ||
15 | `access_time` int(11) NOT NULL, | ||
16 | `asset_flags` int(11) NOT NULL, | ||
17 | `creator_id` varchar(128) NOT NULL, | ||
18 | PRIMARY KEY (`id`) | ||
19 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Version 1'; | ||
20 | |||
21 | CREATE TABLE `xassetsdata` ( | ||
22 | `hash` char(64) NOT NULL, | ||
23 | `data` longblob NOT NULL, | ||
24 | PRIMARY KEY (`hash`) | ||
25 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Version 1'; | ||
26 | |||
27 | COMMIT; \ No newline at end of file | ||
diff --git a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/LocalAssetServiceConnector.cs b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/LocalAssetServiceConnector.cs index 2e6ec90..c78915f 100644 --- a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/LocalAssetServiceConnector.cs +++ b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/LocalAssetServiceConnector.cs | |||
@@ -73,14 +73,17 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset | |||
73 | return; | 73 | return; |
74 | } | 74 | } |
75 | 75 | ||
76 | string serviceDll = assetConfig.GetString("LocalServiceModule", | 76 | string serviceDll = assetConfig.GetString("LocalServiceModule", String.Empty); |
77 | String.Empty); | ||
78 | 77 | ||
79 | if (serviceDll == String.Empty) | 78 | if (serviceDll == String.Empty) |
80 | { | 79 | { |
81 | m_log.Error("[LOCAL ASSET SERVICES CONNECTOR]: No LocalServiceModule named in section AssetService"); | 80 | m_log.Error("[LOCAL ASSET SERVICES CONNECTOR]: No LocalServiceModule named in section AssetService"); |
82 | return; | 81 | return; |
83 | } | 82 | } |
83 | else | ||
84 | { | ||
85 | m_log.DebugFormat("[LOCAL ASSET SERVICES CONNECTOR]: Loading asset service at {0}", serviceDll); | ||
86 | } | ||
84 | 87 | ||
85 | Object[] args = new Object[] { source }; | 88 | Object[] args = new Object[] { source }; |
86 | m_AssetService = ServerUtils.LoadPlugin<IAssetService>(serviceDll, args); | 89 | m_AssetService = ServerUtils.LoadPlugin<IAssetService>(serviceDll, args); |
diff --git a/OpenSim/Services/AssetService/XAssetService.cs b/OpenSim/Services/AssetService/XAssetService.cs new file mode 100644 index 0000000..d161c58 --- /dev/null +++ b/OpenSim/Services/AssetService/XAssetService.cs | |||
@@ -0,0 +1,213 @@ | |||
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 Nini.Config; | ||
33 | using log4net; | ||
34 | using OpenSim.Framework; | ||
35 | using OpenSim.Data; | ||
36 | using OpenSim.Services.Interfaces; | ||
37 | using OpenMetaverse; | ||
38 | |||
39 | namespace OpenSim.Services.AssetService | ||
40 | { | ||
41 | /// <summary> | ||
42 | /// This will be developed into a de-duplicating asset service. | ||
43 | /// XXX: Currently it's a just a copy of the existing AssetService. so please don't attempt to use it. | ||
44 | /// </summary> | ||
45 | public class XAssetService : AssetServiceBase, IAssetService | ||
46 | { | ||
47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | |||
49 | protected static XAssetService m_RootInstance; | ||
50 | |||
51 | public XAssetService(IConfigSource config) : base(config) | ||
52 | { | ||
53 | if (m_RootInstance == null) | ||
54 | { | ||
55 | m_RootInstance = this; | ||
56 | |||
57 | if (m_AssetLoader != null) | ||
58 | { | ||
59 | IConfig assetConfig = config.Configs["AssetService"]; | ||
60 | if (assetConfig == null) | ||
61 | throw new Exception("No AssetService configuration"); | ||
62 | |||
63 | string loaderArgs = assetConfig.GetString("AssetLoaderArgs", | ||
64 | String.Empty); | ||
65 | |||
66 | bool assetLoaderEnabled = assetConfig.GetBoolean("AssetLoaderEnabled", true); | ||
67 | |||
68 | if (assetLoaderEnabled) | ||
69 | { | ||
70 | m_log.DebugFormat("[XASSET SERVICE]: Loading default asset set from {0}", loaderArgs); | ||
71 | |||
72 | m_AssetLoader.ForEachDefaultXmlAsset( | ||
73 | loaderArgs, | ||
74 | delegate(AssetBase a) | ||
75 | { | ||
76 | AssetBase existingAsset = Get(a.ID); | ||
77 | // AssetMetadata existingMetadata = GetMetadata(a.ID); | ||
78 | |||
79 | if (existingAsset == null || Util.SHA1Hash(existingAsset.Data) != Util.SHA1Hash(a.Data)) | ||
80 | { | ||
81 | // m_log.DebugFormat("[ASSET]: Storing {0} {1}", a.Name, a.ID); | ||
82 | Store(a); | ||
83 | } | ||
84 | }); | ||
85 | } | ||
86 | |||
87 | m_log.Debug("[XASSET SERVICE]: Local asset service enabled"); | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | |||
92 | public virtual AssetBase Get(string id) | ||
93 | { | ||
94 | // m_log.DebugFormat("[ASSET SERVICE]: Get asset for {0}", id); | ||
95 | |||
96 | UUID assetID; | ||
97 | |||
98 | if (!UUID.TryParse(id, out assetID)) | ||
99 | { | ||
100 | m_log.WarnFormat("[XASSET SERVICE]: Could not parse requested asset id {0}", id); | ||
101 | return null; | ||
102 | } | ||
103 | |||
104 | try | ||
105 | { | ||
106 | return m_Database.GetAsset(assetID); | ||
107 | } | ||
108 | catch (Exception e) | ||
109 | { | ||
110 | m_log.ErrorFormat("[XASSET SERVICE]: Exception getting asset {0} {1}", assetID, e); | ||
111 | return null; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | public virtual AssetBase GetCached(string id) | ||
116 | { | ||
117 | return Get(id); | ||
118 | } | ||
119 | |||
120 | public virtual AssetMetadata GetMetadata(string id) | ||
121 | { | ||
122 | // m_log.DebugFormat("[XASSET SERVICE]: Get asset metadata for {0}", id); | ||
123 | |||
124 | UUID assetID; | ||
125 | |||
126 | if (!UUID.TryParse(id, out assetID)) | ||
127 | return null; | ||
128 | |||
129 | AssetBase asset = m_Database.GetAsset(assetID); | ||
130 | if (asset != null) | ||
131 | return asset.Metadata; | ||
132 | |||
133 | return null; | ||
134 | } | ||
135 | |||
136 | public virtual byte[] GetData(string id) | ||
137 | { | ||
138 | // m_log.DebugFormat("[XASSET SERVICE]: Get asset data for {0}", id); | ||
139 | |||
140 | UUID assetID; | ||
141 | |||
142 | if (!UUID.TryParse(id, out assetID)) | ||
143 | return null; | ||
144 | |||
145 | AssetBase asset = m_Database.GetAsset(assetID); | ||
146 | return asset.Data; | ||
147 | } | ||
148 | |||
149 | public virtual bool Get(string id, Object sender, AssetRetrieved handler) | ||
150 | { | ||
151 | //m_log.DebugFormat("[XASSET SERVICE]: Get asset async {0}", id); | ||
152 | |||
153 | UUID assetID; | ||
154 | |||
155 | if (!UUID.TryParse(id, out assetID)) | ||
156 | return false; | ||
157 | |||
158 | AssetBase asset = m_Database.GetAsset(assetID); | ||
159 | |||
160 | //m_log.DebugFormat("[XASSET SERVICE]: Got asset {0}", asset); | ||
161 | |||
162 | handler(id, sender, asset); | ||
163 | |||
164 | return true; | ||
165 | } | ||
166 | |||
167 | public virtual string Store(AssetBase asset) | ||
168 | { | ||
169 | if (!m_Database.ExistsAsset(asset.FullID)) | ||
170 | { | ||
171 | // m_log.DebugFormat( | ||
172 | // "[XASSET SERVICE]: Storing asset {0} {1}, bytes {2}", asset.Name, asset.FullID, asset.Data.Length); | ||
173 | m_Database.StoreAsset(asset); | ||
174 | } | ||
175 | // else | ||
176 | // { | ||
177 | // m_log.DebugFormat( | ||
178 | // "[XASSET SERVICE]: Not storing asset {0} {1}, bytes {2} as it already exists", asset.Name, asset.FullID, asset.Data.Length); | ||
179 | // } | ||
180 | |||
181 | return asset.ID; | ||
182 | } | ||
183 | |||
184 | public bool UpdateContent(string id, byte[] data) | ||
185 | { | ||
186 | return false; | ||
187 | } | ||
188 | |||
189 | public virtual bool Delete(string id) | ||
190 | { | ||
191 | m_log.DebugFormat("[XASSET SERVICE]: Deleting asset {0}", id); | ||
192 | UUID assetID; | ||
193 | if (!UUID.TryParse(id, out assetID)) | ||
194 | return false; | ||
195 | |||
196 | AssetBase asset = m_Database.GetAsset(assetID); | ||
197 | if (asset == null) | ||
198 | return false; | ||
199 | |||
200 | if ((int)(asset.Flags & AssetFlags.Maptile) != 0) | ||
201 | { | ||
202 | return m_Database.Delete(id); | ||
203 | } | ||
204 | else | ||
205 | { | ||
206 | m_log.DebugFormat("[XASSET SERVICE]: Request to delete asset {0}, but flags are not Maptile", id); | ||
207 | } | ||
208 | |||
209 | return false; | ||
210 | } | ||
211 | } | ||
212 | } | ||
213 | |||