aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorCharles Krinke2008-07-04 03:11:53 +0000
committerCharles Krinke2008-07-04 03:11:53 +0000
commit23ec21e44a2d7227ca56c31622e9b9b754e52879 (patch)
tree43c85dc02b176f03539ed8617e05d566a4d5db1d
parentMantis#1659. Thank you, Melanie for a patch that: (diff)
downloadopensim-SC_OLD-23ec21e44a2d7227ca56c31622e9b9b754e52879.zip
opensim-SC_OLD-23ec21e44a2d7227ca56c31622e9b9b754e52879.tar.gz
opensim-SC_OLD-23ec21e44a2d7227ca56c31622e9b9b754e52879.tar.bz2
opensim-SC_OLD-23ec21e44a2d7227ca56c31622e9b9b754e52879.tar.xz
Mantis#1647. Thank you very much, Sempuki for a patch that:
Updates the previous module loader work.
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/IPlugin.cs18
-rw-r--r--OpenSim/Framework/PluginLoader.cs195
-rw-r--r--OpenSim/Grid/GridServer/GridServerBase.cs16
-rw-r--r--OpenSim/Grid/GridServer/IGridPlugin.cs11
-rw-r--r--OpenSim/Region/Application/IApplicationPlugin.cs11
-rw-r--r--OpenSim/Region/Application/OpenSimBase.cs13
6 files changed, 184 insertions, 80 deletions
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
38 public PluginNotInitialisedException (string msg) : base(msg) {} 38 public PluginNotInitialisedException (string msg) : base(msg) {}
39 public PluginNotInitialisedException (string msg, Exception e) : base(msg, e) {} 39 public PluginNotInitialisedException (string msg, Exception e) : base(msg, e) {}
40 } 40 }
41 41
42 /// <summary> 42 /// <summary>
43 /// This interface, describes a generic plugin 43 /// This interface, describes a generic plugin
44 /// </summary> 44 /// </summary>
@@ -61,4 +61,20 @@ namespace OpenSim.Framework
61 /// </summary> 61 /// </summary>
62 void Initialise(); 62 void Initialise();
63 } 63 }
64
65 /// <summary>
66 /// Any plugins which need to pass parameters to their initialisers must
67 /// inherit this class and use it to set the PluginLoader Initialiser property
68 /// </summary>
69 public class PluginInitialiserBase
70 {
71 // this would be a lot simpler if C# supported currying or typedefs
72
73 // default initialisation
74 public virtual void Initialise (IPlugin plugin)
75 {
76 plugin.Initialise();
77 }
78 }
79
64} 80}
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
37 /// <summary> 37 /// <summary>
38 /// Exception thrown if an incorrect number of plugins are loaded 38 /// Exception thrown if an incorrect number of plugins are loaded
39 /// </summary> 39 /// </summary>
40 public class PluginCountInvalidException : Exception 40 public class PluginConstraintViolatedException : Exception
41 { 41 {
42 public PluginCountInvalidException () : base() {} 42 public PluginConstraintViolatedException () : base() {}
43 public PluginCountInvalidException (string msg) : base(msg) {} 43 public PluginConstraintViolatedException (string msg) : base(msg) {}
44 public PluginCountInvalidException (string msg, Exception e) : base(msg, e) {} 44 public PluginConstraintViolatedException (string msg, Exception e) : base(msg, e) {}
45 }
46
47 /// <summary>
48 /// Classes wishing to impose constraints on plugin loading must implement
49 /// this class and pass it to PluginLoader AddConstraint()
50 /// </summary>
51 public interface IPluginConstraint
52 {
53 bool Fail (string extpoint);
54 string Message { get; }
45 } 55 }
46 56
47 /// <summary> 57 /// <summary>
@@ -49,26 +59,42 @@ namespace OpenSim.Framework
49 /// </summary> 59 /// </summary>
50 public class PluginLoader <T> : IDisposable where T : IPlugin 60 public class PluginLoader <T> : IDisposable where T : IPlugin
51 { 61 {
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; 62 private const int max_loadable_plugins = 10000;
60 63
61 private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
62 private List<T> loaded = new List<T>(); 64 private List<T> loaded = new List<T>();
63 private List<string> extpoints = new List<string>(); 65 private List<string> extpoints = new List<string>();
64 private Dictionary<string,Range> constraints = new Dictionary<string,Range>(); 66 private PluginInitialiserBase initialiser;
67 private Dictionary<string,IPluginConstraint> constraints
68 = new Dictionary<string,IPluginConstraint>();
69
70 private static readonly ILog log
71 = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
65 72
66 public delegate void Initialiser (IPlugin p); 73 public PluginInitialiserBase Initialiser
67 private void default_initialiser_ (IPlugin p) { p.Initialise(); } 74 {
75 set { initialiser = value; }
76 get { return initialiser; }
77 }
78
79 public List<T> Plugins
80 {
81 get { return loaded; }
82 }
83
84 public PluginLoader ()
85 {
86 Initialiser = new PluginInitialiserBase();
87 }
68 88
69 public PluginLoader (string dir) 89 public PluginLoader (PluginInitialiserBase init)
70 { 90 {
71 AddPluginDir (dir); 91 Initialiser = init;
92 }
93
94 public PluginLoader (PluginInitialiserBase init, string dir)
95 {
96 Initialiser = init;
97 AddPluginDir (dir);
72 } 98 }
73 99
74 public void AddPluginDir (string dir) 100 public void AddPluginDir (string dir)
@@ -84,65 +110,45 @@ namespace OpenSim.Framework
84 extpoints.Add (extpoint); 110 extpoints.Add (extpoint);
85 } 111 }
86 112
87 public void AddConstrainedExtensionPoint (string extpoint, int min, int max) 113 public void AddConstraint (string extpoint, IPluginConstraint cons)
88 { 114 {
89 constraints.Add (extpoint, new Range (min, max)); 115 constraints.Add (extpoint, cons);
90 AddExtensionPoint (extpoint);
91 } 116 }
92 117
93 public void LoadAll (Initialiser initialise) 118 public void Load (string extpoint, string dir)
94 { 119 {
95 foreach (string pt in extpoints) 120 AddPluginDir (dir);
96 Load (pt, initialise); 121 AddExtensionPoint (extpoint);
97 } 122 Load();
98
99 public void Load (string extpoint)
100 {
101 Load (extpoint, default_initialiser_);
102 }
103
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 } 123 }
117 124
118 public void Load (string extpoint, Initialiser initialise, int min, int max) 125 public void Load ()
119 { 126 {
120 suppress_console_output_ (true); 127 suppress_console_output_ (true);
121 AddinManager.Registry.Update (null); 128 AddinManager.Registry.Update (null);
122 suppress_console_output_ (false); 129 suppress_console_output_ (false);
123 130
124 ExtensionNodeList ns = AddinManager.GetExtensionNodes(extpoint); 131 foreach (string ext in extpoints)
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
131 foreach (TypeExtensionNode n in ns)
132 { 132 {
133 T p = (T) n.CreateInstance(); 133 if (constraints.ContainsKey (ext))
134 initialise (p); 134 {
135 Plugins.Add (p); 135 IPluginConstraint cons = constraints [ext];
136 136 if (cons.Fail (ext))
137 log.Info("[PLUGINS]: Loading plugin " + n.Path); 137 throw new PluginConstraintViolatedException (cons.Message);
138 }
139
140 ExtensionNodeList ns = AddinManager.GetExtensionNodes (ext);
141 foreach (TypeExtensionNode n in ns)
142 {
143 T p = (T) n.CreateInstance();
144 Initialiser.Initialise (p);
145 Plugins.Add (p);
146
147 log.Info("[PLUGINS]: Loading plugin " + n.Path);
148 }
138 } 149 }
139 } 150 }
140 151
141 public List<T> Plugins
142 {
143 get { return loaded; }
144 }
145
146 public void Dispose () 152 public void Dispose ()
147 { 153 {
148 foreach (T p in Plugins) 154 foreach (T p in Plugins)
@@ -174,6 +180,73 @@ namespace OpenSim.Framework
174 System.Console.SetOut(prev_console_); 180 System.Console.SetOut(prev_console_);
175 } 181 }
176 } 182 }
183 }
184
185 public class PluginCountConstraint : IPluginConstraint
186 {
187 private int min;
188 private int max;
189
190 public PluginCountConstraint (int exact)
191 {
192 min = exact;
193 max = exact;
194 }
195
196 public PluginCountConstraint (int minimum, int maximum)
197 {
198 min = minimum;
199 max = maximum;
200 }
201
202 public string Message
203 {
204 get
205 {
206 return "The number of plugins is constrained to the interval ["
207 + min + ", " + max + "]";
208 }
209 }
177 210
211 public bool Fail (string extpoint)
212 {
213 ExtensionNodeList ns = AddinManager.GetExtensionNodes (extpoint);
214 if ((ns.Count < min) || (ns.Count > max))
215 return true;
216 else
217 return false;
218 }
219 }
220
221 public class PluginFilenameConstraint : IPluginConstraint
222 {
223 private string filename;
224
225 public PluginFilenameConstraint (string name)
226 {
227 filename = name;
228
229 }
230
231 public string Message
232 {
233 get
234 {
235 return "The plugin must have the following name: " + filename;
236 }
237 }
238
239 public bool Fail (string extpoint)
240 {
241 ExtensionNodeList ns = AddinManager.GetExtensionNodes (extpoint);
242 if (ns.Count != 1)
243 return true;
244
245 string[] path = ns[0].Path.Split('/');
246 if (path [path.Length-1] == filename)
247 return false;
248
249 return true;
250 }
178 } 251 }
179} 252}
diff --git a/OpenSim/Grid/GridServer/GridServerBase.cs b/OpenSim/Grid/GridServer/GridServerBase.cs
index 010c250..3cd7489 100644
--- a/OpenSim/Grid/GridServer/GridServerBase.cs
+++ b/OpenSim/Grid/GridServer/GridServerBase.cs
@@ -87,7 +87,7 @@ namespace OpenSim.Grid.GridServer
87 87
88 AddHttpHandlers(); 88 AddHttpHandlers();
89 89
90 LoadGridPlugins(); 90 LoadPlugins();
91 91
92 m_httpServer.Start(); 92 m_httpServer.Start();
93 93
@@ -116,16 +116,12 @@ namespace OpenSim.Grid.GridServer
116 m_httpServer.AddStreamHandler(new RestStreamHandler("POST", "/regions/", m_gridManager.RestSetRegionMethod)); 116 m_httpServer.AddStreamHandler(new RestStreamHandler("POST", "/regions/", m_gridManager.RestSetRegionMethod));
117 } 117 }
118 118
119 protected void grid_plugin_initialiser_ (IPlugin plugin) 119 protected void LoadPlugins()
120 { 120 {
121 IGridPlugin p = plugin as IGridPlugin; 121 PluginLoader<IGridPlugin> loader =
122 p.Initialise (this); 122 new PluginLoader<IGridPlugin> (new GridPluginInitialiser (this));
123 }
124 123
125 protected void LoadGridPlugins() 124 loader.Load ("/OpenSim/GridServer", ".");
126 {
127 PluginLoader<IGridPlugin> loader = new PluginLoader<IGridPlugin> (".");
128 loader.Load ("/OpenSim/GridServer", grid_plugin_initialiser_);
129 m_plugins = loader.Plugins; 125 m_plugins = loader.Plugins;
130 } 126 }
131 127
@@ -181,7 +177,7 @@ namespace OpenSim.Grid.GridServer
181 177
182 public override void Shutdown() 178 public override void Shutdown()
183 { 179 {
184 foreach (IGridPlugin plugin in m_plugins) plugin.Close(); 180 foreach (IGridPlugin plugin in m_plugins) plugin.Dispose();
185 181
186 base.Shutdown(); 182 base.Shutdown();
187 } 183 }
diff --git a/OpenSim/Grid/GridServer/IGridPlugin.cs b/OpenSim/Grid/GridServer/IGridPlugin.cs
index d51deb3..6593962 100644
--- a/OpenSim/Grid/GridServer/IGridPlugin.cs
+++ b/OpenSim/Grid/GridServer/IGridPlugin.cs
@@ -39,4 +39,15 @@ namespace OpenSim.Grid.GridServer
39 void Initialise(GridServerBase gridServer); 39 void Initialise(GridServerBase gridServer);
40 void Close(); 40 void Close();
41 } 41 }
42
43 public class GridPluginInitialiser : PluginInitialiserBase
44 {
45 private GridServerBase server;
46 public GridPluginInitialiser (GridServerBase s) { server = s; }
47 public override void Initialise (IPlugin plugin)
48 {
49 IGridPlugin p = plugin as IGridPlugin;
50 p.Initialise (server);
51 }
52 }
42} 53}
diff --git a/OpenSim/Region/Application/IApplicationPlugin.cs b/OpenSim/Region/Application/IApplicationPlugin.cs
index e1187ae..f122925 100644
--- a/OpenSim/Region/Application/IApplicationPlugin.cs
+++ b/OpenSim/Region/Application/IApplicationPlugin.cs
@@ -37,4 +37,15 @@ namespace OpenSim
37 { 37 {
38 void Initialise(OpenSimBase openSim); 38 void Initialise(OpenSimBase openSim);
39 } 39 }
40
41 public class ApplicationPluginInitialiser : PluginInitialiserBase
42 {
43 private OpenSimBase server;
44 public ApplicationPluginInitialiser (OpenSimBase s) { server = s; }
45 public override void Initialise (IPlugin plugin)
46 {
47 IApplicationPlugin p = plugin as IApplicationPlugin;
48 p.Initialise (server);
49 }
50 }
40} 51}
diff --git a/OpenSim/Region/Application/OpenSimBase.cs b/OpenSim/Region/Application/OpenSimBase.cs
index 55d19c1..2c2ec09 100644
--- a/OpenSim/Region/Application/OpenSimBase.cs
+++ b/OpenSim/Region/Application/OpenSimBase.cs
@@ -332,16 +332,12 @@ namespace OpenSim
332 m_networkServersInfo.loadFromConfiguration(m_config.Source); 332 m_networkServersInfo.loadFromConfiguration(m_config.Source);
333 } 333 }
334 334
335 protected void plugin_initialiser_ (IPlugin plugin)
336 {
337 IApplicationPlugin p = plugin as IApplicationPlugin;
338 p.Initialise (this);
339 }
340
341 protected void LoadPlugins() 335 protected void LoadPlugins()
342 { 336 {
343 PluginLoader<IApplicationPlugin> loader = new PluginLoader<IApplicationPlugin> ("."); 337 PluginLoader<IApplicationPlugin> loader =
344 loader.Load ("/OpenSim/Startup", plugin_initialiser_); 338 new PluginLoader<IApplicationPlugin> (new ApplicationPluginInitialiser (this));
339
340 loader.Load ("/OpenSim/Startup", ".");
345 m_plugins = loader.Plugins; 341 m_plugins = loader.Plugins;
346 } 342 }
347 343
@@ -733,3 +729,4 @@ namespace OpenSim
733 729
734 730
735 731
732