From 23ec21e44a2d7227ca56c31622e9b9b754e52879 Mon Sep 17 00:00:00 2001 From: Charles Krinke Date: Fri, 4 Jul 2008 03:11:53 +0000 Subject: [PATCH] Mantis#1647. Thank you very much, Sempuki for a patch that: Updates the previous module loader work. --- OpenSim/Framework/IPlugin.cs | 18 +- OpenSim/Framework/PluginLoader.cs | 193 ++++++++++++------ OpenSim/Grid/GridServer/GridServerBase.cs | 16 +- OpenSim/Grid/GridServer/IGridPlugin.cs | 11 + .../Region/Application/IApplicationPlugin.cs | 11 + OpenSim/Region/Application/OpenSimBase.cs | 13 +- 6 files changed, 183 insertions(+), 79 deletions(-) diff --git a/OpenSim/Framework/IPlugin.cs b/OpenSim/Framework/IPlugin.cs index 342918cec1..f7391779ab 100644 --- a/OpenSim/Framework/IPlugin.cs +++ b/OpenSim/Framework/IPlugin.cs @@ -38,7 +38,7 @@ namespace OpenSim.Framework public PluginNotInitialisedException (string msg) : base(msg) {} public PluginNotInitialisedException (string msg, Exception e) : base(msg, e) {} } - + /// /// This interface, describes a generic plugin /// @@ -61,4 +61,20 @@ namespace OpenSim.Framework /// void Initialise(); } + + /// + /// Any plugins which need to pass parameters to their initialisers must + /// inherit this class and use it to set the PluginLoader Initialiser property + /// + public class PluginInitialiserBase + { + // this would be a lot simpler if C# supported currying or typedefs + + // default initialisation + public virtual void Initialise (IPlugin plugin) + { + plugin.Initialise(); + } + } + } diff --git a/OpenSim/Framework/PluginLoader.cs b/OpenSim/Framework/PluginLoader.cs index cd761535a7..9104958e8c 100644 --- a/OpenSim/Framework/PluginLoader.cs +++ b/OpenSim/Framework/PluginLoader.cs @@ -37,11 +37,21 @@ namespace OpenSim.Framework /// /// Exception thrown if an incorrect number of plugins are loaded /// - public class PluginCountInvalidException : Exception + public class PluginConstraintViolatedException : Exception { - public PluginCountInvalidException () : base() {} - public PluginCountInvalidException (string msg) : base(msg) {} - public PluginCountInvalidException (string msg, Exception e) : base(msg, e) {} + public PluginConstraintViolatedException () : base() {} + public PluginConstraintViolatedException (string msg) : base(msg) {} + public PluginConstraintViolatedException (string msg, Exception e) : base(msg, e) {} + } + + /// + /// Classes wishing to impose constraints on plugin loading must implement + /// this class and pass it to PluginLoader AddConstraint() + /// + public interface IPluginConstraint + { + bool Fail (string extpoint); + string Message { get; } } /// @@ -49,26 +59,42 @@ namespace OpenSim.Framework /// public class PluginLoader : IDisposable where T : IPlugin { - private struct Range - { - public int min; - public int max; - public Range (int n, int x) { min=n; max=x; } - } - private const int max_loadable_plugins = 10000; - private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private List loaded = new List(); private List extpoints = new List(); - private Dictionary constraints = new Dictionary(); + private PluginInitialiserBase initialiser; + private Dictionary constraints + = new Dictionary(); - public delegate void Initialiser (IPlugin p); - private void default_initialiser_ (IPlugin p) { p.Initialise(); } + private static readonly ILog log + = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + public PluginInitialiserBase Initialiser + { + set { initialiser = value; } + get { return initialiser; } + } + + public List Plugins + { + get { return loaded; } + } - public PluginLoader (string dir) + public PluginLoader () + { + Initialiser = new PluginInitialiserBase(); + } + + public PluginLoader (PluginInitialiserBase init) { - AddPluginDir (dir); + Initialiser = init; + } + + public PluginLoader (PluginInitialiserBase init, string dir) + { + Initialiser = init; + AddPluginDir (dir); } public void AddPluginDir (string dir) @@ -84,65 +110,45 @@ namespace OpenSim.Framework extpoints.Add (extpoint); } - public void AddConstrainedExtensionPoint (string extpoint, int min, int max) + public void AddConstraint (string extpoint, IPluginConstraint cons) { - constraints.Add (extpoint, new Range (min, max)); - AddExtensionPoint (extpoint); + constraints.Add (extpoint, cons); } - public void LoadAll (Initialiser initialise) + public void Load (string extpoint, string dir) { - foreach (string pt in extpoints) - Load (pt, initialise); + AddPluginDir (dir); + AddExtensionPoint (extpoint); + Load(); } - public void Load (string extpoint) - { - Load (extpoint, default_initialiser_); - } - - public void Load (string extpoint, Initialiser initialise) - { - int min = 0; - int max = max_loadable_plugins; - - if (constraints.ContainsKey (extpoint)) - { - min = constraints[extpoint].min; - max = constraints[extpoint].max; - } - - Load (extpoint, initialise, min, max); - } - - public void Load (string extpoint, Initialiser initialise, int min, int max) + public void Load () { suppress_console_output_ (true); AddinManager.Registry.Update (null); suppress_console_output_ (false); - ExtensionNodeList ns = AddinManager.GetExtensionNodes(extpoint); - - if ((ns.Count < min) || (ns.Count > max)) - throw new PluginCountInvalidException - ("The number of plugins for " + extpoint + - " is constrained to the interval [" + min + ", " + max + "]"); - - foreach (TypeExtensionNode n in ns) + foreach (string ext in extpoints) { - T p = (T) n.CreateInstance(); - initialise (p); - Plugins.Add (p); + if (constraints.ContainsKey (ext)) + { + IPluginConstraint cons = constraints [ext]; + if (cons.Fail (ext)) + throw new PluginConstraintViolatedException (cons.Message); + } - log.Info("[PLUGINS]: Loading plugin " + n.Path); + ExtensionNodeList ns = AddinManager.GetExtensionNodes (ext); + foreach (TypeExtensionNode n in ns) + { + T p = (T) n.CreateInstance(); + Initialiser.Initialise (p); + Plugins.Add (p); + + log.Info("[PLUGINS]: Loading plugin " + n.Path); + } } } - public List Plugins - { - get { return loaded; } - } - public void Dispose () { foreach (T p in Plugins) @@ -174,6 +180,73 @@ namespace OpenSim.Framework System.Console.SetOut(prev_console_); } } + } + public class PluginCountConstraint : IPluginConstraint + { + private int min; + private int max; + + public PluginCountConstraint (int exact) + { + min = exact; + max = exact; + } + + public PluginCountConstraint (int minimum, int maximum) + { + min = minimum; + max = maximum; + } + + public string Message + { + get + { + return "The number of plugins is constrained to the interval [" + + min + ", " + max + "]"; + } + } + + public bool Fail (string extpoint) + { + ExtensionNodeList ns = AddinManager.GetExtensionNodes (extpoint); + if ((ns.Count < min) || (ns.Count > max)) + return true; + else + return false; + } + } + + public class PluginFilenameConstraint : IPluginConstraint + { + private string filename; + + public PluginFilenameConstraint (string name) + { + filename = name; + + } + + public string Message + { + get + { + return "The plugin must have the following name: " + filename; + } + } + + public bool Fail (string extpoint) + { + ExtensionNodeList ns = AddinManager.GetExtensionNodes (extpoint); + if (ns.Count != 1) + return true; + + string[] path = ns[0].Path.Split('/'); + if (path [path.Length-1] == filename) + return false; + + return true; + } } } diff --git a/OpenSim/Grid/GridServer/GridServerBase.cs b/OpenSim/Grid/GridServer/GridServerBase.cs index 010c250286..3cd7489294 100644 --- a/OpenSim/Grid/GridServer/GridServerBase.cs +++ b/OpenSim/Grid/GridServer/GridServerBase.cs @@ -87,7 +87,7 @@ namespace OpenSim.Grid.GridServer AddHttpHandlers(); - LoadGridPlugins(); + LoadPlugins(); m_httpServer.Start(); @@ -116,16 +116,12 @@ namespace OpenSim.Grid.GridServer m_httpServer.AddStreamHandler(new RestStreamHandler("POST", "/regions/", m_gridManager.RestSetRegionMethod)); } - protected void grid_plugin_initialiser_ (IPlugin plugin) + protected void LoadPlugins() { - IGridPlugin p = plugin as IGridPlugin; - p.Initialise (this); - } + PluginLoader loader = + new PluginLoader (new GridPluginInitialiser (this)); - protected void LoadGridPlugins() - { - PluginLoader loader = new PluginLoader ("."); - loader.Load ("/OpenSim/GridServer", grid_plugin_initialiser_); + loader.Load ("/OpenSim/GridServer", "."); m_plugins = loader.Plugins; } @@ -181,7 +177,7 @@ namespace OpenSim.Grid.GridServer public override void Shutdown() { - foreach (IGridPlugin plugin in m_plugins) plugin.Close(); + foreach (IGridPlugin plugin in m_plugins) plugin.Dispose(); base.Shutdown(); } diff --git a/OpenSim/Grid/GridServer/IGridPlugin.cs b/OpenSim/Grid/GridServer/IGridPlugin.cs index d51deb3fc6..6593962586 100644 --- a/OpenSim/Grid/GridServer/IGridPlugin.cs +++ b/OpenSim/Grid/GridServer/IGridPlugin.cs @@ -39,4 +39,15 @@ namespace OpenSim.Grid.GridServer void Initialise(GridServerBase gridServer); void Close(); } + + public class GridPluginInitialiser : PluginInitialiserBase + { + private GridServerBase server; + public GridPluginInitialiser (GridServerBase s) { server = s; } + public override void Initialise (IPlugin plugin) + { + IGridPlugin p = plugin as IGridPlugin; + p.Initialise (server); + } + } } diff --git a/OpenSim/Region/Application/IApplicationPlugin.cs b/OpenSim/Region/Application/IApplicationPlugin.cs index e1187ae2dc..f122925def 100644 --- a/OpenSim/Region/Application/IApplicationPlugin.cs +++ b/OpenSim/Region/Application/IApplicationPlugin.cs @@ -37,4 +37,15 @@ namespace OpenSim { void Initialise(OpenSimBase openSim); } + + public class ApplicationPluginInitialiser : PluginInitialiserBase + { + private OpenSimBase server; + public ApplicationPluginInitialiser (OpenSimBase s) { server = s; } + public override void Initialise (IPlugin plugin) + { + IApplicationPlugin p = plugin as IApplicationPlugin; + p.Initialise (server); + } + } } diff --git a/OpenSim/Region/Application/OpenSimBase.cs b/OpenSim/Region/Application/OpenSimBase.cs index 55d19c1dac..2c2ec09cf8 100644 --- a/OpenSim/Region/Application/OpenSimBase.cs +++ b/OpenSim/Region/Application/OpenSimBase.cs @@ -332,16 +332,12 @@ namespace OpenSim m_networkServersInfo.loadFromConfiguration(m_config.Source); } - protected void plugin_initialiser_ (IPlugin plugin) - { - IApplicationPlugin p = plugin as IApplicationPlugin; - p.Initialise (this); - } - protected void LoadPlugins() { - PluginLoader loader = new PluginLoader ("."); - loader.Load ("/OpenSim/Startup", plugin_initialiser_); + PluginLoader loader = + new PluginLoader (new ApplicationPluginInitialiser (this)); + + loader.Load ("/OpenSim/Startup", "."); m_plugins = loader.Plugins; } @@ -733,3 +729,4 @@ namespace OpenSim +