LocalAssetServiceConnector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 IAssetCache 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<IAssetCache>();
  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. {
  138. if (!m_Cache.Get(id, out asset))
  139. return null;
  140. }
  141. if (asset == null)
  142. {
  143. asset = m_AssetService.Get(id);
  144. if ((m_Cache != null) && (asset != null))
  145. m_Cache.Cache(asset);
  146. // if (null == asset)
  147. // m_log.WarnFormat("[LOCAL ASSET SERVICES CONNECTOR]: Could not synchronously find asset with id {0}", id);
  148. }
  149. return asset;
  150. }
  151. public AssetBase GetCached(string id)
  152. {
  153. // m_log.DebugFormat("[LOCAL ASSET SERVICES CONNECTOR]: Cache request for {0}", id);
  154. AssetBase asset = null;
  155. if (m_Cache != null)
  156. m_Cache.Get(id, out asset);
  157. return asset;
  158. }
  159. public AssetMetadata GetMetadata(string id)
  160. {
  161. AssetBase asset = null;
  162. if (m_Cache != null)
  163. {
  164. if (!m_Cache.Get(id, out asset))
  165. return null;
  166. }
  167. if (asset != null)
  168. return asset.Metadata;
  169. asset = m_AssetService.Get(id);
  170. if (asset != null)
  171. {
  172. if (m_Cache != null)
  173. m_Cache.Cache(asset);
  174. return asset.Metadata;
  175. }
  176. return null;
  177. }
  178. public byte[] GetData(string id)
  179. {
  180. // m_log.DebugFormat("[LOCAL ASSET SERVICES CONNECTOR]: Requesting data for asset {0}", id);
  181. AssetBase asset = null;
  182. if (m_Cache != null)
  183. {
  184. if (!m_Cache.Get(id, out asset))
  185. return null;
  186. }
  187. if (asset != null)
  188. return asset.Data;
  189. asset = m_AssetService.Get(id);
  190. if (asset != null)
  191. {
  192. if (m_Cache != null)
  193. m_Cache.Cache(asset);
  194. return asset.Data;
  195. }
  196. return null;
  197. }
  198. public bool Get(string id, Object sender, AssetRetrieved handler)
  199. {
  200. // m_log.DebugFormat("[LOCAL ASSET SERVICES CONNECTOR]: Asynchronously requesting asset {0}", id);
  201. if (m_Cache != null)
  202. {
  203. AssetBase asset;
  204. if (!m_Cache.Get(id, out asset))
  205. return false;
  206. if (asset != null)
  207. {
  208. Util.FireAndForget(
  209. o => handler(id, sender, asset), null, "LocalAssetServiceConnector.GotFromCacheCallback");
  210. return true;
  211. }
  212. }
  213. return m_AssetService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
  214. {
  215. if ((a != null) && (m_Cache != null))
  216. m_Cache.Cache(a);
  217. // if (null == a)
  218. // m_log.WarnFormat("[LOCAL ASSET SERVICES CONNECTOR]: Could not asynchronously find asset with id {0}", id);
  219. Util.FireAndForget(
  220. o => handler(assetID, s, a), null, "LocalAssetServiceConnector.GotFromServiceCallback");
  221. });
  222. }
  223. public bool[] AssetsExist(string[] ids)
  224. {
  225. return m_AssetService.AssetsExist(ids);
  226. }
  227. public string Store(AssetBase asset)
  228. {
  229. if (m_Cache != null)
  230. m_Cache.Cache(asset);
  231. if (asset.Local)
  232. {
  233. // m_log.DebugFormat(
  234. // "[LOCAL ASSET SERVICE CONNECTOR]: Returning asset {0} {1} without querying database since status Temporary = {2}, Local = {3}",
  235. // asset.Name, asset.ID, asset.Temporary, asset.Local);
  236. return asset.ID;
  237. }
  238. else
  239. {
  240. // m_log.DebugFormat(
  241. // "[LOCAL ASSET SERVICE CONNECTOR]: Passing {0} {1} on to asset service for storage, status Temporary = {2}, Local = {3}",
  242. // asset.Name, asset.ID, asset.Temporary, asset.Local);
  243. return m_AssetService.Store(asset);
  244. }
  245. }
  246. public bool UpdateContent(string id, byte[] data)
  247. {
  248. AssetBase asset = null;
  249. if (m_Cache != null)
  250. m_Cache.Get(id, out asset);
  251. if (asset != null)
  252. {
  253. asset.Data = data;
  254. if (m_Cache != null)
  255. m_Cache.Cache(asset);
  256. }
  257. return m_AssetService.UpdateContent(id, data);
  258. }
  259. public bool Delete(string id)
  260. {
  261. if (m_Cache != null)
  262. m_Cache.Expire(id);
  263. return m_AssetService.Delete(id);
  264. }
  265. }
  266. }