HGAssetServiceConnector.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. namespace OpenSim.Services.Connectors
  35. {
  36. public class HGAssetServiceConnector : IAssetService
  37. {
  38. private static readonly ILog m_log =
  39. LogManager.GetLogger(
  40. MethodBase.GetCurrentMethod().DeclaringType);
  41. private Dictionary<string, AssetServicesConnector> m_connectors = new Dictionary<string, AssetServicesConnector>();
  42. public HGAssetServiceConnector(IConfigSource source)
  43. {
  44. IConfig moduleConfig = source.Configs["Modules"];
  45. if (moduleConfig != null)
  46. {
  47. // string name = moduleConfig.GetString("AssetServices", "");
  48. IConfig assetConfig = source.Configs["AssetService"];
  49. if (assetConfig == null)
  50. {
  51. m_log.Error("[HG ASSET SERVICE]: AssetService missing from OpenSim.ini");
  52. return;
  53. }
  54. m_log.Info("[HG ASSET SERVICE]: HG asset service enabled");
  55. }
  56. }
  57. private bool StringToUrlAndAssetID(string id, out string url, out string assetID)
  58. {
  59. url = String.Empty;
  60. assetID = String.Empty;
  61. Uri assetUri;
  62. if (Uri.TryCreate(id, UriKind.Absolute, out assetUri) &&
  63. assetUri.Scheme == Uri.UriSchemeHttp)
  64. {
  65. url = "http://" + assetUri.Authority;
  66. assetID = assetUri.LocalPath.Trim(new char[] {'/'});
  67. return true;
  68. }
  69. return false;
  70. }
  71. private IAssetService GetConnector(string url)
  72. {
  73. AssetServicesConnector connector = null;
  74. lock (m_connectors)
  75. {
  76. if (m_connectors.ContainsKey(url))
  77. {
  78. connector = m_connectors[url];
  79. }
  80. else
  81. {
  82. // We're instantiating this class explicitly, but this won't
  83. // work in general, because the remote grid may be running
  84. // an asset server that has a different protocol.
  85. // Eventually we will want a piece of protocol asking
  86. // the remote server about its kind. Definitely cool thing to do!
  87. connector = new AssetServicesConnector(url);
  88. m_connectors.Add(url, connector);
  89. }
  90. }
  91. return connector;
  92. }
  93. public AssetBase Get(string id)
  94. {
  95. string url = string.Empty;
  96. string assetID = string.Empty;
  97. if (StringToUrlAndAssetID(id, out url, out assetID))
  98. {
  99. IAssetService connector = GetConnector(url);
  100. return connector.Get(assetID);
  101. }
  102. return null;
  103. }
  104. public AssetBase GetCached(string id)
  105. {
  106. string url = string.Empty;
  107. string assetID = string.Empty;
  108. if (StringToUrlAndAssetID(id, out url, out assetID))
  109. {
  110. IAssetService connector = GetConnector(url);
  111. return connector.GetCached(assetID);
  112. }
  113. return null;
  114. }
  115. public AssetMetadata GetMetadata(string id)
  116. {
  117. string url = string.Empty;
  118. string assetID = string.Empty;
  119. if (StringToUrlAndAssetID(id, out url, out assetID))
  120. {
  121. IAssetService connector = GetConnector(url);
  122. return connector.GetMetadata(assetID);
  123. }
  124. return null;
  125. }
  126. public byte[] GetData(string id)
  127. {
  128. return null;
  129. }
  130. public bool Get(string id, Object sender, AssetRetrieved handler)
  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, sender, handler);
  138. }
  139. return false;
  140. }
  141. public string Store(AssetBase asset)
  142. {
  143. string url = string.Empty;
  144. string assetID = string.Empty;
  145. if (StringToUrlAndAssetID(asset.ID, out url, out assetID))
  146. {
  147. IAssetService connector = GetConnector(url);
  148. // Restore the assetID to a simple UUID
  149. asset.ID = assetID;
  150. return connector.Store(asset);
  151. }
  152. return String.Empty;
  153. }
  154. public bool UpdateContent(string id, byte[] data)
  155. {
  156. return false;
  157. }
  158. public bool Delete(string id)
  159. {
  160. return false;
  161. }
  162. }
  163. }