1
0

HGAssetServiceConnector.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.Collections.Specialized;
  32. using System.Reflection;
  33. using System.Web;
  34. using OpenSim.Framework;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Services.Connectors.Hypergrid;
  37. using OpenSim.Services.Connectors.SimianGrid;
  38. namespace OpenSim.Services.Connectors
  39. {
  40. public class HGAssetServiceConnector : IAssetService
  41. {
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(
  44. MethodBase.GetCurrentMethod().DeclaringType);
  45. private Dictionary<IAssetService, object> m_endpointSerializer = new Dictionary<IAssetService, object>();
  46. private object EndPointLock(IAssetService connector)
  47. {
  48. lock (m_endpointSerializer)
  49. {
  50. object eplock = null;
  51. if (! m_endpointSerializer.TryGetValue(connector, out eplock))
  52. {
  53. eplock = new object();
  54. m_endpointSerializer.Add(connector, eplock);
  55. // m_log.WarnFormat("[WEB UTIL] add a new host to end point serializer {0}",endpoint);
  56. }
  57. return eplock;
  58. }
  59. }
  60. private Dictionary<string, IAssetService> m_connectors = new Dictionary<string, IAssetService>();
  61. public HGAssetServiceConnector(IConfigSource source)
  62. {
  63. IConfig moduleConfig = source.Configs["Modules"];
  64. if (moduleConfig != null)
  65. {
  66. // string name = moduleConfig.GetString("AssetServices", "");
  67. IConfig assetConfig = source.Configs["AssetService"];
  68. if (assetConfig == null)
  69. {
  70. m_log.Error("[HG ASSET SERVICE]: AssetService missing from OpenSim.ini");
  71. return;
  72. }
  73. m_log.Info("[HG ASSET SERVICE]: HG asset service enabled");
  74. }
  75. }
  76. private bool StringToUrlAndAssetID(string id, out string url, out string assetID)
  77. {
  78. url = String.Empty;
  79. assetID = String.Empty;
  80. Uri assetUri;
  81. if (Uri.TryCreate(id, UriKind.Absolute, out assetUri) &&
  82. assetUri.Scheme == Uri.UriSchemeHttp)
  83. {
  84. // Simian
  85. if (assetUri.Query != string.Empty)
  86. {
  87. NameValueCollection qscoll = HttpUtility.ParseQueryString(assetUri.Query);
  88. assetID = qscoll["id"];
  89. if (assetID != null)
  90. url = id.Replace(assetID, ""); // Malformed again, as simian expects
  91. else
  92. url = id; // !!! best effort
  93. }
  94. else // robust
  95. {
  96. url = "http://" + assetUri.Authority;
  97. assetID = assetUri.LocalPath.Trim(new char[] { '/' });
  98. }
  99. return true;
  100. }
  101. m_log.DebugFormat("[HG ASSET SERVICE]: Malformed URL {0}", id);
  102. return false;
  103. }
  104. private IAssetService GetConnector(string url)
  105. {
  106. IAssetService connector = null;
  107. lock (m_connectors)
  108. {
  109. if (m_connectors.ContainsKey(url))
  110. {
  111. connector = m_connectors[url];
  112. }
  113. else
  114. {
  115. // Still not as flexible as I would like this to be,
  116. // but good enough for now
  117. string connectorType = new HeloServicesConnector(url).Helo();
  118. m_log.DebugFormat("[HG ASSET SERVICE]: HELO returned {0}", connectorType);
  119. if (connectorType == "opensim-simian")
  120. {
  121. connector = new SimianAssetServiceConnector(url);
  122. }
  123. else
  124. connector = new AssetServicesConnector(url);
  125. m_connectors.Add(url, connector);
  126. }
  127. }
  128. return connector;
  129. }
  130. public AssetBase Get(string id)
  131. {
  132. string url = string.Empty;
  133. string assetID = string.Empty;
  134. if (StringToUrlAndAssetID(id, out url, out assetID))
  135. {
  136. IAssetService connector = GetConnector(url);
  137. return connector.Get(assetID);
  138. }
  139. return null;
  140. }
  141. public AssetBase GetCached(string id)
  142. {
  143. string url = string.Empty;
  144. string assetID = string.Empty;
  145. if (StringToUrlAndAssetID(id, out url, out assetID))
  146. {
  147. IAssetService connector = GetConnector(url);
  148. return connector.GetCached(assetID);
  149. }
  150. return null;
  151. }
  152. public AssetMetadata GetMetadata(string id)
  153. {
  154. string url = string.Empty;
  155. string assetID = string.Empty;
  156. if (StringToUrlAndAssetID(id, out url, out assetID))
  157. {
  158. IAssetService connector = GetConnector(url);
  159. return connector.GetMetadata(assetID);
  160. }
  161. return null;
  162. }
  163. public byte[] GetData(string id)
  164. {
  165. return null;
  166. }
  167. public bool Get(string id, Object sender, AssetRetrieved handler)
  168. {
  169. string url = string.Empty;
  170. string assetID = string.Empty;
  171. if (StringToUrlAndAssetID(id, out url, out assetID))
  172. {
  173. IAssetService connector = GetConnector(url);
  174. return connector.Get(assetID, sender, handler);
  175. }
  176. return false;
  177. }
  178. public string Store(AssetBase asset)
  179. {
  180. string url = string.Empty;
  181. string assetID = string.Empty;
  182. if (StringToUrlAndAssetID(asset.ID, out url, out assetID))
  183. {
  184. IAssetService connector = GetConnector(url);
  185. // Restore the assetID to a simple UUID
  186. asset.ID = assetID;
  187. lock (EndPointLock(connector))
  188. return connector.Store(asset);
  189. }
  190. return String.Empty;
  191. }
  192. public bool UpdateContent(string id, byte[] data)
  193. {
  194. return false;
  195. }
  196. public bool Delete(string id)
  197. {
  198. return false;
  199. }
  200. }
  201. }