diff options
author | UbitUmarov | 2015-09-01 11:43:07 +0100 |
---|---|---|
committer | UbitUmarov | 2015-09-01 11:43:07 +0100 |
commit | fb78b182520fc9bb0f971afd0322029c70278ea6 (patch) | |
tree | b4e30d383938fdeef8c92d1d1c2f44bb61d329bd /OpenSim/Framework/PluginLoader.cs | |
parent | lixo (diff) | |
parent | Mantis #7713: fixed bug introduced by 1st MOSES patch. (diff) | |
download | opensim-SC_OLD-fb78b182520fc9bb0f971afd0322029c70278ea6.zip opensim-SC_OLD-fb78b182520fc9bb0f971afd0322029c70278ea6.tar.gz opensim-SC_OLD-fb78b182520fc9bb0f971afd0322029c70278ea6.tar.bz2 opensim-SC_OLD-fb78b182520fc9bb0f971afd0322029c70278ea6.tar.xz |
Merge remote-tracking branch 'os/master'
Diffstat (limited to 'OpenSim/Framework/PluginLoader.cs')
-rw-r--r-- | OpenSim/Framework/PluginLoader.cs | 445 |
1 files changed, 445 insertions, 0 deletions
diff --git a/OpenSim/Framework/PluginLoader.cs b/OpenSim/Framework/PluginLoader.cs new file mode 100644 index 0000000..d12aa61 --- /dev/null +++ b/OpenSim/Framework/PluginLoader.cs | |||
@@ -0,0 +1,445 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.IO; | ||
31 | using System.Reflection; | ||
32 | using log4net; | ||
33 | using Mono.Addins; | ||
34 | |||
35 | namespace OpenSim.Framework | ||
36 | { | ||
37 | /// <summary> | ||
38 | /// Exception thrown if an incorrect number of plugins are loaded | ||
39 | /// </summary> | ||
40 | public class PluginConstraintViolatedException : Exception | ||
41 | { | ||
42 | public PluginConstraintViolatedException () : base() {} | ||
43 | public PluginConstraintViolatedException (string msg) : base(msg) {} | ||
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 | string Message { get; } | ||
54 | bool Apply(string extpoint); | ||
55 | } | ||
56 | |||
57 | /// <summary> | ||
58 | /// Classes wishing to select specific plugins from a range of possible options | ||
59 | /// must implement this class and pass it to PluginLoader Load() | ||
60 | /// </summary> | ||
61 | public interface IPluginFilter | ||
62 | { | ||
63 | bool Apply(PluginExtensionNode plugin); | ||
64 | } | ||
65 | |||
66 | /// <summary> | ||
67 | /// Generic Plugin Loader | ||
68 | /// </summary> | ||
69 | public class PluginLoader <T> : IDisposable where T : IPlugin | ||
70 | { | ||
71 | private const int max_loadable_plugins = 10000; | ||
72 | |||
73 | private List<T> loaded = new List<T>(); | ||
74 | private List<string> extpoints = new List<string>(); | ||
75 | private PluginInitialiserBase initialiser; | ||
76 | |||
77 | private Dictionary<string,IPluginConstraint> constraints | ||
78 | = new Dictionary<string,IPluginConstraint>(); | ||
79 | |||
80 | private Dictionary<string,IPluginFilter> filters | ||
81 | = new Dictionary<string,IPluginFilter>(); | ||
82 | |||
83 | private static readonly ILog log | ||
84 | = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
85 | |||
86 | public PluginInitialiserBase Initialiser | ||
87 | { | ||
88 | set { initialiser = value; } | ||
89 | get { return initialiser; } | ||
90 | } | ||
91 | |||
92 | public List<T> Plugins | ||
93 | { | ||
94 | get { return loaded; } | ||
95 | } | ||
96 | |||
97 | public T Plugin | ||
98 | { | ||
99 | get { return (loaded.Count == 1)? loaded [0] : default (T); } | ||
100 | } | ||
101 | |||
102 | public PluginLoader() | ||
103 | { | ||
104 | Initialiser = new PluginInitialiserBase(); | ||
105 | initialise_plugin_dir_("."); | ||
106 | } | ||
107 | |||
108 | public PluginLoader(PluginInitialiserBase init) | ||
109 | { | ||
110 | Initialiser = init; | ||
111 | initialise_plugin_dir_("."); | ||
112 | } | ||
113 | |||
114 | public PluginLoader(PluginInitialiserBase init, string dir) | ||
115 | { | ||
116 | Initialiser = init; | ||
117 | initialise_plugin_dir_(dir); | ||
118 | } | ||
119 | |||
120 | public void Add(string extpoint) | ||
121 | { | ||
122 | if (extpoints.Contains(extpoint)) | ||
123 | return; | ||
124 | |||
125 | extpoints.Add(extpoint); | ||
126 | } | ||
127 | |||
128 | public void Add(string extpoint, IPluginConstraint cons) | ||
129 | { | ||
130 | Add(extpoint); | ||
131 | AddConstraint(extpoint, cons); | ||
132 | } | ||
133 | |||
134 | public void Add(string extpoint, IPluginFilter filter) | ||
135 | { | ||
136 | Add(extpoint); | ||
137 | AddFilter(extpoint, filter); | ||
138 | } | ||
139 | |||
140 | public void AddConstraint(string extpoint, IPluginConstraint cons) | ||
141 | { | ||
142 | constraints.Add(extpoint, cons); | ||
143 | } | ||
144 | |||
145 | public void AddFilter(string extpoint, IPluginFilter filter) | ||
146 | { | ||
147 | filters.Add(extpoint, filter); | ||
148 | } | ||
149 | |||
150 | public void Load(string extpoint) | ||
151 | { | ||
152 | Add(extpoint); | ||
153 | Load(); | ||
154 | } | ||
155 | |||
156 | public void Load() | ||
157 | { | ||
158 | foreach (string ext in extpoints) | ||
159 | { | ||
160 | log.Info("[PLUGINS]: Loading extension point " + ext); | ||
161 | |||
162 | if (constraints.ContainsKey(ext)) | ||
163 | { | ||
164 | IPluginConstraint cons = constraints[ext]; | ||
165 | if (cons.Apply(ext)) | ||
166 | log.Error("[PLUGINS]: " + ext + " failed constraint: " + cons.Message); | ||
167 | } | ||
168 | |||
169 | IPluginFilter filter = null; | ||
170 | |||
171 | if (filters.ContainsKey(ext)) | ||
172 | filter = filters[ext]; | ||
173 | |||
174 | List<T> loadedPlugins = new List<T>(); | ||
175 | foreach (PluginExtensionNode node in AddinManager.GetExtensionNodes(ext)) | ||
176 | { | ||
177 | log.Info("[PLUGINS]: Trying plugin " + node.Path); | ||
178 | |||
179 | if ((filter != null) && (filter.Apply(node) == false)) | ||
180 | continue; | ||
181 | |||
182 | T plugin = (T)node.CreateInstance(); | ||
183 | loadedPlugins.Add(plugin); | ||
184 | } | ||
185 | |||
186 | // We do Initialise() in a second loop after CreateInstance | ||
187 | // So that modules who need init before others can do it | ||
188 | // Example: Script Engine Component System needs to load its components before RegionLoader starts | ||
189 | foreach (T plugin in loadedPlugins) | ||
190 | { | ||
191 | Initialiser.Initialise(plugin); | ||
192 | Plugins.Add(plugin); | ||
193 | } | ||
194 | } | ||
195 | } | ||
196 | |||
197 | /// <summary> | ||
198 | /// Unregisters Mono.Addins event handlers, allowing temporary Mono.Addins | ||
199 | /// data to be garbage collected. Since the plugins created by this loader | ||
200 | /// are meant to outlive the loader itself, they must be disposed separately | ||
201 | /// </summary> | ||
202 | public void Dispose() | ||
203 | { | ||
204 | AddinManager.AddinLoadError -= on_addinloaderror_; | ||
205 | AddinManager.AddinLoaded -= on_addinloaded_; | ||
206 | } | ||
207 | |||
208 | private void initialise_plugin_dir_(string dir) | ||
209 | { | ||
210 | if (AddinManager.IsInitialized == true) | ||
211 | return; | ||
212 | |||
213 | log.Info("[PLUGINS]: Initializing addin manager"); | ||
214 | |||
215 | AddinManager.AddinLoadError += on_addinloaderror_; | ||
216 | AddinManager.AddinLoaded += on_addinloaded_; | ||
217 | |||
218 | //clear_registry_(dir); | ||
219 | |||
220 | //suppress_console_output_(true); | ||
221 | AddinManager.Initialize(dir); | ||
222 | AddinManager.Registry.Update(null); | ||
223 | //suppress_console_output_(false); | ||
224 | } | ||
225 | |||
226 | private void on_addinloaded_(object sender, AddinEventArgs args) | ||
227 | { | ||
228 | log.Info ("[PLUGINS]: Plugin Loaded: " + args.AddinId); | ||
229 | } | ||
230 | |||
231 | private void on_addinloaderror_(object sender, AddinErrorEventArgs args) | ||
232 | { | ||
233 | if (args.Exception == null) | ||
234 | log.Error ("[PLUGINS]: Plugin Error: " | ||
235 | + args.Message); | ||
236 | else | ||
237 | log.Error ("[PLUGINS]: Plugin Error: " | ||
238 | + args.Exception.Message + "\n" | ||
239 | + args.Exception.StackTrace); | ||
240 | } | ||
241 | |||
242 | private void clear_registry_(string dir) | ||
243 | { | ||
244 | // The Mono addin manager (in Mono.Addins.dll version 0.2.0.0) | ||
245 | // occasionally seems to corrupt its addin cache | ||
246 | // Hence, as a temporary solution we'll remove it before each startup | ||
247 | |||
248 | try | ||
249 | { | ||
250 | if (Directory.Exists(dir + "/addin-db-000")) | ||
251 | Directory.Delete(dir + "/addin-db-000", true); | ||
252 | |||
253 | if (Directory.Exists(dir + "/addin-db-001")) | ||
254 | Directory.Delete(dir + "/addin-db-001", true); | ||
255 | } | ||
256 | catch (IOException) | ||
257 | { | ||
258 | // If multiple services are started simultaneously, they may | ||
259 | // each test whether the directory exists at the same time, and | ||
260 | // attempt to delete the directory at the same time. However, | ||
261 | // one of the services will likely succeed first, causing the | ||
262 | // second service to throw an IOException. We catch it here and | ||
263 | // continue on our merry way. | ||
264 | // Mike 2008.08.01, patch from Zaki | ||
265 | } | ||
266 | } | ||
267 | |||
268 | private static TextWriter prev_console_; | ||
269 | public void suppress_console_output_(bool save) | ||
270 | { | ||
271 | if (save) | ||
272 | { | ||
273 | prev_console_ = System.Console.Out; | ||
274 | System.Console.SetOut(new StreamWriter(Stream.Null)); | ||
275 | } | ||
276 | else | ||
277 | { | ||
278 | if (prev_console_ != null) | ||
279 | System.Console.SetOut(prev_console_); | ||
280 | } | ||
281 | } | ||
282 | } | ||
283 | |||
284 | public class PluginExtensionNode : ExtensionNode | ||
285 | { | ||
286 | [NodeAttribute] | ||
287 | string id = ""; | ||
288 | |||
289 | [NodeAttribute] | ||
290 | string provider = ""; | ||
291 | |||
292 | [NodeAttribute] | ||
293 | string type = ""; | ||
294 | |||
295 | Type typeobj; | ||
296 | |||
297 | public string ID { get { return id; } } | ||
298 | public string Provider { get { return provider; } } | ||
299 | public string TypeName { get { return type; } } | ||
300 | |||
301 | public Type TypeObject | ||
302 | { | ||
303 | get | ||
304 | { | ||
305 | if (typeobj != null) | ||
306 | return typeobj; | ||
307 | |||
308 | if (type.Length == 0) | ||
309 | throw new InvalidOperationException("Type name not specified."); | ||
310 | |||
311 | return typeobj = Addin.GetType(type, true); | ||
312 | } | ||
313 | } | ||
314 | |||
315 | public object CreateInstance() | ||
316 | { | ||
317 | return Activator.CreateInstance(TypeObject); | ||
318 | } | ||
319 | } | ||
320 | |||
321 | /// <summary> | ||
322 | /// Constraint that bounds the number of plugins to be loaded. | ||
323 | /// </summary> | ||
324 | public class PluginCountConstraint : IPluginConstraint | ||
325 | { | ||
326 | private int min; | ||
327 | private int max; | ||
328 | |||
329 | public PluginCountConstraint(int exact) | ||
330 | { | ||
331 | min = exact; | ||
332 | max = exact; | ||
333 | } | ||
334 | |||
335 | public PluginCountConstraint(int minimum, int maximum) | ||
336 | { | ||
337 | min = minimum; | ||
338 | max = maximum; | ||
339 | } | ||
340 | |||
341 | public string Message | ||
342 | { | ||
343 | get | ||
344 | { | ||
345 | return "The number of plugins is constrained to the interval [" | ||
346 | + min + ", " + max + "]"; | ||
347 | } | ||
348 | } | ||
349 | |||
350 | public bool Apply (string extpoint) | ||
351 | { | ||
352 | int count = AddinManager.GetExtensionNodes(extpoint).Count; | ||
353 | |||
354 | if ((count < min) || (count > max)) | ||
355 | throw new PluginConstraintViolatedException(Message); | ||
356 | |||
357 | return true; | ||
358 | } | ||
359 | } | ||
360 | |||
361 | /// <summary> | ||
362 | /// Filters out which plugin to load based on the plugin name or names given. Plugin names are contained in | ||
363 | /// their addin.xml | ||
364 | /// </summary> | ||
365 | public class PluginProviderFilter : IPluginFilter | ||
366 | { | ||
367 | private string[] m_filters; | ||
368 | |||
369 | /// <summary> | ||
370 | /// Constructor. | ||
371 | /// </summary> | ||
372 | /// <param name="p"> | ||
373 | /// Plugin name or names on which to filter. Multiple names should be separated by commas. | ||
374 | /// </param> | ||
375 | public PluginProviderFilter(string p) | ||
376 | { | ||
377 | m_filters = p.Split(','); | ||
378 | |||
379 | for (int i = 0; i < m_filters.Length; i++) | ||
380 | { | ||
381 | m_filters[i] = m_filters[i].Trim(); | ||
382 | } | ||
383 | } | ||
384 | |||
385 | /// <summary> | ||
386 | /// Apply this filter to the given plugin. | ||
387 | /// </summary> | ||
388 | /// <param name="plugin"></param> | ||
389 | /// <returns>true if the plugin's name matched one of the filters, false otherwise.</returns> | ||
390 | public bool Apply (PluginExtensionNode plugin) | ||
391 | { | ||
392 | for (int i = 0; i < m_filters.Length; i++) | ||
393 | { | ||
394 | if (m_filters[i] == plugin.Provider) | ||
395 | { | ||
396 | return true; | ||
397 | } | ||
398 | } | ||
399 | |||
400 | return false; | ||
401 | } | ||
402 | } | ||
403 | |||
404 | /// <summary> | ||
405 | /// Filters plugins according to their ID. Plugin IDs are contained in their addin.xml | ||
406 | /// </summary> | ||
407 | public class PluginIdFilter : IPluginFilter | ||
408 | { | ||
409 | private string[] m_filters; | ||
410 | |||
411 | /// <summary> | ||
412 | /// Constructor. | ||
413 | /// </summary> | ||
414 | /// <param name="p"> | ||
415 | /// Plugin ID or IDs on which to filter. Multiple names should be separated by commas. | ||
416 | /// </param> | ||
417 | public PluginIdFilter(string p) | ||
418 | { | ||
419 | m_filters = p.Split(','); | ||
420 | |||
421 | for (int i = 0; i < m_filters.Length; i++) | ||
422 | { | ||
423 | m_filters[i] = m_filters[i].Trim(); | ||
424 | } | ||
425 | } | ||
426 | |||
427 | /// <summary> | ||
428 | /// Apply this filter to <paramref name="plugin" />. | ||
429 | /// </summary> | ||
430 | /// <param name="plugin">PluginExtensionNode instance to check whether it passes the filter.</param> | ||
431 | /// <returns>true if the plugin's ID matches one of the filters, false otherwise.</returns> | ||
432 | public bool Apply (PluginExtensionNode plugin) | ||
433 | { | ||
434 | for (int i = 0; i < m_filters.Length; i++) | ||
435 | { | ||
436 | if (m_filters[i] == plugin.ID) | ||
437 | { | ||
438 | return true; | ||
439 | } | ||
440 | } | ||
441 | |||
442 | return false; | ||
443 | } | ||
444 | } | ||
445 | } | ||