J2EEUtils.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. using System.Diagnostics;
  33. namespace System.Web.J2EE
  34. {
  35. internal static class J2EEUtils
  36. {
  37. #region InputStreamWrapper
  38. public sealed class InputStreamWrapper : Stream
  39. {
  40. readonly java.io.InputStream _ins;
  41. public InputStreamWrapper (java.io.InputStream ins) {
  42. _ins = ins;
  43. }
  44. public override bool CanRead {
  45. get { return true; }
  46. }
  47. public override bool CanSeek {
  48. get { return _ins.markSupported (); }
  49. }
  50. public override bool CanWrite {
  51. get { return false; }
  52. }
  53. public override void Flush () {
  54. }
  55. public override long Length {
  56. get { return _ins.available (); }
  57. }
  58. public override long Position {
  59. get {
  60. throw new NotSupportedException ();
  61. }
  62. set {
  63. throw new NotSupportedException ();
  64. }
  65. }
  66. public override int Read (byte [] buffer, int offset, int count) {
  67. int r = _ins.read (TypeUtils.ToSByteArray (buffer), offset, count);
  68. return r < 0 ? 0 : r;
  69. }
  70. public override long Seek (long offset, SeekOrigin origin) {
  71. throw new NotImplementedException ();
  72. }
  73. public override void SetLength (long value) {
  74. throw new NotSupportedException ();
  75. }
  76. public override void Write (byte [] buffer, int offset, int count) {
  77. throw new NotSupportedException ();
  78. }
  79. public override void Close () {
  80. _ins.close ();
  81. }
  82. }
  83. #endregion
  84. public static int RunProc(string[] cmd)
  85. {
  86. java.lang.Runtime rt = java.lang.Runtime.getRuntime();
  87. java.lang.Process proc = rt.exec(cmd);
  88. StreamGobbler errorGobbler = new
  89. StreamGobbler(proc.getErrorStream(), "ERROR");
  90. StreamGobbler outputGobbler = new
  91. StreamGobbler(proc.getInputStream(), "OUTPUT");
  92. errorGobbler.start();
  93. outputGobbler.start();
  94. int exitVal = proc.waitFor();
  95. return exitVal;
  96. }
  97. }
  98. public class StreamGobbler : java.lang.Thread
  99. {
  100. java.io.InputStream _is;
  101. String _type;
  102. public StreamGobbler(java.io.InputStream ins, String type)
  103. {
  104. this._is = ins;
  105. this._type = type;
  106. }
  107. public override void run()
  108. {
  109. try
  110. {
  111. java.io.InputStreamReader isr = new java.io.InputStreamReader(_is);
  112. java.io.BufferedReader br = new java.io.BufferedReader(isr);
  113. String line=null;
  114. while ( (line = br.readLine()) != null)
  115. {
  116. Debug.WriteLine(_type + ">" + line);
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. Debug.WriteLine (ex);
  122. }
  123. }
  124. }
  125. }
  126. #region FileSystemWatcher Stub
  127. namespace System.IO
  128. {
  129. [DefaultEvent ("Changed")]
  130. #if NET_2_0
  131. [IODescription ("")]
  132. #endif
  133. public class FileSystemWatcher : Component, ISupportInitialize
  134. {
  135. public FileSystemWatcher ()
  136. : this (String.Empty) {
  137. }
  138. public FileSystemWatcher (string path)
  139. : this (path, "*.*") {
  140. }
  141. public FileSystemWatcher (string path, string filter) {
  142. }
  143. #region Properties
  144. [DefaultValue (false)]
  145. [IODescription ("Flag to indicate if this instance is active")]
  146. public bool EnableRaisingEvents {
  147. get { return false; }
  148. set { }
  149. }
  150. [DefaultValue ("*.*")]
  151. [IODescription ("File name filter pattern")]
  152. [RecommendedAsConfigurable (true)]
  153. [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
  154. public string Filter {
  155. get { return "*.*"; }
  156. set { }
  157. }
  158. [DefaultValue (false)]
  159. [IODescription ("Flag to indicate we want to watch subdirectories")]
  160. public bool IncludeSubdirectories {
  161. get { return false; }
  162. set { }
  163. }
  164. [Browsable (false)]
  165. [DefaultValue (8192)]
  166. public int InternalBufferSize {
  167. get { return 8192; }
  168. set { }
  169. }
  170. [DefaultValue (NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite)]
  171. [IODescription ("Flag to indicate which change event we want to monitor")]
  172. public NotifyFilters NotifyFilter {
  173. get { return NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite; }
  174. set { }
  175. }
  176. [DefaultValue ("")]
  177. [IODescription ("The directory to monitor")]
  178. [RecommendedAsConfigurable (true)]
  179. [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
  180. [Editor ("System.Diagnostics.Design.FSWPathEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  181. public string Path {
  182. get { return String.Empty; }
  183. set { }
  184. }
  185. [DefaultValue (null)]
  186. [IODescription ("The object used to marshal the event handler calls resulting from a directory change")]
  187. #if NET_2_0
  188. [Browsable (false)]
  189. #endif
  190. public ISynchronizeInvoke SynchronizingObject {
  191. get { return null; }
  192. set { }
  193. }
  194. #endregion // Properties
  195. #region Methods
  196. protected override void Dispose (bool disposing) {
  197. base.Dispose (disposing);
  198. }
  199. enum EventType
  200. {
  201. FileSystemEvent,
  202. ErrorEvent,
  203. RenameEvent
  204. }
  205. void RaiseEvent (Delegate ev, EventArgs arg, EventType evtype) {
  206. if (ev == null)
  207. return;
  208. if (SynchronizingObject == null) {
  209. Delegate [] delegates = ev.GetInvocationList ();
  210. if (evtype == EventType.RenameEvent) {
  211. foreach (RenamedEventHandler d in delegates) {
  212. d.BeginInvoke (this, (RenamedEventArgs) arg, null, null);
  213. }
  214. }
  215. else if (evtype == EventType.ErrorEvent) {
  216. foreach (ErrorEventHandler d in delegates) {
  217. d.BeginInvoke (this, (ErrorEventArgs) arg, null, null);
  218. }
  219. }
  220. else {
  221. foreach (FileSystemEventHandler d in delegates) {
  222. d.BeginInvoke (this, (FileSystemEventArgs) arg, null, null);
  223. }
  224. }
  225. return;
  226. }
  227. SynchronizingObject.BeginInvoke (ev, new object [] { this, arg });
  228. }
  229. protected void OnChanged (FileSystemEventArgs e) {
  230. RaiseEvent (Changed, e, EventType.FileSystemEvent);
  231. }
  232. protected void OnCreated (FileSystemEventArgs e) {
  233. RaiseEvent (Created, e, EventType.FileSystemEvent);
  234. }
  235. protected void OnDeleted (FileSystemEventArgs e) {
  236. RaiseEvent (Deleted, e, EventType.FileSystemEvent);
  237. }
  238. protected void OnError (ErrorEventArgs e) {
  239. RaiseEvent (Error, e, EventType.ErrorEvent);
  240. }
  241. protected void OnRenamed (RenamedEventArgs e) {
  242. RaiseEvent (Renamed, e, EventType.RenameEvent);
  243. }
  244. public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType) {
  245. return WaitForChanged (changeType, Timeout.Infinite);
  246. }
  247. public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout) {
  248. return new WaitForChangedResult ();
  249. }
  250. #endregion
  251. #region Events and Delegates
  252. [IODescription ("Occurs when a file/directory change matches the filter")]
  253. public event FileSystemEventHandler Changed;
  254. [IODescription ("Occurs when a file/directory creation matches the filter")]
  255. public event FileSystemEventHandler Created;
  256. [IODescription ("Occurs when a file/directory deletion matches the filter")]
  257. public event FileSystemEventHandler Deleted;
  258. [Browsable (false)]
  259. public event ErrorEventHandler Error;
  260. [IODescription ("Occurs when a file/directory rename matches the filter")]
  261. public event RenamedEventHandler Renamed;
  262. #endregion // Events and Delegates
  263. #region ISupportInitialize Members
  264. public void BeginInit () {
  265. }
  266. public void EndInit () {
  267. }
  268. #endregion
  269. }
  270. }
  271. #endregion