OutputCache.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.IO;
  29. using System.Runtime.Serialization.Formatters.Binary;
  30. using System.Security.Permissions;
  31. using System.Web;
  32. using System.Web.Configuration;
  33. namespace System.Web.Caching
  34. {
  35. public static class OutputCache
  36. {
  37. internal const string DEFAULT_PROVIDER_NAME = "AspNetInternalProvider";
  38. static readonly object initLock = new object ();
  39. static bool initialized;
  40. static string defaultProviderName;
  41. static OutputCacheProviderCollection providers;
  42. public static string DefaultProviderName {
  43. get {
  44. Init ();
  45. if (String.IsNullOrEmpty (defaultProviderName))
  46. return DEFAULT_PROVIDER_NAME;
  47. return defaultProviderName;
  48. }
  49. }
  50. public static OutputCacheProviderCollection Providers {
  51. get {
  52. Init ();
  53. return providers;
  54. }
  55. }
  56. [SecurityPermission (SecurityAction.Assert, Flags = SecurityPermissionFlag.SerializationFormatter)]
  57. public static object Deserialize (Stream stream)
  58. {
  59. if (stream == null)
  60. throw new ArgumentNullException ("stream");
  61. object o = new BinaryFormatter ().Deserialize (stream);
  62. if (o == null || IsInvalidType (o))
  63. throw new ArgumentException ("The provided parameter is not of a supported type for serialization and/or deserialization.");
  64. return o;
  65. }
  66. [SecurityPermission (SecurityAction.Assert, Flags = SecurityPermissionFlag.SerializationFormatter)]
  67. public static void Serialize (Stream stream, object data)
  68. {
  69. if (stream == null)
  70. throw new ArgumentNullException ("stream");
  71. // LAMESPEC: data == null doesn't throw ArgumentNullException
  72. if (data == null || IsInvalidType (data))
  73. throw new ArgumentException ("The provided parameter is not of a supported type for serialization and/or deserialization.");
  74. new BinaryFormatter ().Serialize (stream, data);
  75. }
  76. static bool IsInvalidType (object data)
  77. {
  78. return !(data is MemoryResponseElement) &&
  79. !(data is FileResponseElement) &&
  80. !(data is SubstitutionResponseElement);
  81. }
  82. static void Init ()
  83. {
  84. if (initialized)
  85. return;
  86. lock (initLock) {
  87. if (initialized)
  88. return;
  89. var cfg = WebConfigurationManager.GetWebApplicationSection ("system.web/caching/outputCache") as OutputCacheSection;
  90. ProviderSettingsCollection cfgProviders = cfg.Providers;
  91. defaultProviderName = cfg.DefaultProviderName;
  92. if (cfgProviders != null && cfgProviders.Count > 0) {
  93. var coll = new OutputCacheProviderCollection ();
  94. foreach (ProviderSettings ps in cfgProviders)
  95. coll.Add (LoadProvider (ps));
  96. coll.SetReadOnly ();
  97. providers = coll;
  98. }
  99. initialized = true;
  100. }
  101. }
  102. static OutputCacheProvider LoadProvider (ProviderSettings ps)
  103. {
  104. Type type = HttpApplication.LoadType (ps.Type, false);
  105. if (type == null)
  106. throw new ConfigurationErrorsException (String.Format ("Could not load type '{0}'.", ps.Type));
  107. var ret = Activator.CreateInstance (type) as OutputCacheProvider;
  108. ret.Initialize (ps.Name, ps.Parameters);
  109. return ret;
  110. }
  111. }
  112. }