PluginLoader.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 OpenSim 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. using System;
  28. using System.IO;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using log4net;
  32. using Mono.Addins;
  33. namespace OpenSim.Framework
  34. {
  35. /// <summary>
  36. /// Exception thrown if an incorrect number of plugins are loaded
  37. /// </summary>
  38. public class PluginConstraintViolatedException : Exception
  39. {
  40. public PluginConstraintViolatedException () : base() {}
  41. public PluginConstraintViolatedException (string msg) : base(msg) {}
  42. public PluginConstraintViolatedException (string msg, Exception e) : base(msg, e) {}
  43. }
  44. /// <summary>
  45. /// Classes wishing to impose constraints on plugin loading must implement
  46. /// this class and pass it to PluginLoader AddConstraint()
  47. /// </summary>
  48. public interface IPluginConstraint
  49. {
  50. bool Fail (string extpoint);
  51. string Message { get; }
  52. }
  53. /// <summary>
  54. /// Generic Plugin Loader
  55. /// </summary>
  56. public class PluginLoader <T> : IDisposable where T : IPlugin
  57. {
  58. private const int max_loadable_plugins = 10000;
  59. private List<T> loaded = new List<T>();
  60. private List<string> extpoints = new List<string>();
  61. private PluginInitialiserBase initialiser;
  62. private Dictionary<string,IPluginConstraint> constraints
  63. = new Dictionary<string,IPluginConstraint>();
  64. private static readonly ILog log
  65. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  66. public PluginInitialiserBase Initialiser
  67. {
  68. set { initialiser = value; }
  69. get { return initialiser; }
  70. }
  71. public List<T> Plugins
  72. {
  73. get { return loaded; }
  74. }
  75. public PluginLoader ()
  76. {
  77. Initialiser = new PluginInitialiserBase();
  78. }
  79. public PluginLoader (PluginInitialiserBase init)
  80. {
  81. Initialiser = init;
  82. }
  83. public PluginLoader (PluginInitialiserBase init, string dir)
  84. {
  85. Initialiser = init;
  86. AddPluginDir (dir);
  87. }
  88. public void AddPluginDir (string dir)
  89. {
  90. suppress_console_output_ (true);
  91. AddinManager.Initialize (dir);
  92. AddinManager.Registry.Update (null);
  93. suppress_console_output_ (false);
  94. }
  95. public void AddExtensionPoint (string extpoint)
  96. {
  97. extpoints.Add (extpoint);
  98. }
  99. public void AddConstraint (string extpoint, IPluginConstraint cons)
  100. {
  101. constraints.Add (extpoint, cons);
  102. }
  103. public void Load (string extpoint, string dir)
  104. {
  105. AddPluginDir (dir);
  106. AddExtensionPoint (extpoint);
  107. Load();
  108. }
  109. public void Load ()
  110. {
  111. suppress_console_output_ (true);
  112. AddinManager.Registry.Update (null);
  113. suppress_console_output_ (false);
  114. foreach (string ext in extpoints)
  115. {
  116. if (constraints.ContainsKey (ext))
  117. {
  118. IPluginConstraint cons = constraints [ext];
  119. if (cons.Fail (ext))
  120. throw new PluginConstraintViolatedException (cons.Message);
  121. }
  122. ExtensionNodeList ns = AddinManager.GetExtensionNodes (ext);
  123. foreach (TypeExtensionNode n in ns)
  124. {
  125. T p = (T) n.CreateInstance();
  126. Initialiser.Initialise (p);
  127. Plugins.Add (p);
  128. log.Info("[PLUGINS]: Loading plugin " + n.Path);
  129. }
  130. }
  131. }
  132. public void Dispose ()
  133. {
  134. foreach (T p in Plugins)
  135. p.Dispose ();
  136. }
  137. public void ClearCache()
  138. {
  139. // The Mono addin manager (in Mono.Addins.dll version 0.2.0.0) occasionally seems to corrupt its addin cache
  140. // Hence, as a temporary solution we'll remove it before each startup
  141. if (Directory.Exists("addin-db-000"))
  142. Directory.Delete("addin-db-000", true);
  143. if (Directory.Exists("addin-db-001"))
  144. Directory.Delete("addin-db-001", true);
  145. }
  146. private static TextWriter prev_console_;
  147. public void suppress_console_output_ (bool save)
  148. {
  149. if (save)
  150. {
  151. prev_console_ = System.Console.Out;
  152. System.Console.SetOut(new StreamWriter(Stream.Null));
  153. }
  154. else
  155. {
  156. if (prev_console_ != null)
  157. System.Console.SetOut(prev_console_);
  158. }
  159. }
  160. }
  161. public class PluginCountConstraint : IPluginConstraint
  162. {
  163. private int min;
  164. private int max;
  165. public PluginCountConstraint (int exact)
  166. {
  167. min = exact;
  168. max = exact;
  169. }
  170. public PluginCountConstraint (int minimum, int maximum)
  171. {
  172. min = minimum;
  173. max = maximum;
  174. }
  175. public string Message
  176. {
  177. get
  178. {
  179. return "The number of plugins is constrained to the interval ["
  180. + min + ", " + max + "]";
  181. }
  182. }
  183. public bool Fail (string extpoint)
  184. {
  185. ExtensionNodeList ns = AddinManager.GetExtensionNodes (extpoint);
  186. if ((ns.Count < min) || (ns.Count > max))
  187. return true;
  188. else
  189. return false;
  190. }
  191. }
  192. public class PluginFilenameConstraint : IPluginConstraint
  193. {
  194. private string filename;
  195. public PluginFilenameConstraint (string name)
  196. {
  197. filename = name;
  198. }
  199. public string Message
  200. {
  201. get
  202. {
  203. return "The plugin must have the following name: " + filename;
  204. }
  205. }
  206. public bool Fail (string extpoint)
  207. {
  208. ExtensionNodeList ns = AddinManager.GetExtensionNodes (extpoint);
  209. if (ns.Count != 1)
  210. return true;
  211. string[] path = ns[0].Path.Split('/');
  212. if (path [path.Length-1] == filename)
  213. return false;
  214. return true;
  215. }
  216. }
  217. }