Fix for running against RabbitMQ 2.2
[mono.git] / mcs / class / System.Web / System.Web.J2EE / J2EEUtils.cs
1 //
2 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
3 //
4
5 //
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:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 //
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.
24 //
25
26 using System;
27 using System.Web.Util;
28 using System.IO;
29 using vmw.@internal.io;
30 using vmw.common;
31 using System.ComponentModel;
32 using System.Threading;
33 using javax.servlet;
34 using System.Diagnostics;
35
36 namespace System.Web.J2EE
37 {
38         internal static class J2EEUtils
39         {
40                 #region InputStreamWrapper
41
42                 public sealed class InputStreamWrapper : Stream
43                 {
44                         readonly java.io.InputStream _ins;
45
46                         public InputStreamWrapper (java.io.InputStream ins) {
47                                 _ins = ins;
48                         }
49
50                         public override bool CanRead {
51                                 get { return true; }
52                         }
53
54                         public override bool CanSeek {
55                                 get { return _ins.markSupported (); }
56                         }
57
58                         public override bool CanWrite {
59                                 get { return false; }
60                         }
61
62                         public override void Flush () {
63                         }
64
65                         public override long Length {
66                                 get { return _ins.available (); }
67                         }
68
69                         public override long Position {
70                                 get {
71                                         throw new NotSupportedException ();
72                                 }
73                                 set {
74                                         throw new NotSupportedException ();
75                                 }
76                         }
77
78                         public override int Read (byte [] buffer, int offset, int count) {
79                                 int r = _ins.read (TypeUtils.ToSByteArray (buffer), offset, count);
80                                 return r < 0 ? 0 : r;
81                         }
82
83                         public override long Seek (long offset, SeekOrigin origin) {
84                                 throw new NotImplementedException ();
85                         }
86
87                         public override void SetLength (long value) {
88                                 throw new NotSupportedException ();
89                         }
90
91                         public override void Write (byte [] buffer, int offset, int count) {
92                                 throw new NotSupportedException ();
93                         }
94
95                         public override void Close () {
96                                 _ins.close ();
97                         }
98                 }
99
100                 #endregion
101
102                 public static int RunProc(string[] cmd)
103                 {       
104                         java.lang.Runtime rt = java.lang.Runtime.getRuntime();
105                         java.lang.Process proc = rt.exec(cmd);
106                         
107                         StreamGobbler errorGobbler = new 
108                                 StreamGobbler(proc.getErrorStream(), "ERROR");            
109           
110                         StreamGobbler outputGobbler = new 
111                                 StreamGobbler(proc.getInputStream(), "OUTPUT");
112                 
113                         errorGobbler.start();
114                         outputGobbler.start();
115                              
116                         int exitVal = proc.waitFor();
117                         return exitVal; 
118                 }
119         }
120
121         public class StreamGobbler : java.lang.Thread
122         {
123                 java.io.InputStream _is;
124                 String _type;
125     
126                 public StreamGobbler(java.io.InputStream ins, String type)
127                 {
128                         this._is = ins;
129                         this._type = type;
130                 }
131     
132                 public override void run()
133                 {
134                         try
135                         {
136                                 java.io.InputStreamReader isr = new java.io.InputStreamReader(_is);
137                                 java.io.BufferedReader br = new java.io.BufferedReader(isr);
138                                 String line=null;
139                                 while ( (line = br.readLine()) != null)
140                                 {
141                                         Debug.WriteLine(_type + ">" + line); 
142                                 }
143                         } 
144                         catch (Exception ex)
145                         {
146                                 Debug.WriteLine (ex);
147                         }
148                 }
149         }
150 }
151
152 #region FileSystemWatcher Stub
153
154 namespace System.IO
155 {
156         [DefaultEvent ("Changed")]
157 #if NET_2_0
158         [IODescription ("")]
159 #endif
160         public class FileSystemWatcher : Component, ISupportInitialize
161         {
162                 public FileSystemWatcher ()
163                         : this (String.Empty) {
164                 }
165
166                 public FileSystemWatcher (string path)
167                         : this (path, "*.*") {
168                 }
169
170                 public FileSystemWatcher (string path, string filter) {
171                 }
172
173                 #region Properties
174
175                 [DefaultValue (false)]
176                 [IODescription ("Flag to indicate if this instance is active")]
177                 public bool EnableRaisingEvents {
178                         get { return false; }
179                         set { }
180                 }
181
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 "*.*"; }
188                         set { }
189                 }
190
191                 [DefaultValue (false)]
192                 [IODescription ("Flag to indicate we want to watch subdirectories")]
193                 public bool IncludeSubdirectories {
194                         get { return false; }
195                         set { }
196                 }
197
198                 [Browsable (false)]
199                 [DefaultValue (8192)]
200                 public int InternalBufferSize {
201                         get { return 8192; }
202                         set { }
203                 }
204
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; }
209                         set { }
210                 }
211
212                 [DefaultValue ("")]
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)]
217                 public string Path {
218                         get { return String.Empty; }
219                         set { }
220                 }
221
222                 [DefaultValue (null)]
223                 [IODescription ("The object used to marshal the event handler calls resulting from a directory change")]
224 #if NET_2_0
225                 [Browsable (false)]
226 #endif
227                 public ISynchronizeInvoke SynchronizingObject {
228                         get { return null; }
229                         set { }
230                 }
231
232                 #endregion // Properties
233
234                 #region Methods
235
236                 protected override void Dispose (bool disposing) {
237                         base.Dispose (disposing);
238                 }
239
240                 enum EventType
241                 {
242                         FileSystemEvent,
243                         ErrorEvent,
244                         RenameEvent
245                 }
246
247                 void RaiseEvent (Delegate ev, EventArgs arg, EventType evtype) {
248                         if (ev == null)
249                                 return;
250
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);
256                                         }
257                                 }
258                                 else if (evtype == EventType.ErrorEvent) {
259                                         foreach (ErrorEventHandler d in delegates) {
260                                                 d.BeginInvoke (this, (ErrorEventArgs) arg, null, null);
261                                         }
262                                 }
263                                 else {
264                                         foreach (FileSystemEventHandler d in delegates) {
265                                                 d.BeginInvoke (this, (FileSystemEventArgs) arg, null, null);
266                                         }
267                                 }
268                                 return;
269                         }
270
271                         SynchronizingObject.BeginInvoke (ev, new object [] { this, arg });
272                 }
273
274                 protected void OnChanged (FileSystemEventArgs e) {
275                         RaiseEvent (Changed, e, EventType.FileSystemEvent);
276                 }
277
278                 protected void OnCreated (FileSystemEventArgs e) {
279                         RaiseEvent (Created, e, EventType.FileSystemEvent);
280                 }
281
282                 protected void OnDeleted (FileSystemEventArgs e) {
283                         RaiseEvent (Deleted, e, EventType.FileSystemEvent);
284                 }
285
286                 protected void OnError (ErrorEventArgs e) {
287                         RaiseEvent (Error, e, EventType.ErrorEvent);
288                 }
289
290                 protected void OnRenamed (RenamedEventArgs e) {
291                         RaiseEvent (Renamed, e, EventType.RenameEvent);
292                 }
293
294                 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType) {
295                         return WaitForChanged (changeType, Timeout.Infinite);
296                 }
297
298                 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout) {
299                         return new WaitForChangedResult ();
300                 }
301
302                 #endregion
303
304                 #region Events and Delegates
305
306                 [IODescription ("Occurs when a file/directory change matches the filter")]
307                 public event FileSystemEventHandler Changed;
308
309                 [IODescription ("Occurs when a file/directory creation matches the filter")]
310                 public event FileSystemEventHandler Created;
311
312                 [IODescription ("Occurs when a file/directory deletion matches the filter")]
313                 public event FileSystemEventHandler Deleted;
314
315                 [Browsable (false)]
316                 public event ErrorEventHandler Error;
317
318                 [IODescription ("Occurs when a file/directory rename matches the filter")]
319                 public event RenamedEventHandler Renamed;
320
321                 #endregion // Events and Delegates
322
323                 #region ISupportInitialize Members
324
325                 public void BeginInit () {
326                 }
327
328                 public void EndInit () {
329                 }
330
331                 #endregion
332         }
333 }
334 #endregion