InventoryManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 OpenSim 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 System;
  28. using System.IO;
  29. using System.Reflection;
  30. using System.Text;
  31. using System.Xml;
  32. using System.Xml.Serialization;
  33. using libsecondlife;
  34. using log4net;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers;
  37. namespace OpenSim.Grid.InventoryServer
  38. {
  39. public class InventoryManager
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private IInventoryData _databasePlugin;
  43. /// <summary>
  44. /// Adds a new inventory server plugin - user servers will be requested in the order they were loaded.
  45. /// </summary>
  46. /// <param name="FileName">The filename to the inventory server plugin DLL</param>
  47. public void AddDatabasePlugin(string FileName, string dbconnect)
  48. {
  49. m_log.Info("[" + OpenInventory_Main.LogName + "]: Invenstorage: Attempting to load " + FileName);
  50. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  51. m_log.Info("[" + OpenInventory_Main.LogName + "]: " +
  52. "Invenstorage: Found " + pluginAssembly.GetTypes().Length + " interfaces.");
  53. foreach (Type pluginType in pluginAssembly.GetTypes())
  54. {
  55. if (!pluginType.IsAbstract)
  56. {
  57. Type typeInterface = pluginType.GetInterface("IInventoryData", true);
  58. if (typeInterface != null)
  59. {
  60. IInventoryData plug =
  61. (IInventoryData) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  62. plug.Initialise(dbconnect);
  63. _databasePlugin = plug;
  64. m_log.Info("[" + OpenInventory_Main.LogName + "]: " +
  65. "Invenstorage: Added IInventoryData Interface");
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. protected static SerializableInventory loadInventoryFromXmlFile(string fileName)
  72. {
  73. FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  74. XmlReader reader = new XmlTextReader(fs);
  75. XmlSerializer x = new XmlSerializer(typeof (SerializableInventory));
  76. SerializableInventory inventory = (SerializableInventory) x.Deserialize(reader);
  77. fs.Close();
  78. fs.Dispose();
  79. return inventory;
  80. }
  81. protected static void saveInventoryToStream(SerializableInventory inventory, Stream s)
  82. {
  83. XmlTextWriter writer = new XmlTextWriter(s, Encoding.UTF8);
  84. writer.Formatting = Formatting.Indented;
  85. XmlSerializer x = new XmlSerializer(typeof (SerializableInventory));
  86. x.Serialize(writer, inventory);
  87. }
  88. protected static bool fixupFolder(SerializableInventory.SerializableFolder f,
  89. SerializableInventory.SerializableFolder parent)
  90. {
  91. bool modified = false;
  92. // ensure we have a valid folder id
  93. if (f.ID == LLUUID.Zero)
  94. {
  95. f.ID = LLUUID.Random();
  96. modified = true;
  97. }
  98. // ensure we have valid agent id
  99. if (f.Owner == LLUUID.Zero)
  100. {
  101. if (parent != null)
  102. f.Owner = parent.Owner;
  103. else
  104. f.Owner = f.ID;
  105. modified = true;
  106. }
  107. if (f.ParentID == LLUUID.Zero && parent != null)
  108. {
  109. f.ParentID = parent.ID;
  110. modified = true;
  111. }
  112. foreach (SerializableInventory.SerializableFolder child in f.SubFolders)
  113. {
  114. modified |= fixupFolder(child, f);
  115. }
  116. return modified;
  117. }
  118. protected static bool fixupInventory(SerializableInventory inventory)
  119. {
  120. return fixupFolder(inventory.root, null);
  121. }
  122. public class GetInventory : BaseStreamHandler
  123. {
  124. private SerializableInventory _inventory;
  125. private InventoryManager _manager;
  126. public GetInventory(InventoryManager manager)
  127. : base("GET", "/inventory")
  128. {
  129. _manager = manager;
  130. _inventory = loadInventoryFromXmlFile("attic/inventory/Inventory_Library.xml");
  131. if (fixupInventory(_inventory))
  132. {
  133. FileStream fs = new FileStream("attic/inventory/Inventory_Library.xml", FileMode.Truncate, FileAccess.Write);
  134. saveInventoryToStream(_inventory, fs);
  135. fs.Flush();
  136. fs.Close();
  137. m_log.Debug("[" + OpenInventory_Main.LogName + "]: Modified");
  138. }
  139. }
  140. private void CreateDefaultInventory(LLUUID userID)
  141. {
  142. }
  143. private byte[] GetUserInventory(LLUUID userID)
  144. {
  145. m_log.InfoFormat("[" + OpenInventory_Main.LogName + "]: Getting Inventory for user {0}", userID.ToString());
  146. byte[] result = new byte[] {};
  147. InventoryFolderBase fb = _manager._databasePlugin.getUserRootFolder(userID);
  148. if (fb == null)
  149. {
  150. m_log.InfoFormat("[" + OpenInventory_Main.LogName + "]: Inventory not found for user {0}, creating new",
  151. userID.ToString());
  152. CreateDefaultInventory(userID);
  153. }
  154. return result;
  155. }
  156. public override byte[] Handle(string path, Stream request)
  157. {
  158. byte[] result = new byte[] {};
  159. string[] parms = path.Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
  160. if (parms.Length > 1)
  161. {
  162. if (string.Compare(parms[1], "library", true) == 0)
  163. {
  164. MemoryStream ms = new MemoryStream();
  165. saveInventoryToStream(_inventory, ms);
  166. result = ms.GetBuffer();
  167. Array.Resize<byte>(ref result, (int) ms.Length);
  168. }
  169. else if (string.Compare(parms[1], "user", true) == 0)
  170. {
  171. if (parms.Length > 2)
  172. {
  173. result = GetUserInventory(new LLUUID(parms[2]));
  174. }
  175. }
  176. }
  177. return result;
  178. }
  179. }
  180. }
  181. }