InventoryManager.cs 8.3 KB

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