SecurityContextProviderAttribute.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // .NET Compact Framework 1.0 has no support for reading assembly attributes
  20. #if !NETCF
  21. using System;
  22. using System.Reflection;
  23. using log4net.Util;
  24. using log4net.Repository;
  25. using log4net.Core;
  26. namespace log4net.Config
  27. {
  28. /// <summary>
  29. /// Assembly level attribute to configure the <see cref="SecurityContextProvider"/>.
  30. /// </summary>
  31. /// <remarks>
  32. /// <para>
  33. /// This attribute may only be used at the assembly scope and can only
  34. /// be used once per assembly.
  35. /// </para>
  36. /// <para>
  37. /// Use this attribute to configure the <see cref="XmlConfigurator"/>
  38. /// without calling one of the <see cref="M:XmlConfigurator.Configure()"/>
  39. /// methods.
  40. /// </para>
  41. /// </remarks>
  42. /// <author>Nicko Cadell</author>
  43. [AttributeUsage(AttributeTargets.Assembly)]
  44. [Serializable]
  45. public sealed class SecurityContextProviderAttribute : ConfiguratorAttribute
  46. {
  47. #region Constructor
  48. /// <summary>
  49. /// Construct provider attribute with type specified
  50. /// </summary>
  51. /// <param name="providerType">the type of the provider to use</param>
  52. /// <remarks>
  53. /// <para>
  54. /// The provider specified must subclass the <see cref="SecurityContextProvider"/>
  55. /// class.
  56. /// </para>
  57. /// </remarks>
  58. public SecurityContextProviderAttribute(Type providerType) : base(100) /* configurator priority 100 to execute before the XmlConfigurator */
  59. {
  60. m_providerType = providerType;
  61. }
  62. #endregion
  63. #region Public Instance Properties
  64. /// <summary>
  65. /// Gets or sets the type of the provider to use.
  66. /// </summary>
  67. /// <value>
  68. /// the type of the provider to use.
  69. /// </value>
  70. /// <remarks>
  71. /// <para>
  72. /// The provider specified must subclass the <see cref="SecurityContextProvider"/>
  73. /// class.
  74. /// </para>
  75. /// </remarks>
  76. public Type ProviderType
  77. {
  78. get { return m_providerType; }
  79. set { m_providerType = value; }
  80. }
  81. #endregion Public Instance Properties
  82. #region Override ConfiguratorAttribute
  83. /// <summary>
  84. /// Configures the SecurityContextProvider
  85. /// </summary>
  86. /// <param name="sourceAssembly">The assembly that this attribute was defined on.</param>
  87. /// <param name="targetRepository">The repository to configure.</param>
  88. /// <remarks>
  89. /// <para>
  90. /// Creates a provider instance from the <see cref="ProviderType"/> specified.
  91. /// Sets this as the default security context provider <see cref="SecurityContextProvider.DefaultProvider"/>.
  92. /// </para>
  93. /// </remarks>
  94. override public void Configure(Assembly sourceAssembly, ILoggerRepository targetRepository)
  95. {
  96. if (m_providerType == null)
  97. {
  98. LogLog.Error(declaringType, "Attribute specified on assembly ["+sourceAssembly.FullName+"] with null ProviderType.");
  99. }
  100. else
  101. {
  102. LogLog.Debug(declaringType, "Creating provider of type ["+ m_providerType.FullName +"]");
  103. SecurityContextProvider provider = Activator.CreateInstance(m_providerType) as SecurityContextProvider;
  104. if (provider == null)
  105. {
  106. LogLog.Error(declaringType, "Failed to create SecurityContextProvider instance of type ["+m_providerType.Name+"].");
  107. }
  108. else
  109. {
  110. SecurityContextProvider.DefaultProvider = provider;
  111. }
  112. }
  113. }
  114. #endregion
  115. #region Private Instance Fields
  116. private Type m_providerType = null;
  117. #endregion Private Instance Fields
  118. #region Private Static Fields
  119. /// <summary>
  120. /// The fully qualified type of the SecurityContextProviderAttribute class.
  121. /// </summary>
  122. /// <remarks>
  123. /// Used by the internal logger to record the Type of the
  124. /// log message.
  125. /// </remarks>
  126. private readonly static Type declaringType = typeof(SecurityContextProviderAttribute);
  127. #endregion Private Static Fields
  128. }
  129. }
  130. #endif // !NETCF