From c7151a5a2b3298e50028b8f21df067d6399ce821 Mon Sep 17 00:00:00 2001 From: MW Date: Fri, 3 Apr 2009 10:35:47 +0000 Subject: [PATCH] Applied patch from Mantis# 3387, which adds initial support for Dynamically loading IGridServiceModule "modules" in the userserver. Thank you mpallari. --- OpenSim/Grid/Framework/IGridServiceModule.cs | 1 + OpenSim/Grid/GridServer/GridModuleLoader.cs | 77 +++++++++++++++++++ .../GridInfoServiceModule.cs | 7 +- .../MessageServersConnector.cs | 12 ++- .../Grid/UserServer.Modules/UserManager.cs | 27 ++++--- .../UserServerAvatarAppearanceModule.cs | 22 ++++-- .../UserServerCommandModule.cs | 14 +++- .../UserServerEventDispatchModule.cs | 33 +++++--- .../UserServerFriendsModule.cs | 25 ++++-- .../UserServer.Modules/UserServerPlugin.cs | 67 ++++++---------- 10 files changed, 205 insertions(+), 80 deletions(-) create mode 100644 OpenSim/Grid/GridServer/GridModuleLoader.cs diff --git a/OpenSim/Grid/Framework/IGridServiceModule.cs b/OpenSim/Grid/Framework/IGridServiceModule.cs index 97706d36f6..c300c86acb 100644 --- a/OpenSim/Grid/Framework/IGridServiceModule.cs +++ b/OpenSim/Grid/Framework/IGridServiceModule.cs @@ -9,5 +9,6 @@ namespace OpenSim.Grid.Framework void Initialise(IGridServiceCore core); void PostInitialise(); void RegisterHandlers(BaseHttpServer httpServer); + string Name { get; } } } diff --git a/OpenSim/Grid/GridServer/GridModuleLoader.cs b/OpenSim/Grid/GridServer/GridModuleLoader.cs new file mode 100644 index 0000000000..a79b2bedc9 --- /dev/null +++ b/OpenSim/Grid/GridServer/GridModuleLoader.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Text; +using log4net; + +namespace OpenSim.Grid.GridServer +{ + public class GridModuleLoader + { + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + public Dictionary LoadedAssemblys = new Dictionary(); + + public List PickupModules(string path) + { + DirectoryInfo dir = new DirectoryInfo(path); + List modules = new List(); + + foreach (FileInfo fileInfo in dir.GetFiles("*.dll")) + { + List foundModules = this.LoadModules(fileInfo.FullName); + modules.AddRange(foundModules); + } + return modules; + } + + public List LoadModules(string dllName) + { + List modules = new List(); + + Assembly pluginAssembly; + if (!LoadedAssemblys.TryGetValue(dllName, out pluginAssembly)) + { + try + { + pluginAssembly = Assembly.LoadFrom(dllName); + LoadedAssemblys.Add(dllName, pluginAssembly); + } + catch (BadImageFormatException) + { + //m_log.InfoFormat("[MODULES]: The file [{0}] is not a module assembly.", e.FileName); + } + } + + if (pluginAssembly != null) + { + try + { + foreach (Type pluginType in pluginAssembly.GetTypes()) + { + if (pluginType.IsPublic) + { + if (!pluginType.IsAbstract) + { + if (pluginType.GetInterface(typeof(T).Name) != null) + { + modules.Add((T)Activator.CreateInstance(pluginType)); + } + } + } + } + } + catch (Exception e) + { + m_log.ErrorFormat( + "[MODULES]: Could not load types for [{0}]. Exception {1}", pluginAssembly.FullName, e); + + // justincc: Right now this is fatal to really get the user's attention + throw e; + } + } + + return modules; + } + } +} diff --git a/OpenSim/Grid/UserServer.Modules/GridInfoServiceModule.cs b/OpenSim/Grid/UserServer.Modules/GridInfoServiceModule.cs index 33b55b67e8..c817fb4e9a 100644 --- a/OpenSim/Grid/UserServer.Modules/GridInfoServiceModule.cs +++ b/OpenSim/Grid/UserServer.Modules/GridInfoServiceModule.cs @@ -41,7 +41,7 @@ using OpenSim.Grid.Framework; namespace OpenSim.Grid.UserServer.Modules { - public class GridInfoServiceModule + public class GridInfoServiceModule : IGridServiceModule { protected IGridServiceCore m_core; protected GridInfoService m_gridInfoService; @@ -73,5 +73,10 @@ namespace OpenSim.Grid.UserServer.Modules public void Close() { } + + public string Name + { + get { return "GridInfoServiceModule"; } + } } } diff --git a/OpenSim/Grid/UserServer.Modules/MessageServersConnector.cs b/OpenSim/Grid/UserServer.Modules/MessageServersConnector.cs index f9e7889203..81202d4530 100644 --- a/OpenSim/Grid/UserServer.Modules/MessageServersConnector.cs +++ b/OpenSim/Grid/UserServer.Modules/MessageServersConnector.cs @@ -66,7 +66,7 @@ namespace OpenSim.Grid.UserServer.Modules public delegate void RegionShutdownDelegate(UUID regionID); - public class MessageServersConnector + public class MessageServersConnector : IGridServiceModule { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); @@ -508,5 +508,15 @@ namespace OpenSim.Grid.UserServer.Modules response.Value = result; return response; } + + public void Close() + { + } + + public string Name + { + get { return "MessageServersConnector"; } + } + } } diff --git a/OpenSim/Grid/UserServer.Modules/UserManager.cs b/OpenSim/Grid/UserServer.Modules/UserManager.cs index c40201e753..db3eee82bf 100644 --- a/OpenSim/Grid/UserServer.Modules/UserManager.cs +++ b/OpenSim/Grid/UserServer.Modules/UserManager.cs @@ -41,7 +41,7 @@ namespace OpenSim.Grid.UserServer.Modules { public delegate void logOffUser(UUID AgentID); - public class UserManager + public class UserManager : IGridServiceModule { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); @@ -50,24 +50,22 @@ namespace OpenSim.Grid.UserServer.Modules private UserDataBaseService m_userDataBaseService; private BaseHttpServer m_httpServer; + private IGridServiceCore m_core; - /// - /// - /// - /// - public UserManager( UserDataBaseService userDataBaseService) + public UserManager() { - m_userDataBaseService = userDataBaseService; } public void Initialise(IGridServiceCore core) { - + m_core = core; + m_core.RegisterInterface(this); } public void PostInitialise() { - + if (!m_core.TryGet(out m_userDataBaseService)) + m_log.Error("[UserManager]: Failed to fetch database plugin"); } public void RegisterHandlers(BaseHttpServer httpServer) @@ -685,6 +683,15 @@ namespace OpenSim.Grid.UserServer.Modules public void HandleRegionShutdown(UUID regionID) { m_userDataBaseService.LogoutUsers(regionID); - } + } + + public void Close() + { + } + + public string Name + { + get { return "UserManager"; } + } } } diff --git a/OpenSim/Grid/UserServer.Modules/UserServerAvatarAppearanceModule.cs b/OpenSim/Grid/UserServer.Modules/UserServerAvatarAppearanceModule.cs index a53dee7316..77bcda330a 100644 --- a/OpenSim/Grid/UserServer.Modules/UserServerAvatarAppearanceModule.cs +++ b/OpenSim/Grid/UserServer.Modules/UserServerAvatarAppearanceModule.cs @@ -39,26 +39,27 @@ using OpenSim.Grid.Framework; namespace OpenSim.Grid.UserServer.Modules { - public class UserServerAvatarAppearanceModule + public class UserServerAvatarAppearanceModule : IGridServiceModule { - //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private UserDataBaseService m_userDataBaseService; private BaseHttpServer m_httpServer; + private IGridServiceCore m_core; - public UserServerAvatarAppearanceModule(UserDataBaseService userDataBaseService) + public UserServerAvatarAppearanceModule() { - m_userDataBaseService = userDataBaseService; } public void Initialise(IGridServiceCore core) { - + m_core = core; } public void PostInitialise() { - + if (!m_core.TryGet(out m_userDataBaseService)) + m_log.Error("[UserServerAvatarAppearanceModule]: Failed to fetch database plugin"); } public void RegisterHandlers(BaseHttpServer httpServer) @@ -121,5 +122,14 @@ namespace OpenSim.Grid.UserServer.Modules response.Value = responseData; return response; } + + public void Close() + { + } + + public string Name + { + get { return "UserServerAvatarAppearanceModule"; } + } } } diff --git a/OpenSim/Grid/UserServer.Modules/UserServerCommandModule.cs b/OpenSim/Grid/UserServer.Modules/UserServerCommandModule.cs index 256f792a89..ca6c2565e5 100644 --- a/OpenSim/Grid/UserServer.Modules/UserServerCommandModule.cs +++ b/OpenSim/Grid/UserServer.Modules/UserServerCommandModule.cs @@ -44,7 +44,7 @@ using OpenSim.Grid.Framework; namespace OpenSim.Grid.UserServer.Modules { - public class UserServerCommandModule + public class UserServerCommandModule : IGridServiceModule { private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); @@ -368,6 +368,16 @@ namespace OpenSim.Grid.UserServer.Modules break; } } - } + #endregion + + public void Close() + { + } + + public string Name + { + get { return "UserServerCommandModule"; } + } + } } diff --git a/OpenSim/Grid/UserServer.Modules/UserServerEventDispatchModule.cs b/OpenSim/Grid/UserServer.Modules/UserServerEventDispatchModule.cs index 50bd00e1a2..aa9ffa5818 100644 --- a/OpenSim/Grid/UserServer.Modules/UserServerEventDispatchModule.cs +++ b/OpenSim/Grid/UserServer.Modules/UserServerEventDispatchModule.cs @@ -46,32 +46,38 @@ namespace OpenSim.Grid.UserServer.Modules { //Do we actually need these event dispatchers? //shouldn't the other modules just directly register event handlers to each other? - public class UserServerEventDispatchModule + public class UserServerEventDispatchModule : IGridServiceModule { protected UserManager m_userManager; protected MessageServersConnector m_messagesService; protected UserLoginService m_loginService; - public UserServerEventDispatchModule(UserManager userManager, MessageServersConnector messagesService, UserLoginService loginService) + private IGridServiceCore m_core; + + public UserServerEventDispatchModule() { - m_userManager = userManager; - m_messagesService = messagesService; - m_loginService = loginService; } public void Initialise(IGridServiceCore core) { + m_core = core; } public void PostInitialise() { - m_loginService.OnUserLoggedInAtLocation += NotifyMessageServersUserLoggedInToLocation; - m_userManager.OnLogOffUser += NotifyMessageServersUserLoggOff; + if (m_core.TryGet(out m_userManager) && + m_core.TryGet(out m_messagesService) && + m_core.TryGet(out m_loginService)) + { - m_messagesService.OnAgentLocation += HandleAgentLocation; - m_messagesService.OnAgentLeaving += HandleAgentLeaving; - m_messagesService.OnRegionStartup += HandleRegionStartup; - m_messagesService.OnRegionShutdown += HandleRegionShutdown; + m_loginService.OnUserLoggedInAtLocation += NotifyMessageServersUserLoggedInToLocation; + m_userManager.OnLogOffUser += NotifyMessageServersUserLoggOff; + + m_messagesService.OnAgentLocation += HandleAgentLocation; + m_messagesService.OnAgentLeaving += HandleAgentLeaving; + m_messagesService.OnRegionStartup += HandleRegionStartup; + m_messagesService.OnRegionShutdown += HandleRegionShutdown; + } } public void RegisterHandlers(BaseHttpServer httpServer) @@ -136,5 +142,10 @@ namespace OpenSim.Grid.UserServer.Modules m_messagesService.TellMessageServersAboutRegionShutdown(regionID); } #endregion + + public string Name + { + get { return "UserServerEventDispatchModule"; } + } } } diff --git a/OpenSim/Grid/UserServer.Modules/UserServerFriendsModule.cs b/OpenSim/Grid/UserServer.Modules/UserServerFriendsModule.cs index 8fc474fcf1..95ead7390a 100644 --- a/OpenSim/Grid/UserServer.Modules/UserServerFriendsModule.cs +++ b/OpenSim/Grid/UserServer.Modules/UserServerFriendsModule.cs @@ -36,30 +36,32 @@ using OpenSim.Framework; using OpenSim.Framework.Communications; using OpenSim.Framework.Servers; using OpenSim.Grid.Framework; +using OpenSim.Grid.GridServer; namespace OpenSim.Grid.UserServer.Modules { - public class UserServerFriendsModule + public class UserServerFriendsModule : IGridServiceModule { - //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private UserDataBaseService m_userDataBaseService; private BaseHttpServer m_httpServer; + private IGridServiceCore m_core; - public UserServerFriendsModule(UserDataBaseService userDataBaseService) + public UserServerFriendsModule() { - m_userDataBaseService = userDataBaseService; } public void Initialise(IGridServiceCore core) { - + m_core = core; } public void PostInitialise() { - + if (!m_core.TryGet(out m_userDataBaseService)) + m_log.Error("[UserServerFriendsModule]: Failed to fetch database plugin"); } public void RegisterHandlers(BaseHttpServer httpServer) @@ -170,5 +172,14 @@ namespace OpenSim.Grid.UserServer.Modules return FriendListItemListtoXmlRPCResponse(returndata); } + + public void Close() + { + } + + public string Name + { + get { return "UserServerFriendsModule"; } + } } -} \ No newline at end of file +} diff --git a/OpenSim/Grid/UserServer.Modules/UserServerPlugin.cs b/OpenSim/Grid/UserServer.Modules/UserServerPlugin.cs index 70d4708a8f..67f30eacb1 100644 --- a/OpenSim/Grid/UserServer.Modules/UserServerPlugin.cs +++ b/OpenSim/Grid/UserServer.Modules/UserServerPlugin.cs @@ -53,18 +53,7 @@ namespace OpenSim.Grid.UserServer.Modules protected UserDataBaseService m_userDataBaseService; - public UserManager m_userManager; - - protected UserServerAvatarAppearanceModule m_avatarAppearanceModule; - protected UserServerFriendsModule m_friendsModule; - public UserLoginService m_loginService; - public MessageServersConnector m_messagesService; - - protected GridInfoServiceModule m_gridInfoService; - - protected UserServerCommandModule m_consoleCommandModule; - protected UserServerEventDispatchModule m_eventDispatcher; protected BaseHttpServer m_httpServer; @@ -72,6 +61,8 @@ namespace OpenSim.Grid.UserServer.Modules protected ConsoleBase m_console; + protected List m_modules; + public UserServerPlugin() { @@ -133,24 +124,21 @@ namespace OpenSim.Grid.UserServer.Modules m_userDataBaseService = new UserDataBaseService(); m_userDataBaseService.Initialise(m_core); - //TODO: change these modules so they fetch the databaseService class in the PostInitialise method - m_userManager = new UserManager(m_userDataBaseService); - m_userManager.Initialise(m_core); + //DONE: change these modules so they fetch the databaseService class in the PostInitialise method - m_avatarAppearanceModule = new UserServerAvatarAppearanceModule(m_userDataBaseService); - m_avatarAppearanceModule.Initialise(m_core); + GridModuleLoader moduleLoader = new GridModuleLoader(); - m_friendsModule = new UserServerFriendsModule(m_userDataBaseService); - m_friendsModule.Initialise(m_core); + m_modules = moduleLoader.PickupModules("."); - m_consoleCommandModule = new UserServerCommandModule(); - m_consoleCommandModule.Initialise(m_core); + InitializeModules(); + } - m_messagesService = new MessageServersConnector(); - m_messagesService.Initialise(m_core); - - m_gridInfoService = new GridInfoServiceModule(); - m_gridInfoService.Initialise(m_core); + private void InitializeModules() + { + foreach (IGridServiceModule module in m_modules) + { + module.Initialise(m_core); + } } protected virtual void StartOtherComponents(IInterServiceInventoryServices inventoryService) @@ -164,32 +152,24 @@ namespace OpenSim.Grid.UserServer.Modules m_loginService.setloginlevel((int)m_cfg.DefaultUserLevel); m_core.RegisterInterface(m_loginService); //TODO: should be done in the login service - - m_eventDispatcher = new UserServerEventDispatchModule(m_userManager, m_messagesService, m_loginService); - m_eventDispatcher.Initialise(m_core); } protected virtual void PostInitialiseModules() { - m_consoleCommandModule.PostInitialise(); //it will register its Console command handlers in here - m_userDataBaseService.PostInitialise(); - m_messagesService.PostInitialise(); - m_eventDispatcher.PostInitialise(); //it will register event handlers in here - m_gridInfoService.PostInitialise(); - m_userManager.PostInitialise(); - m_avatarAppearanceModule.PostInitialise(); - m_friendsModule.PostInitialise(); + foreach (IGridServiceModule module in m_modules) + { + module.PostInitialise(); + } } protected virtual void RegisterHttpHandlers() { m_loginService.RegisterHandlers(m_httpServer, m_cfg.EnableLLSDLogin, true); - m_userManager.RegisterHandlers(m_httpServer); - m_friendsModule.RegisterHandlers(m_httpServer); - m_avatarAppearanceModule.RegisterHandlers(m_httpServer); - m_messagesService.RegisterHandlers(m_httpServer); - m_gridInfoService.RegisterHandlers(m_httpServer); + foreach (IGridServiceModule module in m_modules) + { + module.RegisterHandlers(m_httpServer); + } } #region IPlugin Members @@ -215,7 +195,10 @@ namespace OpenSim.Grid.UserServer.Modules public void Dispose() { - throw new NotImplementedException(); + foreach (IGridServiceModule module in m_modules) + { + module.Close(); + } } #endregion