merge r67228-r67235, r67237, r67251 and r67256-67259 to trunk (they are
[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
32 using javax.servlet;
33
34 namespace System.Web.J2EE
35 {
36         public class J2EEUtils
37         {
38                 public J2EEUtils()
39                 {
40                 }
41
42                 public static string GetApplicationRealPath(ServletConfig config) 
43                 {
44                         string realFs = config.getInitParameter(J2EEConsts.FILESYSTEM_ACCESS);
45                         if(realFs != null && realFs == J2EEConsts.ACCESS_FULL)
46                         {
47                                 try 
48                                 {
49                                         if(Path.IsPathRooted(config.getServletContext().getRealPath("")))
50                                                 return config.getServletContext().getRealPath("").Replace("\\","/").TrimEnd('/');
51                                 }
52                                 catch (ArgumentException e)
53                                 {
54                                         Console.WriteLine(e.Message);
55                                 }
56                                 catch (Exception e)
57                                 {
58                                         Console.WriteLine(e.Message);
59                                 }
60                         }
61                         return IAppDomainConfig.WAR_ROOT_SYMBOL;
62                 }
63
64                 public static string GetApplicationPhysicalPath(ServletConfig config) {
65                         string path = "";
66                         ServletContext context = config.getServletContext();
67                         string appDir = config.getInitParameter(IAppDomainConfig.APP_DIR_NAME);
68 //                      Console.WriteLine("appdir = {0}", appDir);
69                         if (appDir != null)
70                         {
71                                 try
72                                 {
73                                         java.io.File f = new java.io.File(appDir);
74                                         if(f.exists())
75                                         {
76 //                                              Console.WriteLine("Physical path= {0}", appDir);
77                                                 path = appDir;
78                                         }
79                                 }
80                                 catch (Exception e)
81                                 {
82                                         Console.WriteLine(e.Message + appDir + "is invalid or unaccessible." +
83                                                 " If " + appDir + " really exists, check your security permissions"); 
84                                 };
85                         }                       
86                         if (path == "")
87                         {
88                                 path = GetApplicationRealPath(config);
89                         }
90
91                         if (!path.EndsWith ("/") && !path.EndsWith ("\\"))
92                                 path += "/";
93
94 //                      Console.WriteLine("Physical path= {0}", path); 
95                         return path;
96                 }
97
98                 public static int RunProc(string[] cmd)
99                 {       
100                         java.lang.Runtime rt = java.lang.Runtime.getRuntime();
101                         java.lang.Process proc = rt.exec(cmd);
102                         
103                         StreamGobbler errorGobbler = new 
104                                 StreamGobbler(proc.getErrorStream(), "ERROR");            
105           
106                         StreamGobbler outputGobbler = new 
107                                 StreamGobbler(proc.getInputStream(), "OUTPUT");
108                 
109                         errorGobbler.start();
110                         outputGobbler.start();
111                              
112                         int exitVal = proc.waitFor();
113                         return exitVal; 
114                 }
115         }
116
117         public class StreamGobbler : java.lang.Thread
118         {
119                 java.io.InputStream _is;
120                 String _type;
121     
122                 public StreamGobbler(java.io.InputStream ins, String type)
123                 {
124                         this._is = ins;
125                         this._type = type;
126                 }
127     
128                 public override void run()
129                 {
130                         try
131                         {
132                                 java.io.InputStreamReader isr = new java.io.InputStreamReader(_is);
133                                 java.io.BufferedReader br = new java.io.BufferedReader(isr);
134                                 String line=null;
135                                 while ( (line = br.readLine()) != null)
136                                 {
137 #if DEBUG
138                                         Console.WriteLine(_type + ">" + line); 
139 #endif
140                                 }
141                         } 
142                         catch (Exception ex)
143                         {
144 #if DEBUG
145                                 Console.WriteLine(ex);
146 #endif
147                         }
148                 }
149         }
150 }
151
152 #if !CODEDOM_SUPPORT
153 //stubs for CodeDom symbols
154 namespace System.CodeDom
155 {
156         public class CodeObject
157         {
158                 protected CodeObject () { }
159
160                 public System.Collections.IDictionary UserData { get { throw new NotSupportedException (); } }
161         }
162
163         public class CodeExpression : CodeObject
164         {
165                 protected CodeExpression () { }
166         }
167 }
168 #endif