LocalAssetServiceConnector.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.InfoFormat("[LOCAL ASSET SERVICES CONNECTOR]: Enabled local assets for region {0}", scene.RegionInfo.RegionName);
  112. if (m_Cache != null)
  113. {
  114. m_log.InfoFormat("[LOCAL ASSET SERVICES CONNECTOR]: Enabled asset caching for region {0}", scene.RegionInfo.RegionName);
  115. }
  116. else
  117. {
  118. // Short-circuit directly to storage layer
  119. //
  120. scene.UnregisterModuleInterface<IAssetService>(this);
  121. scene.RegisterModuleInterface<IAssetService>(m_AssetService);
  122. }
  123. }
  124. public AssetBase Get(string id)
  125. {
  126. // m_log.DebugFormat("[LOCAL ASSET SERVICES CONNECTOR]: Synchronously requesting asset {0}", id);
  127. AssetBase asset = null;
  128. if (m_Cache != null)
  129. asset = m_Cache.Get(id);
  130. if (asset == null)
  131. {
  132. asset = m_AssetService.Get(id);
  133. if ((m_Cache != null) && (asset != null))
  134. m_Cache.Cache(asset);
  135. // if (null == asset)
  136. // m_log.WarnFormat("[LOCAL ASSET SERVICES CONNECTOR]: Could not synchronously find asset with id {0}", id);
  137. }
  138. return asset;
  139. }
  140. public AssetBase GetCached(string id)
  141. {
  142. if (m_Cache != null)
  143. return m_Cache.Get(id);
  144. return null;
  145. }
  146. public AssetMetadata GetMetadata(string id)
  147. {
  148. AssetBase asset = null;
  149. if (m_Cache != null)
  150. asset = m_Cache.Get(id);
  151. if (asset != null)
  152. return asset.Metadata;
  153. asset = m_AssetService.Get(id);
  154. if (asset != null)
  155. {
  156. if (m_Cache != null)
  157. m_Cache.Cache(asset);
  158. return asset.Metadata;
  159. }
  160. return null;
  161. }
  162. public byte[] GetData(string id)
  163. {
  164. AssetBase asset = m_Cache.Get(id);
  165. if (asset != null)
  166. return asset.Data;
  167. asset = m_AssetService.Get(id);
  168. if (asset != null)
  169. {
  170. if (m_Cache != null)
  171. m_Cache.Cache(asset);
  172. return asset.Data;
  173. }
  174. return null;
  175. }
  176. public bool Get(string id, Object sender, AssetRetrieved handler)
  177. {
  178. // m_log.DebugFormat("[LOCAL ASSET SERVICES CONNECTOR]: Asynchronously requesting asset {0}", id);
  179. if (m_Cache != null)
  180. {
  181. AssetBase asset = m_Cache.Get(id);
  182. if (asset != null)
  183. {
  184. Util.FireAndForget(delegate { handler(id, sender, asset); });
  185. return true;
  186. }
  187. }
  188. return m_AssetService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
  189. {
  190. if ((a != null) && (m_Cache != null))
  191. m_Cache.Cache(a);
  192. // if (null == a)
  193. // m_log.WarnFormat("[LOCAL ASSET SERVICES CONNECTOR]: Could not asynchronously find asset with id {0}", id);
  194. Util.FireAndForget(delegate { handler(assetID, s, a); });
  195. });
  196. }
  197. public string Store(AssetBase asset)
  198. {
  199. if (m_Cache != null)
  200. m_Cache.Cache(asset);
  201. if (asset.Temporary || asset.Local)
  202. return asset.ID;
  203. return m_AssetService.Store(asset);
  204. }
  205. public bool UpdateContent(string id, byte[] data)
  206. {
  207. AssetBase asset = null;
  208. if (m_Cache != null)
  209. m_Cache.Get(id);
  210. if (asset != null)
  211. {
  212. asset.Data = data;
  213. if (m_Cache != null)
  214. m_Cache.Cache(asset);
  215. }
  216. return m_AssetService.UpdateContent(id, data);
  217. }
  218. public bool Delete(string id)
  219. {
  220. if (m_Cache != null)
  221. m_Cache.Expire(id);
  222. return m_AssetService.Delete(id);
  223. }
  224. }
  225. }