2
0

J2EEUtils.cs 7.4 KB

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