2 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 using System.Web.Util;
29 using vmw.@internal.io;
31 using System.ComponentModel;
32 using System.Threading;
34 using System.Diagnostics;
36 namespace System.Web.J2EE
38 internal static class J2EEUtils
40 #region InputStreamWrapper
42 public sealed class InputStreamWrapper : Stream
44 readonly java.io.InputStream _ins;
46 public InputStreamWrapper (java.io.InputStream ins) {
50 public override bool CanRead {
54 public override bool CanSeek {
55 get { return _ins.markSupported (); }
58 public override bool CanWrite {
62 public override void Flush () {
65 public override long Length {
66 get { return _ins.available (); }
69 public override long Position {
71 throw new NotSupportedException ();
74 throw new NotSupportedException ();
78 public override int Read (byte [] buffer, int offset, int count) {
79 int r = _ins.read (TypeUtils.ToSByteArray (buffer), offset, count);
83 public override long Seek (long offset, SeekOrigin origin) {
84 throw new NotImplementedException ();
87 public override void SetLength (long value) {
88 throw new NotSupportedException ();
91 public override void Write (byte [] buffer, int offset, int count) {
92 throw new NotSupportedException ();
95 public override void Close () {
102 public static int RunProc(string[] cmd)
104 java.lang.Runtime rt = java.lang.Runtime.getRuntime();
105 java.lang.Process proc = rt.exec(cmd);
107 StreamGobbler errorGobbler = new
108 StreamGobbler(proc.getErrorStream(), "ERROR");
110 StreamGobbler outputGobbler = new
111 StreamGobbler(proc.getInputStream(), "OUTPUT");
113 errorGobbler.start();
114 outputGobbler.start();
116 int exitVal = proc.waitFor();
121 public class StreamGobbler : java.lang.Thread
123 java.io.InputStream _is;
126 public StreamGobbler(java.io.InputStream ins, String type)
132 public override void run()
136 java.io.InputStreamReader isr = new java.io.InputStreamReader(_is);
137 java.io.BufferedReader br = new java.io.BufferedReader(isr);
139 while ( (line = br.readLine()) != null)
141 Debug.WriteLine(_type + ">" + line);
146 Debug.WriteLine (ex);
152 #region FileSystemWatcher Stub
156 [DefaultEvent ("Changed")]
160 public class FileSystemWatcher : Component, ISupportInitialize
162 public FileSystemWatcher ()
163 : this (String.Empty) {
166 public FileSystemWatcher (string path)
167 : this (path, "*.*") {
170 public FileSystemWatcher (string path, string filter) {
175 [DefaultValue (false)]
176 [IODescription ("Flag to indicate if this instance is active")]
177 public bool EnableRaisingEvents {
178 get { return false; }
182 [DefaultValue ("*.*")]
183 [IODescription ("File name filter pattern")]
184 [RecommendedAsConfigurable (true)]
185 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
186 public string Filter {
187 get { return "*.*"; }
191 [DefaultValue (false)]
192 [IODescription ("Flag to indicate we want to watch subdirectories")]
193 public bool IncludeSubdirectories {
194 get { return false; }
199 [DefaultValue (8192)]
200 public int InternalBufferSize {
205 [DefaultValue (NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite)]
206 [IODescription ("Flag to indicate which change event we want to monitor")]
207 public NotifyFilters NotifyFilter {
208 get { return NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite; }
213 [IODescription ("The directory to monitor")]
214 [RecommendedAsConfigurable (true)]
215 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
216 [Editor ("System.Diagnostics.Design.FSWPathEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
218 get { return String.Empty; }
222 [DefaultValue (null)]
223 [IODescription ("The object used to marshal the event handler calls resulting from a directory change")]
227 public ISynchronizeInvoke SynchronizingObject {
232 #endregion // Properties
236 protected override void Dispose (bool disposing) {
237 base.Dispose (disposing);
247 void RaiseEvent (Delegate ev, EventArgs arg, EventType evtype) {
251 if (SynchronizingObject == null) {
252 Delegate [] delegates = ev.GetInvocationList ();
253 if (evtype == EventType.RenameEvent) {
254 foreach (RenamedEventHandler d in delegates) {
255 d.BeginInvoke (this, (RenamedEventArgs) arg, null, null);
258 else if (evtype == EventType.ErrorEvent) {
259 foreach (ErrorEventHandler d in delegates) {
260 d.BeginInvoke (this, (ErrorEventArgs) arg, null, null);
264 foreach (FileSystemEventHandler d in delegates) {
265 d.BeginInvoke (this, (FileSystemEventArgs) arg, null, null);
271 SynchronizingObject.BeginInvoke (ev, new object [] { this, arg });
274 protected void OnChanged (FileSystemEventArgs e) {
275 RaiseEvent (Changed, e, EventType.FileSystemEvent);
278 protected void OnCreated (FileSystemEventArgs e) {
279 RaiseEvent (Created, e, EventType.FileSystemEvent);
282 protected void OnDeleted (FileSystemEventArgs e) {
283 RaiseEvent (Deleted, e, EventType.FileSystemEvent);
286 protected void OnError (ErrorEventArgs e) {
287 RaiseEvent (Error, e, EventType.ErrorEvent);
290 protected void OnRenamed (RenamedEventArgs e) {
291 RaiseEvent (Renamed, e, EventType.RenameEvent);
294 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType) {
295 return WaitForChanged (changeType, Timeout.Infinite);
298 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout) {
299 return new WaitForChangedResult ();
304 #region Events and Delegates
306 [IODescription ("Occurs when a file/directory change matches the filter")]
307 public event FileSystemEventHandler Changed;
309 [IODescription ("Occurs when a file/directory creation matches the filter")]
310 public event FileSystemEventHandler Created;
312 [IODescription ("Occurs when a file/directory deletion matches the filter")]
313 public event FileSystemEventHandler Deleted;
316 public event ErrorEventHandler Error;
318 [IODescription ("Occurs when a file/directory rename matches the filter")]
319 public event RenamedEventHandler Renamed;
321 #endregion // Events and Delegates
323 #region ISupportInitialize Members
325 public void BeginInit () {
328 public void EndInit () {