2006-11-14 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Caching / CacheDependency.cs
1 //
2 // System.Web.Caching.CachedDependency
3 //
4 // Author(s):
5 //  Lluis Sanchez (lluis@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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.Collections;
31 using System.IO;
32 using System.Security.Permissions;
33 using System.Text;
34
35 namespace System.Web.Caching
36 {
37 #if NET_2_0
38         // CAS
39         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         public class CacheDependency: IDisposable {
42 #else
43         // CAS - no InheritanceDemand here as the class is sealed
44         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         public sealed class CacheDependency: IDisposable {
46 #endif
47                 string[] cachekeys;
48                 CacheDependency dependency;
49                 DateTime start;
50                 Cache cache;
51 #if !TARGET_JVM
52                 FileSystemWatcher[] watchers;
53 #endif
54                 bool hasChanged;
55                 object locker = new object ();
56 #if NET_2_0
57                 string unique_id;
58
59                 protected CacheDependency ()
60                 {
61                 }
62 #endif
63                 
64                 public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
65                 {
66                 }
67                 
68                 public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
69                 {
70                 }
71                 
72                 public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
73                 {
74                 }
75
76                 public CacheDependency (string [] filenames, DateTime start)
77                         : this (filenames, null, null, start)
78                 {
79                 }
80
81                 public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
82                 {
83                 }
84                 
85                 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
86                 {
87                 }
88                 
89                 public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
90                 {
91                 }
92                 
93                 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
94                 {
95 #if NET_2_0
96                         StringBuilder sb = new StringBuilder ();
97                         sb.Append ("@UID@");
98 #endif
99 #if !TARGET_JVM
100                         if (filenames != null) {
101                                 watchers = new FileSystemWatcher [filenames.Length];
102                                 for (int n=0; n<filenames.Length; n++) {
103                                         FileSystemWatcher watcher = new FileSystemWatcher ();
104 #if NET_2_0
105                                         sb.Append (filenames [n]);
106 #endif
107                                         if (Directory.Exists (filenames [n])) {
108                                                 watcher.Path = filenames [n];
109                                         } else {
110                                                 string parentPath = Path.GetDirectoryName (filenames [n]);
111                                                 if (parentPath != null && Directory.Exists (parentPath)) {
112                                                         watcher.Path = parentPath;
113                                                         watcher.Filter = Path.GetFileName (filenames [n]);
114                                                 } else
115                                                         continue;
116                                         }
117                                         watcher.Created += new FileSystemEventHandler (OnChanged);
118                                         watcher.Changed += new FileSystemEventHandler (OnChanged);
119                                         watcher.Deleted += new FileSystemEventHandler (OnChanged);
120                                         watcher.Renamed += new RenamedEventHandler (OnChanged);
121                                         watcher.EnableRaisingEvents = true;
122                                         watchers [n] = watcher;
123                                 }
124                         }
125 #endif
126                         this.cachekeys = cachekeys;
127 #if NET_2_0
128                         if (cachekeys != null) {
129                                 foreach (string s in cachekeys)
130                                         sb.Append (s);
131                         }
132                         if (sb.Length > 5) // 5 is the length of "@UID@"
133                                 unique_id = sb.ToString ();
134 #endif
135
136                         this.dependency = dependency;
137                         if (dependency != null)
138                                 dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
139                         this.start = start;
140                 }
141 #if !TARGET_JVM
142                 void OnChanged (object sender, FileSystemEventArgs args)
143                 {
144                         if (DateTime.Now < start)
145                                 return;
146                         hasChanged = true;
147                         DisposeWatchers ();
148                         
149                         if (cache != null)
150                                 cache.CheckExpiration ();
151                 }
152         
153                 void DisposeWatchers ()
154                 {
155                         lock (locker) {
156                                 if (watchers != null) {
157                                         foreach (FileSystemWatcher w in watchers)
158                                                 if (w != null)
159                                                         w.Dispose ();
160                                 }
161                                 watchers = null;
162                         }
163                 }
164 #else
165                 void DisposeWatchers ()
166                 {
167                 }
168 #endif
169                 public void Dispose ()
170                 {
171                         DisposeWatchers ();
172                         if (dependency != null)
173                                 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
174                         cache = null;
175                 }
176                 
177                 internal void SetCache (Cache c)
178                 {
179                         cache = c;
180                 }
181                 
182 #if NET_2_0
183                 public virtual string GetUniqueID ()
184                 {
185                         return unique_id;
186                 }
187
188                 public virtual bool HasChanged {
189 #else
190                 public bool HasChanged {
191 #endif
192                         get {
193                                 if (hasChanged)
194                                         return true;
195
196                                 if (DateTime.Now < start)
197                                         return false;
198
199                                 if (cache != null && cachekeys != null) {
200                                         foreach (string key in cachekeys) {
201                                                 if (cache.GetKeyLastChange (key) > start) {
202                                                         hasChanged = true;
203                                                         break;
204                                                 }
205                                         }
206                                 }
207                                 if (hasChanged)
208                                         DisposeWatchers ();
209
210                                 return hasChanged;
211                         }
212                 }
213                 
214                 void OnChildDependencyChanged (object o, EventArgs a)
215                 {
216                         hasChanged = true;
217                         OnDependencyChanged ();
218                 }
219                 
220                 internal void OnDependencyChanged ()
221                 {
222                         if (DependencyChanged != null)
223                                 DependencyChanged (this, null);
224                 }
225                 
226                 internal event EventHandler DependencyChanged;
227         }
228 }