2005-09-02 Sebastien Pouliot <sebastien@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
34 namespace System.Web.Caching
35 {
36 #if NET_2_0
37         // CAS
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         public class CacheDependency: IDisposable {
41 #else
42         // CAS - no InheritanceDemand here as the class is sealed
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         public sealed class CacheDependency: IDisposable {
45 #endif
46                 string[] cachekeys;
47                 CacheDependency dependency;
48                 DateTime start;
49                 Cache cache;
50 #if !TARGET_JVM
51                 FileSystemWatcher[] watchers;
52 #endif
53                 bool hasChanged;
54                 object locker = new object ();
55                 
56                 public CacheDependency (string filename): this (new string[] { filename }, null, null, DateTime.Now)
57                 {
58                 }
59                 
60                 public CacheDependency (string[] filenames): this (filenames, null, null, DateTime.Now)
61                 {
62                 }
63                 
64                 public CacheDependency (string filename, DateTime start): this (new string[] { filename }, null, null, start)
65                 {
66                 }
67
68                 public CacheDependency (string [] filenames, DateTime start)
69                         : this (filenames, null, null, start)
70                 {
71                 }
72
73                 public CacheDependency (string[] filenames, string[] cachekeys): this (filenames, cachekeys, null, DateTime.Now)
74                 {
75                 }
76                 
77                 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency): this (filenames, cachekeys, dependency, DateTime.Now)
78                 {
79                 }
80                 
81                 public CacheDependency (string[] filenames, string[] cachekeys, DateTime start): this (filenames, cachekeys, null, start)
82                 {
83                 }
84                 
85                 public CacheDependency (string[] filenames, string[] cachekeys, CacheDependency dependency, DateTime start)
86                 {
87 #if !TARGET_JVM
88                         if (filenames != null) {
89                                 watchers = new FileSystemWatcher [filenames.Length];
90                                 for (int n=0; n<filenames.Length; n++) {
91                                         FileSystemWatcher watcher = new FileSystemWatcher ();
92                                         if (Directory.Exists (filenames [n])) {
93                                                 watcher.Path = filenames [n];
94                                         } else {
95                                                 string parentPath = Path.GetDirectoryName (filenames [n]);
96                                                 if (parentPath != null && Directory.Exists (parentPath)) {
97                                                         watcher.Path = parentPath;
98                                                         watcher.Filter = Path.GetFileName (filenames [n]);
99                                                 } else
100                                                         continue;
101                                         }
102                                         watcher.Created += new FileSystemEventHandler (OnChanged);
103                                         watcher.Changed += new FileSystemEventHandler (OnChanged);
104                                         watcher.Deleted += new FileSystemEventHandler (OnChanged);
105                                         watcher.Renamed += new RenamedEventHandler (OnChanged);
106                                         watcher.EnableRaisingEvents = true;
107                                         watchers [n] = watcher;
108                                 }
109                         }
110 #endif
111                         this.cachekeys = cachekeys;
112                         this.dependency = dependency;
113                         if (dependency != null)
114                                 dependency.DependencyChanged += new EventHandler (OnChildDependencyChanged);
115                         this.start = start;
116                 }
117                 
118                 void OnChanged (object sender, FileSystemEventArgs args)
119                 {
120                         if (DateTime.Now < start)
121                                 return;
122                         hasChanged = true;
123                         DisposeWatchers ();
124                         
125                         if (cache != null)
126                                 cache.CheckExpiration ();
127                 }
128                 
129 #if TARGET_JVM
130                 void DisposeWatchers ()
131                 {
132                 }
133 #else
134                 void DisposeWatchers ()
135                 {
136                         lock (locker) {
137                                 if (watchers != null) {
138                                         foreach (FileSystemWatcher w in watchers)
139                                                 if (w != null)
140                                                         w.Dispose ();
141                                 }
142                                 watchers = null;
143                         }
144                 }
145 #endif
146                 public void Dispose ()
147                 {
148                         DisposeWatchers ();
149                         if (dependency != null)
150                                 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
151                         cache = null;
152                 }
153                 
154                 internal void SetCache (Cache c)
155                 {
156                         cache = c;
157                 }
158                 
159                 public bool HasChanged {
160                         get {
161                                 if (hasChanged)
162                                         return true;
163
164                                 if (DateTime.Now < start)
165                                         return false;
166
167                                 if (cache != null) {
168                                         if (cachekeys != null) {
169                                                 foreach (string key in cachekeys) {
170                                                         if (cache.GetKeyLastChange (key) > start) {
171                                                                 hasChanged = true;
172                                                                 break;
173                                                         }
174                                                 }
175                                         }
176                                 }
177                                 if (hasChanged)
178                                         DisposeWatchers ();
179
180                                 return hasChanged;
181                         }
182                 }
183                 
184                 void OnChildDependencyChanged (object o, EventArgs a)
185                 {
186                         hasChanged = true;
187                         OnDependencyChanged ();
188                 }
189                 
190                 void OnDependencyChanged ()
191                 {
192                         if (DependencyChanged != null)
193                                 DependencyChanged (this, null);
194                 }
195                 
196                 internal event EventHandler DependencyChanged;
197         }
198 }