HGAssetServiceConnector.cs 6.6 KB

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