1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
// ****************************************************************
// This is free software licensed under the NUnit license. You
// may obtain a copy of the license as well as information regarding
// copyright ownership at http://nunit.org.
// ****************************************************************
using System;
using System.IO;
using System.Timers;
using NUnit.Core;
namespace NUnit.Util
{
/// <summary>
/// AssemblyWatcher keeps track of one or more assemblies to
/// see if they have changed. It incorporates a delayed notification
/// and uses a standard event to notify any interested parties
/// about the change. The path to the assembly is provided as
/// an argument to the event handler so that one routine can
/// be used to handle events from multiple watchers.
/// </summary>
public class AssemblyWatcher : IAssemblyWatcher
{
static Logger log = InternalTrace.GetLogger(typeof(AssemblyWatcher));
private FileSystemWatcher[] fileWatchers;
private FileInfo[] files;
protected System.Timers.Timer timer;
protected string changedAssemblyPath;
protected FileInfo GetFileInfo(int index)
{
return files[index];
}
public void Setup(int delay, string assemblyFileName)
{
Setup(delay, new string[] {assemblyFileName});
}
#if CLR_2_0 || CLR_4_0
public void Setup(int delay, System.Collections.Generic.IList<string> assemblies)
#else
public void Setup(int delay, System.Collections.IList assemblies)
#endif
{
log.Info("Setting up watcher");
files = new FileInfo[assemblies.Count];
fileWatchers = new FileSystemWatcher[assemblies.Count];
for (int i = 0; i < assemblies.Count; i++)
{
log.Debug("Setting up FileSystemWatcher for {0}", assemblies[i]);
files[i] = new FileInfo((string)assemblies[i]);
fileWatchers[i] = new FileSystemWatcher();
fileWatchers[i].Path = files[i].DirectoryName;
fileWatchers[i].Filter = files[i].Name;
fileWatchers[i].NotifyFilter = NotifyFilters.Size | NotifyFilters.LastWrite;
fileWatchers[i].Changed += new FileSystemEventHandler(OnChanged);
fileWatchers[i].EnableRaisingEvents = false;
}
timer = new System.Timers.Timer(delay);
timer.AutoReset = false;
timer.Enabled = false;
timer.Elapsed += new ElapsedEventHandler(OnTimer);
}
public void Start()
{
EnableWatchers( true );
}
public void Stop()
{
EnableWatchers( false );
}
private void EnableWatchers( bool enable )
{
if (fileWatchers != null)
foreach( FileSystemWatcher watcher in fileWatchers )
watcher.EnableRaisingEvents = enable;
}
public void FreeResources()
{
log.Info("FreeResources");
Stop();
if (fileWatchers != null)
{
foreach (FileSystemWatcher watcher in fileWatchers)
{
if (watcher != null)
{
watcher.Changed -= new FileSystemEventHandler(OnChanged);
watcher.Dispose();
}
}
}
if (timer != null)
{
timer.Stop();
timer.Close();
}
fileWatchers = null;
timer = null;
}
public event AssemblyChangedHandler AssemblyChanged;
protected void OnTimer(Object source, ElapsedEventArgs e)
{
lock(this)
{
log.Info("Timer expired");
PublishEvent();
timer.Enabled=false;
}
}
protected void OnChanged(object source, FileSystemEventArgs e)
{
log.Info("File {0} changed", e.Name);
changedAssemblyPath = e.FullPath;
if ( timer != null )
{
lock(this)
{
if(!timer.Enabled)
timer.Enabled=true;
log.Info("Setting timer");
timer.Start();
}
}
else
{
PublishEvent();
}
}
protected void PublishEvent()
{
if (AssemblyChanged != null)
{
log.Debug("Publishing Event to {0} listeners", AssemblyChanged.GetInvocationList().Length);
AssemblyChanged(changedAssemblyPath);
}
}
}
}
|