AssetService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. public class AssetService : AssetServiceBase, IAssetService
  40. {
  41. private static readonly ILog m_log =
  42. LogManager.GetLogger(
  43. MethodBase.GetCurrentMethod().DeclaringType);
  44. protected static AssetService m_RootInstance;
  45. public AssetService(IConfigSource config)
  46. : this(config, "AssetService")
  47. {
  48. }
  49. public AssetService(IConfigSource config, string configName) : base(config, configName)
  50. {
  51. if (m_RootInstance == null)
  52. {
  53. m_RootInstance = this;
  54. if (m_AssetLoader != null)
  55. {
  56. IConfig assetConfig = config.Configs[m_ConfigName];
  57. if (assetConfig == null)
  58. throw new Exception("No " + m_ConfigName + " configuration");
  59. string loaderArgs = assetConfig.GetString("AssetLoaderArgs",
  60. String.Empty);
  61. bool assetLoaderEnabled = assetConfig.GetBoolean("AssetLoaderEnabled", true);
  62. if (assetLoaderEnabled)
  63. {
  64. m_log.DebugFormat("[ASSET SERVICE]: Loading default asset set from {0}", loaderArgs);
  65. m_AssetLoader.ForEachDefaultXmlAsset(
  66. loaderArgs,
  67. delegate(AssetBase a)
  68. {
  69. AssetBase existingAsset = Get(a.ID);
  70. // AssetMetadata existingMetadata = GetMetadata(a.ID);
  71. if (existingAsset == null || Util.SHA1Hash(existingAsset.Data) != Util.SHA1Hash(a.Data))
  72. {
  73. // m_log.DebugFormat("[ASSET]: Storing {0} {1}", a.Name, a.ID);
  74. Store(a);
  75. }
  76. });
  77. }
  78. m_log.Debug("[ASSET SERVICE]: Local asset service enabled");
  79. }
  80. }
  81. }
  82. public virtual AssetBase Get(string id)
  83. {
  84. // m_log.DebugFormat("[ASSET SERVICE]: Get asset for {0}", id);
  85. UUID assetID;
  86. if (!UUID.TryParse(id, out assetID))
  87. {
  88. m_log.WarnFormat("[ASSET SERVICE]: Could not parse requested asset id {0}", id);
  89. return null;
  90. }
  91. try
  92. {
  93. return m_Database.GetAsset(assetID);
  94. }
  95. catch (Exception e)
  96. {
  97. m_log.ErrorFormat("[ASSET SERVICE]: Exception getting asset {0} {1}", assetID, e);
  98. return null;
  99. }
  100. }
  101. public virtual AssetBase GetCached(string id)
  102. {
  103. return Get(id);
  104. }
  105. public virtual AssetMetadata GetMetadata(string id)
  106. {
  107. // m_log.DebugFormat("[ASSET SERVICE]: Get asset metadata for {0}", id);
  108. UUID assetID;
  109. if (!UUID.TryParse(id, out assetID))
  110. return null;
  111. AssetBase asset = m_Database.GetAsset(assetID);
  112. if (asset != null)
  113. return asset.Metadata;
  114. return null;
  115. }
  116. public virtual byte[] GetData(string id)
  117. {
  118. // m_log.DebugFormat("[ASSET SERVICE]: Get asset data for {0}", id);
  119. UUID assetID;
  120. if (!UUID.TryParse(id, out assetID))
  121. return null;
  122. AssetBase asset = m_Database.GetAsset(assetID);
  123. return asset.Data;
  124. }
  125. public virtual bool Get(string id, Object sender, AssetRetrieved handler)
  126. {
  127. //m_log.DebugFormat("[AssetService]: Get asset async {0}", id);
  128. UUID assetID;
  129. if (!UUID.TryParse(id, out assetID))
  130. return false;
  131. AssetBase asset = m_Database.GetAsset(assetID);
  132. //m_log.DebugFormat("[AssetService]: Got asset {0}", asset);
  133. handler(id, sender, asset);
  134. return true;
  135. }
  136. public virtual string Store(AssetBase asset)
  137. {
  138. if (!m_Database.ExistsAsset(asset.FullID))
  139. {
  140. // m_log.DebugFormat(
  141. // "[ASSET SERVICE]: Storing asset {0} {1}, bytes {2}", asset.Name, asset.FullID, asset.Data.Length);
  142. m_Database.StoreAsset(asset);
  143. }
  144. // else
  145. // {
  146. // m_log.DebugFormat(
  147. // "[ASSET SERVICE]: Not storing asset {0} {1}, bytes {2} as it already exists", asset.Name, asset.FullID, asset.Data.Length);
  148. // }
  149. return asset.ID;
  150. }
  151. public bool UpdateContent(string id, byte[] data)
  152. {
  153. return false;
  154. }
  155. public virtual bool Delete(string id)
  156. {
  157. // m_log.DebugFormat("[ASSET SERVICE]: Deleting asset {0}", id);
  158. UUID assetID;
  159. if (!UUID.TryParse(id, out assetID))
  160. return false;
  161. return m_Database.Delete(id);
  162. }
  163. }
  164. }