LocalAssetServiceConnector.cs 10 KB

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