2
0

OutputCache.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // Authors:
  3. // Marek Habersack <[email protected]>
  4. //
  5. // (C) 2010 Novell, Inc (http://novell.com/)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System.Configuration;
  28. using System.Configuration.Provider;
  29. using System.IO;
  30. using System.Runtime.Serialization.Formatters.Binary;
  31. using System.Security.Permissions;
  32. using System.Web;
  33. using System.Web.Configuration;
  34. namespace System.Web.Caching
  35. {
  36. public static class OutputCache
  37. {
  38. internal const string DEFAULT_PROVIDER_NAME = "AspNetInternalProvider";
  39. static readonly object initLock = new object ();
  40. static bool initialized;
  41. static string defaultProviderName;
  42. static OutputCacheProviderCollection providers;
  43. public static string DefaultProviderName {
  44. get {
  45. Init ();
  46. if (String.IsNullOrEmpty (defaultProviderName))
  47. return DEFAULT_PROVIDER_NAME;
  48. return defaultProviderName;
  49. }
  50. }
  51. public static OutputCacheProviderCollection Providers {
  52. get {
  53. Init ();
  54. return providers;
  55. }
  56. }
  57. [SecurityPermission (SecurityAction.Assert, Flags = SecurityPermissionFlag.SerializationFormatter)]
  58. public static object Deserialize (Stream stream)
  59. {
  60. if (stream == null)
  61. throw new ArgumentNullException ("stream");
  62. object o = new BinaryFormatter ().Deserialize (stream);
  63. if (o == null || IsInvalidType (o))
  64. throw new ArgumentException ("The provided parameter is not of a supported type for serialization and/or deserialization.");
  65. return o;
  66. }
  67. [SecurityPermission (SecurityAction.Assert, Flags = SecurityPermissionFlag.SerializationFormatter)]
  68. public static void Serialize (Stream stream, object data)
  69. {
  70. if (stream == null)
  71. throw new ArgumentNullException ("stream");
  72. // LAMESPEC: data == null doesn't throw ArgumentNullException
  73. if (data == null || IsInvalidType (data))
  74. throw new ArgumentException ("The provided parameter is not of a supported type for serialization and/or deserialization.");
  75. new BinaryFormatter ().Serialize (stream, data);
  76. }
  77. static bool IsInvalidType (object data)
  78. {
  79. return !(data is MemoryResponseElement) &&
  80. !(data is FileResponseElement) &&
  81. !(data is SubstitutionResponseElement);
  82. }
  83. static void Init ()
  84. {
  85. if (initialized)
  86. return;
  87. lock (initLock) {
  88. if (initialized)
  89. return;
  90. var cfg = WebConfigurationManager.GetWebApplicationSection ("system.web/caching/outputCache") as OutputCacheSection;
  91. ProviderSettingsCollection cfgProviders = cfg.Providers;
  92. defaultProviderName = cfg.DefaultProviderName;
  93. if (cfgProviders != null && cfgProviders.Count > 0) {
  94. var coll = new OutputCacheProviderCollection ();
  95. foreach (ProviderSettings ps in cfgProviders)
  96. coll.Add (LoadProvider (ps));
  97. coll.SetReadOnly ();
  98. providers = coll;
  99. }
  100. initialized = true;
  101. }
  102. }
  103. static OutputCacheProvider LoadProvider (ProviderSettings ps)
  104. {
  105. Type type = HttpApplication.LoadType (ps.Type, false);
  106. if (type == null)
  107. throw new ConfigurationErrorsException (String.Format ("Could not load type '{0}'.", ps.Type));
  108. var ret = Activator.CreateInstance (type) as OutputCacheProvider;
  109. ret.Initialize (ps.Name, ps.Parameters);
  110. return ret;
  111. }
  112. internal static void RemoveFromProvider (string key, string providerName)
  113. {
  114. if (providerName == null)
  115. return;
  116. OutputCacheProviderCollection providers = Providers;
  117. OutputCacheProvider provider;
  118. if (providers == null || providers.Count == 0)
  119. provider = null;
  120. else
  121. provider = providers [providerName];
  122. if (provider == null)
  123. throw new ProviderException ("Provider '" + providerName + "' was not found.");
  124. provider.Remove (key);
  125. }
  126. }
  127. }