J2EEUtils.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  3. //
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //
  24. using System;
  25. using System.Web.Util;
  26. using System.IO;
  27. using [email protected];
  28. using vmw.common;
  29. using System.ComponentModel;
  30. using System.Threading;
  31. using javax.servlet;
  32. namespace System.Web.J2EE
  33. {
  34. internal static class J2EEUtils
  35. {
  36. #region InputStreamWrapper
  37. public sealed class InputStreamWrapper : Stream
  38. {
  39. readonly java.io.InputStream _ins;
  40. public InputStreamWrapper (java.io.InputStream ins) {
  41. _ins = ins;
  42. }
  43. public override bool CanRead {
  44. get { return true; }
  45. }
  46. public override bool CanSeek {
  47. get { return _ins.markSupported (); }
  48. }
  49. public override bool CanWrite {
  50. get { return false; }
  51. }
  52. public override void Flush () {
  53. }
  54. public override long Length {
  55. get { return _ins.available (); }
  56. }
  57. public override long Position {
  58. get {
  59. throw new NotSupportedException ();
  60. }
  61. set {
  62. throw new NotSupportedException ();
  63. }
  64. }
  65. public override int Read (byte [] buffer, int offset, int count) {
  66. int r = _ins.read (TypeUtils.ToSByteArray (buffer), offset, count);
  67. return r < 0 ? 0 : r;
  68. }
  69. public override long Seek (long offset, SeekOrigin origin) {
  70. throw new NotImplementedException ();
  71. }
  72. public override void SetLength (long value) {
  73. throw new NotSupportedException ();
  74. }
  75. public override void Write (byte [] buffer, int offset, int count) {
  76. throw new NotSupportedException ();
  77. }
  78. public override void Close () {
  79. _ins.close ();
  80. }
  81. }
  82. #endregion
  83. public static int RunProc(string[] cmd)
  84. {
  85. java.lang.Runtime rt = java.lang.Runtime.getRuntime();
  86. java.lang.Process proc = rt.exec(cmd);
  87. StreamGobbler errorGobbler = new
  88. StreamGobbler(proc.getErrorStream(), "ERROR");
  89. StreamGobbler outputGobbler = new
  90. StreamGobbler(proc.getInputStream(), "OUTPUT");
  91. errorGobbler.start();
  92. outputGobbler.start();
  93. int exitVal = proc.waitFor();
  94. return exitVal;
  95. }
  96. }
  97. public class StreamGobbler : java.lang.Thread
  98. {
  99. java.io.InputStream _is;
  100. String _type;
  101. public StreamGobbler(java.io.InputStream ins, String type)
  102. {
  103. this._is = ins;
  104. this._type = type;
  105. }
  106. public override void run()
  107. {
  108. try
  109. {
  110. java.io.InputStreamReader isr = new java.io.InputStreamReader(_is);
  111. java.io.BufferedReader br = new java.io.BufferedReader(isr);
  112. String line=null;
  113. while ( (line = br.readLine()) != null)
  114. {
  115. #if DEBUG
  116. Console.WriteLine(_type + ">" + line);
  117. #endif
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. #if DEBUG
  123. Console.WriteLine(ex);
  124. #endif
  125. }
  126. }
  127. }
  128. }
  129. #region FileSystemWatcher Stub
  130. namespace System.IO
  131. {
  132. [DefaultEvent ("Changed")]
  133. #if NET_2_0
  134. [IODescription ("")]
  135. #endif
  136. public class FileSystemWatcher : Component, ISupportInitialize
  137. {
  138. public FileSystemWatcher ()
  139. : this (String.Empty) {
  140. }
  141. public FileSystemWatcher (string path)
  142. : this (path, "*.*") {
  143. }
  144. public FileSystemWatcher (string path, string filter) {
  145. }
  146. #region Properties
  147. [DefaultValue (false)]
  148. [IODescription ("Flag to indicate if this instance is active")]
  149. public bool EnableRaisingEvents {
  150. get { return false; }
  151. set { }
  152. }
  153. [DefaultValue ("*.*")]
  154. [IODescription ("File name filter pattern")]
  155. [RecommendedAsConfigurable (true)]
  156. [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
  157. public string Filter {
  158. get { return "*.*"; }
  159. set { }
  160. }
  161. [DefaultValue (false)]
  162. [IODescription ("Flag to indicate we want to watch subdirectories")]
  163. public bool IncludeSubdirectories {
  164. get { return false; }
  165. set { }
  166. }
  167. [Browsable (false)]
  168. [DefaultValue (8192)]
  169. public int InternalBufferSize {
  170. get { return 8192; }
  171. set { }
  172. }
  173. [DefaultValue (NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite)]
  174. [IODescription ("Flag to indicate which change event we want to monitor")]
  175. public NotifyFilters NotifyFilter {
  176. get { return NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite; }
  177. set { }
  178. }
  179. [DefaultValue ("")]
  180. [IODescription ("The directory to monitor")]
  181. [RecommendedAsConfigurable (true)]
  182. [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
  183. [Editor ("System.Diagnostics.Design.FSWPathEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  184. public string Path {
  185. get { return String.Empty; }
  186. set { }
  187. }
  188. [DefaultValue (null)]
  189. [IODescription ("The object used to marshal the event handler calls resulting from a directory change")]
  190. #if NET_2_0
  191. [Browsable (false)]
  192. #endif
  193. public ISynchronizeInvoke SynchronizingObject {
  194. get { return null; }
  195. set { }
  196. }
  197. #endregion // Properties
  198. #region Methods
  199. protected override void Dispose (bool disposing) {
  200. base.Dispose (disposing);
  201. }
  202. enum EventType
  203. {
  204. FileSystemEvent,
  205. ErrorEvent,
  206. RenameEvent
  207. }
  208. private void RaiseEvent (Delegate ev, EventArgs arg, EventType evtype) {
  209. if (ev == null)
  210. return;
  211. if (SynchronizingObject == null) {
  212. Delegate [] delegates = ev.GetInvocationList ();
  213. if (evtype == EventType.RenameEvent) {
  214. foreach (RenamedEventHandler d in delegates) {
  215. d.BeginInvoke (this, (RenamedEventArgs) arg, null, null);
  216. }
  217. }
  218. else if (evtype == EventType.ErrorEvent) {
  219. foreach (ErrorEventHandler d in delegates) {
  220. d.BeginInvoke (this, (ErrorEventArgs) arg, null, null);
  221. }
  222. }
  223. else {
  224. foreach (FileSystemEventHandler d in delegates) {
  225. d.BeginInvoke (this, (FileSystemEventArgs) arg, null, null);
  226. }
  227. }
  228. return;
  229. }
  230. SynchronizingObject.BeginInvoke (ev, new object [] { this, arg });
  231. }
  232. protected void OnChanged (FileSystemEventArgs e) {
  233. RaiseEvent (Changed, e, EventType.FileSystemEvent);
  234. }
  235. protected void OnCreated (FileSystemEventArgs e) {
  236. RaiseEvent (Created, e, EventType.FileSystemEvent);
  237. }
  238. protected void OnDeleted (FileSystemEventArgs e) {
  239. RaiseEvent (Deleted, e, EventType.FileSystemEvent);
  240. }
  241. protected void OnError (ErrorEventArgs e) {
  242. RaiseEvent (Error, e, EventType.ErrorEvent);
  243. }
  244. protected void OnRenamed (RenamedEventArgs e) {
  245. RaiseEvent (Renamed, e, EventType.RenameEvent);
  246. }
  247. public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType) {
  248. return WaitForChanged (changeType, Timeout.Infinite);
  249. }
  250. public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout) {
  251. return new WaitForChangedResult ();
  252. }
  253. #endregion
  254. #region Events and Delegates
  255. [IODescription ("Occurs when a file/directory change matches the filter")]
  256. public event FileSystemEventHandler Changed;
  257. [IODescription ("Occurs when a file/directory creation matches the filter")]
  258. public event FileSystemEventHandler Created;
  259. [IODescription ("Occurs when a file/directory deletion matches the filter")]
  260. public event FileSystemEventHandler Deleted;
  261. [Browsable (false)]
  262. public event ErrorEventHandler Error;
  263. [IODescription ("Occurs when a file/directory rename matches the filter")]
  264. public event RenamedEventHandler Renamed;
  265. #endregion // Events and Delegates
  266. #region ISupportInitialize Members
  267. public void BeginInit () {
  268. }
  269. public void EndInit () {
  270. }
  271. #endregion
  272. }
  273. }
  274. #endregion