OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
LibraryModule.cs
Go to the documentation of this file.
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 
32 using OpenSim.Framework;
33 
34 using OpenSim.Region.CoreModules.Avatar.Inventory.Archiver;
35 using OpenSim.Region.Framework;
36 using OpenSim.Region.Framework.Interfaces;
37 using OpenSim.Region.Framework.Scenes;
38 using OpenSim.Services.Interfaces;
39 using OpenSim.Server.Base;
40 
41 using OpenMetaverse;
42 using log4net;
43 using Mono.Addins;
44 using Nini.Config;
46 
47 namespace OpenSim.Region.CoreModules.Framework.Library
48 {
49  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "LibraryModule")]
51  {
52  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
53  private static bool m_HasRunOnce = false;
54 
55  private bool m_Enabled = false;
56 // private string m_LibraryName = "OpenSim Library";
57  private Scene m_Scene;
58 
59  private ILibraryService m_Library;
60 
61  #region ISharedRegionModule
62 
63  public void Initialise(IConfigSource config)
64  {
65  m_Enabled = config.Configs["Modules"].GetBoolean("LibraryModule", m_Enabled);
66  if (m_Enabled)
67  {
68  IConfig libConfig = config.Configs["LibraryService"];
69  if (libConfig != null)
70  {
71  string dllName = libConfig.GetString("LocalServiceModule", string.Empty);
72  m_log.Debug("[LIBRARY MODULE]: Library service dll is " + dllName);
73  if (dllName != string.Empty)
74  {
75  Object[] args = new Object[] { config };
76  m_Library = ServerUtils.LoadPlugin<ILibraryService>(dllName, args);
77  }
78  }
79  }
80  if (m_Library == null)
81  {
82  m_log.Warn("[LIBRARY MODULE]: No local library service. Module will be disabled.");
83  m_Enabled = false;
84  }
85  }
86 
87  public bool IsSharedModule
88  {
89  get { return true; }
90  }
91 
92  public string Name
93  {
94  get { return "Library Module"; }
95  }
96 
97  public Type ReplaceableInterface
98  {
99  get { return null; }
100  }
101 
102  public void AddRegion(Scene scene)
103  {
104  if (!m_Enabled)
105  return;
106 
107  // Store only the first scene
108  if (m_Scene == null)
109  {
110  m_Scene = scene;
111  }
112  scene.RegisterModuleInterface<ILibraryService>(m_Library);
113  }
114 
115  public void RemoveRegion(Scene scene)
116  {
117  if (!m_Enabled)
118  return;
119 
120  scene.UnregisterModuleInterface<ILibraryService>(m_Library);
121  }
122 
123  public void RegionLoaded(Scene scene)
124  {
125  if (!m_Enabled)
126  return;
127 
128  // This will never run more than once, even if the region is restarted
129  if (!m_HasRunOnce)
130  {
131  LoadLibrariesFromArchives();
132  //DumpLibrary();
133  m_HasRunOnce = true;
134  }
135  }
136 
137  public void PostInitialise()
138  {
139  }
140 
141  public void Close()
142  {
143  m_Scene = null;
144  }
145 
146  #endregion ISharedRegionModule
147 
148  #region LoadLibraries
149  private string pathToLibraries = "Library";
150 
151  protected void LoadLibrariesFromArchives()
152  {
153  InventoryFolderImpl lib = m_Library.LibraryRootFolder;
154  if (lib == null)
155  {
156  m_log.Debug("[LIBRARY MODULE]: No library. Ignoring Library Module");
157  return;
158  }
159 
160  RegionInfo regInfo = new RegionInfo();
161  Scene m_MockScene = new Scene(regInfo);
162  LocalInventoryService invService = new LocalInventoryService(lib);
163  m_MockScene.RegisterModuleInterface<IInventoryService>(invService);
164  m_MockScene.RegisterModuleInterface<IAssetService>(m_Scene.AssetService);
165 
166  UserAccount uinfo = new UserAccount(lib.Owner);
167  uinfo.FirstName = "OpenSim";
168  uinfo.LastName = "Library";
169  uinfo.ServiceURLs = new Dictionary<string, object>();
170 
171  foreach (string iarFileName in Directory.GetFiles(pathToLibraries, "*.iar"))
172  {
173  string simpleName = Path.GetFileNameWithoutExtension(iarFileName);
174 
175  m_log.InfoFormat("[LIBRARY MODULE]: Loading library archive {0} ({1})...", iarFileName, simpleName);
176  simpleName = GetInventoryPathFromName(simpleName);
177 
178  InventoryArchiveReadRequest archread = new InventoryArchiveReadRequest(m_MockScene.InventoryService, m_MockScene.AssetService, m_MockScene.UserAccountService, uinfo, simpleName, iarFileName, false);
179  try
180  {
181  HashSet<InventoryNodeBase> nodes = archread.Execute();
182  if (nodes != null && nodes.Count == 0)
183  {
184  // didn't find the subfolder with the given name; place it on the top
185  m_log.InfoFormat("[LIBRARY MODULE]: Didn't find {0} in library. Placing archive on the top level", simpleName);
186  archread.Close();
187  archread = new InventoryArchiveReadRequest(m_MockScene.InventoryService, m_MockScene.AssetService, m_MockScene.UserAccountService, uinfo, "/", iarFileName, false);
188  archread.Execute();
189  }
190 
191  foreach (InventoryNodeBase node in nodes)
192  FixPerms(node);
193  }
194  catch (Exception e)
195  {
196  m_log.DebugFormat("[LIBRARY MODULE]: Exception when processing archive {0}: {1}", iarFileName, e.StackTrace);
197  }
198  finally
199  {
200  archread.Close();
201  }
202  }
203  }
204 
205  private void FixPerms(InventoryNodeBase node)
206  {
207  m_log.DebugFormat("[LIBRARY MODULE]: Fixing perms for {0} {1}", node.Name, node.ID);
208 
209  if (node is InventoryItemBase)
210  {
211  InventoryItemBase item = (InventoryItemBase)node;
212  item.BasePermissions = (uint)PermissionMask.All;
213  item.EveryOnePermissions = (uint)PermissionMask.All - (uint)PermissionMask.Modify;
214  item.CurrentPermissions = (uint)PermissionMask.All;
215  item.NextPermissions = (uint)PermissionMask.All;
216  }
217  }
218 
219 // private void DumpLibrary()
220 // {
221 // InventoryFolderImpl lib = m_Library.LibraryRootFolder;
222 //
223 // m_log.DebugFormat(" - folder {0}", lib.Name);
224 // DumpFolder(lib);
225 // }
226 //
227 // private void DumpLibrary()
228 // {
229 // InventoryFolderImpl lib = m_Scene.CommsManager.UserProfileCacheService.LibraryRoot;
230 //
231 // m_log.DebugFormat(" - folder {0}", lib.Name);
232 // DumpFolder(lib);
233 // }
234 
235  private void DumpFolder(InventoryFolderImpl folder)
236  {
237  foreach (InventoryItemBase item in folder.Items.Values)
238  {
239  m_log.DebugFormat(" --> item {0}", item.Name);
240  }
241  foreach (InventoryFolderImpl f in folder.RequestListOfFolderImpls())
242  {
243  m_log.DebugFormat(" - folder {0}", f.Name);
244  DumpFolder(f);
245  }
246  }
247 
248  private string GetInventoryPathFromName(string name)
249  {
250  string[] parts = name.Split(new char[] { ' ' });
251  if (parts.Length == 3)
252  {
253  name = string.Empty;
254  // cut the last part
255  for (int i = 0; i < parts.Length - 1; i++)
256  name = name + ' ' + parts[i];
257  }
258 
259  return name;
260  }
261 
262  #endregion LoadLibraries
263  }
264 }
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
List< InventoryFolderImpl > RequestListOfFolderImpls()
OpenSim.Framework.RegionInfo RegionInfo
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
Dictionary< UUID, InventoryItemBase > Items
Items that are contained in this folder
virtual UUID Owner
The agent who's inventory this is contained by
void Initialise(IConfigSource config)
This is called to initialize the region module. For shared modules, this is called exactly once...
Inventory Item - contains all the properties associated with an individual inventory piece...
OpenSim.Framework.PermissionMask PermissionMask
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
Interactive OpenSim region server
Definition: OpenSim.cs:55
Common base class for inventory nodes of different types (files, folders, etc.)
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...