InventoryServiceBase.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 System.Collections.Generic;
  28. using System.Reflection;
  29. using log4net;
  30. using OpenMetaverse;
  31. using OpenSim.Data;
  32. namespace OpenSim.Framework.Communications
  33. {
  34. /// <summary>
  35. /// Abstract base class used by local and grid implementations of an inventory service.
  36. /// </summary>
  37. public abstract class InventoryServiceBase : IInterServiceInventoryServices
  38. {
  39. private static readonly ILog m_log
  40. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. protected List<IInventoryDataPlugin> m_plugins = new List<IInventoryDataPlugin>();
  42. #region Plugin methods
  43. /// <summary>
  44. /// Add a new inventory data plugin - plugins will be requested in the order they were added.
  45. /// </summary>
  46. /// <param name="plugin">The plugin that will provide data</param>
  47. public void AddPlugin(IInventoryDataPlugin plugin)
  48. {
  49. m_plugins.Add(plugin);
  50. }
  51. /// <summary>
  52. /// Adds a list of inventory data plugins, as described by `provider'
  53. /// and `connect', to `m_plugins'.
  54. /// </summary>
  55. /// <param name="provider">
  56. /// The filename of the inventory server plugin DLL.
  57. /// </param>
  58. /// <param name="connect">
  59. /// The connection string for the storage backend.
  60. /// </param>
  61. public void AddPlugin(string provider, string connect)
  62. {
  63. m_plugins.AddRange(DataPluginFactory.LoadDataPlugins<IInventoryDataPlugin>(provider, connect));
  64. }
  65. #endregion
  66. #region IInventoryServices methods
  67. public string Host
  68. {
  69. get { return "default"; }
  70. }
  71. public List<InventoryFolderBase> GetInventorySkeleton(UUID userId)
  72. {
  73. // m_log.DebugFormat("[AGENT INVENTORY]: Getting inventory skeleton for {0}", userId);
  74. InventoryFolderBase rootFolder = RequestRootFolder(userId);
  75. // Agent has no inventory structure yet.
  76. if (null == rootFolder)
  77. {
  78. return null;
  79. }
  80. List<InventoryFolderBase> userFolders = new List<InventoryFolderBase>();
  81. userFolders.Add(rootFolder);
  82. foreach (IInventoryDataPlugin plugin in m_plugins)
  83. {
  84. IList<InventoryFolderBase> folders = plugin.getFolderHierarchy(rootFolder.ID);
  85. userFolders.AddRange(folders);
  86. }
  87. // foreach (InventoryFolderBase folder in userFolders)
  88. // {
  89. // m_log.DebugFormat("[AGENT INVENTORY]: Got folder {0} {1}", folder.name, folder.folderID);
  90. // }
  91. return userFolders;
  92. }
  93. // See IInventoryServices
  94. public virtual bool HasInventoryForUser(UUID userID)
  95. {
  96. return false;
  97. }
  98. // See IInventoryServices
  99. public virtual InventoryFolderBase RequestRootFolder(UUID userID)
  100. {
  101. // Retrieve the first root folder we get from the list of plugins.
  102. foreach (IInventoryDataPlugin plugin in m_plugins)
  103. {
  104. InventoryFolderBase rootFolder = plugin.getUserRootFolder(userID);
  105. if (rootFolder != null)
  106. return rootFolder;
  107. }
  108. // Return nothing if no plugin was able to supply a root folder
  109. return null;
  110. }
  111. // See IInventoryServices
  112. public bool CreateNewUserInventory(UUID user)
  113. {
  114. InventoryFolderBase existingRootFolder = RequestRootFolder(user);
  115. if (null != existingRootFolder)
  116. {
  117. m_log.WarnFormat(
  118. "[AGENT INVENTORY]: Did not create a new inventory for user {0} since they already have "
  119. + "a root inventory folder with id {1}",
  120. user, existingRootFolder.ID);
  121. }
  122. else
  123. {
  124. UsersInventory inven = new UsersInventory();
  125. inven.CreateNewInventorySet(user);
  126. AddNewInventorySet(inven);
  127. return true;
  128. }
  129. return false;
  130. }
  131. public List<InventoryItemBase> GetActiveGestures(UUID userId)
  132. {
  133. List<InventoryItemBase> activeGestures = new List<InventoryItemBase>();
  134. foreach (IInventoryDataPlugin plugin in m_plugins)
  135. {
  136. activeGestures.AddRange(plugin.fetchActiveGestures(userId));
  137. }
  138. return activeGestures;
  139. }
  140. #endregion
  141. #region Methods used by GridInventoryService
  142. public List<InventoryFolderBase> RequestSubFolders(UUID parentFolderID)
  143. {
  144. List<InventoryFolderBase> inventoryList = new List<InventoryFolderBase>();
  145. foreach (IInventoryDataPlugin plugin in m_plugins)
  146. {
  147. inventoryList.AddRange(plugin.getInventoryFolders(parentFolderID));
  148. }
  149. return inventoryList;
  150. }
  151. public List<InventoryItemBase> RequestFolderItems(UUID folderID)
  152. {
  153. List<InventoryItemBase> itemsList = new List<InventoryItemBase>();
  154. foreach (IInventoryDataPlugin plugin in m_plugins)
  155. {
  156. itemsList.AddRange(plugin.getInventoryInFolder(folderID));
  157. }
  158. return itemsList;
  159. }
  160. #endregion
  161. // See IInventoryServices
  162. public virtual bool AddFolder(InventoryFolderBase folder)
  163. {
  164. m_log.DebugFormat(
  165. "[AGENT INVENTORY]: Adding folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  166. foreach (IInventoryDataPlugin plugin in m_plugins)
  167. {
  168. plugin.addInventoryFolder(folder);
  169. }
  170. // FIXME: Should return false on failure
  171. return true;
  172. }
  173. // See IInventoryServices
  174. public virtual bool UpdateFolder(InventoryFolderBase folder)
  175. {
  176. m_log.DebugFormat(
  177. "[AGENT INVENTORY]: Updating folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  178. foreach (IInventoryDataPlugin plugin in m_plugins)
  179. {
  180. plugin.updateInventoryFolder(folder);
  181. }
  182. // FIXME: Should return false on failure
  183. return true;
  184. }
  185. // See IInventoryServices
  186. public virtual bool MoveFolder(InventoryFolderBase folder)
  187. {
  188. m_log.DebugFormat(
  189. "[AGENT INVENTORY]: Moving folder {0} {1} to folder {2}", folder.Name, folder.ID, folder.ParentID);
  190. foreach (IInventoryDataPlugin plugin in m_plugins)
  191. {
  192. plugin.moveInventoryFolder(folder);
  193. }
  194. // FIXME: Should return false on failure
  195. return true;
  196. }
  197. // See IInventoryServices
  198. public virtual bool AddItem(InventoryItemBase item)
  199. {
  200. m_log.DebugFormat(
  201. "[AGENT INVENTORY]: Adding item {0} {1} to folder {2}", item.Name, item.ID, item.Folder);
  202. foreach (IInventoryDataPlugin plugin in m_plugins)
  203. {
  204. plugin.addInventoryItem(item);
  205. }
  206. // FIXME: Should return false on failure
  207. return true;
  208. }
  209. // See IInventoryServices
  210. public virtual bool UpdateItem(InventoryItemBase item)
  211. {
  212. m_log.InfoFormat(
  213. "[AGENT INVENTORY]: Updating item {0} {1} in folder {2}", item.Name, item.ID, item.Folder);
  214. foreach (IInventoryDataPlugin plugin in m_plugins)
  215. {
  216. plugin.updateInventoryItem(item);
  217. }
  218. // FIXME: Should return false on failure
  219. return true;
  220. }
  221. // See IInventoryServices
  222. public virtual bool DeleteItem(InventoryItemBase item)
  223. {
  224. m_log.InfoFormat(
  225. "[AGENT INVENTORY]: Deleting item {0} {1} from folder {2}", item.Name, item.ID, item.Folder);
  226. foreach (IInventoryDataPlugin plugin in m_plugins)
  227. {
  228. plugin.deleteInventoryItem(item.ID);
  229. }
  230. // FIXME: Should return false on failure
  231. return true;
  232. }
  233. public virtual InventoryItemBase QueryItem(InventoryItemBase item)
  234. {
  235. foreach (IInventoryDataPlugin plugin in m_plugins)
  236. {
  237. InventoryItemBase result = plugin.queryInventoryItem(item.ID);
  238. if (result != null)
  239. return result;
  240. }
  241. return null;
  242. }
  243. public virtual InventoryFolderBase QueryFolder(InventoryFolderBase item)
  244. {
  245. foreach (IInventoryDataPlugin plugin in m_plugins)
  246. {
  247. InventoryFolderBase result = plugin.queryInventoryFolder(item.ID);
  248. if (result != null)
  249. return result;
  250. }
  251. return null;
  252. }
  253. /// <summary>
  254. /// Purge a folder of all items items and subfolders.
  255. ///
  256. /// FIXME: Really nasty in a sense, because we have to query the database to get information we may
  257. /// already know... Needs heavy refactoring.
  258. /// </summary>
  259. /// <param name="folder"></param>
  260. public virtual bool PurgeFolder(InventoryFolderBase folder)
  261. {
  262. m_log.DebugFormat(
  263. "[AGENT INVENTORY]: Purging folder {0} {1} of its contents", folder.Name, folder.ID);
  264. List<InventoryFolderBase> subFolders = RequestSubFolders(folder.ID);
  265. foreach (InventoryFolderBase subFolder in subFolders)
  266. {
  267. // m_log.DebugFormat("[AGENT INVENTORY]: Deleting folder {0} {1}", subFolder.Name, subFolder.ID);
  268. foreach (IInventoryDataPlugin plugin in m_plugins)
  269. {
  270. plugin.deleteInventoryFolder(subFolder.ID);
  271. }
  272. }
  273. List<InventoryItemBase> items = RequestFolderItems(folder.ID);
  274. foreach (InventoryItemBase item in items)
  275. {
  276. DeleteItem(item);
  277. }
  278. // FIXME: Should return false on failure
  279. return true;
  280. }
  281. private void AddNewInventorySet(UsersInventory inventory)
  282. {
  283. foreach (InventoryFolderBase folder in inventory.Folders.Values)
  284. {
  285. AddFolder(folder);
  286. }
  287. }
  288. public InventoryItemBase GetInventoryItem(UUID itemID)
  289. {
  290. foreach (IInventoryDataPlugin plugin in m_plugins)
  291. {
  292. InventoryItemBase item = plugin.getInventoryItem(itemID);
  293. if (item != null)
  294. return item;
  295. }
  296. return null;
  297. }
  298. /// <summary>
  299. /// Used to create a new user inventory.
  300. /// </summary>
  301. private class UsersInventory
  302. {
  303. public Dictionary<UUID, InventoryFolderBase> Folders = new Dictionary<UUID, InventoryFolderBase>();
  304. public Dictionary<UUID, InventoryItemBase> Items = new Dictionary<UUID, InventoryItemBase>();
  305. public virtual void CreateNewInventorySet(UUID user)
  306. {
  307. InventoryFolderBase folder = new InventoryFolderBase();
  308. folder.ParentID = UUID.Zero;
  309. folder.Owner = user;
  310. folder.ID = UUID.Random();
  311. folder.Name = "My Inventory";
  312. folder.Type = (short)AssetType.Folder;
  313. folder.Version = 1;
  314. Folders.Add(folder.ID, folder);
  315. UUID rootFolder = folder.ID;
  316. folder = new InventoryFolderBase();
  317. folder.ParentID = rootFolder;
  318. folder.Owner = user;
  319. folder.ID = UUID.Random();
  320. folder.Name = "Animations";
  321. folder.Type = (short)AssetType.Animation;
  322. folder.Version = 1;
  323. Folders.Add(folder.ID, folder);
  324. folder = new InventoryFolderBase();
  325. folder.ParentID = rootFolder;
  326. folder.Owner = user;
  327. folder.ID = UUID.Random();
  328. folder.Name = "Body Parts";
  329. folder.Type = (short)AssetType.Bodypart;
  330. folder.Version = 1;
  331. Folders.Add(folder.ID, folder);
  332. folder = new InventoryFolderBase();
  333. folder.ParentID = rootFolder;
  334. folder.Owner = user;
  335. folder.ID = UUID.Random();
  336. folder.Name = "Calling Cards";
  337. folder.Type = (short)AssetType.CallingCard;
  338. folder.Version = 1;
  339. Folders.Add(folder.ID, folder);
  340. folder = new InventoryFolderBase();
  341. folder.ParentID = rootFolder;
  342. folder.Owner = user;
  343. folder.ID = UUID.Random();
  344. folder.Name = "Clothing";
  345. folder.Type = (short)AssetType.Clothing;
  346. folder.Version = 1;
  347. Folders.Add(folder.ID, folder);
  348. folder = new InventoryFolderBase();
  349. folder.ParentID = rootFolder;
  350. folder.Owner = user;
  351. folder.ID = UUID.Random();
  352. folder.Name = "Gestures";
  353. folder.Type = (short)AssetType.Gesture;
  354. folder.Version = 1;
  355. Folders.Add(folder.ID, folder);
  356. folder = new InventoryFolderBase();
  357. folder.ParentID = rootFolder;
  358. folder.Owner = user;
  359. folder.ID = UUID.Random();
  360. folder.Name = "Landmarks";
  361. folder.Type = (short)AssetType.Landmark;
  362. folder.Version = 1;
  363. Folders.Add(folder.ID, folder);
  364. folder = new InventoryFolderBase();
  365. folder.ParentID = rootFolder;
  366. folder.Owner = user;
  367. folder.ID = UUID.Random();
  368. folder.Name = "Lost And Found";
  369. folder.Type = (short)AssetType.LostAndFoundFolder;
  370. folder.Version = 1;
  371. Folders.Add(folder.ID, folder);
  372. folder = new InventoryFolderBase();
  373. folder.ParentID = rootFolder;
  374. folder.Owner = user;
  375. folder.ID = UUID.Random();
  376. folder.Name = "Notecards";
  377. folder.Type = (short)AssetType.Notecard;
  378. folder.Version = 1;
  379. Folders.Add(folder.ID, folder);
  380. folder = new InventoryFolderBase();
  381. folder.ParentID = rootFolder;
  382. folder.Owner = user;
  383. folder.ID = UUID.Random();
  384. folder.Name = "Objects";
  385. folder.Type = (short)AssetType.Object;
  386. folder.Version = 1;
  387. Folders.Add(folder.ID, folder);
  388. folder = new InventoryFolderBase();
  389. folder.ParentID = rootFolder;
  390. folder.Owner = user;
  391. folder.ID = UUID.Random();
  392. folder.Name = "Photo Album";
  393. folder.Type = (short)AssetType.SnapshotFolder;
  394. folder.Version = 1;
  395. Folders.Add(folder.ID, folder);
  396. folder = new InventoryFolderBase();
  397. folder.ParentID = rootFolder;
  398. folder.Owner = user;
  399. folder.ID = UUID.Random();
  400. folder.Name = "Scripts";
  401. folder.Type = (short)AssetType.LSLText;
  402. folder.Version = 1;
  403. Folders.Add(folder.ID, folder);
  404. folder = new InventoryFolderBase();
  405. folder.ParentID = rootFolder;
  406. folder.Owner = user;
  407. folder.ID = UUID.Random();
  408. folder.Name = "Sounds";
  409. folder.Type = (short)AssetType.Sound;
  410. folder.Version = 1;
  411. Folders.Add(folder.ID, folder);
  412. folder = new InventoryFolderBase();
  413. folder.ParentID = rootFolder;
  414. folder.Owner = user;
  415. folder.ID = UUID.Random();
  416. folder.Name = "Textures";
  417. folder.Type = (short)AssetType.Texture;
  418. folder.Version = 1;
  419. Folders.Add(folder.ID, folder);
  420. folder = new InventoryFolderBase();
  421. folder.ParentID = rootFolder;
  422. folder.Owner = user;
  423. folder.ID = UUID.Random();
  424. folder.Name = "Trash";
  425. folder.Type = (short)AssetType.TrashFolder;
  426. folder.Version = 1;
  427. Folders.Add(folder.ID, folder);
  428. }
  429. }
  430. }
  431. }