LocalAssetServiceConnector.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 log4net;
  28. using Nini.Config;
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Reflection;
  32. using OpenSim.Framework;
  33. using OpenSim.Server.Base;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Services.Interfaces;
  37. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
  38. {
  39. public class LocalAssetServicesConnector : ISharedRegionModule, IAssetService
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private IImprovedAssetCache m_Cache = null;
  43. private IAssetService m_AssetService;
  44. private bool m_Enabled = false;
  45. public Type ReplaceableInterface
  46. {
  47. get { return null; }
  48. }
  49. public string Name
  50. {
  51. get { return "LocalAssetServicesConnector"; }
  52. }
  53. public void Initialise(IConfigSource source)
  54. {
  55. IConfig moduleConfig = source.Configs["Modules"];
  56. if (moduleConfig != null)
  57. {
  58. string name = moduleConfig.GetString("AssetServices", "");
  59. if (name == Name)
  60. {
  61. IConfig assetConfig = source.Configs["AssetService"];
  62. if (assetConfig == null)
  63. {
  64. m_log.Error("[LOCAL ASSET SERVICES CONNECTOR]: AssetService missing from OpenSim.ini");
  65. return;
  66. }
  67. string serviceDll = assetConfig.GetString("LocalServiceModule",
  68. String.Empty);
  69. if (serviceDll == String.Empty)
  70. {
  71. m_log.Error("[LOCAL ASSET SERVICES CONNECTOR]: No LocalServiceModule named in section AssetService");
  72. return;
  73. }
  74. Object[] args = new Object[] { source };
  75. m_AssetService = ServerUtils.LoadPlugin<IAssetService>(serviceDll, args);
  76. if (m_AssetService == null)
  77. {
  78. m_log.Error("[LOCAL ASSET SERVICES CONNECTOR]: Can't load asset service");
  79. return;
  80. }
  81. m_Enabled = true;
  82. m_log.Info("[LOCAL ASSET SERVICES CONNECTOR]: Local asset connector enabled");
  83. }
  84. }
  85. }
  86. public void PostInitialise()
  87. {
  88. }
  89. public void Close()
  90. {
  91. }
  92. public void AddRegion(Scene scene)
  93. {
  94. if (!m_Enabled)
  95. return;
  96. scene.RegisterModuleInterface<IAssetService>(this);
  97. }
  98. public void RemoveRegion(Scene scene)
  99. {
  100. }
  101. public void RegionLoaded(Scene scene)
  102. {
  103. if (!m_Enabled)
  104. return;
  105. if (m_Cache == null)
  106. {
  107. m_Cache = scene.RequestModuleInterface<IImprovedAssetCache>();
  108. if (!(m_Cache is ISharedRegionModule))
  109. m_Cache = null;
  110. }
  111. m_log.DebugFormat(
  112. "[LOCAL ASSET SERVICES CONNECTOR]: Enabled connector for region {0}", scene.RegionInfo.RegionName);
  113. if (m_Cache != null)
  114. {
  115. m_log.DebugFormat(
  116. "[LOCAL ASSET SERVICES CONNECTOR]: Enabled asset caching for region {0}",
  117. scene.RegionInfo.RegionName);
  118. }
  119. else
  120. {
  121. // Short-circuit directly to storage layer. This ends up storing temporary and local assets.
  122. //
  123. scene.UnregisterModuleInterface<IAssetService>(this);
  124. scene.RegisterModuleInterface<IAssetService>(m_AssetService);
  125. }
  126. }
  127. public AssetBase Get(string id)
  128. {
  129. // m_log.DebugFormat("[LOCAL ASSET SERVICES CONNECTOR]: Synchronously requesting asset {0}", id);
  130. AssetBase asset = null;
  131. if (m_Cache != null)
  132. asset = m_Cache.Get(id);
  133. if (asset == null)
  134. {
  135. asset = m_AssetService.Get(id);
  136. if ((m_Cache != null) && (asset != null))
  137. m_Cache.Cache(asset);
  138. // if (null == asset)
  139. // m_log.WarnFormat("[LOCAL ASSET SERVICES CONNECTOR]: Could not synchronously find asset with id {0}", id);
  140. }
  141. return asset;
  142. }
  143. public AssetBase GetCached(string id)
  144. {
  145. if (m_Cache != null)
  146. return m_Cache.Get(id);
  147. return null;
  148. }
  149. public AssetMetadata GetMetadata(string id)
  150. {
  151. AssetBase asset = null;
  152. if (m_Cache != null)
  153. asset = m_Cache.Get(id);
  154. if (asset != null)
  155. return asset.Metadata;
  156. asset = m_AssetService.Get(id);
  157. if (asset != null)
  158. {
  159. if (m_Cache != null)
  160. m_Cache.Cache(asset);
  161. return asset.Metadata;
  162. }
  163. return null;
  164. }
  165. public byte[] GetData(string id)
  166. {
  167. // m_log.DebugFormat("[LOCAL ASSET SERVICES CONNECTOR]: Requesting data for asset {0}", id);
  168. AssetBase asset = m_Cache.Get(id);
  169. if (asset != null)
  170. return asset.Data;
  171. asset = m_AssetService.Get(id);
  172. if (asset != null)
  173. {
  174. if (m_Cache != null)
  175. m_Cache.Cache(asset);
  176. return asset.Data;
  177. }
  178. return null;
  179. }
  180. public bool Get(string id, Object sender, AssetRetrieved handler)
  181. {
  182. // m_log.DebugFormat("[LOCAL ASSET SERVICES CONNECTOR]: Asynchronously requesting asset {0}", id);
  183. if (m_Cache != null)
  184. {
  185. AssetBase asset = m_Cache.Get(id);
  186. if (asset != null)
  187. {
  188. Util.FireAndForget(delegate { handler(id, sender, asset); });
  189. return true;
  190. }
  191. }
  192. return m_AssetService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
  193. {
  194. if ((a != null) && (m_Cache != null))
  195. m_Cache.Cache(a);
  196. // if (null == a)
  197. // m_log.WarnFormat("[LOCAL ASSET SERVICES CONNECTOR]: Could not asynchronously find asset with id {0}", id);
  198. Util.FireAndForget(delegate { handler(assetID, s, a); });
  199. });
  200. }
  201. public string Store(AssetBase asset)
  202. {
  203. if (m_Cache != null)
  204. m_Cache.Cache(asset);
  205. if (asset.Temporary || asset.Local)
  206. {
  207. // m_log.DebugFormat(
  208. // "[LOCAL ASSET SERVICE CONNECTOR]: Returning asset {0} {1} without querying database since status Temporary = {2}, Local = {3}",
  209. // asset.Name, asset.ID, asset.Temporary, asset.Local);
  210. return asset.ID;
  211. }
  212. else
  213. {
  214. // m_log.DebugFormat(
  215. // "[LOCAL ASSET SERVICE CONNECTOR]: Passing {0} {1} on to asset service for storage, status Temporary = {2}, Local = {3}",
  216. // asset.Name, asset.ID, asset.Temporary, asset.Local);
  217. return m_AssetService.Store(asset);
  218. }
  219. }
  220. public bool UpdateContent(string id, byte[] data)
  221. {
  222. AssetBase asset = null;
  223. if (m_Cache != null)
  224. m_Cache.Get(id);
  225. if (asset != null)
  226. {
  227. asset.Data = data;
  228. if (m_Cache != null)
  229. m_Cache.Cache(asset);
  230. }
  231. return m_AssetService.UpdateContent(id, data);
  232. }
  233. public bool Delete(string id)
  234. {
  235. if (m_Cache != null)
  236. m_Cache.Expire(id);
  237. return m_AssetService.Delete(id);
  238. }
  239. }
  240. }