diff options
author | Charles Krinke | 2008-07-02 14:14:29 +0000 |
---|---|---|
committer | Charles Krinke | 2008-07-02 14:14:29 +0000 |
commit | 1deaa50240b72a83efd0f345e1e8d8bc4766540a (patch) | |
tree | 57db4081dc2af303ea6713411c4804ee573d2cf2 /OpenSim/Framework/PluginLoader.cs | |
parent | Update svn properties. (diff) | |
download | opensim-SC_OLD-1deaa50240b72a83efd0f345e1e8d8bc4766540a.zip opensim-SC_OLD-1deaa50240b72a83efd0f345e1e8d8bc4766540a.tar.gz opensim-SC_OLD-1deaa50240b72a83efd0f345e1e8d8bc4766540a.tar.bz2 opensim-SC_OLD-1deaa50240b72a83efd0f345e1e8d8bc4766540a.tar.xz |
Mantis#1647. Thank you kindly, Sempuki for a patch that:
attached patch adds support to constrain the number of
plugins that can be loaded per extension point.
Diffstat (limited to 'OpenSim/Framework/PluginLoader.cs')
-rw-r--r-- | OpenSim/Framework/PluginLoader.cs | 75 |
1 files changed, 70 insertions, 5 deletions
diff --git a/OpenSim/Framework/PluginLoader.cs b/OpenSim/Framework/PluginLoader.cs index 081a646..cd76153 100644 --- a/OpenSim/Framework/PluginLoader.cs +++ b/OpenSim/Framework/PluginLoader.cs | |||
@@ -34,10 +34,37 @@ using Mono.Addins; | |||
34 | 34 | ||
35 | namespace OpenSim.Framework | 35 | namespace OpenSim.Framework |
36 | { | 36 | { |
37 | /// <summary> | ||
38 | /// Exception thrown if an incorrect number of plugins are loaded | ||
39 | /// </summary> | ||
40 | public class PluginCountInvalidException : Exception | ||
41 | { | ||
42 | public PluginCountInvalidException () : base() {} | ||
43 | public PluginCountInvalidException (string msg) : base(msg) {} | ||
44 | public PluginCountInvalidException (string msg, Exception e) : base(msg, e) {} | ||
45 | } | ||
46 | |||
47 | /// <summary> | ||
48 | /// Generic Plugin Loader | ||
49 | /// </summary> | ||
37 | public class PluginLoader <T> : IDisposable where T : IPlugin | 50 | public class PluginLoader <T> : IDisposable where T : IPlugin |
38 | { | 51 | { |
52 | private struct Range | ||
53 | { | ||
54 | public int min; | ||
55 | public int max; | ||
56 | public Range (int n, int x) { min=n; max=x; } | ||
57 | } | ||
58 | |||
59 | private const int max_loadable_plugins = 10000; | ||
60 | |||
39 | private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 61 | private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
40 | private List<T> loaded = new List<T>(); | 62 | private List<T> loaded = new List<T>(); |
63 | private List<string> extpoints = new List<string>(); | ||
64 | private Dictionary<string,Range> constraints = new Dictionary<string,Range>(); | ||
65 | |||
66 | public delegate void Initialiser (IPlugin p); | ||
67 | private void default_initialiser_ (IPlugin p) { p.Initialise(); } | ||
41 | 68 | ||
42 | public PluginLoader (string dir) | 69 | public PluginLoader (string dir) |
43 | { | 70 | { |
@@ -52,24 +79,62 @@ namespace OpenSim.Framework | |||
52 | suppress_console_output_ (false); | 79 | suppress_console_output_ (false); |
53 | } | 80 | } |
54 | 81 | ||
55 | public delegate void Initialiser (IPlugin p); | 82 | public void AddExtensionPoint (string extpoint) |
56 | private void default_initialiser_ (IPlugin p) { p.Initialise(); } | 83 | { |
84 | extpoints.Add (extpoint); | ||
85 | } | ||
86 | |||
87 | public void AddConstrainedExtensionPoint (string extpoint, int min, int max) | ||
88 | { | ||
89 | constraints.Add (extpoint, new Range (min, max)); | ||
90 | AddExtensionPoint (extpoint); | ||
91 | } | ||
92 | |||
93 | public void LoadAll (Initialiser initialise) | ||
94 | { | ||
95 | foreach (string pt in extpoints) | ||
96 | Load (pt, initialise); | ||
97 | } | ||
57 | 98 | ||
58 | public void Load (string extpoint) | 99 | public void Load (string extpoint) |
59 | { | 100 | { |
60 | Load (extpoint, default_initialiser_); | 101 | Load (extpoint, default_initialiser_); |
61 | } | 102 | } |
62 | 103 | ||
63 | public void Load (string extpoint, Initialiser initialize) | 104 | public void Load (string extpoint, Initialiser initialise) |
105 | { | ||
106 | int min = 0; | ||
107 | int max = max_loadable_plugins; | ||
108 | |||
109 | if (constraints.ContainsKey (extpoint)) | ||
110 | { | ||
111 | min = constraints[extpoint].min; | ||
112 | max = constraints[extpoint].max; | ||
113 | } | ||
114 | |||
115 | Load (extpoint, initialise, min, max); | ||
116 | } | ||
117 | |||
118 | public void Load (string extpoint, Initialiser initialise, int min, int max) | ||
64 | { | 119 | { |
120 | suppress_console_output_ (true); | ||
121 | AddinManager.Registry.Update (null); | ||
122 | suppress_console_output_ (false); | ||
123 | |||
65 | ExtensionNodeList ns = AddinManager.GetExtensionNodes(extpoint); | 124 | ExtensionNodeList ns = AddinManager.GetExtensionNodes(extpoint); |
125 | |||
126 | if ((ns.Count < min) || (ns.Count > max)) | ||
127 | throw new PluginCountInvalidException | ||
128 | ("The number of plugins for " + extpoint + | ||
129 | " is constrained to the interval [" + min + ", " + max + "]"); | ||
130 | |||
66 | foreach (TypeExtensionNode n in ns) | 131 | foreach (TypeExtensionNode n in ns) |
67 | { | 132 | { |
68 | T p = (T) n.CreateInstance(); | 133 | T p = (T) n.CreateInstance(); |
69 | initialize (p); | 134 | initialise (p); |
70 | Plugins.Add (p); | 135 | Plugins.Add (p); |
71 | 136 | ||
72 | log.Info("[PLUGINS]: Loading plugin " + n.Path + "/" + p.Name); | 137 | log.Info("[PLUGINS]: Loading plugin " + n.Path); |
73 | } | 138 | } |
74 | } | 139 | } |
75 | 140 | ||