The Labs \ Source Viewer \ SSCLI \ System.Net.Configuration \ RequestCachingSectionInternal

  1. //------------------------------------------------------------------------------
  2. // <copyright file="RequestCachingSection.cs" company="Microsoft Corporation">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Net.Configuration
  16. {
  17.     using Microsoft.Win32;
  18.     using System.Configuration;
  19.     using System.Globalization;
  20.     using System.Net.Cache;
  21.     using System.Threading;
  22.    
  23.     public sealed class RequestCachingSection : ConfigurationSection
  24.     {
  25.         public RequestCachingSection()
  26.         {
  27.             this.properties.Add(this.disableAllCaching);
  28.             this.properties.Add(this.defaultPolicyLevel);
  29.             this.properties.Add(this.isPrivateCache);
  30.             this.properties.Add(this.defaultHttpCachePolicy);
  31.             this.properties.Add(this.defaultFtpCachePolicy);
  32.             this.properties.Add(this.unspecifiedMaximumAge);
  33.         }
  34.        
  35.         [ConfigurationProperty(ConfigurationStrings.DefaultHttpCachePolicy)]
  36.         public HttpCachePolicyElement DefaultHttpCachePolicy {
  37.             get { return (HttpCachePolicyElement)this[this.defaultHttpCachePolicy]; }
  38.         }
  39.        
  40.         [ConfigurationProperty(ConfigurationStrings.DefaultFtpCachePolicy)]
  41.         public FtpCachePolicyElement DefaultFtpCachePolicy {
  42.             get { return (FtpCachePolicyElement)this[this.defaultFtpCachePolicy]; }
  43.         }
  44.        
  45.         [ConfigurationProperty(ConfigurationStrings.DefaultPolicyLevel, DefaultValue = (RequestCacheLevel)RequestCacheLevel.BypassCache)]
  46.         public RequestCacheLevel DefaultPolicyLevel {
  47.             get { return (RequestCacheLevel)this[this.defaultPolicyLevel]; }
  48.             set { this[this.defaultPolicyLevel] = value; }
  49.         }
  50.        
  51.         [ConfigurationProperty(ConfigurationStrings.DisableAllCaching, DefaultValue = true)]
  52.         public bool DisableAllCaching {
  53.             get { return (bool)this[this.disableAllCaching]; }
  54.             set { this[this.disableAllCaching] = value; }
  55.         }
  56.        
  57.         [ConfigurationProperty(ConfigurationStrings.IsPrivateCache, DefaultValue = true)]
  58.         public bool IsPrivateCache {
  59.             get { return (bool)this[this.isPrivateCache]; }
  60.             set { this[this.isPrivateCache] = value; }
  61.         }
  62.        
  63.         [ConfigurationProperty(ConfigurationStrings.UnspecifiedMaximumAge, DefaultValue = "1.00:00:00")]
  64.         public TimeSpan UnspecifiedMaximumAge {
  65.             get { return (TimeSpan)this[this.unspecifiedMaximumAge]; }
  66.             set { this[this.unspecifiedMaximumAge] = value; }
  67.         }
  68.        
  69.         //
  70.         // If DisableAllCaching is set once to true it will not change.
  71.         //
  72.         protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
  73.         {
  74.            
  75.             bool tempDisableAllCaching = this.DisableAllCaching;
  76.            
  77.             base.DeserializeElement(reader, serializeCollectionKey);
  78.             if (tempDisableAllCaching) {
  79.                 this.DisableAllCaching = true;
  80.             }
  81.         }
  82.        
  83.         protected override void PostDeserialize()
  84.         {
  85.             // Perf optimization. If the configuration is coming from machine.config
  86.             // It is safe and we don't need to check for permissions.
  87.             if (EvaluationContext.IsMachineLevel)
  88.                 return;
  89.            
  90.             try {
  91.                 ExceptionHelper.WebPermissionUnrestricted.Demand();
  92.             }
  93.             catch (Exception exception) {
  94.                 throw new ConfigurationErrorsException(SR.GetString(SR.net_config_section_permission, ConfigurationStrings.RequestCachingSectionName), exception);
  95.             }
  96.         }
  97.        
  98.         protected override ConfigurationPropertyCollection Properties {
  99.             get { return this.properties; }
  100.         }
  101.        
  102.         ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
  103.        
  104.         readonly ConfigurationProperty defaultHttpCachePolicy = new ConfigurationProperty(ConfigurationStrings.DefaultHttpCachePolicy, typeof(HttpCachePolicyElement), null, ConfigurationPropertyOptions.None);
  105.        
  106.         readonly ConfigurationProperty defaultFtpCachePolicy = new ConfigurationProperty(ConfigurationStrings.DefaultFtpCachePolicy, typeof(FtpCachePolicyElement), null, ConfigurationPropertyOptions.None);
  107.        
  108.         readonly ConfigurationProperty defaultPolicyLevel = new ConfigurationProperty(ConfigurationStrings.DefaultPolicyLevel, typeof(RequestCacheLevel), RequestCacheLevel.BypassCache, ConfigurationPropertyOptions.None);
  109.        
  110.         readonly ConfigurationProperty disableAllCaching = new ConfigurationProperty(ConfigurationStrings.DisableAllCaching, typeof(bool), true, ConfigurationPropertyOptions.None);
  111.        
  112.         readonly ConfigurationProperty isPrivateCache = new ConfigurationProperty(ConfigurationStrings.IsPrivateCache, typeof(bool), true, ConfigurationPropertyOptions.None);
  113.        
  114.         readonly ConfigurationProperty unspecifiedMaximumAge = new ConfigurationProperty(ConfigurationStrings.UnspecifiedMaximumAge, typeof(TimeSpan), TimeSpan.FromDays(1), ConfigurationPropertyOptions.None);
  115.     }
  116.    
  117.     internal sealed class RequestCachingSectionInternal
  118.     {
  119.        
  120.         private RequestCachingSectionInternal()
  121.         {
  122.         }
  123.        
  124.         internal RequestCachingSectionInternal(RequestCachingSection section)
  125.         {
  126.             this.httpRequestCacheValidator = new HttpRequestCacheValidator(false, this.UnspecifiedMaximumAge);
  127.             this.disableAllCaching = true;
  128.            
  129.             FtpCachePolicyElement ftpPolicy = section.DefaultFtpCachePolicy;
  130.            
  131.             if (ftpPolicy.WasReadFromConfig) {
  132.                 this.defaultFtpCachePolicy = new RequestCachePolicy(ftpPolicy.PolicyLevel);
  133.             }
  134.            
  135.         }
  136.        
  137.         static internal object ClassSyncObject {
  138.             get {
  139.                 if (classSyncObject == null) {
  140.                     object o = new object();
  141.                     Interlocked.CompareExchange(ref classSyncObject, o, null);
  142.                 }
  143.                 return classSyncObject;
  144.             }
  145.         }
  146.        
  147.         internal bool DisableAllCaching {
  148.             get { return this.disableAllCaching; }
  149.         }
  150.        
  151.         internal RequestCache DefaultCache {
  152.             get { return this.defaultCache; }
  153.         }
  154.        
  155.         internal RequestCachePolicy DefaultCachePolicy {
  156.             get { return this.defaultCachePolicy; }
  157.         }
  158.        
  159.         internal bool IsPrivateCache {
  160.             get { return this.isPrivateCache; }
  161.         }
  162.        
  163.         internal TimeSpan UnspecifiedMaximumAge {
  164.             get { return this.unspecifiedMaximumAge; }
  165.         }
  166.        
  167.         internal HttpRequestCachePolicy DefaultHttpCachePolicy {
  168.             get { return this.defaultHttpCachePolicy; }
  169.         }
  170.        
  171.         internal RequestCachePolicy DefaultFtpCachePolicy {
  172.             get { return this.defaultFtpCachePolicy; }
  173.         }
  174.        
  175.         internal HttpRequestCacheValidator DefaultHttpValidator {
  176.             get { return this.httpRequestCacheValidator; }
  177.         }
  178.        
  179.         internal FtpRequestCacheValidator DefaultFtpValidator {
  180.             get { return this.ftpRequestCacheValidator; }
  181.         }
  182.        
  183.         static internal RequestCachingSectionInternal GetSection()
  184.         {
  185.             lock (RequestCachingSectionInternal.ClassSyncObject) {
  186.                 RequestCachingSection section = PrivilegedConfigurationManager.GetSection(ConfigurationStrings.RequestCachingSectionPath) as RequestCachingSection;
  187.                 if (section == null)
  188.                     return null;
  189.                
  190.                 try {
  191.                     return new RequestCachingSectionInternal(section);
  192.                 }
  193.                 catch (Exception exception) {
  194.                     if (NclUtilities.IsFatal(exception))
  195.                         throw;
  196.                    
  197.                     throw new ConfigurationErrorsException(SR.GetString(SR.net_config_requestcaching), exception);
  198.                 }
  199.                 catch {
  200.                     throw new ConfigurationErrorsException(SR.GetString(SR.net_config_requestcaching), new Exception(SR.GetString(SR.net_nonClsCompliantException)));
  201.                 }
  202.             }
  203.         }
  204.        
  205.         static object classSyncObject;
  206.         RequestCache defaultCache;
  207.         HttpRequestCachePolicy defaultHttpCachePolicy;
  208.         RequestCachePolicy defaultFtpCachePolicy;
  209.         RequestCachePolicy defaultCachePolicy;
  210.         bool disableAllCaching;
  211.         HttpRequestCacheValidator httpRequestCacheValidator;
  212.         FtpRequestCacheValidator ftpRequestCacheValidator;
  213.         bool isPrivateCache;
  214.         TimeSpan unspecifiedMaximumAge;
  215.     }
  216. }

Developer Fusion