ifdef TARGET_JVM
[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 #if !TARGET_JVM
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                 void DisposeWatchers ()
130                 {
131                         lock (locker) {
132                                 if (watchers != null) {
133                                         foreach (FileSystemWatcher w in watchers)
134                                                 if (w != null)
135                                                         w.Dispose ();
136                                 }
137                                 watchers = null;
138                         }
139                 }
140 #else
141                 void DisposeWatchers ()
142                 {
143                 }
144 #endif
145                 public void Dispose ()
146                 {
147                         DisposeWatchers ();
148                         if (dependency != null)
149                                 dependency.DependencyChanged -= new EventHandler (OnChildDependencyChanged);
150                         cache = null;
151                 }
152                 
153                 internal void SetCache (Cache c)
154                 {
155                         cache = c;
156                 }
157                 
158                 public bool HasChanged {
159                         get {
160                                 if (hasChanged)
161                                         return true;
162
163                                 if (DateTime.Now < start)
164                                         return false;
165
166                                 if (cache != null && cachekeys != null) {
167                                         foreach (string key in cachekeys) {
168                                                 if (cache.GetKeyLastChange (key) > start) {
169                                                         hasChanged = true;
170                                                         break;
171                                                 }
172                                         }
173                                 }
174                                 if (hasChanged)
175                                         DisposeWatchers ();
176
177                                 return hasChanged;
178                         }
179                 }
180                 
181                 void OnChildDependencyChanged (object o, EventArgs a)
182                 {
183                         hasChanged = true;
184                         OnDependencyChanged ();
185                 }
186                 
187                 void OnDependencyChanged ()
188                 {
189                         if (DependencyChanged != null)
190                                 DependencyChanged (this, null);
191                 }
192                 
193                 internal event EventHandler DependencyChanged;
194         }
195 }