XAssetService.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using Nini.Config;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Data;
  35. using OpenSim.Services.Interfaces;
  36. using OpenMetaverse;
  37. namespace OpenSim.Services.AssetService
  38. {
  39. /// <summary>
  40. /// A de-duplicating asset service.
  41. /// </summary>
  42. [Obsolete]
  43. public class XAssetService : XAssetServiceBase, IAssetService
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. protected static XAssetService m_RootInstance;
  47. public XAssetService(IConfigSource config) : this(config, "AssetService") {}
  48. public XAssetService(IConfigSource config, string configName) : base(config, configName)
  49. {
  50. if (m_RootInstance == null)
  51. {
  52. m_RootInstance = this;
  53. if (m_AssetLoader != null)
  54. {
  55. IConfig assetConfig = config.Configs[configName];
  56. if (assetConfig == null)
  57. throw new Exception("No AssetService configuration");
  58. string loaderArgs = assetConfig.GetString("AssetLoaderArgs", String.Empty);
  59. bool assetLoaderEnabled = assetConfig.GetBoolean("AssetLoaderEnabled", true);
  60. if (assetLoaderEnabled && !HasChainedAssetService)
  61. {
  62. m_log.DebugFormat("[XASSET SERVICE]: Loading default asset set from {0}", loaderArgs);
  63. m_AssetLoader.ForEachDefaultXmlAsset(
  64. loaderArgs,
  65. a =>
  66. {
  67. AssetBase existingAsset = Get(a.ID);
  68. // AssetMetadata existingMetadata = GetMetadata(a.ID);
  69. if (existingAsset == null || Util.SHA1Hash(existingAsset.Data) != Util.SHA1Hash(a.Data))
  70. {
  71. // m_log.DebugFormat("[ASSET]: Storing {0} {1}", a.Name, a.ID);
  72. m_Database.StoreAsset(a);
  73. }
  74. });
  75. }
  76. m_log.Debug("[XASSET SERVICE]: Local asset service enabled");
  77. }
  78. }
  79. }
  80. public virtual AssetBase Get(string id)
  81. {
  82. // m_log.DebugFormat("[ASSET SERVICE]: Get asset for {0}", id);
  83. UUID assetID;
  84. if (!UUID.TryParse(id, out assetID))
  85. {
  86. m_log.WarnFormat("[XASSET SERVICE]: Could not parse requested asset id {0}", id);
  87. return null;
  88. }
  89. try
  90. {
  91. AssetBase asset = m_Database.GetAsset(assetID);
  92. if (asset != null)
  93. {
  94. return asset;
  95. }
  96. else if (HasChainedAssetService)
  97. {
  98. asset = m_ChainedAssetService.Get(id);
  99. if (asset != null)
  100. MigrateFromChainedService(asset);
  101. return asset;
  102. }
  103. return null;
  104. }
  105. catch (Exception e)
  106. {
  107. m_log.ErrorFormat("[XASSET SERVICE]: Exception getting asset {0} {1}", assetID, e);
  108. return null;
  109. }
  110. }
  111. public AssetBase Get(string id, string ForeignAssetService, bool dummy)
  112. {
  113. return null;
  114. }
  115. public virtual AssetBase GetCached(string id)
  116. {
  117. return Get(id);
  118. }
  119. public virtual AssetMetadata GetMetadata(string id)
  120. {
  121. // m_log.DebugFormat("[XASSET SERVICE]: Get asset metadata for {0}", id);
  122. AssetBase asset = Get(id);
  123. if (asset != null)
  124. return asset.Metadata;
  125. else
  126. return null;
  127. }
  128. public virtual byte[] GetData(string id)
  129. {
  130. // m_log.DebugFormat("[XASSET SERVICE]: Get asset data for {0}", id);
  131. AssetBase asset = Get(id);
  132. if (asset != null)
  133. return asset.Data;
  134. else
  135. return null;
  136. }
  137. public virtual bool Get(string id, Object sender, AssetRetrieved handler)
  138. {
  139. //m_log.DebugFormat("[XASSET SERVICE]: Get asset async {0}", id);
  140. if (!UUID.TryParse(id, out UUID assetID) || assetID.IsZero())
  141. return false;
  142. AssetBase asset = Get(id);
  143. //m_log.DebugFormat("[XASSET SERVICE]: Got asset {0}", asset);
  144. handler(id, sender, asset);
  145. return true;
  146. }
  147. public virtual bool[] AssetsExist(string[] ids)
  148. {
  149. UUID[] uuid = Array.ConvertAll(ids, id => UUID.Parse(id));
  150. return m_Database.AssetsExist(uuid);
  151. }
  152. public virtual string Store(AssetBase asset)
  153. {
  154. bool exists = m_Database.AssetsExist(new[] { asset.FullID })[0];
  155. if (!exists)
  156. {
  157. // m_log.DebugFormat(
  158. // "[XASSET SERVICE]: Storing asset {0} {1}, bytes {2}", asset.Name, asset.FullID, asset.Data.Length);
  159. m_Database.StoreAsset(asset);
  160. }
  161. // else
  162. // {
  163. // m_log.DebugFormat(
  164. // "[XASSET SERVICE]: Not storing asset {0} {1}, bytes {2} as it already exists", asset.Name, asset.FullID, asset.Data.Length);
  165. // }
  166. return asset.ID;
  167. }
  168. public bool UpdateContent(string id, byte[] data)
  169. {
  170. return false;
  171. }
  172. public virtual bool Delete(string id)
  173. {
  174. // m_log.DebugFormat("[XASSET SERVICE]: Deleting asset {0}", id);
  175. UUID assetID;
  176. if (!UUID.TryParse(id, out assetID))
  177. return false;
  178. if (HasChainedAssetService)
  179. m_ChainedAssetService.Delete(id);
  180. return m_Database.Delete(id);
  181. }
  182. private void MigrateFromChainedService(AssetBase asset)
  183. {
  184. Store(asset);
  185. m_ChainedAssetService.Delete(asset.ID);
  186. }
  187. public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
  188. {
  189. }
  190. }
  191. }