PluginMap.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. using System.Collections;
  21. using log4net.Util;
  22. using log4net.Repository;
  23. namespace log4net.Plugin
  24. {
  25. /// <summary>
  26. /// Map of repository plugins.
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>
  30. /// This class is a name keyed map of the plugins that are
  31. /// attached to a repository.
  32. /// </para>
  33. /// </remarks>
  34. /// <author>Nicko Cadell</author>
  35. /// <author>Gert Driesen</author>
  36. public sealed class PluginMap
  37. {
  38. #region Public Instance Constructors
  39. /// <summary>
  40. /// Constructor
  41. /// </summary>
  42. /// <param name="repository">The repository that the plugins should be attached to.</param>
  43. /// <remarks>
  44. /// <para>
  45. /// Initialize a new instance of the <see cref="PluginMap" /> class with a
  46. /// repository that the plugins should be attached to.
  47. /// </para>
  48. /// </remarks>
  49. public PluginMap(ILoggerRepository repository)
  50. {
  51. m_repository = repository;
  52. }
  53. #endregion Public Instance Constructors
  54. #region Public Instance Properties
  55. /// <summary>
  56. /// Gets a <see cref="IPlugin" /> by name.
  57. /// </summary>
  58. /// <param name="name">The name of the <see cref="IPlugin" /> to lookup.</param>
  59. /// <returns>
  60. /// The <see cref="IPlugin" /> from the map with the name specified, or
  61. /// <c>null</c> if no plugin is found.
  62. /// </returns>
  63. /// <remarks>
  64. /// <para>
  65. /// Lookup a plugin by name. If the plugin is not found <c>null</c>
  66. /// will be returned.
  67. /// </para>
  68. /// </remarks>
  69. public IPlugin this[string name]
  70. {
  71. get
  72. {
  73. if (name == null)
  74. {
  75. throw new ArgumentNullException("name");
  76. }
  77. lock(this)
  78. {
  79. return (IPlugin)m_mapName2Plugin[name];
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Gets all possible plugins as a list of <see cref="IPlugin" /> objects.
  85. /// </summary>
  86. /// <value>All possible plugins as a list of <see cref="IPlugin" /> objects.</value>
  87. /// <remarks>
  88. /// <para>
  89. /// Get a collection of all the plugins defined in this map.
  90. /// </para>
  91. /// </remarks>
  92. public PluginCollection AllPlugins
  93. {
  94. get
  95. {
  96. lock(this)
  97. {
  98. return new PluginCollection(m_mapName2Plugin.Values);
  99. }
  100. }
  101. }
  102. #endregion Public Instance Properties
  103. #region Public Instance Methods
  104. /// <summary>
  105. /// Adds a <see cref="IPlugin" /> to the map.
  106. /// </summary>
  107. /// <param name="plugin">The <see cref="IPlugin" /> to add to the map.</param>
  108. /// <remarks>
  109. /// <para>
  110. /// The <see cref="IPlugin" /> will be attached to the repository when added.
  111. /// </para>
  112. /// <para>
  113. /// If there already exists a plugin with the same name
  114. /// attached to the repository then the old plugin will
  115. /// be <see cref="IPlugin.Shutdown"/> and replaced with
  116. /// the new plugin.
  117. /// </para>
  118. /// </remarks>
  119. public void Add(IPlugin plugin)
  120. {
  121. if (plugin == null)
  122. {
  123. throw new ArgumentNullException("plugin");
  124. }
  125. IPlugin curPlugin = null;
  126. lock(this)
  127. {
  128. // Get the current plugin if it exists
  129. curPlugin = m_mapName2Plugin[plugin.Name] as IPlugin;
  130. // Store new plugin
  131. m_mapName2Plugin[plugin.Name] = plugin;
  132. }
  133. // Shutdown existing plugin with same name
  134. if (curPlugin != null)
  135. {
  136. curPlugin.Shutdown();
  137. }
  138. // Attach new plugin to repository
  139. plugin.Attach(m_repository);
  140. }
  141. /// <summary>
  142. /// Removes a <see cref="IPlugin" /> from the map.
  143. /// </summary>
  144. /// <param name="plugin">The <see cref="IPlugin" /> to remove from the map.</param>
  145. /// <remarks>
  146. /// <para>
  147. /// Remove a specific plugin from this map.
  148. /// </para>
  149. /// </remarks>
  150. public void Remove(IPlugin plugin)
  151. {
  152. if (plugin == null)
  153. {
  154. throw new ArgumentNullException("plugin");
  155. }
  156. lock(this)
  157. {
  158. m_mapName2Plugin.Remove(plugin.Name);
  159. }
  160. }
  161. #endregion Public Instance Methods
  162. #region Private Instance Fields
  163. private readonly Hashtable m_mapName2Plugin = new Hashtable();
  164. private readonly ILoggerRepository m_repository;
  165. #endregion Private Instance Fields
  166. }
  167. }