LibraryService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Xml;
  32. using OpenSim.Framework;
  33. using OpenSim.Services.Base;
  34. using OpenSim.Services.Interfaces;
  35. using log4net;
  36. using Nini.Config;
  37. using OpenMetaverse;
  38. using PermissionMask = OpenSim.Framework.PermissionMask;
  39. namespace OpenSim.Services.InventoryService
  40. {
  41. /// <summary>
  42. /// Basically a hack to give us a Inventory library while we don't have a inventory server
  43. /// once the server is fully implemented then should read the data from that
  44. /// </summary>
  45. public class LibraryService : ServiceBase, ILibraryService
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private InventoryFolderImpl m_LibraryRootFolder;
  49. public InventoryFolderImpl LibraryRootFolder
  50. {
  51. get { return m_LibraryRootFolder; }
  52. }
  53. private UUID libOwner = new UUID("11111111-1111-0000-0000-000100bba000");
  54. /// <summary>
  55. /// Holds the root library folder and all its descendents. This is really only used during inventory
  56. /// setup so that we don't have to repeatedly search the tree of library folders.
  57. /// </summary>
  58. protected Dictionary<UUID, InventoryFolderImpl> libraryFolders
  59. = new Dictionary<UUID, InventoryFolderImpl>();
  60. public LibraryService(IConfigSource config)
  61. : base(config)
  62. {
  63. string pLibrariesLocation = Path.Combine("inventory", "Libraries.xml");
  64. string pLibName = "OpenSim Library";
  65. IConfig libConfig = config.Configs["LibraryService"];
  66. if (libConfig != null)
  67. {
  68. pLibrariesLocation = libConfig.GetString("DefaultLibrary", pLibrariesLocation);
  69. pLibName = libConfig.GetString("LibraryName", pLibName);
  70. }
  71. m_log.Debug("[LIBRARY]: Starting library service...");
  72. m_LibraryRootFolder = new InventoryFolderImpl();
  73. m_LibraryRootFolder.Owner = libOwner;
  74. m_LibraryRootFolder.ID = new UUID("00000112-000f-0000-0000-000100bba000");
  75. m_LibraryRootFolder.Name = pLibName;
  76. m_LibraryRootFolder.ParentID = UUID.Zero;
  77. m_LibraryRootFolder.Type = (short)8;
  78. m_LibraryRootFolder.Version = (ushort)1;
  79. libraryFolders.Add(m_LibraryRootFolder.ID, m_LibraryRootFolder);
  80. LoadLibraries(pLibrariesLocation);
  81. }
  82. public InventoryItemBase CreateItem(UUID inventoryID, UUID assetID, string name, string description,
  83. int assetType, int invType, UUID parentFolderID)
  84. {
  85. InventoryItemBase item = new InventoryItemBase();
  86. item.Owner = libOwner;
  87. item.CreatorId = libOwner.ToString();
  88. item.ID = inventoryID;
  89. item.AssetID = assetID;
  90. item.Description = description;
  91. item.Name = name;
  92. item.AssetType = assetType;
  93. item.InvType = invType;
  94. item.Folder = parentFolderID;
  95. item.BasePermissions = 0x7FFFFFFF;
  96. item.EveryOnePermissions = 0x7FFFFFFF;
  97. item.CurrentPermissions = 0x7FFFFFFF;
  98. item.NextPermissions = 0x7FFFFFFF;
  99. return item;
  100. }
  101. /// <summary>
  102. /// Use the asset set information at path to load assets
  103. /// </summary>
  104. /// <param name="path"></param>
  105. /// <param name="assets"></param>
  106. protected void LoadLibraries(string librariesControlPath)
  107. {
  108. m_log.InfoFormat("[LIBRARY INVENTORY]: Loading library control file {0}", librariesControlPath);
  109. LoadFromFile(librariesControlPath, "Libraries control", ReadLibraryFromConfig);
  110. }
  111. /// <summary>
  112. /// Read a library set from config
  113. /// </summary>
  114. /// <param name="config"></param>
  115. protected void ReadLibraryFromConfig(IConfig config, string path)
  116. {
  117. string basePath = Path.GetDirectoryName(path);
  118. string foldersPath
  119. = Path.Combine(
  120. basePath, config.GetString("foldersFile", String.Empty));
  121. LoadFromFile(foldersPath, "Library folders", ReadFolderFromConfig);
  122. string itemsPath
  123. = Path.Combine(
  124. basePath, config.GetString("itemsFile", String.Empty));
  125. LoadFromFile(itemsPath, "Library items", ReadItemFromConfig);
  126. }
  127. /// <summary>
  128. /// Read a library inventory folder from a loaded configuration
  129. /// </summary>
  130. /// <param name="source"></param>
  131. private void ReadFolderFromConfig(IConfig config, string path)
  132. {
  133. InventoryFolderImpl folderInfo = new InventoryFolderImpl();
  134. folderInfo.ID = new UUID(config.GetString("folderID", m_LibraryRootFolder.ID.ToString()));
  135. folderInfo.Name = config.GetString("name", "unknown");
  136. folderInfo.ParentID = new UUID(config.GetString("parentFolderID", m_LibraryRootFolder.ID.ToString()));
  137. folderInfo.Type = (short)config.GetInt("type", 8);
  138. folderInfo.Owner = libOwner;
  139. folderInfo.Version = 1;
  140. if (libraryFolders.ContainsKey(folderInfo.ParentID))
  141. {
  142. InventoryFolderImpl parentFolder = libraryFolders[folderInfo.ParentID];
  143. libraryFolders.Add(folderInfo.ID, folderInfo);
  144. parentFolder.AddChildFolder(folderInfo);
  145. // m_log.InfoFormat("[LIBRARY INVENTORY]: Adding folder {0} ({1})", folderInfo.name, folderInfo.folderID);
  146. }
  147. else
  148. {
  149. m_log.WarnFormat(
  150. "[LIBRARY INVENTORY]: Couldn't add folder {0} ({1}) since parent folder with ID {2} does not exist!",
  151. folderInfo.Name, folderInfo.ID, folderInfo.ParentID);
  152. }
  153. }
  154. /// <summary>
  155. /// Read a library inventory item metadata from a loaded configuration
  156. /// </summary>
  157. /// <param name="source"></param>
  158. private void ReadItemFromConfig(IConfig config, string path)
  159. {
  160. InventoryItemBase item = new InventoryItemBase();
  161. item.Owner = libOwner;
  162. item.CreatorId = libOwner.ToString();
  163. item.ID = new UUID(config.GetString("inventoryID", m_LibraryRootFolder.ID.ToString()));
  164. item.AssetID = new UUID(config.GetString("assetID", item.ID.ToString()));
  165. item.Folder = new UUID(config.GetString("folderID", m_LibraryRootFolder.ID.ToString()));
  166. item.Name = config.GetString("name", String.Empty);
  167. item.Description = config.GetString("description", item.Name);
  168. item.InvType = config.GetInt("inventoryType", 0);
  169. item.AssetType = config.GetInt("assetType", item.InvType);
  170. item.CurrentPermissions = (uint)config.GetLong("currentPermissions", (uint)PermissionMask.All);
  171. item.NextPermissions = (uint)config.GetLong("nextPermissions", (uint)PermissionMask.All);
  172. item.EveryOnePermissions
  173. = (uint)config.GetLong("everyonePermissions", (uint)PermissionMask.All - (uint)PermissionMask.Modify);
  174. item.BasePermissions = (uint)config.GetLong("basePermissions", (uint)PermissionMask.All);
  175. item.Flags = (uint)config.GetInt("flags", 0);
  176. if (libraryFolders.ContainsKey(item.Folder))
  177. {
  178. InventoryFolderImpl parentFolder = libraryFolders[item.Folder];
  179. try
  180. {
  181. parentFolder.Items.Add(item.ID, item);
  182. }
  183. catch (Exception)
  184. {
  185. m_log.WarnFormat("[LIBRARY INVENTORY] Item {1} [{0}] not added, duplicate item", item.ID, item.Name);
  186. }
  187. }
  188. else
  189. {
  190. m_log.WarnFormat(
  191. "[LIBRARY INVENTORY]: Couldn't add item {0} ({1}) since parent folder with ID {2} does not exist!",
  192. item.Name, item.ID, item.Folder);
  193. }
  194. }
  195. private delegate void ConfigAction(IConfig config, string path);
  196. /// <summary>
  197. /// Load the given configuration at a path and perform an action on each Config contained within it
  198. /// </summary>
  199. /// <param name="path"></param>
  200. /// <param name="fileDescription"></param>
  201. /// <param name="action"></param>
  202. private static void LoadFromFile(string path, string fileDescription, ConfigAction action)
  203. {
  204. if (File.Exists(path))
  205. {
  206. try
  207. {
  208. XmlConfigSource source = new XmlConfigSource(path);
  209. for (int i = 0; i < source.Configs.Count; i++)
  210. {
  211. action(source.Configs[i], path);
  212. }
  213. }
  214. catch (XmlException e)
  215. {
  216. m_log.ErrorFormat("[LIBRARY INVENTORY]: Error loading {0} : {1}", path, e);
  217. }
  218. }
  219. else
  220. {
  221. m_log.ErrorFormat("[LIBRARY INVENTORY]: {0} file {1} does not exist!", fileDescription, path);
  222. }
  223. }
  224. /// <summary>
  225. /// Looks like a simple getter, but is written like this for some consistency with the other Request
  226. /// methods in the superclass
  227. /// </summary>
  228. /// <returns></returns>
  229. public Dictionary<UUID, InventoryFolderImpl> GetAllFolders()
  230. {
  231. Dictionary<UUID, InventoryFolderImpl> fs = new Dictionary<UUID, InventoryFolderImpl>();
  232. fs.Add(m_LibraryRootFolder.ID, m_LibraryRootFolder);
  233. List<InventoryFolderImpl> fis = TraverseFolder(m_LibraryRootFolder);
  234. foreach (InventoryFolderImpl f in fis)
  235. {
  236. fs.Add(f.ID, f);
  237. }
  238. //return libraryFolders;
  239. return fs;
  240. }
  241. private List<InventoryFolderImpl> TraverseFolder(InventoryFolderImpl node)
  242. {
  243. List<InventoryFolderImpl> folders = node.RequestListOfFolderImpls();
  244. List<InventoryFolderImpl> subs = new List<InventoryFolderImpl>();
  245. foreach (InventoryFolderImpl f in folders)
  246. subs.AddRange(TraverseFolder(f));
  247. folders.AddRange(subs);
  248. return folders;
  249. }
  250. }
  251. }