~ubuntu-branches/debian/sid/nunit/sid

« back to all changes in this revision

Viewing changes to src/ClientUtilities/util/AssemblyWatcher.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2014-09-16 13:43:36 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140916134336-kjxz48tty6lx2ja5
Tags: 2.6.3+dfsg-1
* [c7bd1b5] Imported Upstream version 2.6.3+dfsg
* [bcb4bf8] Move nunit-console-runner to GAC-installed libnunit2.6, 
  don't treat it as a private lib. This lib is signed, and treated 
  as a GAC lib by consumers such as MonoDevelop.
* [7f08e99] Bump version to 2.6.3 as required
* [84535eb] Refreshed patches
* [8479f61] Split package up into per-assembly packages. This makes 
  ABI tracking easier in the future, as we can meaningfully have GAC 
  policy for cases where ABI isn't truly bumped, and no policy for 
  cases where it is. For example, if nunit.framework bumps ABI but 
  nunit.core does not, previously we would need to rebuild everything 
  using NUnit, but under the new split packaging, that rebuild would 
  not be needed for apps only using nunit.core.
* [17a7dc7] Add missing nunit.mocks.dll to nunit.pc

Show diffs side-by-side

added added

removed removed

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