From 23ec21e44a2d7227ca56c31622e9b9b754e52879 Mon Sep 17 00:00:00 2001 From: Charles Krinke Date: Fri, 4 Jul 2008 03:11:53 +0000 Subject: 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 | 195 ++++++++++++++++++++++++++------------ 2 files changed, 151 insertions(+), 62 deletions(-) (limited to 'OpenSim/Framework') diff --git a/OpenSim/Framework/IPlugin.cs b/OpenSim/Framework/IPlugin.cs index 342918c..f739177 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 cd76153..9104958 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(); + + private static readonly ILog log + = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); - public delegate void Initialiser (IPlugin p); - private void default_initialiser_ (IPlugin p) { p.Initialise(); } + public PluginInitialiserBase Initialiser + { + set { initialiser = value; } + get { return initialiser; } + } + + public List Plugins + { + get { return loaded; } + } + + public PluginLoader () + { + Initialiser = new PluginInitialiserBase(); + } - public PluginLoader (string dir) + 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); - } - - 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); + AddPluginDir (dir); + AddExtensionPoint (extpoint); + Load(); } - 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); - - log.Info("[PLUGINS]: Loading plugin " + n.Path); + if (constraints.ContainsKey (ext)) + { + IPluginConstraint cons = constraints [ext]; + if (cons.Fail (ext)) + throw new PluginConstraintViolatedException (cons.Message); + } + + 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; + } } } -- cgit v1.1