aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim
diff options
context:
space:
mode:
authorJustin Clark-Casey (justincc)2012-03-09 00:19:42 +0000
committerJustin Clark-Casey (justincc)2012-03-09 00:19:42 +0000
commit034c9d6bcc697a1b0e3702d4281235a0eb4d5d4e (patch)
treeb02db4d4281e1cb537479a98f42e99e978a9752f /OpenSim
parentMove "change region" command into general category (diff)
parentminor: move some compression related var setup inside compression if/then switch (diff)
downloadopensim-SC-034c9d6bcc697a1b0e3702d4281235a0eb4d5d4e.zip
opensim-SC-034c9d6bcc697a1b0e3702d4281235a0eb4d5d4e.tar.gz
opensim-SC-034c9d6bcc697a1b0e3702d4281235a0eb4d5d4e.tar.bz2
opensim-SC-034c9d6bcc697a1b0e3702d4281235a0eb4d5d4e.tar.xz
Merge branch 'xassetservice'
Diffstat (limited to 'OpenSim')
-rw-r--r--OpenSim/Data/MySQL/MySQLXAssetData.cs500
-rw-r--r--OpenSim/Data/MySQL/Resources/XAssetStore.migrations27
-rw-r--r--OpenSim/Region/CoreModules/ServiceConnectorsOut/Asset/LocalAssetServiceConnector.cs7
-rw-r--r--OpenSim/Services/AssetService/XAssetService.cs213
4 files changed, 745 insertions, 2 deletions
diff --git a/OpenSim/Data/MySQL/MySQLXAssetData.cs b/OpenSim/Data/MySQL/MySQLXAssetData.cs
new file mode 100644
index 0000000..95ef72a
--- /dev/null
+++ b/OpenSim/Data/MySQL/MySQLXAssetData.cs
@@ -0,0 +1,500 @@
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
28using System;
29using System.Collections.Generic;
30using System.Data;
31using System.IO;
32using System.IO.Compression;
33using System.Reflection;
34using System.Security.Cryptography;
35using System.Text;
36using log4net;
37using MySql.Data.MySqlClient;
38using OpenMetaverse;
39using OpenSim.Framework;
40using OpenSim.Data;
41
42namespace OpenSim.Data.MySQL
43{
44 public class MySQLXAssetData : AssetDataBase
45 {
46 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47
48 protected virtual Assembly Assembly
49 {
50 get { return GetType().Assembly; }
51 }
52
53 private bool m_enableCompression = false;
54 private string m_connectionString;
55 private object m_dbLock = new object();
56
57 /// <summary>
58 /// We can reuse this for all hashing since all methods are single-threaded through m_dbBLock
59 /// </summary>
60 private HashAlgorithm hasher = new SHA256CryptoServiceProvider();
61
62 #region IPlugin Members
63
64 public override string Version { get { return "1.0.0.0"; } }
65
66 /// <summary>
67 /// <para>Initialises Asset interface</para>
68 /// <para>
69 /// <list type="bullet">
70 /// <item>Loads and initialises the MySQL storage plugin.</item>
71 /// <item>Warns and uses the obsolete mysql_connection.ini if connect string is empty.</item>
72 /// <item>Check for migration</item>
73 /// </list>
74 /// </para>
75 /// </summary>
76 /// <param name="connect">connect string</param>
77 public override void Initialise(string connect)
78 {
79 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
80 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
81 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
82 m_log.ErrorFormat("[MYSQL XASSETDATA]: THIS PLUGIN IS STRICTLY EXPERIMENTAL.");
83 m_log.ErrorFormat("[MYSQL XASSETDATA]: DO NOT USE FOR ANY DATA THAT YOU DO NOT MIND LOSING.");
84 m_log.ErrorFormat("[MYSQL XASSETDATA]: DATABASE TABLES CAN CHANGE AT ANY TIME, CAUSING EXISTING DATA TO BE LOST.");
85 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
86 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
87 m_log.ErrorFormat("[MYSQL XASSETDATA]: ***********************************************************");
88
89 m_connectionString = connect;
90
91 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
92 {
93 dbcon.Open();
94 Migration m = new Migration(dbcon, Assembly, "XAssetStore");
95 m.Update();
96 }
97 }
98
99 public override void Initialise()
100 {
101 throw new NotImplementedException();
102 }
103
104 public override void Dispose() { }
105
106 /// <summary>
107 /// The name of this DB provider
108 /// </summary>
109 override public string Name
110 {
111 get { return "MySQL XAsset storage engine"; }
112 }
113
114 #endregion
115
116 #region IAssetDataPlugin Members
117
118 /// <summary>
119 /// Fetch Asset <paramref name="assetID"/> from database
120 /// </summary>
121 /// <param name="assetID">Asset UUID to fetch</param>
122 /// <returns>Return the asset</returns>
123 /// <remarks>On failure : throw an exception and attempt to reconnect to database</remarks>
124 override public AssetBase GetAsset(UUID assetID)
125 {
126// m_log.DebugFormat("[MYSQL XASSET DATA]: Looking for asset {0}", assetID);
127
128 AssetBase asset = null;
129 lock (m_dbLock)
130 {
131 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
132 {
133 dbcon.Open();
134
135 using (MySqlCommand cmd = new MySqlCommand(
136 "SELECT name, description, asset_type, local, temporary, asset_flags, creator_id, data FROM xassetsmeta JOIN xassetsdata ON xassetsmeta.hash = xassetsdata.hash WHERE id=?id",
137 dbcon))
138 {
139 cmd.Parameters.AddWithValue("?id", assetID.ToString());
140
141 try
142 {
143 using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
144 {
145 if (dbReader.Read())
146 {
147 asset = new AssetBase(assetID, (string)dbReader["name"], (sbyte)dbReader["asset_type"], dbReader["creator_id"].ToString());
148 asset.Data = (byte[])dbReader["data"];
149 asset.Description = (string)dbReader["description"];
150
151 string local = dbReader["local"].ToString();
152 if (local.Equals("1") || local.Equals("true", StringComparison.InvariantCultureIgnoreCase))
153 asset.Local = true;
154 else
155 asset.Local = false;
156
157 asset.Temporary = Convert.ToBoolean(dbReader["temporary"]);
158 asset.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]);
159
160 if (m_enableCompression)
161 {
162 using (GZipStream decompressionStream = new GZipStream(new MemoryStream(asset.Data), CompressionMode.Decompress))
163 {
164 MemoryStream outputStream = new MemoryStream();
165 WebUtil.CopyTo(decompressionStream, outputStream, int.MaxValue);
166 // int compressedLength = asset.Data.Length;
167 asset.Data = outputStream.ToArray();
168
169 // m_log.DebugFormat(
170 // "[XASSET DB]: Decompressed {0} {1} to {2} bytes from {3}",
171 // asset.ID, asset.Name, asset.Data.Length, compressedLength);
172 }
173 }
174 }
175 }
176 }
177 catch (Exception e)
178 {
179 m_log.Error("[MYSQL XASSET DATA]: MySql failure fetching asset " + assetID + ": " + e.Message);
180 }
181 }
182 }
183 }
184
185 return asset;
186 }
187
188 /// <summary>
189 /// Create an asset in database, or update it if existing.
190 /// </summary>
191 /// <param name="asset">Asset UUID to create</param>
192 /// <remarks>On failure : Throw an exception and attempt to reconnect to database</remarks>
193 override public void StoreAsset(AssetBase asset)
194 {
195 lock (m_dbLock)
196 {
197 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
198 {
199 dbcon.Open();
200
201 using (MySqlTransaction transaction = dbcon.BeginTransaction())
202 {
203 string assetName = asset.Name;
204 if (asset.Name.Length > 64)
205 {
206 assetName = asset.Name.Substring(0, 64);
207 m_log.Warn("[XASSET DB]: Name field truncated from " + asset.Name.Length + " to " + assetName.Length + " characters on add");
208 }
209
210 string assetDescription = asset.Description;
211 if (asset.Description.Length > 64)
212 {
213 assetDescription = asset.Description.Substring(0, 64);
214 m_log.Warn("[XASSET DB]: Description field truncated from " + asset.Description.Length + " to " + assetDescription.Length + " characters on add");
215 }
216
217 if (m_enableCompression)
218 {
219 MemoryStream outputStream = new MemoryStream();
220
221 using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress, false))
222 {
223 // Console.WriteLine(WebUtil.CopyTo(new MemoryStream(asset.Data), compressionStream, int.MaxValue));
224 // We have to close the compression stream in order to make sure it writes everything out to the underlying memory output stream.
225 compressionStream.Close();
226 byte[] compressedData = outputStream.ToArray();
227 asset.Data = compressedData;
228 }
229 }
230
231 byte[] hash = hasher.ComputeHash(asset.Data);
232
233// m_log.DebugFormat(
234// "[XASSET DB]: Compressed data size for {0} {1}, hash {2} is {3}",
235// asset.ID, asset.Name, hash, compressedData.Length);
236
237 try
238 {
239 using (MySqlCommand cmd =
240 new MySqlCommand(
241 "replace INTO xassetsmeta(id, hash, name, description, asset_type, local, temporary, create_time, access_time, asset_flags, creator_id)" +
242 "VALUES(?id, ?hash, ?name, ?description, ?asset_type, ?local, ?temporary, ?create_time, ?access_time, ?asset_flags, ?creator_id)",
243 dbcon))
244 {
245 // create unix epoch time
246 int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow);
247 cmd.Parameters.AddWithValue("?id", asset.ID);
248 cmd.Parameters.AddWithValue("?hash", hash);
249 cmd.Parameters.AddWithValue("?name", assetName);
250 cmd.Parameters.AddWithValue("?description", assetDescription);
251 cmd.Parameters.AddWithValue("?asset_type", asset.Type);
252 cmd.Parameters.AddWithValue("?local", asset.Local);
253 cmd.Parameters.AddWithValue("?temporary", asset.Temporary);
254 cmd.Parameters.AddWithValue("?create_time", now);
255 cmd.Parameters.AddWithValue("?access_time", now);
256 cmd.Parameters.AddWithValue("?creator_id", asset.Metadata.CreatorID);
257 cmd.Parameters.AddWithValue("?asset_flags", (int)asset.Flags);
258 cmd.ExecuteNonQuery();
259 }
260 }
261 catch (Exception e)
262 {
263 m_log.ErrorFormat("[ASSET DB]: MySQL failure creating asset metadata {0} with name \"{1}\". Error: {2}",
264 asset.FullID, asset.Name, e.Message);
265
266 transaction.Rollback();
267
268 return;
269 }
270
271 if (!ExistsData(dbcon, transaction, hash))
272 {
273 try
274 {
275 using (MySqlCommand cmd =
276 new MySqlCommand(
277 "INSERT INTO xassetsdata(hash, data) VALUES(?hash, ?data)",
278 dbcon))
279 {
280 cmd.Parameters.AddWithValue("?hash", hash);
281 cmd.Parameters.AddWithValue("?data", asset.Data);
282 cmd.ExecuteNonQuery();
283 }
284 }
285 catch (Exception e)
286 {
287 m_log.ErrorFormat("[XASSET DB]: MySQL failure creating asset data {0} with name \"{1}\". Error: {2}",
288 asset.FullID, asset.Name, e.Message);
289
290 transaction.Rollback();
291
292 return;
293 }
294 }
295
296 transaction.Commit();
297 }
298 }
299 }
300 }
301
302// private void UpdateAccessTime(AssetBase asset)
303// {
304// lock (m_dbLock)
305// {
306// using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
307// {
308// dbcon.Open();
309// MySqlCommand cmd =
310// new MySqlCommand("update assets set access_time=?access_time where id=?id",
311// dbcon);
312//
313// // need to ensure we dispose
314// try
315// {
316// using (cmd)
317// {
318// // create unix epoch time
319// int now = (int)Utils.DateTimeToUnixTime(DateTime.UtcNow);
320// cmd.Parameters.AddWithValue("?id", asset.ID);
321// cmd.Parameters.AddWithValue("?access_time", now);
322// cmd.ExecuteNonQuery();
323// cmd.Dispose();
324// }
325// }
326// catch (Exception e)
327// {
328// m_log.ErrorFormat(
329// "[ASSETS DB]: " +
330// "MySql failure updating access_time for asset {0} with name {1}" + Environment.NewLine + e.ToString()
331// + Environment.NewLine + "Attempting reconnection", asset.FullID, asset.Name);
332// }
333// }
334// }
335//
336// }
337
338 /// <summary>
339 /// We assume we already have the m_dbLock.
340 /// </summary>
341 /// TODO: need to actually use the transaction.
342 /// <param name="dbcon"></param>
343 /// <param name="transaction"></param>
344 /// <param name="hash"></param>
345 /// <returns></returns>
346 private bool ExistsData(MySqlConnection dbcon, MySqlTransaction transaction, byte[] hash)
347 {
348// m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid);
349
350 bool exists = false;
351
352 using (MySqlCommand cmd = new MySqlCommand("SELECT hash FROM xassetsdata WHERE hash=?hash", dbcon))
353 {
354 cmd.Parameters.AddWithValue("?hash", hash);
355
356 try
357 {
358 using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
359 {
360 if (dbReader.Read())
361 {
362// m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid);
363 exists = true;
364 }
365 }
366 }
367 catch (Exception e)
368 {
369 m_log.ErrorFormat(
370 "[XASSETS DB]: MySql failure in ExistsData fetching hash {0}. Exception {1}{2}",
371 hash, e.Message, e.StackTrace);
372 }
373 }
374
375 return exists;
376 }
377
378 /// <summary>
379 /// Check if the asset exists in the database
380 /// </summary>
381 /// <param name="uuid">The asset UUID</param>
382 /// <returns>true if it exists, false otherwise.</returns>
383 override public bool ExistsAsset(UUID uuid)
384 {
385// m_log.DebugFormat("[ASSETS DB]: Checking for asset {0}", uuid);
386
387 bool assetExists = false;
388
389 lock (m_dbLock)
390 {
391 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
392 {
393 dbcon.Open();
394 using (MySqlCommand cmd = new MySqlCommand("SELECT id FROM xassetsmeta WHERE id=?id", dbcon))
395 {
396 cmd.Parameters.AddWithValue("?id", uuid.ToString());
397
398 try
399 {
400 using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
401 {
402 if (dbReader.Read())
403 {
404// m_log.DebugFormat("[ASSETS DB]: Found asset {0}", uuid);
405 assetExists = true;
406 }
407 }
408 }
409 catch (Exception e)
410 {
411 m_log.ErrorFormat(
412 "[XASSETS DB]: MySql failure fetching asset {0}" + Environment.NewLine + e.ToString(), uuid);
413 }
414 }
415 }
416 }
417
418 return assetExists;
419 }
420
421 /// <summary>
422 /// Returns a list of AssetMetadata objects. The list is a subset of
423 /// the entire data set offset by <paramref name="start" /> containing
424 /// <paramref name="count" /> elements.
425 /// </summary>
426 /// <param name="start">The number of results to discard from the total data set.</param>
427 /// <param name="count">The number of rows the returned list should contain.</param>
428 /// <returns>A list of AssetMetadata objects.</returns>
429 public override List<AssetMetadata> FetchAssetMetadataSet(int start, int count)
430 {
431 List<AssetMetadata> retList = new List<AssetMetadata>(count);
432
433 lock (m_dbLock)
434 {
435 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
436 {
437 dbcon.Open();
438 MySqlCommand cmd = new MySqlCommand("SELECT name,description,asset_type,temporary,id,asset_flags,creator_id FROM xassetsmeta LIMIT ?start, ?count", dbcon);
439 cmd.Parameters.AddWithValue("?start", start);
440 cmd.Parameters.AddWithValue("?count", count);
441
442 try
443 {
444 using (MySqlDataReader dbReader = cmd.ExecuteReader())
445 {
446 while (dbReader.Read())
447 {
448 AssetMetadata metadata = new AssetMetadata();
449 metadata.Name = (string)dbReader["name"];
450 metadata.Description = (string)dbReader["description"];
451 metadata.Type = (sbyte)dbReader["asset_type"];
452 metadata.Temporary = Convert.ToBoolean(dbReader["temporary"]); // Not sure if this is correct.
453 metadata.Flags = (AssetFlags)Convert.ToInt32(dbReader["asset_flags"]);
454 metadata.FullID = DBGuid.FromDB(dbReader["id"]);
455 metadata.CreatorID = dbReader["creator_id"].ToString();
456
457 // We'll ignore this for now - it appears unused!
458// metadata.SHA1 = dbReader["hash"]);
459
460 retList.Add(metadata);
461 }
462 }
463 }
464 catch (Exception e)
465 {
466 m_log.Error("[XASSETS DB]: MySql failure fetching asset set" + Environment.NewLine + e.ToString());
467 }
468 }
469 }
470
471 return retList;
472 }
473
474 public override bool Delete(string id)
475 {
476// m_log.DebugFormat("[XASSETS DB]: Deleting asset {0}", id);
477
478 lock (m_dbLock)
479 {
480 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
481 {
482 dbcon.Open();
483
484 using (MySqlCommand cmd = new MySqlCommand("delete from xassetsmeta where id=?id", dbcon))
485 {
486 cmd.Parameters.AddWithValue("?id", id);
487 cmd.ExecuteNonQuery();
488 }
489
490 // TODO: How do we deal with data from deleted assets? Probably not easily reapable unless we
491 // keep a reference count (?)
492 }
493 }
494
495 return true;
496 }
497
498 #endregion
499 }
500} \ 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..d3cca5e
--- /dev/null
+++ b/OpenSim/Data/MySQL/Resources/XAssetStore.migrations
@@ -0,0 +1,27 @@
1# -----------------
2:VERSION 1
3
4BEGIN;
5
6CREATE TABLE `xassetsmeta` (
7 `id` char(36) NOT NULL,
8 `hash` binary(32) 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
21CREATE TABLE `xassetsdata` (
22 `hash` binary(32) NOT NULL,
23 `data` longblob NOT NULL,
24 PRIMARY KEY (`hash`)
25) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Version 1';
26
27COMMIT; \ 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
28using System;
29using System.Collections.Generic;
30using System.IO;
31using System.Reflection;
32using Nini.Config;
33using log4net;
34using OpenSim.Framework;
35using OpenSim.Data;
36using OpenSim.Services.Interfaces;
37using OpenMetaverse;
38
39namespace 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