Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Caching / AggregateCacheDependency.cs
1 //
2 // System.Web.Compilation.AggregateCacheDependency
3 //
4 // Authors:
5 //   Marek Habersack (grendello@gmail.com)
6 //
7 // (C) 2006 Marek Habersack
8 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Collections.Generic;
32 using System.Text;
33 using System.Web;
34
35 namespace System.Web.Caching 
36 {
37         public sealed class AggregateCacheDependency : CacheDependency
38         {
39                 object dependenciesLock = new object();
40                 List <CacheDependency> dependencies;
41                 
42                 public AggregateCacheDependency ()
43                 {
44                         FinishInit ();
45                 }
46
47                 public void Add (params CacheDependency [] dependencies)
48                 {
49                         if (dependencies == null)
50                                 throw new ArgumentNullException ("dependencies");
51                         if (dependencies.Length == 0)
52                                 return;
53                         
54                         bool somethingChanged = false;
55                         foreach (CacheDependency dep in dependencies)
56                                 if (dep == null || dep.IsUsed)
57                                         throw new InvalidOperationException ("Cache dependency already in use");
58                                 else if (!somethingChanged && dep != null && dep.HasChanged)
59                                         somethingChanged = true;
60
61                         lock (dependenciesLock) {
62                                 if (this.dependencies == null)
63                                         this.dependencies = new List <CacheDependency> (dependencies.Length);
64                                 foreach (CacheDependency dep in dependencies)
65                                         if (dep != null)
66                                                 dep.DependencyChanged += new EventHandler (OnAnyChanged);
67                                 
68                                 this.dependencies.AddRange (dependencies);
69                                 base.Start = DateTime.UtcNow;
70                         }
71                         if (somethingChanged)
72                                 base.NotifyDependencyChanged (this, null);
73                 }
74
75                 public override string GetUniqueID ()
76                 {
77                         if (dependencies == null || dependencies.Count == 0)
78                                 return null;
79                         
80                         StringBuilder sb = new StringBuilder ();
81                         lock (dependenciesLock) {
82                                 string depid = null;
83                                 foreach (CacheDependency dep in dependencies) {
84                                         depid = dep.GetUniqueID ();
85                                         if (String.IsNullOrEmpty (depid))
86                                                 return null;
87                                         sb.Append (depid);
88                                         sb.Append (';');
89                                 }
90                         }
91                         return sb.ToString ();
92                 }
93
94                 protected override void DependencyDispose ()
95                 {
96                         // MSDN doesn't document it as being part of the class, but assembly
97                         // comparison shows that it does exist in this type, so we're just calling
98                         // the base class here
99                         base.DependencyDispose ();
100                 }
101                 
102                 internal override void DependencyDisposeInternal ()
103                 {
104                         if (dependencies != null && dependencies.Count > 0)
105                                 foreach (CacheDependency dep in dependencies)
106                                         dep.DependencyChanged -= new EventHandler (OnAnyChanged);
107                 }
108                 
109                 void OnAnyChanged (object sender, EventArgs args)
110                 {
111                         base.NotifyDependencyChanged (sender, args);
112                 }
113         }
114 }
115
116
117