InventoryManager.cs 8.3 KB

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