2010-04-15 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mainsoft.Web / Mainsoft.Web / J2EEUtils.cs
blob7895ea4e7bbebb0ec870e50a7858bb551bf51ecb
1 //
2 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
3 //
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:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
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.
26 using System;
27 using System.Web.Util;
28 using System.IO;
29 using vmw.@internal.io;
30 using vmw.common;
32 using javax.servlet;
33 using System.Web;
34 using Mainsoft.Web.Hosting;
35 using System.Diagnostics;
37 namespace Mainsoft.Web
39 internal static class J2EEUtils
41 //public static string GetInitParameterByHierarchy(ServletConfig config, string name)
42 //{
43 // if (config == null)
44 // throw new ArgumentNullException("config");
46 // string value = config.getInitParameter(name);
47 // if (value != null)
48 // return value;
50 // return config.getServletContext().getInitParameter(name);
51 //}
53 public static string GetApplicationRealPath (ServletContext context) {
54 return GetApplicationRealPath (context, "/");
57 public static string GetApplicationRealPath (ServletContext context, string appVirtualPath)
59 string realFs = context.getInitParameter (J2EEConsts.FILESYSTEM_ACCESS);
60 if (realFs == null || realFs == J2EEConsts.ACCESS_FULL) {
61 try {
62 string realPath = context.getRealPath (appVirtualPath);
63 if (!String.IsNullOrEmpty (realPath)) {
64 if (!String.IsNullOrEmpty (appVirtualPath) &&
65 appVirtualPath [appVirtualPath.Length - 1] == '/')
66 if (realPath [realPath.Length - 1] != Path.DirectorySeparatorChar)
67 realPath += Path.DirectorySeparatorChar;
69 return realPath;
72 catch (Exception e) {
73 Trace.WriteLine (e.ToString());
76 return IAppDomainConfig.WAR_ROOT_SYMBOL + appVirtualPath;
79 public static string GetApplicationPhysicalPath (ServletContext context) {
80 string path = String.Empty;
82 string appDir = context.getInitParameter(IAppDomainConfig.APP_DIR_NAME);
83 if (appDir != null) {
84 try {
85 if (Directory.Exists(appDir))
86 path = appDir;
88 catch (Exception e) {
89 Trace.WriteLine (e.Message + appDir + "is invalid or unaccessible." +
90 " If " + appDir + " really exists, check your security permissions");
93 if (path.Length == 0)
94 path = GetApplicationRealPath (context);
96 return path;
99 static internal BaseWorkerRequest GetWorkerRequest (HttpContext context) {
100 IServiceProvider sp = (IServiceProvider) context;
101 return (BaseWorkerRequest) sp.GetService (typeof (HttpWorkerRequest));